UNPKG

ngx-bootstrap

Version:
1,440 lines (1,413 loc) 264 kB
function mod(n, x) { return (n % x + x) % x; } function absFloor(num) { return num < 0 ? Math.ceil(num) || 0 : Math.floor(num); } function isString(str) { return typeof str === 'string'; } function isDate(value) { return value instanceof Date || Object.prototype.toString.call(value) === '[object Date]'; } function isBoolean(value) { return value === true || value === false; } function isDateValid(date) { return date && date.getTime && !isNaN(date.getTime()); } // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type function isFunction(fn) { return (fn instanceof Function || Object.prototype.toString.call(fn) === '[object Function]'); } function isNumber(value) { return typeof value === 'number' || Object.prototype.toString.call(value) === '[object Number]'; } function isArray(input) { return (input instanceof Array || Object.prototype.toString.call(input) === '[object Array]'); } function hasOwnProp(a /*object*/, b) { return Object.prototype.hasOwnProperty.call(a, b); } function isObject(input /*object*/) { // IE8 will treat undefined and null as object if it wasn't for // input != null return (input != null && Object.prototype.toString.call(input) === '[object Object]'); } function isObjectEmpty(obj) { if (Object.getOwnPropertyNames) { return (Object.getOwnPropertyNames(obj).length === 0); } let k; for (k in obj) { // eslint-disable-next-line no-prototype-builtins if (obj.hasOwnProperty(k)) { return false; } } return true; } function isUndefined(input) { return input === void 0; } function toInt(argumentForCoercion) { const coercedNumber = +argumentForCoercion; let value = 0; if (coercedNumber !== 0 && isFinite(coercedNumber)) { value = absFloor(coercedNumber); } return value; } const aliases = {}; const _mapUnits = { date: 'day', hour: 'hours', minute: 'minutes', second: 'seconds', millisecond: 'milliseconds' }; function addUnitAlias(unit, shorthand) { const lowerCase = unit.toLowerCase(); let _unit = unit; if (lowerCase in _mapUnits) { _unit = _mapUnits[lowerCase]; } aliases[lowerCase] = aliases[`${lowerCase}s`] = aliases[shorthand] = _unit; } function normalizeUnits(units) { return isString(units) ? aliases[units] || aliases[units.toLowerCase()] : undefined; } function normalizeObjectUnits(inputObject) { const normalizedInput = {}; let normalizedProp; let prop; for (prop in inputObject) { if (hasOwnProp(inputObject, prop)) { normalizedProp = normalizeUnits(prop); if (normalizedProp) { normalizedInput[normalizedProp] = inputObject[prop]; } } } return normalizedInput; } // place in new Date([array]) const YEAR = 0; const MONTH = 1; const DATE = 2; const HOUR = 3; const MINUTE = 4; const SECOND = 5; const MILLISECOND = 6; const WEEK = 7; const WEEKDAY = 8; function zeroFill(num, targetLength, forceSign) { const absNumber = `${Math.abs(num)}`; const zerosToFill = targetLength - absNumber.length; const sign = num >= 0; const _sign = sign ? (forceSign ? '+' : '') : '-'; // todo: this is crazy slow const _zeros = Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1); return (_sign + _zeros + absNumber); } const formatFunctions = {}; const formatTokenFunctions = {}; const formattingTokens = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Qo?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g; // token: 'M' // padded: ['MM', 2] // ordinal: 'Mo' // callback: function () { this.month() + 1 } function addFormatToken(token, padded, ordinal, callback) { if (token) { formatTokenFunctions[token] = callback; } if (padded) { formatTokenFunctions[padded[0]] = function () { return zeroFill(callback.apply(null, arguments), padded[1], padded[2]); }; } if (ordinal) { formatTokenFunctions[ordinal] = function (date, opts) { return opts.locale.ordinal(callback.apply(null, arguments), token); }; } } function makeFormatFunction(format) { const array = format.match(formattingTokens); const length = array.length; const formatArr = new Array(length); for (let i = 0; i < length; i++) { formatArr[i] = formatTokenFunctions[array[i]] ? formatTokenFunctions[array[i]] : removeFormattingTokens(array[i]); } return function (date, locale, isUTC, offset = 0) { let output = ''; for (let j = 0; j < length; j++) { output += isFunction(formatArr[j]) ? formatArr[j].call(null, date, { format, locale, isUTC, offset }) : formatArr[j]; } return output; }; } function removeFormattingTokens(input) { if (input.match(/\[[\s\S]/)) { return input.replace(/^\[|\]$/g, ''); } return input.replace(/\\/g, ''); } // eslint-disable-next-line @typescript-eslint/no-unused-vars function createUTCDate(y, m, d) { // eslint-disable-next-line prefer-rest-params const date = new Date(Date.UTC.apply(null, arguments)); // the Date.UTC function remaps years 0-99 to 1900-1999 if (y < 100 && y >= 0 && isFinite(date.getUTCFullYear())) { date.setUTCFullYear(y); } return date; } function createDate(y, m = 0, d = 1, h = 0, M = 0, s = 0, ms = 0) { const date = new Date(y, m, d, h, M, s, ms); // the date constructor remaps years 0-99 to 1900-1999 if (y < 100 && y >= 0 && isFinite(date.getFullYear())) { date.setFullYear(y); } return date; } function getHours(date, isUTC = false) { return isUTC ? date.getUTCHours() : date.getHours(); } function getMinutes(date, isUTC = false) { return isUTC ? date.getUTCMinutes() : date.getMinutes(); } function getSeconds(date, isUTC = false) { return isUTC ? date.getUTCSeconds() : date.getSeconds(); } function getMilliseconds(date, isUTC = false) { return isUTC ? date.getUTCMilliseconds() : date.getMilliseconds(); } function getTime(date) { return date.getTime(); } function getDay(date, isUTC = false) { return isUTC ? date.getUTCDay() : date.getDay(); } function getDate(date, isUTC = false) { return isUTC ? date.getUTCDate() : date.getDate(); } function getMonth(date, isUTC = false) { return isUTC ? date.getUTCMonth() : date.getMonth(); } function getFullYear(date, isUTC = false) { return isUTC ? date.getUTCFullYear() : date.getFullYear(); } function getUnixTime(date) { return Math.floor(date.valueOf() / 1000); } function unix(date) { return Math.floor(date.valueOf() / 1000); } function getFirstDayOfMonth(date) { return createDate(date.getFullYear(), date.getMonth(), 1, date.getHours(), date.getMinutes(), date.getSeconds()); } function daysInMonth$1(date) { return _daysInMonth(date.getFullYear(), date.getMonth()); } function _daysInMonth(year, month) { return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); } function isFirstDayOfWeek(date, firstDayOfWeek) { return date.getDay() === Number(firstDayOfWeek); } function isSameMonth(date1, date2) { if (!date1 || !date2) { return false; } return isSameYear(date1, date2) && getMonth(date1) === getMonth(date2); } function isSameYear(date1, date2) { if (!date1 || !date2) { return false; } return getFullYear(date1) === getFullYear(date2); } function isSameDay$1(date1, date2) { if (!date1 || !date2) { return false; } return (isSameYear(date1, date2) && isSameMonth(date1, date2) && getDate(date1) === getDate(date2)); } const match1 = /\d/; // 0 - 9 const match2 = /\d\d/; // 00 - 99 const match3 = /\d{3}/; // 000 - 999 const match4 = /\d{4}/; // 0000 - 9999 const match6 = /[+-]?\d{6}/; // -999999 - 999999 const match1to2 = /\d\d?/; // 0 - 99 const match3to4 = /\d\d\d\d?/; // 999 - 9999 const match5to6 = /\d\d\d\d\d\d?/; // 99999 - 999999 const match1to3 = /\d{1,3}/; // 0 - 999 const match1to4 = /\d{1,4}/; // 0 - 9999 const match1to6 = /[+-]?\d{1,6}/; // -999999 - 999999 const matchUnsigned = /\d+/; // 0 - inf const matchSigned = /[+-]?\d+/; // -inf - inf const matchOffset = /Z|[+-]\d\d:?\d\d/gi; // +00:00 -00:00 +0000 -0000 or Z const matchShortOffset = /Z|[+-]\d\d(?::?\d\d)?/gi; // +00 -00 +00:00 -00:00 +0000 -0000 or Z const matchTimestamp = /[+-]?\d+(\.\d{1,3})?/; // 123456789 123456789.123 // any word (or two) characters or numbers including two/three word month in arabic. // includes scottish gaelic two word and hyphenated months const matchWord = /[0-9]{0,256}['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{1,256}|[\u0600-\u06FF\/]{1,256}(\s*?[\u0600-\u06FF]{1,256}){1,2}/i; const regexes = {}; function addRegexToken(token, regex, strictRegex) { if (isFunction(regex)) { regexes[token] = regex; return; } regexes[token] = function (isStrict, locale) { return (isStrict && strictRegex) ? strictRegex : regex; }; } function getParseRegexForToken(token, locale) { const _strict = false; if (!hasOwnProp(regexes, token)) { return new RegExp(unescapeFormat(token)); } return regexes[token](_strict, locale); } // Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript function unescapeFormat(str) { return regexEscape(str .replace('\\', '') .replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, (matched, p1, p2, p3, p4) => p1 || p2 || p3 || p4)); } function regexEscape(str) { return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); } const tokens = {}; function addParseToken(token, callback) { const _token = isString(token) ? [token] : token; let func = callback; if (isNumber(callback)) { func = function (input, array, config) { array[callback] = toInt(input); return config; }; } if (isArray(_token) && isFunction(func)) { let i; for (i = 0; i < _token.length; i++) { tokens[_token[i]] = func; } } } function addWeekParseToken(token, callback) { addParseToken(token, function (input, array, config, _token) { config._w = config._w || {}; return callback(input, config._w, config, _token); }); } function addTimeToArrayFromToken(token, input, config) { if (input != null && hasOwnProp(tokens, token)) { tokens[token](input, config._a, config, token); } return config; } const priorities = {}; function addUnitPriority(unit, priority) { priorities[unit] = priority; } /* export function getPrioritizedUnits(unitsObj) { const units = []; let unit; for (unit in unitsObj) { if (unitsObj.hasOwnProperty(unit)) { units.push({ unit, priority: priorities[unit] }); } } units.sort(function (a, b) { return a.priority - b.priority; }); return units; } */ function initDayOfMonth() { // FORMATTING addFormatToken('D', ['DD', 2, false], 'Do', function (date, opts) { return getDate(date, opts.isUTC) .toString(10); }); // ALIASES addUnitAlias('date', 'D'); // PRIOROITY addUnitPriority('date', 9); // PARSING addRegexToken('D', match1to2); addRegexToken('DD', match1to2, match2); addRegexToken('Do', function (isStrict, locale) { return locale._dayOfMonthOrdinalParse || locale._ordinalParse; }); addParseToken(['D', 'DD'], DATE); addParseToken('Do', function (input, array, config) { array[DATE] = toInt(input.match(match1to2)[0]); return config; }); } function defaultParsingFlags() { // We need to deep clone this object. return { empty: false, unusedTokens: [], unusedInput: [], overflow: -2, charsLeftOver: 0, nullInput: false, invalidMonth: null, invalidFormat: false, userInvalidated: false, iso: false, parsedDateParts: [], meridiem: null, rfc2822: false, weekdayMismatch: false }; } function getParsingFlags(config) { if (config._pf == null) { config._pf = defaultParsingFlags(); } return config._pf; } // FORMATTING function getYear(date, opts) { if (opts.locale.getFullYear) { return opts.locale.getFullYear(date, opts.isUTC).toString(); } return getFullYear(date, opts.isUTC).toString(); } function initYear() { addFormatToken('Y', null, null, function (date, opts) { const y = getFullYear(date, opts.isUTC); return y <= 9999 ? y.toString(10) : `+${y}`; }); addFormatToken(null, ['YY', 2, false], null, function (date, opts) { return (getFullYear(date, opts.isUTC) % 100).toString(10); }); addFormatToken(null, ['YYYY', 4, false], null, getYear); addFormatToken(null, ['YYYYY', 5, false], null, getYear); addFormatToken(null, ['YYYYYY', 6, true], null, getYear); // ALIASES addUnitAlias('year', 'y'); // PRIORITIES addUnitPriority('year', 1); // PARSING addRegexToken('Y', matchSigned); addRegexToken('YY', match1to2, match2); addRegexToken('YYYY', match1to4, match4); addRegexToken('YYYYY', match1to6, match6); addRegexToken('YYYYYY', match1to6, match6); addParseToken(['YYYYY', 'YYYYYY'], YEAR); addParseToken('YYYY', function (input, array, config) { array[YEAR] = input.length === 2 ? parseTwoDigitYear(input) : toInt(input); return config; }); addParseToken('YY', function (input, array, config) { array[YEAR] = parseTwoDigitYear(input); return config; }); addParseToken('Y', function (input, array, config) { array[YEAR] = parseInt(input, 10); return config; }); } function parseTwoDigitYear(input) { return toInt(input) + (toInt(input) > 68 ? 1900 : 2000); } function daysInYear(year) { return isLeapYear(year) ? 366 : 365; } function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } // todo: this is duplicate, source in date-getters.ts function daysInMonth(year, month) { if (isNaN(year) || isNaN(month)) { return NaN; } const modMonth = mod(month, 12); const _year = year + (month - modMonth) / 12; return modMonth === 1 ? isLeapYear(_year) ? 29 : 28 : (31 - modMonth % 7 % 2); } function initMonth() { // FORMATTING addFormatToken('M', ['MM', 2, false], 'Mo', function (date, opts) { return (getMonth(date, opts.isUTC) + 1).toString(10); }); addFormatToken('MMM', null, null, function (date, opts) { return opts.locale.monthsShort(date, opts.format, opts.isUTC); }); addFormatToken('MMMM', null, null, function (date, opts) { return opts.locale.months(date, opts.format, opts.isUTC); }); // ALIASES addUnitAlias('month', 'M'); // PRIORITY addUnitPriority('month', 8); // PARSING addRegexToken('M', match1to2); addRegexToken('MM', match1to2, match2); addRegexToken('MMM', function (isStrict, locale) { return locale.monthsShortRegex(isStrict); }); addRegexToken('MMMM', function (isStrict, locale) { return locale.monthsRegex(isStrict); }); addParseToken(['M', 'MM'], function (input, array, config) { array[MONTH] = toInt(input) - 1; return config; }); addParseToken(['MMM', 'MMMM'], function (input, array, config, token) { const month = config._locale.monthsParse(input, token, config._strict); // if we didn't find a month name, mark the date as invalid. if (month != null) { array[MONTH] = month; } else { getParsingFlags(config).invalidMonth = !!input; } return config; }); } const defaultTimeUnit = { year: 0, month: 0, day: 0, hour: 0, minute: 0, seconds: 0 }; function shiftDate(date, unit) { const _unit = Object.assign({}, defaultTimeUnit, unit); const year = date.getFullYear() + (_unit.year || 0); const month = date.getMonth() + (_unit.month || 0); let day = date.getDate() + (_unit.day || 0); if (_unit.month && !_unit.day) { day = Math.min(day, daysInMonth(year, month)); } return createDate(year, month, day, date.getHours() + (_unit.hour || 0), date.getMinutes() + (_unit.minute || 0), date.getSeconds() + (_unit.seconds || 0)); } function setFullDate(date, unit) { return createDate(getNum(date.getFullYear(), unit.year), getNum(date.getMonth(), unit.month), 1, // day, to avoid issue with wrong months selection at the end of current month (#5371) getNum(date.getHours(), unit.hour), getNum(date.getMinutes(), unit.minute), getNum(date.getSeconds(), unit.seconds), getNum(date.getMilliseconds(), unit.milliseconds)); } function getNum(def, num) { return isNumber(num) ? num : def; } function setFullYear(date, value, isUTC) { const _month = getMonth(date, isUTC); const _date = getDate(date, isUTC); const _year = getFullYear(date, isUTC); if (isLeapYear(_year) && _month === 1 && _date === 29) { const _daysInMonth = daysInMonth(value, _month); isUTC ? date.setUTCFullYear(value, _month, _daysInMonth) : date.setFullYear(value, _month, _daysInMonth); } isUTC ? date.setUTCFullYear(value) : date.setFullYear(value); return date; } function setMonth(date, value, isUTC) { const dayOfMonth = Math.min(getDate(date), daysInMonth(getFullYear(date), value)); isUTC ? date.setUTCMonth(value, dayOfMonth) : date.setMonth(value, dayOfMonth); return date; } function setDay(date, value, isUTC) { isUTC ? date.setUTCDate(value) : date.setDate(value); return date; } function setHours(date, value, isUTC) { isUTC ? date.setUTCHours(value) : date.setHours(value); return date; } function setMinutes(date, value, isUTC) { isUTC ? date.setUTCMinutes(value) : date.setMinutes(value); return date; } function setSeconds(date, value, isUTC) { isUTC ? date.setUTCSeconds(value) : date.setSeconds(value); return date; } function setMilliseconds(date, value, isUTC) { isUTC ? date.setUTCMilliseconds(value) : date.setMilliseconds(value); return date; } function setDate(date, value, isUTC) { isUTC ? date.setUTCDate(value) : date.setDate(value); return date; } function setTime(date, value) { date.setTime(value); return date; } // fastest way to clone date // https://jsperf.com/clone-date-object2 function cloneDate(date) { return new Date(date.getTime()); } function startOf(date, unit, isUTC) { const _date = cloneDate(date); // the following switch intentionally omits break keywords // to utilize falling through the cases. switch (unit) { case 'year': setMonth(_date, 0, isUTC); /* falls through */ case 'quarter': case 'month': setDate(_date, 1, isUTC); /* falls through */ case 'week': case 'isoWeek': case 'day': case 'date': setHours(_date, 0, isUTC); /* falls through */ case 'hours': setMinutes(_date, 0, isUTC); /* falls through */ case 'minutes': setSeconds(_date, 0, isUTC); /* falls through */ case 'seconds': setMilliseconds(_date, 0, isUTC); } // weeks are a special case if (unit === 'week') { setLocaleDayOfWeek(_date, 0, { isUTC }); } if (unit === 'isoWeek') { setISODayOfWeek(_date, 1); } // quarters are also special if (unit === 'quarter') { setMonth(_date, Math.floor(getMonth(_date, isUTC) / 3) * 3, isUTC); } return _date; } function endOf(date, unit, isUTC) { let _unit = unit; // 'date' is an alias for 'day', so it should be considered as such. if (_unit === 'date') { _unit = 'day'; } const start = startOf(date, _unit, isUTC); const _step = add(start, 1, _unit === 'isoWeek' ? 'week' : _unit, isUTC); const res = subtract(_step, 1, 'milliseconds', isUTC); return res; } function initDayOfYear() { // FORMATTING addFormatToken('DDD', ['DDDD', 3, false], 'DDDo', function (date) { return getDayOfYear(date) .toString(10); }); // ALIASES addUnitAlias('dayOfYear', 'DDD'); // PRIORITY addUnitPriority('dayOfYear', 4); addRegexToken('DDD', match1to3); addRegexToken('DDDD', match3); addParseToken(['DDD', 'DDDD'], function (input, array, config) { config._dayOfYear = toInt(input); return config; }); } function getDayOfYear(date, isUTC) { const date1 = +startOf(date, 'day', isUTC); const date2 = +startOf(date, 'year', isUTC); const someDate = date1 - date2; const oneDay = 1000 * 60 * 60 * 24; return Math.round(someDate / oneDay) + 1; } function setDayOfYear(date, input) { const dayOfYear = getDayOfYear(date); return add(date, (input - dayOfYear), 'day'); } /** * * @param {number} year * @param {number} dow - start-of-first-week * @param {number} doy - start-of-year * @returns {number} */ function firstWeekOffset(year, dow, doy) { // first-week day -- which january is always in the first week (4 for iso, 1 for other) const fwd = dow - doy + 7; // first-week day local weekday -- which local weekday is fwd const fwdlw = (createUTCDate(year, 0, fwd).getUTCDay() - dow + 7) % 7; return -fwdlw + fwd - 1; } // https://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday function dayOfYearFromWeeks(year, week, weekday, dow, doy) { const localWeekday = (7 + weekday - dow) % 7; const weekOffset = firstWeekOffset(year, dow, doy); const dayOfYear = 1 + 7 * (week - 1) + localWeekday + weekOffset; let resYear; let resDayOfYear; if (dayOfYear <= 0) { resYear = year - 1; resDayOfYear = daysInYear(resYear) + dayOfYear; } else if (dayOfYear > daysInYear(year)) { resYear = year + 1; resDayOfYear = dayOfYear - daysInYear(year); } else { resYear = year; resDayOfYear = dayOfYear; } return { year: resYear, dayOfYear: resDayOfYear }; } function weekOfYear(date, dow, doy, isUTC) { const weekOffset = firstWeekOffset(getFullYear(date, isUTC), dow, doy); const week = Math.floor((getDayOfYear(date, isUTC) - weekOffset - 1) / 7) + 1; let resWeek; let resYear; if (week < 1) { resYear = getFullYear(date, isUTC) - 1; resWeek = week + weeksInYear(resYear, dow, doy); } else if (week > weeksInYear(getFullYear(date, isUTC), dow, doy)) { resWeek = week - weeksInYear(getFullYear(date, isUTC), dow, doy); resYear = getFullYear(date, isUTC) + 1; } else { resYear = getFullYear(date, isUTC); resWeek = week; } return { week: resWeek, year: resYear }; } function weeksInYear(year, dow, doy) { const weekOffset = firstWeekOffset(year, dow, doy); const weekOffsetNext = firstWeekOffset(year + 1, dow, doy); return (daysInYear(year) - weekOffset + weekOffsetNext) / 7; } const MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s)+MMMM?/; const defaultLocaleMonths = 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'); const defaultLocaleMonthsShort = 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'); const defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'); const defaultLocaleWeekdaysShort = 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'); const defaultLocaleWeekdaysMin = 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'); const defaultLongDateFormat = { LTS: 'h:mm:ss A', LT: 'h:mm A', L: 'MM/DD/YYYY', LL: 'MMMM D, YYYY', LLL: 'MMMM D, YYYY h:mm A', LLLL: 'dddd, MMMM D, YYYY h:mm A' }; const defaultOrdinal = '%d'; const defaultDayOfMonthOrdinalParse = /\d{1,2}/; const defaultMonthsShortRegex = matchWord; const defaultMonthsRegex = matchWord; class Locale { constructor(config) { if (config) { this.set(config); } } set(config) { let confKey; for (confKey in config) { // eslint-disable-next-line no-prototype-builtins if (!config.hasOwnProperty(confKey)) { continue; } const prop = config[confKey]; const key = (isFunction(prop) ? confKey : `_${confKey}`); this[key] = prop; } this._config = config; } calendar(key, date, now) { const output = this._calendar[key] || this._calendar["sameElse"]; return isFunction(output) ? output.call(null, date, now) : output; } longDateFormat(key) { const format = this._longDateFormat[key]; const formatUpper = this._longDateFormat[key.toUpperCase()]; if (format || !formatUpper) { return format; } this._longDateFormat[key] = formatUpper.replace(/MMMM|MM|DD|dddd/g, function (val) { return val.slice(1); }); return this._longDateFormat[key]; } get invalidDate() { return this._invalidDate; } set invalidDate(val) { this._invalidDate = val; } ordinal(num, token) { return this._ordinal.replace('%d', num.toString(10)); } preparse(str, format) { return str; } getFullYear(date, isUTC = false) { return getFullYear(date, isUTC); } postformat(str) { return str; } relativeTime(num, withoutSuffix, str, isFuture) { const output = this._relativeTime[str]; return (isFunction(output)) ? output(num, withoutSuffix, str, isFuture) : output.replace(/%d/i, num.toString(10)); } pastFuture(diff, output) { const format = this._relativeTime[diff > 0 ? 'future' : 'past']; return isFunction(format) ? format(output) : format.replace(/%s/i, output); } months(date, format, isUTC = false) { if (!date) { return isArray(this._months) ? this._months : this._months.standalone; } if (isArray(this._months)) { return this._months[getMonth(date, isUTC)]; } const key = (this._months.isFormat || MONTHS_IN_FORMAT).test(format) ? 'format' : 'standalone'; return this._months[key][getMonth(date, isUTC)]; } monthsShort(date, format, isUTC = false) { if (!date) { return isArray(this._monthsShort) ? this._monthsShort : this._monthsShort.standalone; } if (isArray(this._monthsShort)) { return this._monthsShort[getMonth(date, isUTC)]; } const key = MONTHS_IN_FORMAT.test(format) ? 'format' : 'standalone'; return this._monthsShort[key][getMonth(date, isUTC)]; } monthsParse(monthName, format, strict) { let date; let regex; if (this._monthsParseExact) { return this.handleMonthStrictParse(monthName, format, strict); } if (!this._monthsParse) { this._monthsParse = []; this._longMonthsParse = []; this._shortMonthsParse = []; } // TODO: add sorting // Sorting makes sure if one month (or abbr) is a prefix of another // see sorting in computeMonthsParse let i; for (i = 0; i < 12; i++) { // make the regex if we don't have it already date = new Date(Date.UTC(2000, i)); if (strict && !this._longMonthsParse[i]) { const _months = this.months(date, '', true).replace('.', ''); const _shortMonths = this.monthsShort(date, '', true).replace('.', ''); this._longMonthsParse[i] = new RegExp(`^${_months}$`, 'i'); this._shortMonthsParse[i] = new RegExp(`^${_shortMonths}$`, 'i'); } if (!strict && !this._monthsParse[i]) { regex = `^${this.months(date, '', true)}|^${this.monthsShort(date, '', true)}`; this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i'); } // testing the regex if (strict && format === 'MMMM' && this._longMonthsParse[i].test(monthName)) { return i; } if (strict && format === 'MMM' && this._shortMonthsParse[i].test(monthName)) { return i; } if (!strict && this._monthsParse[i].test(monthName)) { return i; } } } monthsRegex(isStrict) { if (this._monthsParseExact) { if (!hasOwnProp(this, '_monthsRegex')) { this.computeMonthsParse(); } if (isStrict) { return this._monthsStrictRegex; } return this._monthsRegex; } if (!hasOwnProp(this, '_monthsRegex')) { this._monthsRegex = defaultMonthsRegex; } return this._monthsStrictRegex && isStrict ? this._monthsStrictRegex : this._monthsRegex; } monthsShortRegex(isStrict) { if (this._monthsParseExact) { if (!hasOwnProp(this, '_monthsRegex')) { this.computeMonthsParse(); } if (isStrict) { return this._monthsShortStrictRegex; } return this._monthsShortRegex; } if (!hasOwnProp(this, '_monthsShortRegex')) { this._monthsShortRegex = defaultMonthsShortRegex; } return this._monthsShortStrictRegex && isStrict ? this._monthsShortStrictRegex : this._monthsShortRegex; } /** Week */ week(date, isUTC) { return weekOfYear(date, this._week.dow, this._week.doy, isUTC).week; } firstDayOfWeek() { return this._week.dow; } firstDayOfYear() { return this._week.doy; } weekdays(date, format, isUTC) { if (!date) { return isArray(this._weekdays) ? this._weekdays : this._weekdays.standalone; } if (isArray(this._weekdays)) { return this._weekdays[getDay(date, isUTC)]; } const _key = this._weekdays.isFormat.test(format) ? 'format' : 'standalone'; return this._weekdays[_key][getDay(date, isUTC)]; } weekdaysMin(date, format, isUTC) { return date ? this._weekdaysMin[getDay(date, isUTC)] : this._weekdaysMin; } weekdaysShort(date, format, isUTC) { return date ? this._weekdaysShort[getDay(date, isUTC)] : this._weekdaysShort; } // proto.weekdaysParse = localeWeekdaysParse; weekdaysParse(weekdayName, format, strict) { let i; let regex; if (this._weekdaysParseExact) { return this.handleWeekStrictParse(weekdayName, format, strict); } if (!this._weekdaysParse) { this._weekdaysParse = []; this._minWeekdaysParse = []; this._shortWeekdaysParse = []; this._fullWeekdaysParse = []; } for (i = 0; i < 7; i++) { // make the regex if we don't have it already // fix: here is the issue const date = setDayOfWeek(new Date(Date.UTC(2000, 1)), i, null, true); if (strict && !this._fullWeekdaysParse[i]) { this._fullWeekdaysParse[i] = new RegExp(`^${this.weekdays(date, '', true).replace('.', '\.?')}$`, 'i'); this._shortWeekdaysParse[i] = new RegExp(`^${this.weekdaysShort(date, '', true).replace('.', '\.?')}$`, 'i'); this._minWeekdaysParse[i] = new RegExp(`^${this.weekdaysMin(date, '', true).replace('.', '\.?')}$`, 'i'); } if (!this._weekdaysParse[i]) { regex = `^${this.weekdays(date, '', true)}|^${this.weekdaysShort(date, '', true)}|^${this.weekdaysMin(date, '', true)}`; this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i'); } if (!isArray(this._fullWeekdaysParse) || !isArray(this._shortWeekdaysParse) || !isArray(this._minWeekdaysParse) || !isArray(this._weekdaysParse)) { return; } // testing the regex if (strict && format === 'dddd' && this._fullWeekdaysParse[i].test(weekdayName)) { return i; } else if (strict && format === 'ddd' && this._shortWeekdaysParse[i].test(weekdayName)) { return i; } else if (strict && format === 'dd' && this._minWeekdaysParse[i].test(weekdayName)) { return i; } else if (!strict && this._weekdaysParse[i].test(weekdayName)) { return i; } } } // proto.weekdaysRegex = weekdaysRegex; weekdaysRegex(isStrict) { if (this._weekdaysParseExact) { if (!hasOwnProp(this, '_weekdaysRegex')) { this.computeWeekdaysParse(); } if (isStrict) { return this._weekdaysStrictRegex; } else { return this._weekdaysRegex; } } else { if (!hasOwnProp(this, '_weekdaysRegex')) { this._weekdaysRegex = matchWord; } return this._weekdaysStrictRegex && isStrict ? this._weekdaysStrictRegex : this._weekdaysRegex; } } // proto.weekdaysShortRegex = weekdaysShortRegex; // proto.weekdaysMinRegex = weekdaysMinRegex; weekdaysShortRegex(isStrict) { if (this._weekdaysParseExact) { if (!hasOwnProp(this, '_weekdaysRegex')) { this.computeWeekdaysParse(); } if (isStrict) { return this._weekdaysShortStrictRegex; } else { return this._weekdaysShortRegex; } } else { if (!hasOwnProp(this, '_weekdaysShortRegex')) { this._weekdaysShortRegex = matchWord; } return this._weekdaysShortStrictRegex && isStrict ? this._weekdaysShortStrictRegex : this._weekdaysShortRegex; } } weekdaysMinRegex(isStrict) { if (this._weekdaysParseExact) { if (!hasOwnProp(this, '_weekdaysRegex')) { this.computeWeekdaysParse(); } if (isStrict) { return this._weekdaysMinStrictRegex; } else { return this._weekdaysMinRegex; } } else { if (!hasOwnProp(this, '_weekdaysMinRegex')) { this._weekdaysMinRegex = matchWord; } return this._weekdaysMinStrictRegex && isStrict ? this._weekdaysMinStrictRegex : this._weekdaysMinRegex; } } isPM(input) { // IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays // Using charAt should be more compatible. return input.toLowerCase().charAt(0) === 'p'; } meridiem(hours, minutes, isLower) { if (hours > 11) { return isLower ? 'pm' : 'PM'; } return isLower ? 'am' : 'AM'; } formatLongDate(key) { this._longDateFormat = this._longDateFormat ? this._longDateFormat : defaultLongDateFormat; const format = this._longDateFormat[key]; const formatUpper = this._longDateFormat[key.toUpperCase()]; if (format || !formatUpper) { return format; } this._longDateFormat[key] = formatUpper.replace(/MMMM|MM|DD|dddd/g, (val) => { return val.slice(1); }); return this._longDateFormat[key]; } handleMonthStrictParse(monthName, format, strict) { const llc = monthName.toLocaleLowerCase(); let i; let ii; let mom; if (!this._monthsParse) { // this is not used this._monthsParse = []; this._longMonthsParse = []; this._shortMonthsParse = []; for (i = 0; i < 12; ++i) { mom = new Date(2000, i); this._shortMonthsParse[i] = this.monthsShort(mom, '').toLocaleLowerCase(); this._longMonthsParse[i] = this.months(mom, '').toLocaleLowerCase(); } } if (strict) { if (format === 'MMM') { ii = this._shortMonthsParse.indexOf(llc); return ii !== -1 ? ii : null; } ii = this._longMonthsParse.indexOf(llc); return ii !== -1 ? ii : null; } if (format === 'MMM') { ii = this._shortMonthsParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._longMonthsParse.indexOf(llc); return ii !== -1 ? ii : null; } ii = this._longMonthsParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._shortMonthsParse.indexOf(llc); return ii !== -1 ? ii : null; } handleWeekStrictParse(weekdayName, format, strict) { let ii; const llc = weekdayName.toLocaleLowerCase(); if (!this._weekdaysParse) { this._weekdaysParse = []; this._shortWeekdaysParse = []; this._minWeekdaysParse = []; let i; for (i = 0; i < 7; ++i) { const date = setDayOfWeek(new Date(Date.UTC(2000, 1)), i, null, true); this._minWeekdaysParse[i] = this.weekdaysMin(date).toLocaleLowerCase(); this._shortWeekdaysParse[i] = this.weekdaysShort(date).toLocaleLowerCase(); this._weekdaysParse[i] = this.weekdays(date, '').toLocaleLowerCase(); } } if (!isArray(this._weekdaysParse) || !isArray(this._shortWeekdaysParse) || !isArray(this._minWeekdaysParse)) { return; } if (strict) { if (format === 'dddd') { ii = this._weekdaysParse.indexOf(llc); return ii !== -1 ? ii : null; } else if (format === 'ddd') { ii = this._shortWeekdaysParse.indexOf(llc); return ii !== -1 ? ii : null; } else { ii = this._minWeekdaysParse.indexOf(llc); return ii !== -1 ? ii : null; } } else { if (format === 'dddd') { ii = this._weekdaysParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._shortWeekdaysParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._minWeekdaysParse.indexOf(llc); return ii !== -1 ? ii : null; } else if (format === 'ddd') { ii = this._shortWeekdaysParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._weekdaysParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._minWeekdaysParse.indexOf(llc); return ii !== -1 ? ii : null; } else { ii = this._minWeekdaysParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._weekdaysParse.indexOf(llc); if (ii !== -1) { return ii; } ii = this._shortWeekdaysParse.indexOf(llc); return ii !== -1 ? ii : null; } } } computeMonthsParse() { const shortPieces = []; const longPieces = []; const mixedPieces = []; let date; let i; for (i = 0; i < 12; i++) { // make the regex if we don't have it already date = new Date(2000, i); shortPieces.push(this.monthsShort(date, '')); longPieces.push(this.months(date, '')); mixedPieces.push(this.months(date, '')); mixedPieces.push(this.monthsShort(date, '')); } // Sorting makes sure if one month (or abbr) is a prefix of another it // will match the longer piece. shortPieces.sort(cmpLenRev); longPieces.sort(cmpLenRev); mixedPieces.sort(cmpLenRev); for (i = 0; i < 12; i++) { shortPieces[i] = regexEscape(shortPieces[i]); longPieces[i] = regexEscape(longPieces[i]); } for (i = 0; i < 24; i++) { mixedPieces[i] = regexEscape(mixedPieces[i]); } this._monthsRegex = new RegExp(`^(${mixedPieces.join('|')})`, 'i'); this._monthsShortRegex = this._monthsRegex; this._monthsStrictRegex = new RegExp(`^(${longPieces.join('|')})`, 'i'); this._monthsShortStrictRegex = new RegExp(`^(${shortPieces.join('|')})`, 'i'); } computeWeekdaysParse() { const minPieces = []; const shortPieces = []; const longPieces = []; const mixedPieces = []; let i; for (i = 0; i < 7; i++) { // make the regex if we don't have it already // let mom = createUTC([2000, 1]).day(i); const date = setDayOfWeek(new Date(Date.UTC(2000, 1)), i, null, true); const minp = this.weekdaysMin(date); const shortp = this.weekdaysShort(date); const longp = this.weekdays(date); minPieces.push(minp); shortPieces.push(shortp); longPieces.push(longp); mixedPieces.push(minp); mixedPieces.push(shortp); mixedPieces.push(longp); } // Sorting makes sure if one weekday (or abbr) is a prefix of another it // will match the longer piece. minPieces.sort(cmpLenRev); shortPieces.sort(cmpLenRev); longPieces.sort(cmpLenRev); mixedPieces.sort(cmpLenRev); for (i = 0; i < 7; i++) { shortPieces[i] = regexEscape(shortPieces[i]); longPieces[i] = regexEscape(longPieces[i]); mixedPieces[i] = regexEscape(mixedPieces[i]); } this._weekdaysRegex = new RegExp(`^(${mixedPieces.join('|')})`, 'i'); this._weekdaysShortRegex = this._weekdaysRegex; this._weekdaysMinRegex = this._weekdaysRegex; this._weekdaysStrictRegex = new RegExp(`^(${longPieces.join('|')})`, 'i'); this._weekdaysShortStrictRegex = new RegExp(`^(${shortPieces.join('|')})`, 'i'); this._weekdaysMinStrictRegex = new RegExp(`^(${minPieces.join('|')})`, 'i'); } } function cmpLenRev(a, b) { return b.length - a.length; } const defaultCalendar = { sameDay: '[Today at] LT', nextDay: '[Tomorrow at] LT', nextWeek: 'dddd [at] LT', lastDay: '[Yesterday at] LT', lastWeek: '[Last] dddd [at] LT', sameElse: 'L' }; const defaultInvalidDate = 'Invalid date'; const defaultLocaleWeek = { dow: 0, // Sunday is the first day of the week. doy: 6 // The week that contains Jan 1st is the first week of the year. }; const defaultLocaleMeridiemParse = /[ap]\.?m?\.?/i; const defaultRelativeTime = { future: 'in %s', past: '%s ago', s: 'a few seconds', ss: '%d seconds', m: 'a minute', mm: '%d minutes', h: 'an hour', hh: '%d hours', d: 'a day', dd: '%d days', M: 'a month', MM: '%d months', y: 'a year', yy: '%d years' }; const baseConfig = { calendar: defaultCalendar, longDateFormat: defaultLongDateFormat, invalidDate: defaultInvalidDate, ordinal: defaultOrdinal, dayOfMonthOrdinalParse: defaultDayOfMonthOrdinalParse, relativeTime: defaultRelativeTime, months: defaultLocaleMonths, monthsShort: defaultLocaleMonthsShort, week: defaultLocaleWeek, weekdays: defaultLocaleWeekdays, weekdaysMin: defaultLocaleWeekdaysMin, weekdaysShort: defaultLocaleWeekdaysShort, meridiemParse: defaultLocaleMeridiemParse }; // compare two arrays, return the number of differences function compareArrays(array1, array2, dontConvert) { const len = Math.min(array1.length, array2.length); const lengthDiff = Math.abs(array1.length - array2.length); let diffs = 0; let i; for (i = 0; i < len; i++) { if ((dontConvert && array1[i] !== array2[i]) || (!dontConvert && toInt(array1[i]) !== toInt(array2[i]))) { diffs++; } } return diffs + lengthDiff; } // FORMATTING function initWeek() { addFormatToken('w', ['ww', 2, false], 'wo', function (date, opts) { return getWeek(date, opts.locale) .toString(10); }); addFormatToken('W', ['WW', 2, false], 'Wo', function (date) { return getISOWeek(date) .toString(10); }); // ALIASES addUnitAlias('week', 'w'); addUnitAlias('isoWeek', 'W'); // PRIORITIES addUnitPriority('week', 5); addUnitPriority('isoWeek', 5); // PARSING addRegexToken('w', match1to2); addRegexToken('ww', match1to2, match2); addRegexToken('W', match1to2); addRegexToken('WW', match1to2, match2); addWeekParseToken(['w', 'ww', 'W', 'WW'], function (input, week, config, token) { week[token.substr(0, 1)] = toInt(input); return config; }); // export function getSetWeek (input) { // var week = this.localeData().week(this); // return input == null ? week : this.add((input - week) * 7, 'd'); // } } function setWeek(date, input, locale = getLocale()) { const week = getWeek(date, locale); return add(date, (input - week) * 7, 'day'); } function getWeek(date, locale = getLocale(), isUTC) { return locale.week(date, isUTC); } // export function getSetISOWeek (input) { // var week = weekOfYear(this, 1, 4).week; // return input == null ? week : this.add((input - week) * 7, 'd'); // } function setISOWeek(date, input) { const week = getISOWeek(date); return add(date, (input - week) * 7, 'day'); } function getISOWeek(date, isUTC) { return weekOfYear(date, 1, 4, isUTC).week; } // FORMATTING function initWeekYear() { addFormatToken(null, ['gg', 2, false], null, function (date, opts) { // return this.weekYear() % 100; return (getWeekYear(date, opts.locale) % 100).toString(); }); addFormatToken(null, ['GG', 2, false], null, function (date) { // return this.isoWeekYear() % 100; return (getISOWeekYear(date) % 100).toString(); }); addWeekYearFormatToken('gggg', _getWeekYearFormatCb); addWeekYearFormatToken('ggggg', _getWeekYearFormatCb); addWeekYearFormatToken('GGGG', _getISOWeekYearFormatCb); addWeekYearFormatToken('GGGGG', _getISOWeekYearFormatCb); // ALIASES addUnitAlias('weekYear', 'gg'); addUnitAlias('isoWeekYear', 'GG'); // PRIORITY addUnitPriority('weekYear', 1); addUnitPriority('isoWeekYear', 1); // PARSING addRegexToken('G', matchSigned); addRegexToken('g', matchSigned); addRegexToken('GG', match1to2, match2); addRegexToken('gg', match1to2, match2); addRegexToken('GGGG', match1to4, match4); addRegexToken('gggg', match1to4, match4); addRegexToken('GGGGG', match1to6, match6); addRegexToken('ggggg', match1to6, match6); addWeekParseToken(['gggg', 'ggggg', 'GGGG', 'GGGGG'], function (input, week, config, token) { week[token.substr(0, 2)] = toInt(input); return config; }); addWeekParseToken(['gg', 'GG'], function (input, week, config, token) { week[token] = parseTwoDigitYear(input); return config; }); } function addWeekYearFormatToken(token, getter) { addFormatToken(null, [token, token.length, false], null, getter); } function _getWeekYearFormatCb(date, opts) { return getWeekYear(date, opts.locale).toString(); } function _getISOWeekYearFormatCb(date) { return getISOWeekYear(date).toString(); } // MOMENTS function getSetWeekYear(date, input, locale = getLocale(), isUTC) { return getSetWeekYearHelper(date, input, // this.week(), getWeek(date, locale, isUTC), // this.weekday(), getLocaleDayOfWeek(date, locale, isUTC), locale.firstDayOfWeek(), locale.firstDayOfYear(), isUTC); } function getWeekYear(date, locale = getLocale(), isUTC) { return weekOfYear(date, locale.firstDayOfWeek(), locale.firstDayOfYear(), isUTC).year; } function getSetISOWeekYear(date, input, isUTC) { return getSetWeekYearHelper(date, input, getISOWeek(date, isUTC), getISODayOfWeek(date, isUTC), 1, 4); } function getISOWeekYear(date, isUTC) { return weekOfYear(date, 1, 4, isUTC).year; } function getISOWeeksInYear(date, isUTC) { return weeksInYear(getFullYear(date, isUTC), 1, 4); } function getWeeksInYear(date, isUTC, locale = getLocale()) { return weeksInYear(getFullYear(date, isUTC), locale.firstDayOfWeek(), locale.firstDayOfYear()); } function getSetWeekYearHelper(date, input, week, weekday, dow, doy, isUTC) { if (!input) { return getWeekYear(date, void 0, isUTC); } const weeksTarget = weeksInYear(input, dow, doy); const _week = week > weeksTarget ? weeksTarget : week;