nhb-toolbox 4.28.10 → 4.28.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/cjs/constants.js +2 -1
- package/dist/cjs/date/Chronos.js +50 -51
- package/dist/cjs/date/guards.js +1 -3
- package/dist/cjs/date/helpers.js +81 -0
- package/dist/cjs/date/plugins/businessPlugin.js +84 -20
- package/dist/cjs/date/plugins/dayPartPlugin.js +2 -2
- package/dist/cjs/date/plugins/durationPlugin.js +4 -4
- package/dist/cjs/date/plugins/fromNowPlugin.js +3 -3
- package/dist/cjs/date/plugins/greetingPlugin.js +3 -3
- package/dist/cjs/date/plugins/palindromePlugin.js +2 -2
- package/dist/cjs/date/plugins/relativeTimePlugin.js +14 -14
- package/dist/cjs/date/plugins/roundPlugin.js +5 -5
- package/dist/cjs/date/plugins/seasonPlugin.js +5 -7
- package/dist/cjs/date/plugins/timeZonePlugin.js +15 -23
- package/dist/cjs/date/plugins/zodiacPlugin.js +3 -3
- package/dist/cjs/date/timezone.js +2 -1
- package/dist/cjs/date/utils.js +23 -68
- package/dist/cjs/hash/Signet.js +3 -2
- package/dist/cjs/hash/helpers.js +0 -5
- package/dist/cjs/utils/index.js +1 -1
- package/dist/dts/array/Finder.d.ts +5 -5
- package/dist/dts/array/basics.d.ts +3 -2
- package/dist/dts/array/calc.d.ts +6 -5
- package/dist/dts/array/transform.d.ts +2 -1
- package/dist/dts/colors/types.d.ts +2 -2
- package/dist/dts/constants.d.ts +1 -1
- package/dist/dts/date/Chronos.d.ts +14 -6
- package/dist/dts/date/constants.d.ts +2 -2
- package/dist/dts/date/helpers.d.ts +16 -0
- package/dist/dts/date/plugins/businessPlugin.d.ts +257 -8
- package/dist/dts/date/plugins/dayPartPlugin.d.ts +2 -4
- package/dist/dts/date/plugins/durationPlugin.d.ts +2 -4
- package/dist/dts/date/plugins/fromNowPlugin.d.ts +2 -4
- package/dist/dts/date/plugins/greetingPlugin.d.ts +2 -4
- package/dist/dts/date/plugins/palindromePlugin.d.ts +2 -3
- package/dist/dts/date/plugins/relativeTimePlugin.d.ts +2 -4
- package/dist/dts/date/plugins/roundPlugin.d.ts +2 -4
- package/dist/dts/date/plugins/seasonPlugin.d.ts +2 -4
- package/dist/dts/date/plugins/timeZonePlugin.d.ts +2 -4
- package/dist/dts/date/plugins/zodiacPlugin.d.ts +2 -4
- package/dist/dts/date/timezone.d.ts +2 -0
- package/dist/dts/date/types.d.ts +49 -61
- package/dist/dts/date/utils.d.ts +15 -3
- package/dist/dts/guards/non-primitives.d.ts +2 -2
- package/dist/dts/hash/Signet.d.ts +4 -3
- package/dist/dts/hash/helpers.d.ts +0 -4
- package/dist/dts/hash/types.d.ts +1 -0
- package/dist/dts/http-status/HttpStatus.d.ts +3 -2
- package/dist/dts/number/basics.d.ts +4 -4
- package/dist/dts/number/types.d.ts +2 -1
- package/dist/dts/number/utilities.d.ts +4 -4
- package/dist/dts/object/types.d.ts +2 -2
- package/dist/dts/string/types.d.ts +6 -0
- package/dist/dts/types/index.d.ts +6 -4
- package/dist/dts/utils/index.d.ts +3 -3
- package/dist/esm/constants.js +1 -1
- package/dist/esm/date/Chronos.js +51 -52
- package/dist/esm/date/guards.js +2 -4
- package/dist/esm/date/helpers.js +73 -0
- package/dist/esm/date/plugins/businessPlugin.js +84 -20
- package/dist/esm/date/plugins/dayPartPlugin.js +2 -2
- package/dist/esm/date/plugins/durationPlugin.js +4 -4
- package/dist/esm/date/plugins/fromNowPlugin.js +3 -3
- package/dist/esm/date/plugins/greetingPlugin.js +3 -3
- package/dist/esm/date/plugins/palindromePlugin.js +2 -2
- package/dist/esm/date/plugins/relativeTimePlugin.js +14 -14
- package/dist/esm/date/plugins/roundPlugin.js +5 -5
- package/dist/esm/date/plugins/seasonPlugin.js +5 -7
- package/dist/esm/date/plugins/timeZonePlugin.js +16 -24
- package/dist/esm/date/plugins/zodiacPlugin.js +3 -3
- package/dist/esm/date/timezone.js +1 -0
- package/dist/esm/date/utils.js +20 -66
- package/dist/esm/hash/Signet.js +2 -1
- package/dist/esm/hash/helpers.js +0 -2
- package/dist/esm/utils/index.js +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,18 @@ All notable changes to the package will be documented here.
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## [4.28.20] - 2025-12-07
|
|
10
|
+
|
|
11
|
+
### 🕧 Updates in Chronos
|
|
12
|
+
|
|
13
|
+
- **Added** *new methods* for *business plugin:* `nextWorkday`, `nextWeekend`, `previousWorkday`, `previousWeekend`, `workdaysBetween`, `workdaysInMonth`, `workdaysInYear`.
|
|
14
|
+
- **Optimized** *time complexity* for `getDatesForDay` and `getDatesInRange` for *longer time ranges*.
|
|
15
|
+
|
|
16
|
+
### 🛠️ Other Updates
|
|
17
|
+
|
|
18
|
+
- **Added** *new utilities*: `getTimeZoneIds` to get *time zone identifiers* for a given *UTC offset* and `getNativeTimeZoneId` to get *current system's time zone identifier*.
|
|
19
|
+
- **Added** *new utility types* `Maybe<T>`, `Alphabet<T>`, `IsAlphabet<T>` and `SpecialCharacter`.
|
|
20
|
+
|
|
9
21
|
## [4.28.10] - 2025-12-03
|
|
10
22
|
|
|
11
23
|
- **Added** *new* `addOrOverrideCode` method and **modified** *existing* + **introduced** *new types* for `HttpStatus` class.
|
package/dist/cjs/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.STATUS_CODES = exports.HTTP_STATUS_CODES = exports.HTTP_STATUS = exports.HTTP_CODES = exports.COUNTRIES = exports.CATEGORIZED_UNITS = exports.LOWERCASED_WORDS = exports.LOCALE_CODES = exports.GENERAL_UNITS = exports.FRANKFURTER_CURRENCIES = exports.CURRENCY_LOCALES = exports.CURRENCY_CODES = exports.CSS_COLORS = exports.NUMBER_COLOR_PALETTE = exports.ALPHABET_COLOR_PALETTE = exports.WESTERN_SEASONS = exports.US_ACADEMIC_SEASONS = exports.SEASON_PRESETS = exports.PHILIPPINES_SEASONS = exports.JAPAN_SEASONS = exports.INDIA_VEDIC_SEASONS = exports.INDIA_TAMIL_SEASONS = exports.INDIA_IMD_SEASONS = exports.ETHIOPIA_SEASONS = exports.BANGLADESH_SEASONS = exports.AUSTRALIA_SEASONS = exports.TZ_ABBREVIATIONS = exports.TIME_ZONES_NATIVE = exports.TIME_ZONES = exports.TIME_ZONE_LABELS = exports.TIME_ZONE_IDS = exports.IANA_TZ_IDS = exports.WESTERN_ZODIAC_SIGNS = exports.WEEK_DAYS = exports.VEDIC_ZODIAC_SIGNS = exports.MONTHS = void 0;
|
|
3
|
+
exports.STATUS_CODES = exports.HTTP_STATUS_CODES = exports.HTTP_STATUS = exports.HTTP_CODES = exports.COUNTRIES = exports.CATEGORIZED_UNITS = exports.LOWERCASED_WORDS = exports.LOCALE_CODES = exports.GENERAL_UNITS = exports.FRANKFURTER_CURRENCIES = exports.CURRENCY_LOCALES = exports.CURRENCY_CODES = exports.CSS_COLORS = exports.NUMBER_COLOR_PALETTE = exports.ALPHABET_COLOR_PALETTE = exports.WESTERN_SEASONS = exports.US_ACADEMIC_SEASONS = exports.SEASON_PRESETS = exports.PHILIPPINES_SEASONS = exports.JAPAN_SEASONS = exports.INDIA_VEDIC_SEASONS = exports.INDIA_TAMIL_SEASONS = exports.INDIA_IMD_SEASONS = exports.ETHIOPIA_SEASONS = exports.BANGLADESH_SEASONS = exports.AUSTRALIA_SEASONS = exports.TZ_ABBREVIATIONS = exports.TIME_ZONES_NATIVE = exports.TIME_ZONES = exports.TIME_ZONE_LABELS = exports.TIME_ZONE_IDS = exports.NATIVE_TZ_IDS = exports.IANA_TZ_IDS = exports.WESTERN_ZODIAC_SIGNS = exports.WEEK_DAYS = exports.VEDIC_ZODIAC_SIGNS = exports.MONTHS = void 0;
|
|
4
4
|
var constants_1 = require("./date/constants");
|
|
5
5
|
Object.defineProperty(exports, "MONTHS", { enumerable: true, get: function () { return constants_1.MONTHS; } });
|
|
6
6
|
Object.defineProperty(exports, "VEDIC_ZODIAC_SIGNS", { enumerable: true, get: function () { return constants_1.VEDIC_ZODIAC_SIGNS; } });
|
|
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "WEEK_DAYS", { enumerable: true, get: function ()
|
|
|
8
8
|
Object.defineProperty(exports, "WESTERN_ZODIAC_SIGNS", { enumerable: true, get: function () { return constants_1.WESTERN_ZODIAC_SIGNS; } });
|
|
9
9
|
var timezone_1 = require("./date/timezone");
|
|
10
10
|
Object.defineProperty(exports, "IANA_TZ_IDS", { enumerable: true, get: function () { return timezone_1.IANA_TZ_IDS; } });
|
|
11
|
+
Object.defineProperty(exports, "NATIVE_TZ_IDS", { enumerable: true, get: function () { return timezone_1.NATIVE_TZ_IDS; } });
|
|
11
12
|
Object.defineProperty(exports, "TIME_ZONE_IDS", { enumerable: true, get: function () { return timezone_1.TIME_ZONE_IDS; } });
|
|
12
13
|
Object.defineProperty(exports, "TIME_ZONE_LABELS", { enumerable: true, get: function () { return timezone_1.TIME_ZONE_LABELS; } });
|
|
13
14
|
Object.defineProperty(exports, "TIME_ZONES", { enumerable: true, get: function () { return timezone_1.TIME_ZONES; } });
|
package/dist/cjs/date/Chronos.js
CHANGED
|
@@ -6,6 +6,7 @@ const non_primitives_1 = require("../guards/non-primitives");
|
|
|
6
6
|
const primitives_1 = require("../guards/primitives");
|
|
7
7
|
const constants_1 = require("./constants");
|
|
8
8
|
const guards_1 = require("./guards");
|
|
9
|
+
const helpers_1 = require("./helpers");
|
|
9
10
|
const utils_1 = require("./utils");
|
|
10
11
|
class Chronos {
|
|
11
12
|
#date;
|
|
@@ -25,6 +26,9 @@ class Chronos {
|
|
|
25
26
|
toNewDate(instance, value) {
|
|
26
27
|
return instance.#toNewDate(value);
|
|
27
28
|
},
|
|
29
|
+
cast(date) {
|
|
30
|
+
return _a.#cast(date);
|
|
31
|
+
},
|
|
28
32
|
};
|
|
29
33
|
origin;
|
|
30
34
|
native;
|
|
@@ -90,11 +94,11 @@ class Chronos {
|
|
|
90
94
|
return true;
|
|
91
95
|
}
|
|
92
96
|
$getNativeTimeZoneName(tzId) {
|
|
93
|
-
const $tzId = tzId ||
|
|
94
|
-
return
|
|
97
|
+
const $tzId = tzId || (0, utils_1.getNativeTimeZoneId)();
|
|
98
|
+
return (0, helpers_1._resolveNativeTzName)($tzId, 'long', this.#date) ?? $tzId;
|
|
95
99
|
}
|
|
96
100
|
$getNativeTimeZoneId() {
|
|
97
|
-
return
|
|
101
|
+
return (0, utils_1.getNativeTimeZoneId)();
|
|
98
102
|
}
|
|
99
103
|
get #timestamp() {
|
|
100
104
|
return this.#date.getTime();
|
|
@@ -126,19 +130,6 @@ class Chronos {
|
|
|
126
130
|
#cloneStates(instance, origin) {
|
|
127
131
|
return instance.#withOrigin(origin, this.#offset, this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
128
132
|
}
|
|
129
|
-
#getNativeTzName(tzId) {
|
|
130
|
-
try {
|
|
131
|
-
const tzDetails = new Intl.DateTimeFormat('en', {
|
|
132
|
-
timeZone: tzId,
|
|
133
|
-
timeZoneName: 'long',
|
|
134
|
-
}).formatToParts(this.#date);
|
|
135
|
-
const tzPart = tzDetails.find((p) => p.type === 'timeZoneName');
|
|
136
|
-
return tzPart?.value;
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
return undefined;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
133
|
#format(format, useUTC = false) {
|
|
143
134
|
const $date = this.#date;
|
|
144
135
|
const $utcDate = this.toDate();
|
|
@@ -147,7 +138,7 @@ class Chronos {
|
|
|
147
138
|
};
|
|
148
139
|
const y = _getUnitValue('FullYear'), mo = _getUnitValue('Month'), d = _getUnitValue('Day'), dt = _getUnitValue('Date'), h = _getUnitValue('Hours'), m = _getUnitValue('Minutes'), s = _getUnitValue('Seconds'), ms = _getUnitValue('Milliseconds');
|
|
149
140
|
const offset = useUTC ? 'Z' : this.getTimeZoneOffset();
|
|
150
|
-
return (0,
|
|
141
|
+
return (0, helpers_1._formatDateCore)(format, y, mo, d, dt, h, m, s, ms, offset);
|
|
151
142
|
}
|
|
152
143
|
#removeUTCFromISO(local = true) {
|
|
153
144
|
const search = /\.\d+(Z|[+-]\d{2}:\d{2})?$/;
|
|
@@ -602,38 +593,42 @@ class Chronos {
|
|
|
602
593
|
return constants_1.MONTHS[index ?? this.month];
|
|
603
594
|
}
|
|
604
595
|
getDatesInRange(options) {
|
|
605
|
-
let startDate = this.clone(), endDate =
|
|
596
|
+
let startDate = this.clone(), endDate = this.addWeeks(4);
|
|
606
597
|
const { format = 'local', onlyDays, skipDays, roundDate = false } = options ?? {};
|
|
607
598
|
if (options) {
|
|
608
599
|
if ('from' in options || 'to' in options) {
|
|
609
|
-
if (options?.from)
|
|
610
|
-
startDate = _a.#cast(options
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
endDate = _a.#cast(options?.to);
|
|
614
|
-
}
|
|
600
|
+
if (options?.from)
|
|
601
|
+
startDate = _a.#cast(options.from);
|
|
602
|
+
if (options?.to)
|
|
603
|
+
endDate = _a.#cast(options.to);
|
|
615
604
|
}
|
|
616
605
|
else if ('span' in options || 'unit' in options) {
|
|
617
|
-
const { span = 4, unit = 'week' } = options
|
|
606
|
+
const { span = 4, unit = 'week' } = options;
|
|
618
607
|
endDate = startDate.add(span, unit);
|
|
619
608
|
}
|
|
620
609
|
}
|
|
621
|
-
|
|
622
|
-
|
|
610
|
+
if (roundDate) {
|
|
611
|
+
startDate = startDate.startOf('day');
|
|
612
|
+
endDate = endDate.startOf('day');
|
|
613
|
+
}
|
|
614
|
+
const skipSet = new Set(((0, non_primitives_1.isValidArray)(onlyDays) ? onlyDays
|
|
623
615
|
: (0, non_primitives_1.isValidArray)(skipDays) ? skipDays
|
|
624
|
-
: []).map((day) => (
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
616
|
+
: []).map((day) => ((0, primitives_1.isNumber)(day) ? day : constants_1.DAYS.indexOf(day))));
|
|
617
|
+
const dates = [];
|
|
618
|
+
const startTime = startDate.#timestamp;
|
|
619
|
+
const endTime = endDate.#timestamp;
|
|
620
|
+
const step = (startTime <= endTime ? 1 : -1) * 86400000;
|
|
621
|
+
const totalDays = Math.floor(Math.abs(endTime - startTime) / 86400000);
|
|
622
|
+
for (let i = 0; i <= totalDays; i++) {
|
|
623
|
+
const ts = startTime + i * step;
|
|
624
|
+
const wDay = new Date(ts).getDay();
|
|
625
|
+
const include = (0, non_primitives_1.isValidArray)(onlyDays) ? skipSet.has(wDay) : !skipSet.has(wDay);
|
|
626
|
+
if (include) {
|
|
627
|
+
const chr = new _a(ts).#withOrigin('clone', startDate.#offset, startDate.timeZoneName, startDate.timeZoneId, startDate.$tzTracker);
|
|
628
|
+
dates.push(format === 'local' ? chr.toLocalISOString() : chr.toISOString());
|
|
633
629
|
}
|
|
634
|
-
current = current.add(1, 'day');
|
|
635
630
|
}
|
|
636
|
-
return
|
|
631
|
+
return dates;
|
|
637
632
|
}
|
|
638
633
|
static parse(dateStr, format) {
|
|
639
634
|
const tokenPatterns = {
|
|
@@ -740,28 +735,32 @@ class Chronos {
|
|
|
740
735
|
const { format = 'local', roundDate = false } = options ?? {};
|
|
741
736
|
if (options) {
|
|
742
737
|
if ('from' in options || 'to' in options) {
|
|
743
|
-
if (options?.from)
|
|
738
|
+
if (options?.from)
|
|
744
739
|
startDate = _a.#cast(options?.from);
|
|
745
|
-
|
|
746
|
-
if (options?.to) {
|
|
740
|
+
if (options?.to)
|
|
747
741
|
endDate = _a.#cast(options?.to);
|
|
748
|
-
}
|
|
749
742
|
}
|
|
750
743
|
else if ('span' in options || 'unit' in options) {
|
|
751
744
|
const { span = 4, unit = 'week' } = options ?? {};
|
|
752
745
|
endDate = startDate.add(span, unit);
|
|
753
746
|
}
|
|
754
747
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
while (current.weekDay !== dayIndex) {
|
|
759
|
-
current = current.add(1, 'day');
|
|
748
|
+
if (roundDate) {
|
|
749
|
+
startDate = startDate.startOf('day');
|
|
750
|
+
endDate = endDate.startOf('day');
|
|
760
751
|
}
|
|
761
752
|
const result = [];
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
753
|
+
const step = (startDate.isBefore(endDate, 'day') ? 1 : -1) * 86400000;
|
|
754
|
+
const totalDays = Math.abs(endDate.diff(startDate, 'day'));
|
|
755
|
+
const currentTime = startDate.#timestamp;
|
|
756
|
+
let firstOffset = 0;
|
|
757
|
+
while (new Date(currentTime + firstOffset * step).getDay() !== constants_1.DAYS.indexOf(day)) {
|
|
758
|
+
firstOffset++;
|
|
759
|
+
}
|
|
760
|
+
for (let i = firstOffset; i <= totalDays; i += 7) {
|
|
761
|
+
const ts = currentTime + i * step;
|
|
762
|
+
const chr = new _a(ts).#withOrigin('clone', startDate.#offset, startDate.timeZoneName, startDate.timeZoneId, startDate.$tzTracker);
|
|
763
|
+
result.push(format === 'local' ? chr.toLocalISOString() : chr.toISOString());
|
|
765
764
|
}
|
|
766
765
|
return result;
|
|
767
766
|
}
|
|
@@ -792,7 +791,7 @@ class Chronos {
|
|
|
792
791
|
year = date;
|
|
793
792
|
}
|
|
794
793
|
else {
|
|
795
|
-
year = new
|
|
794
|
+
year = new Date(date).getFullYear();
|
|
796
795
|
}
|
|
797
796
|
}
|
|
798
797
|
else {
|
package/dist/cjs/date/guards.js
CHANGED
|
@@ -28,9 +28,7 @@ function isValidTimeZoneId(value) {
|
|
|
28
28
|
return (0, primitives_1.isNonEmptyString)(value) ? new Set([...timezone_1.IANA_TZ_IDS]).has(value) : false;
|
|
29
29
|
}
|
|
30
30
|
function isNativeTimeZoneId(value) {
|
|
31
|
-
return (0, primitives_1.isNonEmptyString)(value) ?
|
|
32
|
-
new Set(Intl.supportedValuesOf('timeZone')).has(value)
|
|
33
|
-
: false;
|
|
31
|
+
return (0, primitives_1.isNonEmptyString)(value) ? new Set(timezone_1.NATIVE_TZ_IDS).has(value) : false;
|
|
34
32
|
}
|
|
35
33
|
function isLeapYear(year) {
|
|
36
34
|
const $year = (0, utilities_1.normalizeNumber)(year);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._secToDate = exports._toSeconds = void 0;
|
|
4
|
+
exports._formatDateCore = _formatDateCore;
|
|
5
|
+
exports._resolveNativeTzName = _resolveNativeTzName;
|
|
6
|
+
exports._gmtToUtcOffset = _gmtToUtcOffset;
|
|
7
|
+
const utilities_1 = require("../number/utilities");
|
|
8
|
+
const constants_1 = require("./constants");
|
|
9
|
+
function _formatDateCore(format, year, month, day, date, hours, minutes, seconds, milliseconds, offset) {
|
|
10
|
+
const dateComponents = {
|
|
11
|
+
YYYY: String(year),
|
|
12
|
+
YY: String(year).slice(-2),
|
|
13
|
+
yyyy: String(year),
|
|
14
|
+
yy: String(year).slice(-2),
|
|
15
|
+
M: String(month + 1),
|
|
16
|
+
MM: String(month + 1).padStart(2, '0'),
|
|
17
|
+
mmm: constants_1.MONTHS[month].slice(0, 3),
|
|
18
|
+
mmmm: constants_1.MONTHS[month],
|
|
19
|
+
d: constants_1.DAYS[day].slice(0, 2),
|
|
20
|
+
dd: constants_1.DAYS[day].slice(0, 3),
|
|
21
|
+
ddd: constants_1.DAYS[day],
|
|
22
|
+
D: String(date),
|
|
23
|
+
DD: String(date).padStart(2, '0'),
|
|
24
|
+
Do: (0, utilities_1.getOrdinal)(date),
|
|
25
|
+
H: String(hours),
|
|
26
|
+
HH: String(hours).padStart(2, '0'),
|
|
27
|
+
h: String(hours % 12 || 12),
|
|
28
|
+
hh: String(hours % 12 || 12).padStart(2, '0'),
|
|
29
|
+
m: String(minutes),
|
|
30
|
+
mm: String(minutes).padStart(2, '0'),
|
|
31
|
+
s: String(seconds),
|
|
32
|
+
ss: String(seconds).padStart(2, '0'),
|
|
33
|
+
ms: String(milliseconds),
|
|
34
|
+
mss: String(milliseconds).padStart(3, '0'),
|
|
35
|
+
a: hours < 12 ? 'am' : 'pm',
|
|
36
|
+
A: hours < 12 ? 'AM' : 'PM',
|
|
37
|
+
ZZ: offset,
|
|
38
|
+
};
|
|
39
|
+
const tokenRegex = new RegExp(`^(${constants_1.SORTED_TIME_FORMATS.join('|')})`);
|
|
40
|
+
let result = '';
|
|
41
|
+
let i = 0;
|
|
42
|
+
while (i < format.length) {
|
|
43
|
+
if (format[i] === '[') {
|
|
44
|
+
const end = format.indexOf(']', i);
|
|
45
|
+
if (end !== -1) {
|
|
46
|
+
result += format.slice(i + 1, end);
|
|
47
|
+
i = end + 1;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const match = tokenRegex.exec(format.slice(i));
|
|
52
|
+
if (match) {
|
|
53
|
+
result += dateComponents[match[0]];
|
|
54
|
+
i += match[0].length;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
result += format[i];
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
const _toSeconds = (ms) => Math.floor(ms / 1000);
|
|
64
|
+
exports._toSeconds = _toSeconds;
|
|
65
|
+
const _secToDate = (sec) => new Date(sec * 1000);
|
|
66
|
+
exports._secToDate = _secToDate;
|
|
67
|
+
function _resolveNativeTzName(tzId, type, date) {
|
|
68
|
+
try {
|
|
69
|
+
const parts = new Intl.DateTimeFormat('en', {
|
|
70
|
+
timeZone: tzId,
|
|
71
|
+
timeZoneName: type,
|
|
72
|
+
}).formatToParts(date);
|
|
73
|
+
return parts.find((p) => p.type === 'timeZoneName')?.value;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function _gmtToUtcOffset(gmt) {
|
|
80
|
+
return gmt === 'GMT' ? 'UTC+00:00' : gmt?.replace(/^GMT/, 'UTC');
|
|
81
|
+
}
|
|
@@ -4,29 +4,95 @@ exports.businessPlugin = void 0;
|
|
|
4
4
|
const non_primitives_1 = require("../../guards/non-primitives");
|
|
5
5
|
const primitives_1 = require("../../guards/primitives");
|
|
6
6
|
const constants_1 = require("../constants");
|
|
7
|
-
const businessPlugin = (
|
|
8
|
-
const { internalDate: $Date } =
|
|
9
|
-
|
|
7
|
+
const businessPlugin = ($Chronos) => {
|
|
8
|
+
const { internalDate: $Date, withOrigin, cast, offset } = $Chronos[constants_1.INTERNALS];
|
|
9
|
+
const _buildWeekendMask = (weekDef, length) => {
|
|
10
|
+
const weekendDays = (0, non_primitives_1.isValidArray)(weekDef) ?
|
|
11
|
+
[...weekDef].sort()
|
|
12
|
+
: Array.from({ length }, (_, i) => (weekDef + 7 - 1 - i) % 7).sort();
|
|
13
|
+
const mask = new Array(7).fill(true);
|
|
14
|
+
for (const d of weekendDays)
|
|
15
|
+
mask[d] = false;
|
|
16
|
+
return mask;
|
|
17
|
+
};
|
|
18
|
+
const _countWorkdays = (wStart, totalDays, mask, step = 1) => {
|
|
19
|
+
let total = Math.floor(totalDays / 7) * mask.filter(Boolean).length;
|
|
20
|
+
let dayIndex = wStart % 7;
|
|
21
|
+
for (let i = 0; i < totalDays % 7; i++) {
|
|
22
|
+
if (mask[dayIndex])
|
|
23
|
+
total++;
|
|
24
|
+
dayIndex = (dayIndex + step + 7) % 7;
|
|
25
|
+
}
|
|
26
|
+
return total;
|
|
27
|
+
};
|
|
28
|
+
$Chronos.prototype.isWeekend = function (wDef = 0, wLen = 2) {
|
|
10
29
|
const day = $Date(this).getDay();
|
|
11
30
|
if ((0, non_primitives_1.isValidArray)(wDef)) {
|
|
12
31
|
return wDef.includes(day);
|
|
13
32
|
}
|
|
14
33
|
const lastDayOfWeek = (wDef + 6) % 7;
|
|
15
|
-
const
|
|
16
|
-
return
|
|
34
|
+
const weekendDays = Array.from({ length: wLen }, (_, i) => (lastDayOfWeek - i + 7) % 7);
|
|
35
|
+
return weekendDays.includes(day);
|
|
17
36
|
};
|
|
18
|
-
|
|
19
|
-
if ((0, non_primitives_1.isValidArray)(wDef)) {
|
|
20
|
-
return !this.isWeekend(wDef);
|
|
21
|
-
}
|
|
37
|
+
$Chronos.prototype.isWorkday = function (wDef = 0, wLen = 2) {
|
|
22
38
|
return !this.isWeekend(wDef, wLen);
|
|
23
39
|
};
|
|
24
|
-
|
|
40
|
+
$Chronos.prototype.nextWorkday = function (wDef = 0, wLen = 2) {
|
|
41
|
+
let nxtWrk = this.addDays(1);
|
|
42
|
+
while (nxtWrk.isWeekend(wDef, wLen)) {
|
|
43
|
+
nxtWrk = nxtWrk.addDays(1);
|
|
44
|
+
}
|
|
45
|
+
return withOrigin(nxtWrk.startOf('day'), 'nextWorkday', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
46
|
+
};
|
|
47
|
+
$Chronos.prototype.previousWorkday = function (wDef = 0, wLen = 2) {
|
|
48
|
+
let prevWrk = this.addDays(-1);
|
|
49
|
+
while (prevWrk.isWeekend(wDef, wLen)) {
|
|
50
|
+
prevWrk = prevWrk.addDays(-1);
|
|
51
|
+
}
|
|
52
|
+
return withOrigin(prevWrk.startOf('day'), 'previousWorkday', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
53
|
+
};
|
|
54
|
+
$Chronos.prototype.nextWeekend = function (wDef = 0, wLen = 2) {
|
|
55
|
+
let nxtWknd = this.addDays(1);
|
|
56
|
+
while (!nxtWknd.isWeekend(wDef, wLen)) {
|
|
57
|
+
nxtWknd = nxtWknd.addDays(1);
|
|
58
|
+
}
|
|
59
|
+
return withOrigin(nxtWknd.startOf('day'), 'nextWeekend', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
60
|
+
};
|
|
61
|
+
$Chronos.prototype.previousWeekend = function (wDef = 0, wLen = 2) {
|
|
62
|
+
let prevWknd = this.addDays(-1);
|
|
63
|
+
while (!prevWknd.isWeekend(wDef, wLen)) {
|
|
64
|
+
prevWknd = prevWknd.addDays(-1);
|
|
65
|
+
}
|
|
66
|
+
return withOrigin(prevWknd.startOf('day'), 'previousWeekend', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
67
|
+
};
|
|
68
|
+
$Chronos.prototype.workdaysBetween = function (to, wDef = 0, wLen = 2) {
|
|
69
|
+
const end = cast(to).startOf('day');
|
|
70
|
+
const start = this.clone().startOf('day');
|
|
71
|
+
if (start.isSame(end, 'day'))
|
|
72
|
+
return 0;
|
|
73
|
+
const step = start.isBefore(end, 'day') ? 1 : -1;
|
|
74
|
+
const totalDays = Math.abs(end.diff(start, 'day'));
|
|
75
|
+
const weekendMask = _buildWeekendMask(wDef, wLen);
|
|
76
|
+
const startWeekday = (start.isoWeekDay + step) % 7;
|
|
77
|
+
return _countWorkdays(startWeekday, totalDays, weekendMask, step);
|
|
78
|
+
};
|
|
79
|
+
$Chronos.prototype.workdaysInMonth = function (wDef = 0, wLen = 2) {
|
|
80
|
+
const daysInMonth = this.daysInMonth();
|
|
81
|
+
const weekendMask = _buildWeekendMask(wDef, wLen);
|
|
82
|
+
const startWeekday = this.startOf('month').isoWeekDay % 7;
|
|
83
|
+
return _countWorkdays(startWeekday, daysInMonth, weekendMask);
|
|
84
|
+
};
|
|
85
|
+
$Chronos.prototype.workdaysInYear = function (wDef = 0, wLen = 2) {
|
|
86
|
+
const daysInYear = this.isLeapYear() ? 366 : 365;
|
|
87
|
+
const weekendMask = _buildWeekendMask(wDef, wLen);
|
|
88
|
+
const startWeekday = this.startOf('year').isoWeekDay % 7;
|
|
89
|
+
return _countWorkdays(startWeekday, daysInYear, weekendMask);
|
|
90
|
+
};
|
|
91
|
+
$Chronos.prototype.isBusinessHour = function (options) {
|
|
25
92
|
const _isBusinessHour = () => {
|
|
26
93
|
const { businessStartHour = 9, businessEndHour = 17 } = options ?? {};
|
|
27
|
-
if (businessStartHour === businessEndHour)
|
|
94
|
+
if (businessStartHour === businessEndHour)
|
|
28
95
|
return false;
|
|
29
|
-
}
|
|
30
96
|
const hour = $Date(this).getHours();
|
|
31
97
|
if (businessStartHour < businessEndHour) {
|
|
32
98
|
return hour >= businessStartHour && hour < businessEndHour;
|
|
@@ -36,20 +102,18 @@ const businessPlugin = (ChronosClass) => {
|
|
|
36
102
|
}
|
|
37
103
|
};
|
|
38
104
|
if (options && 'weekendDays' in options && !(0, primitives_1.isUndefined)(options?.weekendDays)) {
|
|
39
|
-
return this.isWorkday(options
|
|
105
|
+
return this.isWorkday(options.weekendDays) && _isBusinessHour();
|
|
40
106
|
}
|
|
41
107
|
const { weekStartsOn = 0, weekendLength = 2 } = (options ?? {});
|
|
42
108
|
return this.isWorkday(weekStartsOn, weekendLength) && _isBusinessHour();
|
|
43
109
|
};
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const adjusted = (month - startMonth + 12) % 12;
|
|
110
|
+
$Chronos.prototype.toFiscalQuarter = function (startMonth = 7) {
|
|
111
|
+
const adjusted = (this.isoMonth - startMonth + 12) % 12;
|
|
47
112
|
return (Math.floor(adjusted / 3) + 1);
|
|
48
113
|
};
|
|
49
|
-
|
|
50
|
-
const year =
|
|
51
|
-
|
|
52
|
-
if (month >= 6) {
|
|
114
|
+
$Chronos.prototype.toAcademicYear = function () {
|
|
115
|
+
const year = this.year;
|
|
116
|
+
if (this.month >= 6) {
|
|
53
117
|
return `${year}-${year + 1}`;
|
|
54
118
|
}
|
|
55
119
|
else {
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dayPartPlugin = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
|
-
const dayPartPlugin = (
|
|
6
|
-
|
|
5
|
+
const dayPartPlugin = ($Chronos) => {
|
|
6
|
+
$Chronos.prototype.getPartOfDay = function (config) {
|
|
7
7
|
const hour = this.hour;
|
|
8
8
|
const ranges = {
|
|
9
9
|
...constants_1.DATE_PART_RANGES,
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.durationPlugin = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
|
-
const durationPlugin = (
|
|
6
|
-
const { toNewDate } =
|
|
5
|
+
const durationPlugin = ($Chronos) => {
|
|
6
|
+
const { toNewDate } = $Chronos[constants_1.INTERNALS];
|
|
7
7
|
const _normalizeDuration = (result, absolute, isFuture) => {
|
|
8
8
|
const entries = Object.entries(result);
|
|
9
9
|
const updated = { ...result };
|
|
@@ -21,7 +21,7 @@ const durationPlugin = (ChronosClass) => {
|
|
|
21
21
|
}
|
|
22
22
|
return updated;
|
|
23
23
|
};
|
|
24
|
-
|
|
24
|
+
$Chronos.prototype.duration = function (toTime, absolute = true) {
|
|
25
25
|
const now = this.toDate();
|
|
26
26
|
const target = toNewDate(this, toTime);
|
|
27
27
|
const isFuture = target > now;
|
|
@@ -67,7 +67,7 @@ const durationPlugin = (ChronosClass) => {
|
|
|
67
67
|
};
|
|
68
68
|
return _normalizeDuration(result, absolute, isFuture);
|
|
69
69
|
};
|
|
70
|
-
|
|
70
|
+
$Chronos.prototype.durationString = function (options) {
|
|
71
71
|
const { toTime, absolute = true, maxUnits = 7, separator = ', ', style = 'full', showZero = false, } = options ?? {};
|
|
72
72
|
const duration = this.duration(toTime, absolute);
|
|
73
73
|
const units = {
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.fromNowPlugin = void 0;
|
|
4
4
|
const convert_1 = require("../../string/convert");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
|
-
const fromNowPlugin = (
|
|
7
|
-
const { toNewDate } =
|
|
8
|
-
|
|
6
|
+
const fromNowPlugin = ($Chronos) => {
|
|
7
|
+
const { toNewDate } = $Chronos[constants_1.INTERNALS];
|
|
8
|
+
$Chronos.prototype.fromNow = function (level = 'second', wSP = true, time) {
|
|
9
9
|
const now = toNewDate(this, time);
|
|
10
10
|
const target = this.toDate();
|
|
11
11
|
const isFuture = target > now;
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.greetingPlugin = void 0;
|
|
4
4
|
const greet_1 = require("../greet");
|
|
5
|
-
const greetingPlugin = (
|
|
6
|
-
|
|
5
|
+
const greetingPlugin = ($Chronos) => {
|
|
6
|
+
$Chronos.prototype.getGreeting = function (configs) {
|
|
7
7
|
const currentTime = this.formatStrict('HH:mm');
|
|
8
8
|
return (0, greet_1.getGreeting)({ currentTime, ...configs });
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
$Chronos.prototype.greet = function (configs) {
|
|
11
11
|
return this.getGreeting(configs);
|
|
12
12
|
};
|
|
13
13
|
};
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.palindromePlugin = void 0;
|
|
4
4
|
const guards_1 = require("../../string/guards");
|
|
5
|
-
const palindromePlugin = (
|
|
6
|
-
|
|
5
|
+
const palindromePlugin = ($Chronos) => {
|
|
6
|
+
$Chronos.prototype.isPalindromeDate = function (shortYear = false) {
|
|
7
7
|
const padded = this.formatStrict(shortYear ? 'YY-MM-DD' : 'YYYY-MM-DD');
|
|
8
8
|
const normal = this.formatStrict(shortYear ? 'YY-M-D' : 'YYYY-M-D');
|
|
9
9
|
return (0, guards_1.isPalindrome)(padded) || (0, guards_1.isPalindrome)(normal);
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.relativeTimePlugin = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
|
-
const relativeTimePlugin = (
|
|
6
|
-
const { toNewDate } =
|
|
7
|
-
|
|
5
|
+
const relativeTimePlugin = ($Chronos) => {
|
|
6
|
+
const { toNewDate } = $Chronos[constants_1.INTERNALS];
|
|
7
|
+
$Chronos.prototype.getRelativeYear = function (time) {
|
|
8
8
|
const $date = this.toDate();
|
|
9
9
|
const now = toNewDate(this, time);
|
|
10
10
|
let years = $date.getFullYear() - now.getFullYear();
|
|
@@ -15,7 +15,7 @@ const relativeTimePlugin = (ChronosClass) => {
|
|
|
15
15
|
}
|
|
16
16
|
return years;
|
|
17
17
|
};
|
|
18
|
-
|
|
18
|
+
$Chronos.prototype.getRelativeMonth = function (time) {
|
|
19
19
|
const $date = this.toDate();
|
|
20
20
|
const now = toNewDate(this, time);
|
|
21
21
|
let months = ($date.getFullYear() - now.getFullYear()) * 12 +
|
|
@@ -26,11 +26,11 @@ const relativeTimePlugin = (ChronosClass) => {
|
|
|
26
26
|
}
|
|
27
27
|
return months;
|
|
28
28
|
};
|
|
29
|
-
|
|
29
|
+
$Chronos.prototype.getRelativeWeek = function (time) {
|
|
30
30
|
const relativeDays = this.getRelativeDay(time);
|
|
31
31
|
return Math.floor(relativeDays / 7);
|
|
32
32
|
};
|
|
33
|
-
|
|
33
|
+
$Chronos.prototype.getRelativeDay = function (time) {
|
|
34
34
|
const now = toNewDate(this, time);
|
|
35
35
|
now.setHours(0, 0, 0, 0);
|
|
36
36
|
const $date = new Date(this.toDate());
|
|
@@ -39,22 +39,22 @@ const relativeTimePlugin = (ChronosClass) => {
|
|
|
39
39
|
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
|
40
40
|
return diffDays;
|
|
41
41
|
};
|
|
42
|
-
|
|
42
|
+
$Chronos.prototype.getRelativeHour = function (time) {
|
|
43
43
|
const diff = this.getTimeStamp() - toNewDate(this, time).getTime();
|
|
44
44
|
return Math.floor(diff / (1000 * 60 * 60));
|
|
45
45
|
};
|
|
46
|
-
|
|
46
|
+
$Chronos.prototype.getRelativeMinute = function (time) {
|
|
47
47
|
const diff = this.getTimeStamp() - toNewDate(this, time).getTime();
|
|
48
48
|
return Math.floor(diff / (1000 * 60));
|
|
49
49
|
};
|
|
50
|
-
|
|
50
|
+
$Chronos.prototype.getRelativeSecond = function (time) {
|
|
51
51
|
const diff = this.getTimeStamp() - toNewDate(this, time).getTime();
|
|
52
52
|
return Math.floor(diff / 1000);
|
|
53
53
|
};
|
|
54
|
-
|
|
54
|
+
$Chronos.prototype.getRelativeMilliSecond = function (time) {
|
|
55
55
|
return this.getTimeStamp() - toNewDate(this, time).getTime();
|
|
56
56
|
};
|
|
57
|
-
|
|
57
|
+
$Chronos.prototype.compare = function (unit = 'minute', time) {
|
|
58
58
|
switch (unit) {
|
|
59
59
|
case 'year':
|
|
60
60
|
return this.getRelativeYear(time);
|
|
@@ -76,13 +76,13 @@ const relativeTimePlugin = (ChronosClass) => {
|
|
|
76
76
|
throw new RangeError(`Unsupported time unit: ${unit}`);
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
|
-
|
|
79
|
+
$Chronos.prototype.isToday = function () {
|
|
80
80
|
return this.getRelativeDay() === 0;
|
|
81
81
|
};
|
|
82
|
-
|
|
82
|
+
$Chronos.prototype.isTomorrow = function () {
|
|
83
83
|
return this.getRelativeDay() === 1;
|
|
84
84
|
};
|
|
85
|
-
|
|
85
|
+
$Chronos.prototype.isYesterday = function () {
|
|
86
86
|
return this.getRelativeDay() === -1;
|
|
87
87
|
};
|
|
88
88
|
};
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.roundPlugin = void 0;
|
|
4
4
|
const utilities_1 = require("../../number/utilities");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
|
-
const roundPlugin = (
|
|
7
|
-
const { internalDate, withOrigin } =
|
|
8
|
-
|
|
6
|
+
const roundPlugin = ($Chronos) => {
|
|
7
|
+
const { internalDate, withOrigin, offset } = $Chronos[constants_1.INTERNALS];
|
|
8
|
+
$Chronos.prototype.round = function (unit, nearest = 1) {
|
|
9
9
|
const date = new Date(internalDate(this));
|
|
10
10
|
switch (unit) {
|
|
11
11
|
case 'millisecond': {
|
|
@@ -56,7 +56,7 @@ const roundPlugin = (ChronosClass) => {
|
|
|
56
56
|
const diffToStart = Math.abs(date.getTime() - startOfWeek.getTime());
|
|
57
57
|
const diffToEnd = Math.abs(endOfWeek.getTime() - date.getTime());
|
|
58
58
|
const rounded = diffToEnd < diffToStart ? endOfWeek : startOfWeek;
|
|
59
|
-
return withOrigin(new
|
|
59
|
+
return withOrigin(new $Chronos(rounded), 'round');
|
|
60
60
|
}
|
|
61
61
|
case 'month': {
|
|
62
62
|
const fullMonth = date.getMonth() + date.getDate() / this.lastDateOfMonth;
|
|
@@ -78,7 +78,7 @@ const roundPlugin = (ChronosClass) => {
|
|
|
78
78
|
default:
|
|
79
79
|
return this;
|
|
80
80
|
}
|
|
81
|
-
return withOrigin(new
|
|
81
|
+
return withOrigin(new $Chronos(date), 'round', offset(this), this.timeZoneName, this.timeZoneId, this.$tzTracker);
|
|
82
82
|
};
|
|
83
83
|
};
|
|
84
84
|
exports.roundPlugin = roundPlugin;
|