Skip to content

Commit

Permalink
feat: make getDepartmentNameById function
Browse files Browse the repository at this point in the history
  • Loading branch information
EATSTEAK committed Aug 20, 2022
1 parent 1e3febf commit 66c3418
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions packages/client/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
export const getCookieValue = (name: string) =>
document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || '';

export function getDepartmentLockerCountsByFloor(config: ServiceConfig, departmentId: string): { [floor: string]: number } {

export function getDepartmentLockerCountsByFloor(
config: ServiceConfig,
departmentId: string
): { [floor: string]: number } {
function countLockers([from, to]: [number, number]): number {
return to - from + 1;
}
const deptLockerSubsections: [string, LockerSubsection[]][] = Object.values(config.buildings).flatMap((building) => {

const deptLockerSubsections: [string, LockerSubsection[]][] = Object.values(
config.buildings
).flatMap((building) => {
return Object.entries(building.lockers)
.map<[string, LockerSubsection[]]>(([floor, sections]) => {
const subsections: LockerSubsection[] = Object.values(sections)
.flatMap<LockerSubsection>((section: LockerSection) => section.subsections
.filter((subsection) => subsection.department === departmentId)
)
return [floor, subsections];
}).filter(([floor, subsections]) => subsections.length)
.map<[string, LockerSubsection[]]>(([floor, sections]) => {
const subsections: LockerSubsection[] = Object.values(sections).flatMap<LockerSubsection>(
(section: LockerSection) =>
section.subsections.filter((subsection) => subsection.department === departmentId)
);
return [floor, subsections];
})
.filter(([floor, subsections]) => subsections.length);
});
return deptLockerSubsections.reduce<{ [floor: string]: number }>((result, [floor, subsections]) => {
if(typeof result[floor] !== 'number') result[floor] = 0;
result[floor] += subsections.reduce<number>((a, subsection) => a + countLockers(subsection.range), 0);
return result;
},{});
};
return deptLockerSubsections.reduce<{ [floor: string]: number }>(
(result, [floor, subsections]) => {
if (typeof result[floor] !== 'number') result[floor] = 0;
result[floor] += subsections.reduce<number>(
(a, subsection) => a + countLockers(subsection.range),
0
);
return result;
},
{}
);
}

export function getDepartmentNameById(configs: Config[], departmentId: string): string | undefined {
return configs.find((conf) => conf.id === departmentId)?.name;
}

0 comments on commit 66c3418

Please sign in to comment.