25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
import type { TranslationKey, UiLanguage } from "./i18n/translations";
|
||
import type { JobApplication } from "./types";
|
||
|
||
type SalaryFields = Pick<JobApplication, "salary" | "salaryMin" | "salaryMax" | "salaryCurrency" | "salaryPeriod">;
|
||
|
||
/** Localised structured salary, otherwise the original free-text field. */
|
||
export function formatSalary(job: SalaryFields, language: UiLanguage, t: (key: TranslationKey) => string): string | null {
|
||
const { salaryMin, salaryMax, salaryCurrency, salaryPeriod } = job;
|
||
if (salaryMin == null && salaryMax == null) {
|
||
return job.salary?.trim() || null;
|
||
}
|
||
|
||
const fmt = (value: number) => value.toLocaleString(language === "nb" ? "nb-NO" : "en");
|
||
const range = salaryMin != null && salaryMax != null && salaryMin !== salaryMax
|
||
? `${fmt(salaryMin)}–${fmt(salaryMax)}`
|
||
: fmt((salaryMin ?? salaryMax) as number);
|
||
const currency = salaryCurrency ? ` ${salaryCurrency}` : "";
|
||
const period = salaryPeriod?.toLowerCase();
|
||
const periodLabel = period === "year" ? t("salaryPeriodYear")
|
||
: period === "month" ? t("salaryPeriodMonth")
|
||
: period === "hour" ? t("salaryPeriodHour")
|
||
: salaryPeriod;
|
||
return `${range}${currency}${periodLabel ? `/${periodLabel}` : ""}`;
|
||
}
|