Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recruitment task - Władysław Januszewski #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/constatnts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @format */

export const MEMBERS_ON_PAGE = 8;
export const PATHS_TO_FILES = ["crew.json", "crew.yaml"];
17 changes: 17 additions & 0 deletions lib/crew.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
/**
* @format
* @todo Prepare a method to return a list of crew members
* @description The list should only include crew members aged 30 to 40
*/

import { CrewMember } from "@/models/CrewMembers";
import { MEMBERS_ON_PAGE, PATHS_TO_FILES } from "./constatnts";
import { getLocalData } from "./getMockData";

export const getCrewMembers = (page: number) =>
getLocalData(PATHS_TO_FILES).then((data) => {
const filteredMembers = data.filter((item: CrewMember) => item.age >= 30 && item.age <= 40);

const membersOnPage = filteredMembers.slice(
(page - 1) * MEMBERS_ON_PAGE,
page * MEMBERS_ON_PAGE
);

return { membersOnPage, countMembers: filteredMembers.length };
});
54 changes: 54 additions & 0 deletions lib/getMockData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/** @format */

import fs from "fs";
import yaml from "js-yaml";
import { sortAlphabeticOrder } from "./sortAlphabeticOrder";
import { CrewMember, JsonFileType, YamlFileType } from "@/models/CrewMembers";

export async function getLocalData(paths: string[]): Promise<CrewMember[]> {
let objectData: CrewMember[] = [];

paths.forEach((filePath) => {
const extension = filePath.split(".").pop();

const file = fs.readFileSync(`./${filePath}`, "utf8");

let parsedData = [];
let transformedObject: CrewMember[] = [];

switch (extension) {
case "json":
parsedData = JSON.parse(file) as JsonFileType[];

transformedObject = parsedData.map(
({ firstName, lastName, nationality, age, profession }) => ({
fullName: `${firstName} ${lastName}`,
nationality,
age,
profession,
})
);

objectData = [...objectData, ...transformedObject];

break;

case "yaml":
parsedData = yaml.load(file) as YamlFileType[];

transformedObject = parsedData.map(({ name, nationality, years_old, occupation }) => ({
fullName: name,
nationality,
age: years_old,
profession: occupation,
}));

objectData = [...objectData, ...transformedObject];

break;
default:
throw new Error("Unsupported file extension");
}
});
return sortAlphabeticOrder(objectData);
}
14 changes: 14 additions & 0 deletions lib/sortAlphabeticOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @format */

import { CrewMember } from "@/models/CrewMembers";

export const sortAlphabeticOrder = (objectData: CrewMember[]) =>
objectData.sort(function (a, b) {
if (a.fullName < b.fullName) {
return -1;
}
if (a.fullName > b.fullName) {
return 1;
}
return 0;
});
30 changes: 30 additions & 0 deletions models/CrewMembers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** @format */

export type JsonFileType = {
firstName: string;
lastName: string;
nationality: string;
age: number;
profession: string;
};

export type YamlFileType = {
name: string;
nationality: string;
years_old: number;
occupation: string;
};

export type FileType = JsonFileType | YamlFileType;

export type CrewMember = {
fullName: string;
nationality: string;
age: number;
profession: string;
};

export interface SchemaCrewMembers {
members: CrewMember[] | [];
countMembers: number;
}
Loading