ngx-bootstrap
Version:
Angular Bootstrap
1,267 lines (1,245 loc) • 288 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('ngx-bootstrap/chronos', ['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global['ngx-bootstrap'] = global['ngx-bootstrap'] || {}, global['ngx-bootstrap'].chronos = {})));
}(this, (function (exports) { 'use strict';
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/ban-types
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);
}
var 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) {
var coercedNumber = +argumentForCoercion;
var value = 0;
if (coercedNumber !== 0 && isFinite(coercedNumber)) {
value = absFloor(coercedNumber);
}
return value;
}
var aliases = {};
var _mapUnits = {
date: 'day',
hour: 'hours',
minute: 'minutes',
second: 'seconds',
millisecond: 'milliseconds'
};
function addUnitAlias(unit, shorthand) {
var lowerCase = unit.toLowerCase();
var _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) {
var normalizedInput = {};
var normalizedProp;
var 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])
var YEAR = 0;
var MONTH = 1;
var DATE = 2;
var HOUR = 3;
var MINUTE = 4;
var SECOND = 5;
var MILLISECOND = 6;
var WEEK = 7;
var WEEKDAY = 8;
function zeroFill(num, targetLength, forceSign) {
var absNumber = "" + Math.abs(num);
var zerosToFill = targetLength - absNumber.length;
var sign = num >= 0;
var _sign = sign ? (forceSign ? '+' : '') : '-';
// todo: this is crazy slow
var _zeros = Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1);
return (_sign + _zeros + absNumber);
}
var formatFunctions = {};
var formatTokenFunctions = {};
var 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) {
var array = format.match(formattingTokens);
var length = array.length;
var formatArr = new Array(length);
for (var i = 0; i < length; i++) {
formatArr[i] = formatTokenFunctions[array[i]]
? formatTokenFunctions[array[i]]
: removeFormattingTokens(array[i]);
}
return function (date, locale, isUTC, offset) {
if (offset === void 0) { offset = 0; }
var output = '';
for (var j = 0; j < length; j++) {
output += isFunction(formatArr[j])
? formatArr[j].call(null, date, { format: format, locale: locale, isUTC: isUTC, offset: 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
var 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, d, h, M, s, ms) {
if (m === void 0) { m = 0; }
if (d === void 0) { d = 1; }
if (h === void 0) { h = 0; }
if (M === void 0) { M = 0; }
if (s === void 0) { s = 0; }
if (ms === void 0) { ms = 0; }
var 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) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCHours() : date.getHours();
}
function getMinutes(date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCMinutes() : date.getMinutes();
}
function getSeconds(date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCSeconds() : date.getSeconds();
}
function getMilliseconds(date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCMilliseconds() : date.getMilliseconds();
}
function getTime(date) {
return date.getTime();
}
function getDay(date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCDay() : date.getDay();
}
function getDate(date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCDate() : date.getDate();
}
function getMonth(date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return isUTC ? date.getUTCMonth() : date.getMonth();
}
function getFullYear(date, isUTC) {
if (isUTC === void 0) { 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(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(date1, date2) {
if (!date1 || !date2) {
return false;
}
return (isSameYear(date1, date2) &&
isSameMonth(date1, date2) &&
getDate(date1) === getDate(date2));
}
var match1 = /\d/; // 0 - 9
var match2 = /\d\d/; // 00 - 99
var match3 = /\d{3}/; // 000 - 999
var match4 = /\d{4}/; // 0000 - 9999
var match6 = /[+-]?\d{6}/; // -999999 - 999999
var match1to2 = /\d\d?/; // 0 - 99
var match3to4 = /\d\d\d\d?/; // 999 - 9999
var match5to6 = /\d\d\d\d\d\d?/; // 99999 - 999999
var match1to3 = /\d{1,3}/; // 0 - 999
var match1to4 = /\d{1,4}/; // 0 - 9999
var match1to6 = /[+-]?\d{1,6}/; // -999999 - 999999
var matchUnsigned = /\d+/; // 0 - inf
var matchSigned = /[+-]?\d+/; // -inf - inf
var matchOffset = /Z|[+-]\d\d:?\d\d/gi; // +00:00 -00:00 +0000 -0000 or Z
var matchShortOffset = /Z|[+-]\d\d(?::?\d\d)?/gi; // +00 -00 +00:00 -00:00 +0000 -0000 or Z
var 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
var 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;
var 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) {
var _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, function (matched, p1, p2, p3, p4) { return p1 || p2 || p3 || p4; }));
}
function regexEscape(str) {
return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
var tokens = {};
function addParseToken(token, callback) {
var _token = isString(token) ? [token] : token;
var func = callback;
if (isNumber(callback)) {
func = function (input, array, config) {
array[callback] = toInt(input);
return config;
};
}
if (isArray(_token) && isFunction(func)) {
var i = void 0;
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;
}
var 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) {
var 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$1(year, month) {
if (isNaN(year) || isNaN(month)) {
return NaN;
}
var modMonth = mod(month, 12);
var _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) {
var 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;
});
}
var defaultTimeUnit = {
year: 0,
month: 0,
day: 0,
hour: 0,
minute: 0,
seconds: 0
};
function shiftDate(date, unit) {
var _unit = Object.assign({}, defaultTimeUnit, unit);
var year = date.getFullYear() + (_unit.year || 0);
var month = date.getMonth() + (_unit.month || 0);
var day = date.getDate() + (_unit.day || 0);
if (_unit.month && !_unit.day) {
day = Math.min(day, daysInMonth$1(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) {
var _month = getMonth(date, isUTC);
var _date = getDate(date, isUTC);
var _year = getFullYear(date, isUTC);
if (isLeapYear(_year) && _month === 1 && _date === 29) {
var _daysInMonth = daysInMonth$1(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) {
var dayOfMonth = Math.min(getDate(date), daysInMonth$1(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) {
var _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: 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) {
var _unit = unit;
// 'date' is an alias for 'day', so it should be considered as such.
if (_unit === 'date') {
_unit = 'day';
}
var start = startOf(date, _unit, isUTC);
var _step = add(start, 1, _unit === 'isoWeek' ? 'week' : _unit, isUTC);
var 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) {
var date1 = +startOf(date, 'day', isUTC);
var date2 = +startOf(date, 'year', isUTC);
var someDate = date1 - date2;
var oneDay = 1000 * 60 * 60 * 24;
return Math.round(someDate / oneDay) + 1;
}
function setDayOfYear(date, input) {
var 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)
var fwd = dow - doy + 7;
// first-week day local weekday -- which local weekday is fwd
var 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) {
var localWeekday = (7 + weekday - dow) % 7;
var weekOffset = firstWeekOffset(year, dow, doy);
var dayOfYear = 1 + 7 * (week - 1) + localWeekday + weekOffset;
var resYear;
var 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) {
var weekOffset = firstWeekOffset(getFullYear(date, isUTC), dow, doy);
var week = Math.floor((getDayOfYear(date, isUTC) - weekOffset - 1) / 7) + 1;
var resWeek;
var 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) {
var weekOffset = firstWeekOffset(year, dow, doy);
var weekOffsetNext = firstWeekOffset(year + 1, dow, doy);
return (daysInYear(year) - weekOffset + weekOffsetNext) / 7;
}
var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s)+MMMM?/;
var defaultLocaleMonths = 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_');
var defaultLocaleMonthsShort = 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_');
var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
var defaultLocaleWeekdaysShort = 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_');
var defaultLocaleWeekdaysMin = 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_');
var 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'
};
var defaultOrdinal = '%d';
var defaultDayOfMonthOrdinalParse = /\d{1,2}/;
var defaultMonthsShortRegex = matchWord;
var defaultMonthsRegex = matchWord;
var Locale = /** @class */ (function () {
function Locale(config) {
if (config) {
this.set(config);
}
}
Locale.prototype.set = function (config) {
var confKey;
for (confKey in config) {
// eslint-disable-next-line no-prototype-builtins
if (!config.hasOwnProperty(confKey)) {
continue;
}
var prop = config[confKey];
var key = (isFunction(prop) ? confKey : "_" + confKey);
this[key] = prop;
}
this._config = config;
};
Locale.prototype.calendar = function (key, date, now) {
var output = this._calendar[key] || this._calendar.sameElse;
return isFunction(output) ? output.call(null, date, now) : output;
};
Locale.prototype.longDateFormat = function (key) {
var format = this._longDateFormat[key];
var 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];
};
Object.defineProperty(Locale.prototype, "invalidDate", {
get: function () {
return this._invalidDate;
},
set: function (val) {
this._invalidDate = val;
},
enumerable: false,
configurable: true
});
Locale.prototype.ordinal = function (num, token) {
return this._ordinal.replace('%d', num.toString(10));
};
Locale.prototype.preparse = function (str, format) {
return str;
};
Locale.prototype.getFullYear = function (date, isUTC) {
if (isUTC === void 0) { isUTC = false; }
return getFullYear(date, isUTC);
};
Locale.prototype.postformat = function (str) {
return str;
};
Locale.prototype.relativeTime = function (num, withoutSuffix, str, isFuture) {
var output = this._relativeTime[str];
return (isFunction(output)) ?
output(num, withoutSuffix, str, isFuture) :
output.replace(/%d/i, num.toString(10));
};
Locale.prototype.pastFuture = function (diff, output) {
var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
return isFunction(format) ? format(output) : format.replace(/%s/i, output);
};
Locale.prototype.months = function (date, format, isUTC) {
if (isUTC === void 0) { isUTC = false; }
if (!date) {
return isArray(this._months)
? this._months
: this._months.standalone;
}
if (isArray(this._months)) {
return this._months[getMonth(date, isUTC)];
}
var key = (this._months.isFormat || MONTHS_IN_FORMAT).test(format)
? 'format'
: 'standalone';
return this._months[key][getMonth(date, isUTC)];
};
Locale.prototype.monthsShort = function (date, format, isUTC) {
if (isUTC === void 0) { isUTC = false; }
if (!date) {
return isArray(this._monthsShort)
? this._monthsShort
: this._monthsShort.standalone;
}
if (isArray(this._monthsShort)) {
return this._monthsShort[getMonth(date, isUTC)];
}
var key = MONTHS_IN_FORMAT.test(format) ? 'format' : 'standalone';
return this._monthsShort[key][getMonth(date, isUTC)];
};
Locale.prototype.monthsParse = function (monthName, format, strict) {
var date;
var 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
var 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]) {
var _months = this.months(date, '', true).replace('.', '');
var _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;
}
}
};
Locale.prototype.monthsRegex = function (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;
};
Locale.prototype.monthsShortRegex = function (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 */
Locale.prototype.week = function (date, isUTC) {
return weekOfYear(date, this._week.dow, this._week.doy, isUTC).week;
};
Locale.prototype.firstDayOfWeek = function () {
return this._week.dow;
};
Locale.prototype.firstDayOfYear = function () {
return this._week.doy;
};
Locale.prototype.weekdays = function (date, format, isUTC) {
if (!date) {
return isArray(this._weekdays)
? this._weekdays
: this._weekdays.standalone;
}
if (isArray(this._weekdays)) {
return this._weekdays[getDay(date, isUTC)];
}
var _key = this._weekdays.isFormat.test(format)
? 'format'
: 'standalone';
return this._weekdays[_key][getDay(date, isUTC)];
};
Locale.prototype.weekdaysMin = function (date, format, isUTC) {
return date ? this._weekdaysMin[getDay(date, isUTC)] : this._weekdaysMin;
};
Locale.prototype.weekdaysShort = function (date, format, isUTC) {
return date ? this._weekdaysShort[getDay(date, isUTC)] : this._weekdaysShort;
};
// proto.weekdaysParse = localeWeekdaysParse;
Locale.prototype.weekdaysParse = function (weekdayName, format, strict) {
var i;
var 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
var 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;
Locale.prototype.weekdaysRegex = function (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;
Locale.prototype.weekdaysShortRegex = function (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;
}
};
Locale.prototype.weekdaysMinRegex = function (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;
}
};
Locale.prototype.isPM = function (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';
};
Locale.prototype.meridiem = function (hours, minutes, isLower) {
if (hours > 11) {
return isLower ? 'pm' : 'PM';
}
return isLower ? 'am' : 'AM';
};
Locale.prototype.formatLongDate = function (key) {
this._longDateFormat = this._longDateFormat ? this._longDateFormat : defaultLongDateFormat;
var format = this._longDateFormat[key];
var 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];
};
Locale.prototype.handleMonthStrictParse = function (monthName, format, strict) {
var llc = monthName.toLocaleLowerCase();
var i;
var ii;
var 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;
};
Locale.prototype.handleWeekStrictParse = function (weekdayName, format, strict) {
var ii;
var llc = weekdayName.toLocaleLowerCase();
if (!this._weekdaysParse) {
this._weekdaysParse = [];
this._shortWeekdaysParse = [];
this._minWeekdaysParse = [];
var i = void 0;
for (i = 0; i < 7; ++i) {
var 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;
}
}
};
Locale.prototype.computeMonthsParse = function () {
var shortPieces = [];
var longPieces = [];
var mixedPieces = [];
var date;
var 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');
};
Locale.prototype.computeWeekdaysParse = function () {
var minPieces = [];
var shortPieces = [];
var longPieces = [];
var mixedPieces = [];
var i;
for (i = 0; i < 7; i++) {
// make the regex if we don't have it already
// let mom = createUTC([2000, 1]).day(i);
var date = setDayOfWeek(new Date(Date.UTC(2000, 1)), i, null, true);
var minp = this.weekdaysMin(date);
var shortp = this.weekdaysShort(date);
var 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]);
mixedPie