refactor: migrate helper functions mixin

This commit is contained in:
Sebin Nyshkim 2023-01-20 00:25:49 +01:00
parent 6494354a6d
commit 5b5c027ea6
2 changed files with 61 additions and 57 deletions

61
src/helpers.ts Normal file
View file

@ -0,0 +1,61 @@
const getClientLocale = (): string => {
return navigator.languages.length > 0 ? navigator.languages[0] : "en-US";
};
const getAge = (dateOfBirth: Date): number => {
const today = new Date();
const thisYear = today.getFullYear();
const thisMonth = today.getMonth();
const thisDay = today.getDate();
const dobYear = dateOfBirth.getFullYear();
const dobMonth = dateOfBirth.getMonth();
const dobDay = dateOfBirth.getDate();
let age = thisYear - dobYear;
if (thisMonth < dobMonth) age--;
if (thisMonth === dobMonth && thisDay < dobDay) age--;
return age;
};
const toImperial = (cm: number): string => {
const realFeet = (cm * 0.3937) / 12;
const feet = Math.floor(realFeet);
const inches = Math.round((realFeet - feet) * 12);
return `${feet}'${inches}"`;
};
const toInch = (cm: number): string => {
return `${Math.round(cm / 2.45)} in`;
};
const toLbs = (kg: number): number => {
const nearExact = kg / 0.45359237;
const lbs = Math.floor(nearExact);
return lbs;
};
const toFahrenheit = (celsius: number): number => {
return celsius * 1.8 + 32;
};
const dateFormat = new Intl.DateTimeFormat(getClientLocale(), {
year: "numeric",
month: "long",
day: "2-digit",
});
export {
getClientLocale,
getAge,
toImperial,
toInch,
toLbs,
toFahrenheit,
dateFormat,
};

View file

@ -1,57 +0,0 @@
export default {
methods: {
getClientLocale() {
if (navigator.language) {
return navigator.languages[0];
} else if (navigator.userLanguage) {
return navigator.userLanguage; // IE < 11
} else if (navigator.browserLanguage) {
return navigator.userLanguage;
} else if (navigator.systemLanguage) {
return navigator.systemLanguage;
}
},
getAge(dateOfBirth) {
const today = new Date();
const thisYear = today.getFullYear();
const thisMonth = today.getMonth();
const thisDay = today.getDate();
const dobYear = dateOfBirth.getFullYear();
const dobMonth = dateOfBirth.getMonth();
const dobDay = dateOfBirth.getDate();
let age = thisYear - dobYear;
if (thisMonth < dobMonth) age--;
if (thisMonth === dobMonth && thisDay < dobDay) age--;
return age;
},
toImperial(cm) {
const realFeet = (cm * 0.3937) / 12;
const feet = Math.floor(realFeet);
const inches = Math.round((realFeet - feet) * 12);
return `${feet}'${inches}"`;
},
toInch(cm) {
return `${Math.round(cm / 2.45)} in`;
},
toLbs(kg) {
const nearExact = kg / 0.45359237;
const lbs = Math.floor(nearExact);
return lbs;
},
toFahrenheit(celsius) {
return celsius * 1.8 + 32;
},
},
};