ngx-bootstrap
Version:
Angular Bootstrap
708 lines • 23.6 kB
JavaScript
import { add, subtract } from '../index';
import { getDate, getFullYear, getHours, getMilliseconds, getMinutes, getMonth, getSeconds } from '../utils/date-getters';
import { setDate, setFullYear, setHours, setMilliseconds, setMinutes, setMonth, setSeconds } from '../utils/date-setters';
import { cloneDate } from '../create/clone';
import { isArray, isBoolean, isDate, isDateValid, isFunction, isNumber, isObject, isString, isUndefined } from '../utils/type-checks';
import { formatDate } from '../format';
import { ISO_8601, RFC_2822 } from '../create/from-string-and-format';
import { getDateOffset, getUTCOffset, hasAlignedHourOffset, isDaylightSavingTime, setOffsetToParsedOffset, setUTCOffset } from '../units/offset';
import { isLeapYear, parseTwoDigitYear } from '../units/year';
import { isAfter, isBefore, isBetween, isSame, isSameOrAfter, isSameOrBefore } from '../utils/date-compare';
import { daysInMonth } from '../units/month';
import { getDayOfWeek, getISODayOfWeek, getLocaleDayOfWeek, parseWeekday, setDayOfWeek, setISODayOfWeek, setLocaleDayOfWeek } from '../units/day-of-week';
import { getISOWeek, getWeek, setISOWeek, setWeek } from '../units/week';
import { getISOWeeksInYear, getISOWeekYear, getSetISOWeekYear, getSetWeekYear, getWeeksInYear, getWeekYear } from '../units/week-year';
import { endOf, startOf } from '../utils/start-end-of';
import { getQuarter, setQuarter } from '../units/quarter';
import { getDayOfYear, setDayOfYear } from '../units/day-of-year';
import { getZoneAbbr, getZoneName } from '../units/timezone';
import { diff } from '../moment/diff';
import { calendar } from '../moment/calendar';
import { defineLocale, getLocale, getSetGlobalLocale, listLocales } from '../locale/locales';
import { max, min } from '../moment/min-max';
import { isDuration } from '../duration/constructor';
import { createLocalOrUTC } from '../create/from-anything';
import { createDuration } from '../duration/create';
export const moment = _moment;
function _moment(input, format, localeKey, strict, isUTC) {
if (input instanceof Khronos) {
const _date = input.clone();
return isUTC ? _date.utc() : _date;
}
if (isBoolean(localeKey)) {
return new Khronos(input, format, null, localeKey, isUTC);
}
return new Khronos(input, format, localeKey, strict, isUTC);
}
moment.utc = (input, format, localeKey, strict) => {
return _moment(input, format, localeKey, strict, true);
};
moment.parseZone = (input, format, localeKey, strict) => {
return _moment(input, format, localeKey, strict, true).parseZone();
};
moment.locale = getSetGlobalLocale;
moment.localeData = (key) => {
if (key instanceof Khronos) {
return key.localeData();
}
return getLocale(key);
};
// moment.utc = createUTC;
moment.unix = (inp) => new Khronos(inp * 1000);
moment.ISO_8601 = ISO_8601;
moment.RFC_2822 = RFC_2822;
moment.defineLocale = defineLocale;
moment.parseTwoDigitYear = parseTwoDigitYear;
moment.isDate = isDate;
moment.invalid = function _invalid() {
return new Khronos(new Date(NaN));
};
// duration(inp?: Duration | DateInput | Khronos, unit?: MomentUnitOfTime): Duration;
moment.duration = (input, unit) => {
const _unit = mapUnitOfTime(unit);
if (isDate(input)) {
throw new Error('todo implement');
}
if (input == null) {
return createDuration();
}
if (isDuration(input)) {
return createDuration(input, _unit, { _locale: input._locale });
}
if (isString(input) || isNumber(input) || isDuration(input) || isObject(input)) {
return createDuration(input, _unit);
}
throw new Error('todo implement');
};
moment.min = function _min(...dates) {
const _firstArg = dates[0];
const _dates = (isArray(_firstArg) ? _firstArg : dates)
.map((date) => _moment(date))
.map(date => date.toDate());
const _date = min(..._dates);
return new Khronos(_date);
};
moment.max = function _max(...dates) {
const _firstArg = dates[0];
const _dates = (isArray(_firstArg) ? _firstArg : dates)
.map((date) => _moment(date))
.map(date => date.toDate());
const _date = max(..._dates);
return new Khronos(_date);
};
moment.locales = () => {
return listLocales();
};
const _unitsPriority = {
year: 1,
month: 8,
week: 5,
isoWeek: 5,
day: 11,
weekday: 11,
isoWeekday: 11,
hours: 13,
weekYear: 1,
isoWeekYear: 1,
quarter: 7,
date: 9,
dayOfYear: 4,
minutes: 14,
seconds: 15,
milliseconds: 16
};
// todo: do I need 2 mappers?
const _timeHashMap = {
y: 'year',
years: 'year',
year: 'year',
M: 'month',
months: 'month',
month: 'month',
w: 'week',
weeks: 'week',
week: 'week',
d: 'day',
days: 'day',
day: 'day',
date: 'date',
dates: 'date',
D: 'date',
h: 'hours',
hour: 'hours',
hours: 'hours',
m: 'minutes',
minute: 'minutes',
minutes: 'minutes',
s: 'seconds',
second: 'seconds',
seconds: 'seconds',
ms: 'milliseconds',
millisecond: 'milliseconds',
milliseconds: 'milliseconds',
quarter: 'quarter',
quarters: 'quarter',
q: 'quarter',
Q: 'quarter',
isoWeek: 'isoWeek',
isoWeeks: 'isoWeek',
W: 'isoWeek',
weekYear: 'weekYear',
weekYears: 'weekYear',
gg: 'weekYears',
isoWeekYear: 'isoWeekYear',
isoWeekYears: 'isoWeekYear',
GG: 'isoWeekYear',
dayOfYear: 'dayOfYear',
dayOfYears: 'dayOfYear',
DDD: 'dayOfYear',
weekday: 'weekday',
weekdays: 'weekday',
e: 'weekday',
isoWeekday: 'isoWeekday',
isoWeekdays: 'isoWeekday',
E: 'isoWeekday'
};
function mapUnitOfTime(period) {
return _timeHashMap[period];
}
function mapMomentInputObject(obj) {
const _res = {};
return Object.keys(obj)
.reduce((res, key) => {
res[mapUnitOfTime(key)] = obj[key];
return res;
}, _res);
}
export class Khronos {
constructor(input, format, localeKey, strict = false, isUTC = false, offset) {
this._date = new Date();
this._isUTC = false;
// locale will be needed to format invalid date message
this._locale = getLocale(localeKey);
// parse invalid input
if (input === '' || input === null || (isNumber(input) && isNaN(input))) {
this._date = new Date(NaN);
return this;
}
this._isUTC = isUTC;
if (this._isUTC) {
this._offset = 0;
}
if (offset || offset === 0) {
this._offset = offset;
}
this._isStrict = strict;
this._format = format;
if (!input && input !== 0 && !format) {
this._date = new Date();
return this;
}
if (isDate(input)) {
this._date = cloneDate(input);
return this;
}
// this._date = parseDate(input, format, localeKey, strict, isUTC);
const config = createLocalOrUTC(input, format, localeKey, strict, isUTC);
this._date = config._d;
this._offset = isNumber(config._offset) ? config._offset : this._offset;
this._isUTC = config._isUTC;
this._isStrict = config._strict;
this._format = config._f;
this._tzm = config._tzm;
}
_toConfig() {
return { _isUTC: this._isUTC, _locale: this._locale, _offset: this._offset, _tzm: this._tzm };
}
locale(localeKey) {
if (isUndefined(localeKey)) {
return this._locale._abbr;
}
if (localeKey instanceof Khronos) {
this._locale = localeKey._locale;
return this;
}
const newLocaleData = getLocale(localeKey);
if (newLocaleData != null) {
this._locale = newLocaleData;
}
return this;
}
localeData() {
return this._locale;
}
// Basic
add(val, period) {
if (isString(val)) {
this._date = add(this._date, parseInt(val, 10), mapUnitOfTime(period));
}
if (isNumber(val)) {
this._date = add(this._date, val, mapUnitOfTime(period));
}
if (isObject(val)) {
const _mapped = mapMomentInputObject(val);
Object.keys(_mapped)
.forEach((key) => add(this._date, _mapped[key], key));
}
return this;
}
// fixme: for some reason here 'null' for time is fine
calendar(time, formats) {
const _time = time instanceof Khronos ? time : new Khronos(time || new Date());
const _offset = (this._offset || 0) - (_time._offset || 0);
const _config = Object.assign(this._toConfig(), { _offset });
return calendar(this._date, _time._date, formats, this._locale, _config);
}
clone() {
const localeKey = this._locale && this._locale._abbr || 'en';
// return new Khronos(cloneDate(this._date), this._format, localeKey, this._isStrict, this._isUTC);
// fails if isUTC and offset
// return new Khronos(new Date(this.valueOf()),
return new Khronos(this._date, this._format, localeKey, this._isStrict, this._isUTC, this._offset);
}
diff(b, unitOfTime, precise) {
const unit = mapUnitOfTime(unitOfTime);
const _b = b instanceof Khronos ? b : new Khronos(b);
// const zoneDelta = (_b.utcOffset() - this.utcOffset());
// const config = Object.assign(this._toConfig(), {
// _offset: 0,
// _isUTC: true,
// _zoneDelta: zoneDelta
// });
// return diff(new Date(this.valueOf()), new Date(_b.valueOf()), unit, precise, config);
return diff(this._date, _b.toDate(), unit, precise, this._toConfig());
}
endOf(period) {
const _per = mapUnitOfTime(period);
this._date = endOf(this._date, _per, this._isUTC);
return this;
}
format(format) {
return formatDate(this._date, format, this._locale && this._locale._abbr, this._isUTC, this._offset);
}
// todo: implement
from(time, withoutSuffix) {
const _time = _moment(time);
if (this.isValid() && _time.isValid()) {
return createDuration({ to: this.toDate(), from: _time.toDate() })
.locale(this.locale())
.humanize(!withoutSuffix);
}
return this.localeData().invalidDate;
}
fromNow(withoutSuffix) {
return this.from(new Date(), withoutSuffix);
}
to(inp, suffix) {
throw new Error(`TODO: Implement`);
}
toNow(withoutPrefix) {
throw new Error(`TODO: Implement`);
}
subtract(val, period) {
if (isString(val)) {
this._date = subtract(this._date, parseInt(val, 10), mapUnitOfTime(period));
return this;
}
if (isNumber(val)) {
this._date = subtract(this._date, val, mapUnitOfTime(period));
}
if (isObject(val)) {
const _mapped = mapMomentInputObject(val);
Object.keys(_mapped)
.forEach((key) => subtract(this._date, _mapped[key], key));
}
return this;
}
get(period) {
if (period === 'dayOfYear') {
return this.dayOfYear();
}
const unit = mapUnitOfTime(period);
switch (unit) {
case 'year':
return this.year();
case 'month':
return this.month();
// | 'week'
case 'date':
return this.date();
case 'day':
return this.day();
case 'hours':
return this.hours();
case 'minutes':
return this.minutes();
case 'seconds':
return this.seconds();
case 'milliseconds':
return this.milliseconds();
case 'week':
return this.week();
case 'isoWeek':
return this.isoWeek();
case 'weekYear':
return this.weekYear();
case 'isoWeekYear':
return this.isoWeekYear();
case 'weekday':
return this.weekday();
case 'isoWeekday':
return this.isoWeekday();
case 'quarter':
return this.quarter();
default:
throw new Error(`Unknown moment.get('${period}')`);
}
}
set(period, input) {
if (isString(period)) {
const unit = mapUnitOfTime(period);
switch (unit) {
case 'year':
return this.year(input);
case 'month':
return this.month(input);
// | 'week'
case 'day':
return this.day(input);
case 'date':
return this.date(input);
case 'hours':
return this.hours(input);
case 'minutes':
return this.minutes(input);
case 'seconds':
return this.seconds(input);
case 'milliseconds':
return this.milliseconds(input);
case 'week':
return this.week(input);
case 'isoWeek':
return this.isoWeek(input);
case 'weekYear':
return this.weekYear(input);
case 'isoWeekYear':
return this.isoWeekYear(input);
case 'weekday':
return this.weekday(input);
case 'isoWeekday':
return this.isoWeekday(input);
case 'quarter':
return this.quarter(input);
default:
throw new Error(`Unknown moment.get('${period}')`);
}
}
if (isObject(period)) {
const _mapped = mapMomentInputObject(period);
Object.keys(_mapped)
.sort(function (a, b) {
return _unitsPriority[a] - _unitsPriority[b];
})
.forEach((key) => this.set(key, _mapped[key]));
}
return this;
}
toString() {
return this.format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
}
toISOString() {
if (!this.isValid()) {
return null;
}
if (getFullYear(this._date, true) < 0 || getFullYear(this._date, true) > 9999) {
return this.format('YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
}
if (isFunction(Date.prototype.toISOString)) {
// native implementation is ~50x faster, use it when we can
return this.toDate().toISOString();
}
return this.format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
}
inspect() {
throw new Error('TODO: implement');
}
toJSON() {
return this.toISOString();
}
toDate() {
return new Date(this.valueOf());
}
toObject() {
return {
// years: getFullYear(this._date, this._isUTC),
// months: getMonth(this._date, this._isUTC),
year: getFullYear(this._date, this._isUTC),
month: getMonth(this._date, this._isUTC),
date: getDate(this._date, this._isUTC),
hours: getHours(this._date, this._isUTC),
minutes: getMinutes(this._date, this._isUTC),
seconds: getSeconds(this._date, this._isUTC),
milliseconds: getMilliseconds(this._date, this._isUTC)
};
}
toArray() {
return [this.year(), this.month(), this.date(), this.hour(), this.minute(), this.second(), this.millisecond()];
}
// Dates boolean algebra
isAfter(date, unit) {
const _unit = unit ? mapUnitOfTime(unit) : void 0;
return isAfter(this._date, date.toDate(), _unit);
}
isBefore(date, unit) {
const _unit = unit ? mapUnitOfTime(unit) : void 0;
return isBefore(this.toDate(), date.toDate(), _unit);
}
isBetween(from, to, unit, inclusivity) {
const _unit = unit ? mapUnitOfTime(unit) : void 0;
return isBetween(this.toDate(), from.toDate(), to.toDate(), _unit, inclusivity);
}
isSame(date, unit) {
const _unit = unit ? mapUnitOfTime(unit) : void 0;
return isSame(this._date, date.toDate(), _unit);
}
isSameOrAfter(date, unit) {
const _unit = unit ? mapUnitOfTime(unit) : void 0;
return isSameOrAfter(this._date, date.toDate(), _unit);
}
isSameOrBefore(date, unit) {
const _unit = unit ? mapUnitOfTime(unit) : void 0;
return isSameOrBefore(this._date, date.toDate(), _unit);
}
isValid() {
return isDateValid(this._date);
}
valueOf() {
return this._date.valueOf() - ((this._offset || 0) * 60000);
}
unix() {
// return getUnixTime(this._date);
return Math.floor(this.valueOf() / 1000);
}
utcOffset(b, keepLocalTime) {
const _config = this._toConfig();
if (!b && b !== 0) {
return getUTCOffset(this._date, _config);
}
this._date = setUTCOffset(this._date, b, keepLocalTime, false, _config);
this._offset = _config._offset;
this._isUTC = _config._isUTC;
return this;
}
utc(keepLocalTime) {
return this.utcOffset(0, keepLocalTime);
}
local(keepLocalTime) {
if (this._isUTC) {
this.utcOffset(0, keepLocalTime);
this._isUTC = false;
if (keepLocalTime) {
this.subtract(getDateOffset(this._date), 'm');
}
}
return this;
}
parseZone(input) {
const _config = this._toConfig();
this._date = setOffsetToParsedOffset(this._date, input, _config);
this._offset = _config._offset;
this._isUTC = _config._isUTC;
return this;
}
hasAlignedHourOffset(input) {
return hasAlignedHourOffset(this._date, input ? input._date : void 0);
}
isDST() {
return isDaylightSavingTime(this._date);
}
isLocal() {
return !this._isUTC;
}
isUtcOffset() {
return this._isUTC;
}
isUTC() {
return this.isUtc();
}
isUtc() {
return this._isUTC && this._offset === 0;
}
// Timezone
zoneAbbr() {
return getZoneAbbr(this._isUTC);
}
zoneName() {
return getZoneName(this._isUTC);
}
year(year) {
if (!year && year !== 0) {
return getFullYear(this._date, this._isUTC);
}
this._date = cloneDate(setFullYear(this._date, year));
return this;
}
weekYear(val) {
if (!val && val !== 0) {
return getWeekYear(this._date, this._locale, this.isUTC());
}
const date = getSetWeekYear(this._date, val, this._locale, this.isUTC());
if (isDate(date)) {
this._date = date;
}
return this;
}
isoWeekYear(val) {
if (!val && val !== 0) {
return getISOWeekYear(this._date, this.isUTC());
}
const date = getSetISOWeekYear(this._date, val, this.isUtc());
if (isDate(date)) {
this._date = date;
}
return this;
}
isLeapYear() {
return isLeapYear(getFullYear(this.toDate(), this.isUTC()));
}
month(month) {
if (!month && month !== 0) {
return getMonth(this._date, this._isUTC);
}
let _month = month;
if (isString(month)) {
const locale = this._locale || getLocale();
_month = locale.monthsParse(month);
}
if (isNumber(_month)) {
this._date = cloneDate(setMonth(this._date, _month, this._isUTC));
}
return this;
}
hour(hours) {
return this.hours(hours);
}
hours(hours) {
if (!hours && hours !== 0) {
return getHours(this._date, this._isUTC);
}
this._date = cloneDate(setHours(this._date, hours, this._isUTC));
return this;
}
minute(minutes) {
return this.minutes(minutes);
}
minutes(minutes) {
if (!minutes && minutes !== 0) {
return getMinutes(this._date, this._isUTC);
}
this._date = cloneDate(setMinutes(this._date, minutes, this._isUTC));
return this;
}
second(seconds) {
return this.seconds(seconds);
}
seconds(seconds) {
if (!seconds && seconds !== 0) {
return getSeconds(this._date, this._isUTC);
}
this._date = cloneDate(setSeconds(this._date, seconds, this._isUTC));
return this;
}
millisecond(ms) {
return this.milliseconds(ms);
}
milliseconds(seconds) {
if (!seconds && seconds !== 0) {
return getMilliseconds(this._date, this._isUTC);
}
this._date = cloneDate(setMilliseconds(this._date, seconds, this._isUTC));
return this;
}
date(date) {
if (!date && date !== 0) {
return getDate(this._date, this._isUTC);
}
this._date = cloneDate(setDate(this._date, date, this._isUTC));
return this;
}
day(input) {
if (!input && input !== 0) {
return getDayOfWeek(this._date, this._isUTC);
}
let _input = input;
if (isString(input)) {
_input = parseWeekday(input, this._locale);
}
if (isNumber(_input)) {
this._date = setDayOfWeek(this._date, _input, this._locale, this._isUTC);
}
return this;
}
weekday(val) {
if (!val && val !== 0) {
return getLocaleDayOfWeek(this._date, this._locale, this._isUTC);
}
this._date = setLocaleDayOfWeek(this._date, val, { locale: this._locale, isUTC: this._isUTC });
return this;
}
isoWeekday(val) {
if (!val && val !== 0) {
return getISODayOfWeek(this._date);
}
this._date = setISODayOfWeek(this._date, val);
return this;
}
dayOfYear(val) {
if (!val && val !== 0) {
return getDayOfYear(this._date);
}
this._date = setDayOfYear(this._date, val);
return this;
}
week(input) {
if (!input && input !== 0) {
return getWeek(this._date, this._locale);
}
this._date = setWeek(this._date, input, this._locale);
return this;
}
weeks(input) {
return this.week(input);
}
isoWeek(val) {
if (!val && val !== 0) {
return getISOWeek(this._date);
}
this._date = setISOWeek(this._date, val);
return this;
}
isoWeeks(val) {
return this.isoWeek(val);
}
weeksInYear() {
return getWeeksInYear(this._date, this._isUTC, this._locale);
}
isoWeeksInYear() {
return getISOWeeksInYear(this._date, this._isUTC);
}
daysInMonth() {
return daysInMonth(getFullYear(this._date, this._isUTC), getMonth(this._date, this._isUTC));
}
quarter(val) {
if (!val && val !== 0) {
return getQuarter(this._date, this._isUTC);
}
this._date = setQuarter(this._date, val, this._isUTC);
return this;
}
quarters(val) {
return this.quarter(val);
}
startOf(period) {
const _per = mapUnitOfTime(period);
this._date = startOf(this._date, _per, this._isUTC);
return this;
}
}
//# sourceMappingURL=chain.js.map