@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
128 lines (127 loc) • 3.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.localeDefault = localeDefault;
exports.getLocale = getLocale;
exports.parse = parse;
exports.format = format;
exports.placeholder = placeholder;
const locale_info_1 = __importDefault(require("./locale-info"));
function localeDefault(inputLocale) {
if (inputLocale)
return inputLocale;
let locale = (typeof navigator !== "undefined" &&
(navigator.language || navigator.userLanguage)) ||
"en-US";
try {
Intl.DateTimeFormat.supportedLocalesOf(locale);
return locale;
}
catch (_error) {
return "en-US";
}
}
const localeCache = new Map();
function getLocale(locale) {
if (!locale) {
locale = localeDefault();
}
if (localeCache.has(locale)) {
return localeCache.get(locale);
}
let info = Object.assign({}, locale_info_1.default._);
let curr = locale_info_1.default;
const parts = locale.split("-");
for (let part of parts) {
part = part.toLowerCase();
if (curr[part]) {
curr = curr[part];
info = Object.assign(Object.assign({}, info), curr._);
}
else {
break;
}
}
localeCache.set(locale, info);
return info;
}
function parse(value, locale) {
const { o: order, s: sep } = getLocale(locale);
const parts = [];
const firstEnd = value.indexOf(sep[0].trim());
parts.push(value.slice(0, firstEnd).trim());
const secondEnd = value.indexOf(sep[1].trim(), firstEnd + 1);
parts.push(value.slice(firstEnd + 1, secondEnd).trim());
if (sep[2]) {
const thirdEnd = value.indexOf(sep[2].trim(), secondEnd + 1);
parts.push(value
.slice(secondEnd + 1, thirdEnd === -1 ? undefined : thirdEnd)
.trim());
}
else {
parts.push(value.slice(secondEnd + 1).trim());
}
if (parts.length !== 3) {
return null;
}
const parsed = {};
for (const i in parts) {
const num = parseInt(parts[i]);
if (isNaN(num)) {
return null;
}
parsed[order[i]] = num;
}
if (parsed.y < 100) {
// 2-digit year
// if year is less than 50, assume 2000s, otherwise 1900s
if (parsed.y < 50) {
parsed.y += 2000;
}
else {
parsed.y += 1900;
}
}
const iso = `${padStart(parsed.y, 4)}-${padStart(parsed.m, 2)}-${padStart(parsed.d, 2)}`;
if (isNaN(new Date(iso).getTime())) {
return null;
}
return iso;
}
function format(date, locale) {
if (!/^\d\d\d\d-\d\d-\d\d$/g.test(date)) {
return "";
}
const { o: order, s: sep } = getLocale(locale);
const [y, m, d] = date.split("-");
const parts = { y, m, d };
let result = "";
for (let i = 0; i < 3; i++) {
result += parts[order[i]];
if (sep[i]) {
result += sep[i];
}
}
return result;
}
function placeholder(locale) {
const { o: order, s: sep, y, m, d } = getLocale(locale);
const parts = {
y: `${y}${y}${y}${y}`,
m: `${m}${m}`,
d: `${d}${d}`,
};
let result = "";
for (let i = 0; i < 3; i++) {
result += parts[order[i]];
if (sep[i]) {
result += sep[i];
}
}
return result;
}
function padStart(num, digits) {
return String(num).slice(-digits).padStart(digits, "0");
}