move most isikukood.js usage to utils
This commit is contained in:
61
lib/utils.ts
61
lib/utils.ts
@@ -91,8 +91,61 @@ export function getBmiBackgroundColor(bmiStatus: BmiCategory | null): string {
|
||||
}
|
||||
|
||||
export function getGenderStringFromPersonalCode(personalCode: string) {
|
||||
const person = new Isikukood(personalCode);
|
||||
if (person.getGender() === Gender.FEMALE) return 'common:female';
|
||||
if (person.getGender() === Gender.MALE) return 'common:male';
|
||||
return 'common:unknown';
|
||||
switch (PersonalCode.parsePersonalCode(personalCode).gender) {
|
||||
case 'F':
|
||||
return 'common:female';
|
||||
case 'M':
|
||||
return 'common:male';
|
||||
default:
|
||||
return 'common:unknown';
|
||||
}
|
||||
}
|
||||
|
||||
type AgeRange = '18-29' | '30-39' | '40-49' | '50-59' | '60';
|
||||
export default class PersonalCode {
|
||||
static getPersonalCode(personalCode: string | null) {
|
||||
if (!personalCode) {
|
||||
return null;
|
||||
}
|
||||
if (personalCode.toLowerCase().startsWith('ee')) {
|
||||
return personalCode.substring(2);
|
||||
}
|
||||
return personalCode;
|
||||
}
|
||||
|
||||
static parsePersonalCode(personalCode: string): {
|
||||
ageRange: AgeRange;
|
||||
gender: 'M' | 'F';
|
||||
dob: Date;
|
||||
age: number;
|
||||
} {
|
||||
const parsed = new Isikukood(personalCode);
|
||||
const ageRange = (() => {
|
||||
const age = parsed.getAge();
|
||||
if (age >= 18 && age <= 29) {
|
||||
return '18-29';
|
||||
}
|
||||
if (age >= 30 && age <= 39) {
|
||||
return '30-39';
|
||||
}
|
||||
if (age >= 40 && age <= 49) {
|
||||
return '40-49';
|
||||
}
|
||||
if (age >= 50 && age <= 59) {
|
||||
return '50-59';
|
||||
}
|
||||
if (age >= 60) {
|
||||
return '60';
|
||||
}
|
||||
throw new Error('Age range not supported');
|
||||
})();
|
||||
const gender = parsed.getGender() === Gender.MALE ? 'M' : 'F';
|
||||
|
||||
return {
|
||||
ageRange,
|
||||
gender,
|
||||
dob: parsed.getBirthday(),
|
||||
age: parsed.getAge(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user