ngx-bootstrap
Version:
Native Angular Bootstrap Components
550 lines • 22.1 kB
JavaScript
// tslint:disable:max-file-line-count max-line-length cyclomatic-complexity
import { weekOfYear } from '../units/week-calendar-utils';
import { hasOwnProp, isArray, isFunction } from '../utils/type-checks';
import { getDay, getMonth } from '../utils/date-getters';
import { matchWord, regexEscape } from '../parse/regex';
import { setDayOfWeek } from '../units/day-of-week';
var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s)+MMMM?/;
export var defaultLocaleMonths = 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_');
export var defaultLocaleMonthsShort = 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_');
export var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
export var defaultLocaleWeekdaysShort = 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_');
export var defaultLocaleWeekdaysMin = 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_');
export 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'
};
export var defaultOrdinal = '%d';
export 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) {
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: true,
configurable: true
});
Locale.prototype.ordinal = function (num, token) {
return this._ordinal.replace('%d', num.toString(10));
};
Locale.prototype.preparse = function (str) {
return str;
};
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');
}
// test 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 */
/** Week */
Locale.prototype.week = /** 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;
// proto.weekdaysParse = localeWeekdaysParse;
Locale.prototype.weekdaysParse =
// proto.weekdaysParse = localeWeekdaysParse;
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;
}
// test 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;
// proto.weekdaysRegex = weekdaysRegex;
Locale.prototype.weekdaysRegex =
// proto.weekdaysRegex = 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;
// proto.weekdaysShortRegex = weekdaysShortRegex;
// proto.weekdaysMinRegex = weekdaysMinRegex;
Locale.prototype.weekdaysShortRegex =
// proto.weekdaysShortRegex = weekdaysShortRegex;
// proto.weekdaysMinRegex = weekdaysMinRegex;
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]);
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');
};
return Locale;
}());
export { Locale };
function cmpLenRev(a, b) {
return b.length - a.length;
}
//# sourceMappingURL=locale.class.js.map