diff --git a/packages/client/src/lib/utils.ts b/packages/client/src/lib/utils.ts index b78c0bd7..145671f1 100644 --- a/packages/client/src/lib/utils.ts +++ b/packages/client/src/lib/utils.ts @@ -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((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( + (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((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( + (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; +}