react-application-core
Version:
A react-based application core for the business applications.
1,301 lines • 51.3 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateConverter = void 0;
var inversify_1 = require("inversify");
var R = require("ramda");
var moment = require("moment");
require("moment-timezone");
var di_1 = require("../../di");
var definitions_interface_1 = require("../../definitions.interface");
var util_1 = require("../../util");
var definition_1 = require("../../definition");
/**
* @stable [28.12.2020]
* @param cronDayFactory
*/
var getCalendarWeekEntities = function (cronDayFactory) {
if (cronDayFactory === void 0) { cronDayFactory = function (index) { return index; }; }
return (__assign({ id: 0 }, R.mergeAll(util_1.ArrayUtils
.makeArray(definition_1.DefaultEntities.NUMBER_OF_DAYS_PER_WEEK)
.map(function (_, index) {
var _a;
return (_a = {},
_a[index] = {
cronDay: cronDayFactory(index),
current: true,
day: index,
},
_a);
}))));
};
var DateConverter = /** @class */ (function () {
/**
* @stable [22.01.2020]
*/
function DateConverter() {
this.appOnlineStartingDate = Date.now();
this.environment.setVariable(definition_1.EnvironmentGlobalVariablesEnum.DATE_CONVERTER, this);
}
DateConverter_1 = DateConverter;
Object.defineProperty(DateConverter.prototype, "currentDate", {
/**
* @stable [15.01.2021]
*/
get: function () {
return this.dateTimeSettings.currentDate;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "uiDateFormat", {
/**
* @stable [23.09.2020]
*/
get: function () {
return this.dateTimeSettings.uiDateFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "weekdays", {
/**
* @stable [14.01.2021]
*/
get: function () {
return this.getWeekdays();
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "weekdaysShort", {
/**
* @stable [10.08.2021]
*/
get: function () {
return this.getWeekdaysShort();
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "weekdaysShortest", {
/**
* @stable [14.01.2021]
*/
get: function () {
return this.getWeekdaysShortest();
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "appOnlineLifeTimeInSeconds", {
/**
* @stable [22.01.2020]
* @returns {number}
*/
get: function () {
return Math.round((Date.now() - this.appOnlineStartingDate) * 0.001);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "appOnlineLifeTimeInHours", {
/**
* @stable [22.01.2020]
* @returns {number}
*/
get: function () {
return Math.round(this.appOnlineLifeTimeInSeconds / 3600);
},
enumerable: false,
configurable: true
});
/**
* @stable [20.10.2020]
* @param date1
* @param date2
*/
DateConverter.prototype.compare = function (date1, date2) {
var date1$ = date1;
var date2$ = date2;
return R.equals(date1, date2)
? 0
: (date1$ instanceof Date && date2$ instanceof Date
? date1$ === date2$ ? 0 : (date1$ > date2$ ? 1 : -1)
: NaN);
};
/**
* @stable [20.10.2020]
* @param date1
* @param date2
*/
DateConverter.prototype.equal = function (date1, date2) {
return this.compare(date1, date2) === 0;
};
/**
* @deprecated
*/
DateConverter.prototype.format = function (date, inputFormat, outputFormat) {
if (R.isNil(date) || R.isEmpty(date)) {
return '';
}
else {
var momentDate = this.toMomentDate(date, inputFormat);
return momentDate.isValid()
? momentDate.format(outputFormat)
: String(date);
}
};
/**
* @stable [26.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.dateAsString = function (cfg) {
var date = cfg.date, outputFormat = cfg.outputFormat;
if (date instanceof Date || util_1.ObjectUtils.isObjectNotEmpty(date)) {
return this.processValidMomentDate(cfg, function (mDate) { return mDate.format(outputFormat); }) || String(date);
}
else {
return util_1.ConditionUtils.orUndef(!cfg.returnUndef, function () { return ''; });
}
};
/**
* @stable [03.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asStartUnitOf = function (cfg) {
return this.processValidMomentDate(__assign({ date: this.getCurrentDate() }, cfg), function (mDate) { return mDate.startOf(cfg.unit); });
};
/**
* @stable [07.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {IDayOfYearEntity}
*/
DateConverter.prototype.asDayOfYearEntity = function (cfg) {
return this.processValidMomentDate(cfg, function (mDate) { return ({ day: mDate.date(), month: mDate.month(), year: mDate.year() }); });
};
/**
* @stable [03.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asEndUnitOf = function (cfg) {
return this.processValidMomentDate(__assign({ date: this.getCurrentDate() }, cfg), function (mDate) { return mDate.endOf(cfg.unit); });
};
/**
* @stable [21.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asFirstDayOfWeek = function (cfg) {
return this.asStartUnitOf(__assign(__assign({}, cfg), { unit: this.weekUnit }));
};
/**
* @stable [03.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asFirstDayOfMonth = function (cfg) {
return this.asStartUnitOf(__assign(__assign({}, cfg), { unit: 'month' }));
};
/**
* @stable [21.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asFirstDayOfQuarter = function (cfg) {
return this.asStartUnitOf(__assign(__assign({}, cfg), { unit: 'quarter' }));
};
/**
* @stable [21.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asFirstDayOfYear = function (cfg) {
return this.asStartUnitOf(__assign(__assign({}, cfg), { unit: 'year' }));
};
/**
* @stable [21.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asFirstDayOfWeekAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asFirstDayOfWeek(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [06.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asFirstDayOfMonthAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asFirstDayOfMonth(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [21.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asFirstDayOfQuarterAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asFirstDayOfQuarter(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [21.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asFirstDayOfYearAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asFirstDayOfYear(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [03.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asLastDayOfQuarter = function (cfg) {
return this.asEndUnitOf(__assign(__assign({}, cfg), { unit: 'quarter' }));
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asLastDayOfQuarterAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asLastDayOfQuarter(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [03.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asLastDayOfMonth = function (cfg) {
return this.asEndUnitOf(__assign(__assign({}, cfg), { unit: 'month' }));
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asLastDayOfMonthAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asLastDayOfMonth(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asLastDayOfWeek = function (cfg) {
return this.asEndUnitOf(__assign(__assign({}, cfg), { unit: this.weekUnit }));
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asLastDayOfWeekAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asLastDayOfWeek(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [08.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asLastDayOfYear = function (cfg) {
return this.asEndUnitOf(__assign(__assign({}, cfg), { unit: 'year' }));
};
/**
* @stable [08.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asLastDayOfYearAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.asLastDayOfYear(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [02.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.addDuration = function (cfg) {
return this.processValidMomentDate(cfg, function (mDate) { return mDate.add(cfg.duration, cfg.unit); });
};
/**
* @stable [06.01.2020]
* @tested
* @param {IDateTimeConfigEntity} cfg
* @returns {number}
*/
DateConverter.prototype.asAbsoluteDayOfYear = function (cfg) {
return this.processValidMomentDate(__assign({ date: this.getCurrentDate() }, cfg), function (mDate) { return mDate.dayOfYear(); });
};
/**
* @stable [02.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {moment.Moment}
*/
DateConverter.prototype.addDays = function (cfg) {
return this.addDuration(__assign(__assign({}, cfg), { unit: 'days' }));
};
/**
* @stable [08.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.addDaysAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.addDays(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [03.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.addMonths = function (cfg) {
return this.addDuration(__assign(__assign({}, cfg), { unit: 'months' }));
};
/**
* @stable [06.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.addMonthsAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.addMonths(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [06.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.addQuartersAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.addQuarters(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.addQuarters = function (cfg) {
return this.addDuration(__assign(__assign({}, cfg), { unit: 'quarters' }));
};
/**
* @stable [08.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.addYears = function (cfg) {
return this.addDuration(__assign(__assign({}, cfg), { unit: 'years' }));
};
/**
* @stable [08.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.addYearsAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.addYears(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.addWeeks = function (cfg) {
return this.addDuration(__assign(__assign({}, cfg), { unit: 'weeks' }));
};
/**
* @stable [07.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.addWeeksAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.addWeeks(cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @stable [02.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.addDaysToUiDate = function (cfg) {
return this.addDays(__assign(__assign({}, cfg), { inputFormat: this.uiDateFormat }));
};
/**
* @stable [02.01.2019]
* @tested
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.addDaysToUiDateAsDate = function (cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.addDaysToUiDate(cfg), function (momentDate) { return momentDate.toDate(); });
};
/**
* @stable [05.11.2020]
* @param cfg
*/
DateConverter.prototype.asDaysOfMonth = function (cfg) {
var _this = this;
var firstDayOfMonth = this.asFirstDayOfMonthAsDate(cfg);
return util_1.ArrayUtils
.makeArray(this.asLastDayOfMonthAsDate(cfg).getDate())
.map(function (_, index) { return _this.addDays({ date: firstDayOfMonth, duration: index }); });
};
/**
* @stable [05.11.2020]
* @param cfg
*/
DateConverter.prototype.asDaysOfMonthAsDates = function (cfg) {
var _this = this;
return this.asDaysOfMonth(cfg).map(function (date) { return _this.asDate({ date: date }); });
};
/**
* @stable [23.01.2021]
* @param cfg
*/
DateConverter.prototype.fromDateTimeToDateString = function (cfg) {
return this.dateAsString(__assign(__assign({}, cfg), { inputFormat: this.dateTimeFormat }));
};
/**
* @stable [23.01.2021]
* @param cfg
*/
DateConverter.prototype.fromDateTimeToPstTimeString = function (cfg) {
return this.fromDateTimeToDateString(__assign(__assign({}, cfg), { outputFormat: this.pstTimeFormat }));
};
/**
* @deprecated
*/
DateConverter.prototype.fromDateTimeToArbitraryFormat = function (date, outputFormat) {
return this.format(date, this.dateTimeFormat, outputFormat);
};
/**
* @stable [09.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.dateAsPstDateString = function (cfg) {
return this.dateAsString(__assign(__assign({}, cfg), { outputFormat: this.pstDateFormat }));
};
/**
* @stable [09.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.dateAsPstTimeString = function (cfg) {
return this.dateAsString(__assign(__assign({}, cfg), { outputFormat: this.pstTimeFormat }));
};
/**
* @deprecated
*/
DateConverter.prototype.fromDateTimeToDate = function (date) {
return this.fromDateTimeToArbitraryFormat(date, this.dateFormat);
};
/**
* @stable [02.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.dateAsDateTimeString = function (cfg) {
return this.dateAsString(__assign(__assign({}, cfg), { outputFormat: this.dateTimeFormat }));
};
/**
* @stable [05.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.dateAsDateString = function (cfg) {
return this.dateAsString(__assign(__assign({}, cfg), { outputFormat: this.dateFormat }));
};
/**
* @stable [26.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.dateAsUiDateString = function (cfg) {
return this.dateAsString(__assign({ outputFormat: this.uiDateFormat }, cfg));
};
/**
* @stable [07.12.2020]
* @param cfg
*/
DateConverter.prototype.fromDateTimeToUiDateTimeString = function (cfg) {
return this.dateAsString(__assign(__assign({ inputFormat: this.dateTimeFormat }, cfg), { outputFormat: this.concatUiDateTime(cfg.outputFormat, cfg.outputTimeFormat) }));
};
/**
* @stable [09.11.2018]
* @param {DateTimeLikeTypeT} date [Example: 2018-04-07T20:54:45+03:00]
* @returns {string} [Example: 20:54:45]
*/
DateConverter.prototype.fromDateTimeToTime = function (date) {
return this.fromDateTimeToArbitraryFormat(date, this.timeFormat);
};
/**
* @deptecated
*/
DateConverter.prototype.fromDateTimeToDateTime = function (date) {
return this.fromDateTimeToArbitraryFormat(date, this.dateTimeFormat);
};
/**
* @stable [21.12.2020]
* @param cfg
*/
DateConverter.prototype.fromUiDateTimeToDateTimeString = function (cfg) {
return this.dateAsString(__assign(__assign({ outputFormat: this.dateTimeFormat }, cfg), { date: (cfg.date + " " + util_1.NvlUtils.nvl(cfg.time, this.uiDefaultTime)).trim(), inputFormat: (util_1.NvlUtils.nvl(cfg.inputFormat, this.uiDateFormat) + " " + util_1.NvlUtils.nvl(cfg.inputTimeFormat, this.uiTimeFormat)).trim() }));
};
/**
* @stable [03.05.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {number}
*/
DateConverter.prototype.asWeekdayNumber = function (cfg) {
var _this = this;
return this.processValidMomentDate(__assign({ date: this.getCurrentDate() }, cfg), function (mDate) { return _this.isIsoWeek ? mDate.isoWeekday() : mDate.weekday(); });
};
/**
* @stable [14.01.2020]
* @param cfg
*/
DateConverter.prototype.isoWeekDayAsOrdinaryDay = function (cfg) {
var index = cfg.index;
return util_1.DateUtils.isoWeekDayAsOrdinaryDay(index);
};
/**
* @stable [15.01.2020]
* @param date1
* @param date2
*/
DateConverter.prototype.isFuture = function (date1, date2) {
return this.compare(date1, util_1.NvlUtils.nvl(date2, this.currentDate)) > 0;
};
/**
* @stable [15.01.2021]
* @param date1
* @param date2
*/
DateConverter.prototype.isPast = function (date1, date2) {
return this.compare(date1, util_1.NvlUtils.nvl(date2, this.currentDate)) < 0;
};
/**
* @stable [02.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.fromUiDateToDateTimeString = function (cfg) {
return this.dateAsString(__assign(__assign({}, cfg), { outputFormat: this.dateTimeFormat, inputFormat: this.uiDateFormat }));
};
/**
* @stable [09.11.2018]
* @param {TEntity} entity
* @param {string} dateFieldName
* @param {string} timeFieldName
* @param {(entity: TEntity) => string} dateResolver
* @returns {IKeyValue}
*/
DateConverter.prototype.splitToDateTimeFields = function (entity, dateFieldName, timeFieldName, dateResolver) {
var _a;
var _this = this;
var date = dateResolver(entity);
return util_1.defValuesFilter((_a = {},
_a[timeFieldName] = util_1.ConditionUtils.orUndef(date, function () { return _this.fromDateTimeToTime(date); }),
_a[dateFieldName] = util_1.ConditionUtils.orUndef(date, function () { return _this.fromDateTimeToDate(date); }),
_a));
};
/**
* @deprecated
*/
DateConverter.prototype.tryAddXDurationAsMomentDate = function (unit, duration, date, inputFormat) {
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
var momentDate = this.toMomentDate(date, inputFormat);
return util_1.orNull(momentDate.isValid(), function () { return momentDate.add(duration, unit); });
};
/**
* @stable [07.01.2019]
* @param {moment.DurationInputArg2} unit
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {Date}
*/
DateConverter.prototype.tryAddXDuration = function (unit, duration, date, inputFormat) {
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return util_1.ConditionUtils.ifNotNilThanValue(this.tryAddXDurationAsMomentDate(unit, duration, date, inputFormat), function (value) { return value.toDate(); });
};
/**
* @stable [07.01.2019]
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {Date}
*/
DateConverter.prototype.tryAddXDays = function (duration, date, inputFormat) {
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return this.tryAddXDuration('days', duration, date, inputFormat);
};
/**
* @stable [15.01.2021]
* @param date
*/
DateConverter.prototype.asDayOfYear = function (date) {
var d = new Date(date || this.currentDate);
d.setHours(0, 0, 0, 0);
return d;
};
/**
* @stable [07.01.2019]
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {Date}
*/
DateConverter.prototype.tryAddXMonths = function (duration, date, inputFormat) {
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return this.tryAddXDuration('months', duration, date, inputFormat);
};
/**
* @stable [10.02.2019]
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {Date}
*/
DateConverter.prototype.tryAddXQuarters = function (duration, date, inputFormat) {
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return this.tryAddXDuration('quarters', duration, date, inputFormat);
};
/**
* @stable [10.02.2019]
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {Date}
*/
DateConverter.prototype.tryAddXYears = function (duration, date, inputFormat) {
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return this.tryAddXDuration('years', duration, date, inputFormat);
};
/**
* @stable [09.02.2019]
* @param {moment.DurationInputArg2} unit
* @param {moment.unitOfTime.StartOf} startOf
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {moment.Moment}
*/
DateConverter.prototype.tryGetFirstDayOfXAsMomentDate = function (unit, startOf, duration, date, inputFormat) {
if (duration === void 0) { duration = 0; }
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return util_1.ConditionUtils.ifNotNilThanValue(this.tryAddXDurationAsMomentDate(unit, duration, date, inputFormat), function (value) { return value.startOf(startOf); });
};
/**
* @stable [09.02.2019]
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {moment.Moment}
*/
DateConverter.prototype.tryGetFirstDayOfWeekAsMomentDate = function (duration, date, inputFormat) {
if (duration === void 0) { duration = 0; }
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return this.tryGetFirstDayOfXAsMomentDate('weeks', this.weekUnit, duration, date, inputFormat);
};
/**
* @stable [09.02.2019]
* @param {moment.DurationInputArg1} duration
* @param {DateTimeLikeTypeT} date
* @param {string | undefined} inputFormat
* @returns {Date}
*/
DateConverter.prototype.tryGetFirstDayOfWeek = function (duration, date, inputFormat) {
if (duration === void 0) { duration = 0; }
if (date === void 0) { date = this.currentDate; }
if (inputFormat === void 0) { inputFormat = this.uiDateFormat; }
return util_1.ConditionUtils.ifNotNilThanValue(this.tryGetFirstDayOfWeekAsMomentDate(duration, date, inputFormat), function (value) { return value.toDate(); });
};
/**
* @stable [08.08.2019]
* @param {DateTimeLikeTypeT} date
* @param {string} inputFormat
* @returns {boolean}
*/
DateConverter.prototype.isDateBelongsToCurrentWeek = function (date, inputFormat) {
if (inputFormat === void 0) { inputFormat = this.dateFormat; }
var momentDate = this.toMomentDate(date, inputFormat);
return momentDate.isValid() && (momentDate.toDate() >= this.tryGetFirstDayOfWeek());
};
/**
* @stable [26.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.fromDateToUiDateString = function (cfg) {
return this.dateAsUiDateString(__assign({ strict: false, inputFormat: this.dateFormat }, cfg));
};
/**
* @stable [26.03.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {string}
*/
DateConverter.prototype.fromDateTimeToUiDateString = function (cfg) {
return this.dateAsUiDateString(__assign({ inputFormat: this.dateTimeFormat }, cfg));
};
/**
* @stable [08.01.2020]
* @param {IDayOfYearEntity} entity
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.fromDayOfYearEntity = function (entity, cfg) {
var mDate = this.asMomentDate(__assign({ date: this.asDayOfYear() }, cfg));
if (util_1.TypeUtils.isNumber(entity.month)) {
mDate.month(entity.month);
}
if (util_1.TypeUtils.isNumber(entity.year)) {
mDate.year(entity.year);
}
if (util_1.TypeUtils.isNumber(entity.day)) {
mDate.date(entity.day);
}
return util_1.orNull(mDate.isValid(), function () { return mDate; });
};
/**
* @stable [08.01.2020]
* @param {IDayOfYearEntity} entity
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.fromDayOfYearEntityAsDate = function (entity, cfg) {
return util_1.ConditionUtils.ifNotNilThanValue(this.fromDayOfYearEntity(entity, cfg), function (mDate) { return mDate.toDate(); });
};
/**
* @deprecated
*/
DateConverter.prototype.get30DaysAgo = function () {
return this.getXDaysAgo(30);
};
/**
* @deprecated
*/
DateConverter.prototype.getXDaysAgo = function (days) {
return this.getCurrentMomentDate().subtract(days, 'days').toDate();
};
/**
* @stable [25.12.2019]
* @tested
* @returns {Date}
*/
DateConverter.prototype.getCurrentDate = function () {
return this.currentDate;
};
/**
* @stable [26.03.2020]
* @returns {string}
*/
DateConverter.prototype.currentDateAsUiDateString = function (cfg) {
return this.dateAsUiDateString(__assign(__assign({}, cfg), { date: this.currentDate }));
};
/**
* @stable [14.01.2020]
* @param cfg
*/
DateConverter.prototype.getWeekdays = function (cfg) {
return this.getWeekLocale(cfg)
// @ts-ignore TODO
.weekdays(true);
};
/**
* @stable [14.01.2020]
* @param cfg
*/
DateConverter.prototype.getWeekdaysShort = function (cfg) {
return this.getWeekLocale(cfg)
// @ts-ignore TODO
.weekdaysShort(true);
};
/**
* @stable [14.01.2020]
* @param cfg
*/
DateConverter.prototype.getWeekdaysShortest = function (cfg) {
return this.getWeekLocale(cfg)
// @ts-ignore TODO
.weekdaysMin(true);
};
/**
* @stable [27.12.2020]
* @param cfg
*/
DateConverter.prototype.getCalendarWeekEntity = function (cfg) {
var _a = (cfg || {}).isoWeek, isoWeek = _a === void 0 ? this.isIsoWeek : _a;
return isoWeek
? DateConverter_1.ISO_CALENDAR_WEEK_ENTITIES
: DateConverter_1.DEFAULT_CALENDAR_WEEK_ENTITIES;
};
/**
* @stable [28.12.2020]
* @param cfg
*/
DateConverter.prototype.asCronDay = function (cfg) {
var index = (cfg || {}).index;
return this.getCalendarWeekEntity(cfg)[index].cronDay;
};
/**
* @stable [14.01.2021]
* @param cfg
*/
DateConverter.prototype.getWeekday = function (cfg) {
return this.getWeekdays(cfg)[cfg.index];
};
/**
* @stable [14.01.2021]
* @param cfg
*/
DateConverter.prototype.getWeekdayShort = function (cfg) {
return this.getWeekdaysShort(cfg)[cfg.index];
};
/**
* @stable [14.01.2021]
* @param cfg
*/
DateConverter.prototype.getWeekdayShortest = function (cfg) {
return this.getWeekdaysShortest(cfg)[cfg.index];
};
/**
* @stable [22.01.2020]
* @param {IPersonAgeConfigEntity} cfg
* @returns {number}
*/
DateConverter.prototype.asPersonAge = function (cfg) {
var _this = this;
var $cfg = __assign({ date: this.getCurrentDate(), inputFormat: this.uiDateFormat }, cfg);
return this.processValidMomentDate($cfg, function (mDate) { return mDate.diff(_this.asMomentDate({ date: cfg.birthday, inputFormat: $cfg.inputFormat }), 'years'); });
};
/**
* @stable [03.04.2019]
* @param {DateTimeLikeTypeT} date
* @param {string} inputFormat
* @returns {Date}
*/
DateConverter.prototype.toDate = function (date, inputFormat) {
return this.toMomentDate(date, inputFormat).toDate();
};
/**
* @deprecated Use asMomentDate
*/
DateConverter.prototype.toMomentDate = function (date, inputFormat, strict) {
if (strict === void 0) { strict = true; }
var momentDate = date instanceof Date
? moment(date)
: moment(date, inputFormat, strict);
var zone = this.timeZone;
return zone ? moment.tz(date, zone) : momentDate;
};
/**
* @stable [02.01.2019]
* @param {IDateTimeConfigEntity} cfg
* @returns {MomentT}
*/
DateConverter.prototype.asMomentDate = function (cfg) {
var date = cfg.date, inputFormat = cfg.inputFormat, _a = cfg.strict, strict = _a === void 0 ? true : _a, _b = cfg.zone, zone = _b === void 0 ? this.timeZone : _b;
if (moment.isMoment(date)) {
return date;
}
return zone
? (date instanceof Date
? moment.tz(date, zone)
: moment.tz(date, inputFormat, strict, zone))
: (date instanceof Date
? moment(date)
: moment(date, inputFormat, strict));
};
/**
* @stable [07.01.2020]
* @param {IDateTimeConfigEntity} cfg
* @returns {Date}
*/
DateConverter.prototype.asDate = function (cfg) {
return this.processValidMomentDate(cfg, function (mDate) { return mDate.toDate(); });
};
/**
* @stable [07.01.2020]
* @tested
* @param {IDayOfYearEntity} o1
* @param {IDayOfYearEntity} o2
* @returns {number}
*/
DateConverter.prototype.compareDayOfYearEntity = function (o1, o2) {
o1 = o1 || {};
o2 = o2 || {};
return this.compare(this.fromDayOfYearEntityAsDate(o1), this.fromDayOfYearEntityAsDate(o2));
};
/**
* @stable [07.01.2020]
* @tested
* @param {IFromToDayOfYearEntity} range
* @param {IDayOfYearEntity} entity
* @returns {IFromToDayOfYearEntity}
*/
DateConverter.prototype.selectDaysOfYearRange = function (range, entity) {
var _a = range || {}, _b = _a.to, to = _b === void 0 ? {} : _b, _c = _a.from, from = _c === void 0 ? {} : _c;
var day = entity.day, month = entity.month, year = entity.year;
var onlyFrom = entity.from === true;
var onlyTo = entity.to === true;
var isDateToEmpty = R.isEmpty(to);
var isDateFromEmpty = R.isEmpty(from);
if (onlyFrom) { // Manual set
return { to: to, from: { day: day, month: month, year: year } };
}
else if (onlyTo) { // Manual set
return { from: from, to: { day: day, month: month, year: year } };
}
if (isDateToEmpty && isDateFromEmpty) {
return ({ from: { day: day, month: month, year: year }, to: to });
}
else if (!isDateFromEmpty && !isDateToEmpty) {
return ({ from: { day: day, month: month, year: year }, to: {} });
}
else if (!isDateFromEmpty) {
var compareResult = this.compareDayOfYearEntity(from, entity);
if (compareResult >= 0) {
return ({ from: compareResult === 0 ? {} : { day: day, month: month, year: year }, to: to });
}
else {
return ({ from: from, to: { day: day, month: month, year: year } });
}
}
else if (!isDateToEmpty) {
// The "from value" is not set
var compareResult = this.compareDayOfYearEntity(entity, to);
if (compareResult >= 0) {
return ({ from: compareResult === 0 ? {} : __assign({}, to), to: { day: day, month: month, year: year } });
}
else {
return ({ from: { day: day, month: month, year: year }, to: to });
}
}
return ({ from: from, to: to });
};
/**
* @stable [08.01.2020]
* @param {IMinMaxDatesRangeConfigEntity} cfg
* @returns {boolean}
*/
DateConverter.prototype.isDateBelongToDatesRange = function (cfg) {
var minDate = util_1.NvlUtils.nvl(cfg.minDate, this.dateTimeSettings.minDate);
var maxDate = util_1.NvlUtils.nvl(cfg.maxDate, this.dateTimeSettings.maxDate);
return this.compare(cfg.date, minDate) >= 0 && this.compare(maxDate, cfg.date) >= 0;
};
/**
* @stable [07.01.2020]
* @tested
* @param {IFromToDayOfYearEntity} range
* @param {IDayOfYearEntity} entity
* @returns {boolean}
*/
DateConverter.prototype.isDayOfYearBelongToDaysOfYearRange = function (range, entity) {
var _a = range.from, from = _a === void 0 ? {} : _a, _b = range.to, to = _b === void 0 ? {} : _b;
var isDateToEmpty = R.isEmpty(to);
var isDateFromEmpty = R.isEmpty(from);
if (!isDateFromEmpty && isDateToEmpty) {
return this.compareDayOfYearEntity(entity, from) === 0;
}
if (isDateFromEmpty && !isDateToEmpty) {
return this.compareDayOfYearEntity(entity, to) === 0;
}
if (!isDateToEmpty && !isDateFromEmpty) {
var compareResult1 = this.compareDayOfYearEntity(entity, from);
var compareResult2 = this.compareDayOfYearEntity(entity, to);
return compareResult1 >= 0 && compareResult2 <= 0;
}
return false;
};
/**
* @stable [07.03.2020]
* @param {IDayOfYearEntity} entity1
* @param {IDayOfYearEntity} entity2
* @returns {boolean}
*/
DateConverter.prototype.isDayOfYearEqualOtherDayOfYear = function (entity1, entity2) {
return !R.isNil(entity1) && !R.isNil(entity2)
&& (entity1.day === entity2.day
&& entity1.month === entity2.month
&& entity1.year === entity2.year);
};
/**
* @stable [28.09.2020]
* @param cfg
*/
DateConverter.prototype.isDateValid = function (cfg) {
var date = this.asDate(cfg);
return date instanceof Date && date.getFullYear() >= 1900;
};
/**
* @stable [03.01.2020]
* @param {IDateTimeConfigEntity} $cfg
* @returns {ICalendarEntity}
*/
DateConverter.prototype.asCalendar = function ($cfg) {
var _this = this;
var useSyntheticCalendar = ($cfg === null || $cfg === void 0 ? void 0 : $cfg.useSyntheticCalendar) === true;
var cfg = __assign(__assign(__assign({}, $cfg), { isoWeek: useSyntheticCalendar || util_1.NvlUtils.nvl($cfg === null || $cfg === void 0 ? void 0 : $cfg.isoWeek, this.isIsoWeek) }), (useSyntheticCalendar
? util_1.TypeUtils.asType({ date: new Date('01/01/1900') }) // The date "01/01/1900" starts on Monday.
: ({})));
var isoWeek = cfg.isoWeek;
var firstDayOfMonthAsMDate = this.asFirstDayOfMonth(cfg);
var firstDayOfIsoWeek = firstDayOfMonthAsMDate.isoWeekday();
var maxWeeksCount = 6;
var today = this.asDayOfYear();
var data = [];
var currentDate;
var currentMonthValue;
var dayEntity;
var buildDayEntity = function (itm, mDate) { return (__assign(__assign({ current: false, next: false, previous: false }, itm), { day: mDate.date(), month: mDate.month(), today: _this.compare(today, mDate.toDate()) === 0, year: mDate.year() })); };
for (var i = 0; i < maxWeeksCount; i++) {
var row = { id: i };
data.push(row);
for (var j = 0; j < definition_1.DefaultEntities.NUMBER_OF_DAYS_PER_WEEK; j++) {
if (R.isNil(currentDate)) {
if (firstDayOfIsoWeek === j + 1) { // The ISO day of the week: 1 === Monday and 7 === Sunday
currentDate = firstDayOfMonthAsMDate.toDate();
currentMonthValue = firstDayOfMonthAsMDate.month();
dayEntity = buildDayEntity({ current: true, date: currentDate }, firstDayOfMonthAsMDate);
row[j] = dayEntity;
for (var k = j - 1, m = 1; k >= 0; k--) {
var mDate = this.addDays({ date: currentDate, duration: -1 * m++ });
dayEntity = buildDayEntity({ previous: true, date: mDate.toDate() }, mDate);
row[k] = dayEntity;
}
}
}
else {
var currentMDate = this.addDays({ date: currentDate, duration: 1 });
currentDate = currentMDate.toDate();
var next = currentMDate.month() !== currentMonthValue;
dayEntity = buildDayEntity({ current: !next, next: next, date: currentDate }, currentMDate);
row[j] = dayEntity;
}
}
}
var result = {
days: data,
daysLabels: this.getWeekdaysShortest(cfg),
};
if (!isoWeek) {
result.days = data.map(function (row, index) {
var newRow = __assign({}, row);
for (var i = 0; i < definition_1.DefaultEntities.NUMBER_OF_DAYS_PER_WEEK - 1; i++) {
newRow[i + 1] = row[i];
}
var startingDate = data[index - 1];
if (R.isNil(startingDate)) {
var startingMDate = _this.addDays({ date: data[0][0].date, duration: -1 });
dayEntity = buildDayEntity({ previous: true, date: startingMDate.toDate() }, startingMDate);
newRow[0] = dayEntity;
}
else {
newRow[0] = data[index - 1][definition_1.DefaultEntities.NUMBER_OF_DAYS_PER_WEEK - 1];
}
return newRow;
});
}
var truncateFirstWeek = true;
var truncateLastWeek = true;
var days = result.days;
for (var j = 0; j < definition_1.DefaultEntities.NUMBER_OF_DAYS_PER_WEEK; j++) {
truncateFirstWeek = truncateFirstWeek && days[0][j].current === false;
truncateLastWeek = truncateLastWeek && days[days.length - 1][j].current === false;
}
return __assign(__assign({ isoWeek: isoWeek }, result), { days: __spreadArrays((truncateFirstWeek ? [] : [days[0]]), days.slice(1, days.length - 1), (truncateLastWeek ? [] : [days[days.length - 1]])) });
};
/**
* @stable [28.12.2020]
* @param cfg
*/
DateConverter.prototype.asCalendarWeekEntity = function (cfg) {
return {
days: [this.getCalendarWeekEntity(cfg)],
daysLabels: this.getWeekdaysShortest(cfg),
};
};
/**
* @stable [21.12.2020]
* @param cfg
* @param handler
* @private
*/
DateConverter.prototype.processValidMomentDate = function (cfg, handler) {
var momentDate = this.asMomentDate(cfg);
return momentDate.isValid()
? handler(momentDate)
: (cfg.returnUndef ? definitions_interface_1.UNDEF : null);
};
/**
* @deprecated
*/
DateConverter.prototype.getCurrentMomentDate = function () {
return this.toMomentDate(this.currentDate);
};
/**
* @stable [07.12.2020]
* @param uiDateFormat
* @param uiTimeFormat
* @private
*/
DateConverter.prototype.concatUiDateTime = function (uiDateFormat, uiTimeFormat) {
return (util_1.NvlUtils.nvl(uiDateFormat, this.uiDateFormat) + " " + util_1.NvlUtils.nvl(uiTimeFormat, this.uiTimeFormat)).trim();
};
/**
* @stable [14.01.2020]
* @param cfg
* @private
*/
DateConverter.prototype.getWeekLocale = function (cfg) {
var _a = cfg || {}, _b = _a.isoWeek, isoWeek = _b === void 0 ? this.isIsoWeek : _b, _c = _a.locale, locale = _c === void 0 ? this.locale : _c;
return moment
.localeData(isoWeek && locale === definition_1.DefaultEntities.LOCALE
? 'en-gb' // https://en.wikipedia.org/wiki/Date_and_time_notation_in_the_United_Kingdom
: locale);
};
Object.defineProperty(DateConverter.prototype, "timeZone", {
/**
* @stable [11.08.2018]
* @returns {string}
*/
get: function () {
return this.dateTimeSettings.timeZone;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "dateTimeFormat", {
get: function () {
return this.dateTimeSettings.dateTimeFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "dateFormat", {
/**
* @stable [24.09.2020]
*/
get: function () {
return this.dateTimeSettings.dateFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "timeFormat", {
get: function () {
return this.dateTimeSettings.timeFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "uiTimeFormat", {
/**
* @stable [25.12.2019]
* @returns {string}
*/
get: function () {
return this.dateTimeSettings.uiTimeFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "uiDefaultTime", {
/**
* @stable [25.12.2019]
* @returns {string}
*/
get: function () {
return this.dateTimeSettings.uiDefaultTime;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "pstTimeFormat", {
/**
* @stable [02.01.2019]
* @returns {string}
*/
get: function () {
return this.dateTimeSettings.pstTimeFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "weekUnit", {
/**
* @stable [03.05.2020]
* @returns {string}
*/
get: function () {
return this.isIsoWeek ? 'isoWeek' : 'week';
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "isIsoWeek", {
/**
* https://en.wikipedia.org/wiki/ISO_week_date
* Weeks start with Monday.
*
* @stable [28.12.2020]
* @private
*/
get: function () {
return this.dateTimeSettings.isoWeek;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "locale", {
/**
* @stable [14.01.2020]
* @private
*/
get: function () {
return this.settings.locale;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.prototype, "pstDateFormat", {
get: function () {
return this.dateTimeSettings.pstDateFormat;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DateConverter.p