@fremtind/jkl-formatters-util
Version:
Formatters for common data formats in Jøkul
46 lines (45 loc) • 1.83 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);
var formatDate_exports = {};
__export(formatDate_exports, {
DATE_REGEX: () => DATE_REGEX,
formatDate: () => formatDate,
formatDateString: () => formatDateString
});
module.exports = __toCommonJS(formatDate_exports);
function formatDate(date) {
const day = "".concat(date.getDate()).padStart(2, "0");
const month = "".concat(date.getMonth() + 1).padStart(2, "0");
return "".concat(day, ".").concat(month, ".").concat(date.getFullYear());
}
const DATE_REGEX = {
// Tillater datoer på formene DDMMYY og DDMMYYYY
full: /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])(\d{2}|\d{4})$/,
partial: /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])?(\d{1,4})?$/
};
function formatDateString(input, options) {
const strippedInput = input.replace(/\D/g, "");
const regex = (options == null ? void 0 : options.partial) ? DATE_REGEX.partial : DATE_REGEX.full;
const match = strippedInput.match(regex);
if (!match) {
return input;
}
return match.slice(1).filter(Boolean).join(".");
}
//# sourceMappingURL=formatDate.js.map