UNPKG

tempe

Version:

Featherlight (< 2kB) helper for simple javascript date formatting without all of those superpowers

176 lines (158 loc) 4.62 kB
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } function parseStringFormat(stringFormat) { var regexp = /\bY{2,4}\b|\bM{1,4}\b|\bD{1,2}\b|\bd{1,2}\b|\bh{1,2}\b|\bH{1,2}\b|\bm{1,2}\b|\bs{1,2}\b/g; var matchedFormats = stringFormat.match(regexp) || []; return matchedFormats; } var PADDED_FORMATS = ['hh', 'DD', 'mm', 'ss', 'HH']; var FORMATS = { YYYY: { year: 'numeric' }, YYY: { year: 'numeric' }, YY: { year: '2-digit' }, MMMM: { month: 'long' }, MMM: { month: 'short' }, MM: { month: '2-digit' }, M: { month: 'numeric' }, DD: { day: '2-digit' }, D: { day: 'numeric' }, dd: { weekday: 'long' }, d: { weekday: 'short' }, hh: { hour: 'numeric', hour12: false }, h: { hour: 'numeric', hour12: false }, HH: { hour: 'numeric', hour12: true }, H: { hour: 'numeric', hour12: true }, mm: { minute: 'numeric' }, m: { minute: 'numeric' }, ss: { second: 'numeric' }, s: { second: 'numeric' } }; function translateValue(dateObject, stringFormat, locale) { var format = FORMATS[stringFormat]; var value = dateObject.toLocaleString(locale, format); var shouldPad = PADDED_FORMATS.includes(stringFormat); return shouldPad ? value.padStart(2, '0') : value; } var TempeDate = /*#__PURE__*/function () { function TempeDate(dateObject) { this.date = dateObject; return this; } /** * Format the inputted date object * @param stringFormat printed format such as DD MMM YYYY * @param locale locale code with BCP 47 standard language tag * @param calendarType the calendar types, usually affect the year. Possible input includes but not limited to "gregory", "buddhist", "islamic", "japanese" * @returns string */ var _proto = TempeDate.prototype; _proto.format = function format(stringFormat, locale, calendarType) { var processedLocale = locale || 'en'; /** Some locale defaulted to their own calendar (e.g. Thailand that automatically use Buddhist year), /* while sometimes we want to use standard gregorian calendar /* For reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat */ var processedLocaleWithCalendarType = calendarType ? processedLocale + "-u-ca-" + calendarType : processedLocale; var matchedFormatsFromString = parseStringFormat(stringFormat); var mappedValues = this.mapAvailableStringFormatToValue(matchedFormatsFromString, processedLocaleWithCalendarType); var keys = Object.keys(mappedValues); var finalResult = keys.reduce(function (result, currentKey) { var regexToReplace = new RegExp('\\b' + currentKey + '\\b', 'g'); result = result.replace(regexToReplace, mappedValues[currentKey]); return result; }, stringFormat); return finalResult; }; _proto.mapAvailableStringFormatToValue = function mapAvailableStringFormatToValue(stringFormats, locale) { var _this = this; var values = stringFormats.reduce(function (valueMap, currentStringFormat) { valueMap[currentStringFormat] = translateValue(_this.date, currentStringFormat, locale); return valueMap; }, {}); return values; }; return TempeDate; }(); function init() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } // @ts-ignore var passedDate = args ? _construct(Date, args) : new Date(); return new TempeDate(passedDate); } export default init; //# sourceMappingURL=tempe.esm.js.map