@norges-domstoler/dds-formatting
Version:
Text formatting functions used in Elsa - domstolenes designsystem
146 lines (137 loc) • 4.18 kB
JavaScript
// src/containsOnlyNumbers.ts
var containsOnlyNumbers = (value) => value.match(/^\d+$/) !== null;
// src/replaceAll.ts
var replaceAll = (source, find, replace) => {
return source.split(find).join(replace);
};
// src/bank/formatBankAccountNumber.ts
var formatBankAccountNumber = (bankAccountNr) => {
const noWhitespace = replaceAll(bankAccountNr, " ", "");
if (containsOnlyNumbers(noWhitespace) && noWhitespace.length <= 11) {
const groups = [
noWhitespace.slice(0, 4),
noWhitespace.slice(4, 6),
noWhitespace.slice(6, 11)
].filter((el) => el !== "");
return groups.join(" ");
}
return bankAccountNr;
};
// src/beloep/formatBeloep.ts
var formatBeloep = (beloep) => {
return new Intl.NumberFormat("nb-NO", {
style: "currency",
currency: "NOK",
maximumFractionDigits: 2
}).format(beloep);
};
// src/beloep/formatInputBeloep.ts
var NUMBER_WITH_TWO_DECIMALS = /^[0-9]*([.][0-9]{0,2})?$/;
function formatInputBeloep(beloep, withTrailingDecimals) {
let value = beloep;
if (value === null || value === void 0) {
return "";
}
if (value.includes(",")) {
value = value.replace(",", ".");
}
if (value.startsWith(",")) {
value = "0" + value;
}
if (!NUMBER_WITH_TWO_DECIMALS.test(value)) {
return "";
}
const numberValue = parseFloat(value);
if (isNaN(numberValue)) return "";
return new Intl.NumberFormat("nb-NO", {
style: "decimal",
minimumFractionDigits: withTrailingDecimals ? 2 : 0,
maximumFractionDigits: 2
}).format(numberValue);
}
// src/dato/formatDato.ts
var isValidDate = (date) => {
return isNaN(date.valueOf()) === false;
};
var formatTime = (date, options = { hour: "numeric", minute: "numeric" }) => {
const myDate = new Date(date);
if (isValidDate(myDate)) {
return new Intl.DateTimeFormat("no-NO", options).format(myDate);
}
return date;
};
var formatDate = (date) => {
const myDate = new Date(date);
if (isValidDate(myDate)) {
return new Intl.DateTimeFormat("no-NO", {
year: "numeric",
month: "2-digit",
day: "2-digit"
}).format(myDate);
}
return date;
};
var formatDateTime = (date, options = { hour: "numeric", minute: "numeric" }) => {
const myDate = new Date(date);
if (isValidDate(myDate)) {
const formattedDate = formatDate(myDate);
const formattedTime = formatTime(myDate, options);
if (typeof formattedDate === "string" && typeof formattedTime === "string") {
return formattedDate + " " + formattedTime;
}
return date;
}
return date;
};
// src/foedselsnummer/formatFoedselsnummer.ts
var formatFoedselsnummer = (foedselsnr) => {
const noWhitespace = replaceAll(foedselsnr, " ", "");
if (containsOnlyNumbers(noWhitespace) && noWhitespace.length < 12) {
if (noWhitespace.length <= 6) {
return noWhitespace;
}
return `${noWhitespace.slice(0, 6)} ${noWhitespace.slice(6)}`;
}
return foedselsnr;
};
// src/organisation/formatOrganisationNumber.ts
var formatOrganisationNumber = (organisationNumber) => {
const noWhitespace = replaceAll(organisationNumber, " ", "");
if (containsOnlyNumbers(noWhitespace) && noWhitespace.length <= 9) {
return [
noWhitespace.slice(0, 3),
noWhitespace.slice(3, 6),
noWhitespace.slice(6, 9)
].filter((el) => el !== "").join(" ");
}
return organisationNumber;
};
// src/phone/formatMobilePhoneNumber.ts
var formatMobilePhoneNumber = (phone) => {
const noWhitespace = replaceAll(phone, " ", "");
if (containsOnlyNumbers(noWhitespace) && noWhitespace.length <= 8) {
const groups = [
noWhitespace.slice(0, 2),
noWhitespace.slice(2, 4),
noWhitespace.slice(4, 6),
noWhitespace.slice(6, 8)
].filter((el) => el !== "");
return groups.join(" ");
}
return phone;
};
// src/phone/formatLandlinePhoneNumber.ts
var formatLandlinePhoneNumber = (phone) => formatMobilePhoneNumber(phone);
export {
formatBankAccountNumber,
formatBeloep,
formatDate,
formatDateTime,
formatFoedselsnummer,
formatInputBeloep,
formatLandlinePhoneNumber,
formatMobilePhoneNumber,
formatOrganisationNumber,
formatTime
};
//# sourceMappingURL=index.mjs.map