@razorpay/blade
Version:
The Design System that powers Razorpay
72 lines (67 loc) • 3.15 kB
JavaScript
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import dayjs from 'dayjs';
var _excluded = ["formatter"];
var dayjs_locales = ['af', 'ar', 'ar-dz', 'ar-kw', 'ar-ly', 'ar-ma', 'ar-sa', 'ar-tn', 'az', 'be', 'bg', 'bm', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cv', 'cy', 'da', 'de', 'de-at', 'de-ch', 'dv', 'el', 'en', 'en-au', 'en-ca', 'en-gb', 'en-ie', 'en-il', 'en-nz', 'en-SG', 'eo', 'es', 'es-do', 'es-us', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'fr-ca', 'fr-ch', 'fy', 'ga', 'gd', 'gl', 'gom-latn', 'gu', 'he', 'hi', 'hr', 'hu', 'hy-am', 'id', 'is', 'it', 'it-ch', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'lb', 'lo', 'lt', 'lv', 'me', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'ms-my', 'mt', 'my', 'nb', 'ne', 'nl', 'nl-be', 'nn', 'oc-lnc', 'pa-in', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sd', 'se', 'si', 'sk', 'sl', 'sq', 'sr', 'sr-cyrl', 'ss', 'sv', 'sw', 'ta', 'te', 'tet', 'tg', 'th', 'tl-ph', 'tlh', 'tr', 'tzl', 'tzm', 'tzm-latn', 'ug-cn', 'uk', 'ur', 'uz', 'uz-latn', 'vi', 'x-pseudo', 'yo', 'zh-cn', 'zh-hk', 'zh-tw'];
// https://github.com/iamkun/dayjs/issues/732#issuecomment-554383261
function patchLocale(locale) {
if (['en', 'en-us'].includes(locale)) return 'en';
if (locale === 'zn') return 'zh-cn';
if (locale === 'no') return 'nb';
if (dayjs_locales.includes(locale)) return locale;
return false;
}
function convertIntlToDayjsLocale(lang) {
lang = lang.toLowerCase();
var locale = patchLocale(lang) || lang.includes('-') && patchLocale(lang.split('-')[0]);
if (!locale) return 'en';
return "".concat(locale);
}
function isScriptLoaded(src) {
return Boolean(document.querySelector("script[src=\"".concat(src, "\"]")));
}
/**
* Used to dynamically load a script
*/
function loadScript(src, callback) {
if (isScriptLoaded(src)) {
callback === null || callback === void 0 ? void 0 : callback();
return;
}
var localeScript = document.createElement('script');
localeScript.async = true;
localeScript.src = src;
localeScript.onload = function () {
return callback === null || callback === void 0 ? void 0 : callback();
};
document.head.appendChild(localeScript);
}
function defaultDateFormatter(_ref) {
var type = _ref.type,
date = _ref.date,
locale = _ref.locale,
format = _ref.format,
labelSeparator = _ref.labelSeparator;
var formatDate = function formatDate(value) {
return dayjs(value).locale(locale).format(format);
};
if (type === 'default') {
return date === null ? '' : formatDate(date);
}
if (type === 'range' && Array.isArray(date)) {
if (date[0] && date[1]) {
return "".concat(formatDate(date[0]), " ").concat(labelSeparator, " ").concat(formatDate(date[1]));
}
if (date[0]) {
return "".concat(formatDate(date[0]), " ").concat(labelSeparator, " ");
}
return '';
}
return '';
}
function getFormattedDate(_ref2) {
var formatter = _ref2.formatter,
others = _objectWithoutProperties(_ref2, _excluded);
return (formatter || defaultDateFormatter)(others);
}
export { convertIntlToDayjsLocale, getFormattedDate, loadScript };
//# sourceMappingURL=utils.js.map