@norges-domstoler/dds-formatting
Version:
Text formatting functions used in Elsa - domstolenes designsystem
182 lines (171 loc) • 5.65 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
formatBankAccountNumber: () => formatBankAccountNumber,
formatBeloep: () => formatBeloep,
formatDate: () => formatDate,
formatDateTime: () => formatDateTime,
formatFoedselsnummer: () => formatFoedselsnummer,
formatInputBeloep: () => formatInputBeloep,
formatLandlinePhoneNumber: () => formatLandlinePhoneNumber,
formatMobilePhoneNumber: () => formatMobilePhoneNumber,
formatOrganisationNumber: () => formatOrganisationNumber,
formatTime: () => formatTime
});
module.exports = __toCommonJS(index_exports);
// 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);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
formatBankAccountNumber,
formatBeloep,
formatDate,
formatDateTime,
formatFoedselsnummer,
formatInputBeloep,
formatLandlinePhoneNumber,
formatMobilePhoneNumber,
formatOrganisationNumber,
formatTime
});
//# sourceMappingURL=index.js.map