UNPKG

@zohodesk/components

Version:

Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development

259 lines (226 loc) • 7.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDate = formatDate; exports.getDiffObj = getDiffObj; exports.getI18NInfo = getI18NInfo; exports.getI18NValue = getI18NValue; exports.getMonthEnd = getMonthEnd; exports.getTimeOffset = getTimeOffset; exports.getValues = getValues; exports.isToday = isToday; exports.isTomorrow = isTomorrow; exports.isTwoWeeksOrMore = isTwoWeeksOrMore; exports.isWithinAWeek = isWithinAWeek; exports.isYesterday = isYesterday; exports.pad = pad; exports.replaceI18NValuesWithRegex = replaceI18NValuesWithRegex; exports.unescapeUnicode = unescapeUnicode; var _GMTZones = require("./GMTZones.js"); function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } var dateFormat = { dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM'] }; function pad(n) { var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; var z = arguments.length > 2 ? arguments[2] : undefined; z = z || '0'; n = "".concat(n); return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; } function getTimeOffset() { var GMT = new Date(); var matchingOffset = Object.keys(_GMTZones.offsetMap).find(function (offset) { return GMT.getTimezoneOffset() === Number(offset); }); return matchingOffset ? _GMTZones.offsetMap[matchingOffset].toString().split(' ') : ['', '']; } function formatDate(dateMill, mask) { var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, i18nShortMonths = _ref.i18nShortMonths, i18nMonths = _ref.i18nMonths; var date = new Date(dateMill); var O = getTimeOffset()[0]; var Z = getTimeOffset()[1]; var d = date.getDate(); var D = date.getDay(); var m = date.getMonth(); var y = date.getFullYear(); var H = date.getHours(); var M = date.getMinutes(); var s = date.getSeconds(); var L = date.getMilliseconds(); var defaultShortMonths = dateFormat.monthNames.slice(0, 12); var defaultMonths = dateFormat.monthNames.slice(12); var resolvedShortMonths = Array.isArray(i18nShortMonths) ? i18nShortMonths : defaultShortMonths; var resolvedMonths = Array.isArray(i18nMonths) ? i18nMonths : defaultMonths; var flags = { d: d, O: O, dd: pad(d, 2), ddd: dateFormat.dayNames[D], dddd: dateFormat.dayNames[D + 7], D: d, DD: pad(d, 2), DDD: dateFormat.dayNames[D], DDDD: dateFormat.dayNames[D + 7], M: m + 1, MM: pad(m + 1, 2), MMM: resolvedShortMonths[m], MMMM: resolvedMonths[m], yy: String(y).slice(2), YY: String(y).slice(2), yyyy: y, YYYY: y, h: H % 12 || 12, hh: pad(H % 12 || 12, 2), H: H, HH: pad(H, 2), m: M, mm: pad(M, 2), s: s, ss: pad(s, 2), l: pad(L, 3), L: pad(Math.round(L / 10)), t: H < 12 ? dateFormat.timeNames[0] : dateFormat.timeNames[1], A: H < 12 ? dateFormat.timeNames[6] : dateFormat.timeNames[7], a: H < 12 ? dateFormat.timeNames[6] : dateFormat.timeNames[7], T: H < 12 ? dateFormat.timeNames[4] : dateFormat.timeNames[5], Z: Z, VV: Z, SSS: pad(L, 3) }; var token = /D{1,4}|d{1,4}|M{1,4}|YYYY|yyyy|YY|O|Z|VV|SSS|yy?|([HhmsAa])\1?|[LloSZWN]|\[[^\]]*\]|'[^']*'/g; var dat = mask.replace(token, function (match) { if (match in flags) { return flags[match]; } return match.slice(1, match.length - 1); }); return dat; } function replaceI18NValuesWithRegex(i18nStr, values) { if (typeof values !== 'undefined') { if (Array.isArray(values)) { for (var i = 0; i < values.length; i++) { i18nStr = i18nStr.replace(new RegExp("\\{".concat(i, "\\}"), 'g'), values[i]); } } else { i18nStr = i18nStr.replace(new RegExp('\\{0\\}', 'g'), values); } } return i18nStr; } function unescapeUnicode(str) { return str.replace(/\\u([a-fA-F0-9]{4})/g, function (g, m1) { return String.fromCharCode(parseInt(m1, 16)); }); } function getValues() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var diff = arguments.length > 1 ? arguments[1] : undefined; return params.map(function (param) { return diff[param]; }); } function getI18NValue(i18n) { if (typeof i18n === 'undefined') { return function (key) { return key; }; } return function (key, values) { var i18nStr = i18n[key]; if (i18nStr === undefined) { return key; } i18nStr = replaceI18NValuesWithRegex(i18nStr, values); return unescapeUnicode(i18nStr); }; } // function getValues(params = [], diff) { // return params.map(param => { // return diff[param]; // }); // } function getI18NInfo(toDateObj, props, diffObj) { var key = null, values, text = null; if (typeof props === 'function') { var value = props(diffObj1); key = value.key; values = getValues(value.params, diffObj); } else if (_typeof(props) === 'object') { key = props.key; values = getValues(props.params, diffObj); } else if (typeof props === 'string') { text = toDateObj.format(props); } return { key: key, values: values, text: text }; } function isToday(fromDate, toDate) { var TODAY = toDate.clone().startOf('day'); return fromDate.isSame(TODAY, 'd'); } function isYesterday(fromDate, toDate) { var YESTERDAY = toDate.clone().subtract(1, 'days').startOf('day'); return fromDate.isSame(YESTERDAY, 'd'); } function isTomorrow(fromDate, toDate) { return isYesterday(toDate, fromDate); } function isWithinAWeek(fromDate, toDate) { var A_WEEK_OLD = toDate.clone().subtract(7, 'days').startOf('day'); return fromDate.isAfter(A_WEEK_OLD); } function isTwoWeeksOrMore(fromDate, toDate) { return !isWithinAWeek(fromDate, toDate); } var oneYearInMillis = 31536000000; var oneDayInMillis = 86400000; var oneHourInMillis = 3600000; var oneMinuteInMillis = 60000; function convertAsNonExponential(number) { if (number.toString().toLowerCase().indexOf('e') !== -1) { return number.toFixed(20); } return number; } function getDiffObj(diff) { diff = Math.abs(diff); var diffYears = diff / oneYearInMillis; diffYears = convertAsNonExponential(diffYears); var diffDays = diff % oneYearInMillis / oneDayInMillis; diffDays = convertAsNonExponential(diffDays); var diffHours = diff % oneDayInMillis / oneHourInMillis; diffHours = convertAsNonExponential(diffHours); var diffMinutes = diff % oneDayInMillis % oneHourInMillis / oneMinuteInMillis; var diffSeconds = diff % oneDayInMillis % oneHourInMillis % oneMinuteInMillis / 1000; diffDays = parseInt(diffDays); diffHours = parseInt(diffHours); diffMinutes = parseInt(diffMinutes); diffSeconds = parseInt(diffSeconds); diffYears = parseInt(diffYears); return { y: diffYears, yd: diffDays, h: diffHours, m: diffMinutes, s: diffSeconds }; } function getMonthEnd(month, year) { var monthend = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var isLeapYear = year % 400 === 0 || year % 4 === 0 && year % 100 !== 0; if (month === 1 && isLeapYear) { return 29; } return monthend[month]; }