-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Uladzislau
committed
Jan 9, 2024
1 parent
69c92bf
commit 7e78457
Showing
18 changed files
with
560 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.