forked from TEAM-MAT/lockerweb
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from EATSTEAK/feat/getDepartmentById_function
getDepartmentById 함수 추가
- Loading branch information
Showing
1 changed file
with
32 additions
and
16 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 |
---|---|---|
@@ -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; | ||
} |