Files
jobtrackingapp/job-tracker-ui/src/salary.ts
T

25 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}` : ""}`;
}