Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- /* eslint-disable no-console */
- const mock_1 = require("./mock");
- function lab() {
- const refactorUser = (user) => {
- const refactoredUser = {
- gender: user.gender.charAt(0).toUpperCase() + user.gender.slice(1),
- title: user.name.title,
- full_name: `${user.name.first} ${user.name.last}`,
- city: user.location.city,
- state: user.location.state,
- country: user.location.country,
- postcode: user.location.postcode,
- coordinates: user.location.coordinates,
- timezone: user.location.timezone,
- email: user.email,
- id: user.id.name !== '' ? `${user.id.name}${user.id.value}` : undefined,
- dob: new Date(user.dob.date),
- age: user.dob.age,
- phone: user.phone,
- picture_large: user.picture.large,
- picture_thumbnail: user.picture.thumbnail,
- };
- return refactoredUser;
- };
- const getRandomCourse = () => {
- const courses = ['Mathematics', 'Physics', 'English', 'Computer Science', 'Dancing', 'Chess', 'Biology',
- 'Chemistry', 'Law', 'Art', 'Medicine', 'Statistics'];
- const randomIndex = Math.floor(Math.random() * courses.length);
- return courses[randomIndex];
- };
- const mergeUsers = (users, additionalInfo) => {
- const res = [];
- additionalInfo.forEach((additionalUser) => {
- var _a, _b, _c;
- if (users.find((user) => user.full_name === additionalUser.full_name) === undefined) {
- const userToAdd = {
- gender: additionalUser.gender.charAt(0).toUpperCase()
- + additionalUser.gender.slice(1),
- title: additionalUser.title,
- full_name: additionalUser.full_name,
- city: additionalUser.city,
- state: additionalUser.state,
- country: additionalUser.country,
- postcode: Number(additionalUser.postcode),
- coordinates: additionalUser.coordinates,
- timezone: additionalUser.timezone,
- email: additionalUser.email,
- dob: additionalUser.b_day === undefined
- ? undefined : new Date(additionalUser.b_day),
- age: additionalUser.age,
- phone: additionalUser.phone,
- picture_large: additionalUser.picture_large,
- picture_thumbnail: additionalUser.picture_thumbnail,
- id: additionalUser.id,
- favorite: additionalUser.favorite,
- course: (_a = additionalUser.course) !== null && _a !== void 0 ? _a : getRandomCourse(),
- bg_color: (_b = additionalUser.bg_color) !== null && _b !== void 0 ? _b : '#ffffff',
- note: (_c = additionalUser.note) !== null && _c !== void 0 ? _c : null,
- };
- res.push(userToAdd);
- }
- });
- users.forEach((user) => {
- var _a, _b, _c, _d;
- const details = additionalInfo
- .find((userDetails) => user.full_name === userDetails.full_name);
- if (details !== undefined) {
- const userDetails = {
- favorite: (_a = details.favorite) !== null && _a !== void 0 ? _a : false,
- course: (_b = details.course) !== null && _b !== void 0 ? _b : getRandomCourse(),
- bg_color: (_c = details.bg_color) !== null && _c !== void 0 ? _c : '#ffffff',
- note: (_d = details.note) !== null && _d !== void 0 ? _d : null,
- };
- res.push(Object.assign(Object.assign({}, user), userDetails));
- }
- else {
- const userDetails = {
- favorite: false,
- course: getRandomCourse(),
- bg_color: '#ffffff',
- note: null,
- };
- res.push(Object.assign(Object.assign({}, user), userDetails));
- }
- });
- return res;
- };
- const isCapitalized = (str) => {
- if (str.length === 0) {
- return true;
- }
- return str.charAt(0) === str.charAt(0).toUpperCase();
- };
- const validateUser = (user) => {
- var _a, _b, _c, _d, _e, _f;
- if (!isCapitalized((_a = user.full_name) !== null && _a !== void 0 ? _a : '')) {
- return false;
- }
- if (!isCapitalized((_b = user.gender) !== null && _b !== void 0 ? _b : '')) {
- return false;
- }
- if (!isCapitalized((_c = user.note) !== null && _c !== void 0 ? _c : '')) {
- return false;
- }
- if (!isCapitalized((_d = user.state) !== null && _d !== void 0 ? _d : '')) {
- return false;
- }
- if (!isCapitalized((_e = user.city) !== null && _e !== void 0 ? _e : '')) {
- return false;
- }
- if (!isCapitalized((_f = user.country) !== null && _f !== void 0 ? _f : '')) {
- return false;
- }
- const phoneRegex = /\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/;
- if (!phoneRegex.test((user.phone)) && user.phone !== undefined) {
- return false;
- }
- const emailRegex = /^((?!\.)[\w-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/;
- if (!emailRegex.test(user.email) && user.email !== undefined) {
- return false;
- }
- return true;
- };
- let Gender;
- (function (Gender) {
- Gender["MALE"] = "Male";
- Gender["FEMALE"] = "Female";
- })(Gender || (Gender = {}));
- class Filter {
- constructor(country, age, gender, favorite) {
- this.country = country;
- this.age = age;
- this.gender = gender;
- this.favorite = favorite;
- }
- filter(users) {
- let res = users;
- if (this.country) {
- res = res.filter((user) => user.country === this.country);
- }
- if (this.age) {
- res = res.filter((user) => user.age >= this.age.min && user.age <= this.age.max);
- }
- if (this.gender) {
- res = res.filter((user) => user.gender === this.gender.valueOf());
- }
- if (this.favorite) {
- res = res.filter((user) => user.favorite === this.favorite);
- }
- return res;
- }
- }
- let SortBy;
- (function (SortBy) {
- SortBy["NAME"] = "full_name";
- SortBy["AGE"] = "age";
- SortBy["DOB"] = "dob";
- SortBy["COUNTRY"] = "country";
- })(SortBy || (SortBy = {}));
- class Sorter {
- constructor(sortBy) {
- this.sortBy = sortBy;
- }
- sort(users, ascending = true) {
- return users.sort((lhs, rhs) => {
- if (lhs[this.sortBy.valueOf()] < rhs[this.sortBy.valueOf()]) {
- return ascending ? -1 : 1;
- }
- if (lhs[this.sortBy.valueOf()] > rhs[this.sortBy.valueOf()]) {
- return ascending ? 1 : -1;
- }
- return 0;
- });
- }
- }
- const search = (users, query) => users.filter((user) => {
- if (user.full_name.toLowerCase().search(String(query).toLowerCase()) !== -1) {
- return true;
- }
- if (user.note !== null) {
- if (user.note.toLowerCase().search(String(query).toLowerCase()) !== -1) {
- return true;
- }
- }
- if (!Number.isNaN(Number(query))) {
- if (user.age === undefined || user.age === null) {
- return false;
- }
- return user.age === Number(query);
- }
- return false;
- });
- // eslint-disable-next-line max-len
- const calculatePct = (partial, full) => `${((partial.length / full.length) * 100).toFixed(2)}%`;
- console.log('--- Merged data from the mock ---');
- const users = mergeUsers(mock_1.randomUserMock.map((user) => refactorUser(user)), mock_1.additionalUsers);
- console.log(users);
- console.log('--- User validation results ---');
- console.log(users.map((user) => `${user.full_name} -- ${validateUser(user)}`));
- console.log('--- Sorted by date of birth ---');
- const sorter = new Sorter(SortBy.DOB);
- const sorted = sorter.sort(users).map((user) => `${user.full_name} -- ${user.dob}`);
- console.log(sorted);
- console.log('--- Filter test | Country: Germany | Gender: Female ---');
- const germanyFilter = new Filter('Germany', undefined, Gender.FEMALE);
- const germanyUsers = germanyFilter.filter(users);
- console.log(germanyUsers);
- console.log('--- Search test | Query: 39 ---');
- const searchRes1 = search(users, 39);
- console.log(searchRes1);
- console.log(`Found users are ${calculatePct(searchRes1, users)} of total number`);
- console.log('--- Search test | Query: b ---');
- const searchRes2 = search(users, 'b');
- console.log(searchRes2);
- console.log(`Found users are ${calculatePct(searchRes2, users)} of total number`);
- }
- exports.default = lab;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement