@zelgadis87/utils-core 5.2.9 → 5.2.11
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 +17 -1
- package/dist/time/TimeInstant.d.ts +38 -10
- package/dist/time/TimeInstantBuilder.d.ts +23 -14
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/empties.d.ts +1 -0
- package/esbuild/index.cjs +393 -189
- package/esbuild/index.cjs.map +4 -4
- package/esbuild/index.mjs +393 -202
- package/esbuild/index.mjs.map +4 -4
- package/package.json +1 -4
- package/src/time/TimeInstant.ts +519 -42
- package/src/time/TimeInstantBuilder.ts +19 -9
- package/src/utils/empties.ts +2 -0
package/esbuild/index.mjs
CHANGED
|
@@ -1,167 +1,3 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
-
mod
|
|
25
|
-
));
|
|
26
|
-
|
|
27
|
-
// ../../node_modules/.pnpm/small-date@2.0.1/node_modules/small-date/lib/format.js
|
|
28
|
-
var require_format = __commonJS({
|
|
29
|
-
"../../node_modules/.pnpm/small-date@2.0.1/node_modules/small-date/lib/format.js"(exports) {
|
|
30
|
-
"use strict";
|
|
31
|
-
Object.defineProperty(exports, "__esModule", {
|
|
32
|
-
value: true
|
|
33
|
-
});
|
|
34
|
-
exports["default"] = format2;
|
|
35
|
-
function ownKeys(object, enumerableOnly) {
|
|
36
|
-
var keys = Object.keys(object);
|
|
37
|
-
if (Object.getOwnPropertySymbols) {
|
|
38
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
39
|
-
if (enumerableOnly) {
|
|
40
|
-
symbols = symbols.filter(function(sym) {
|
|
41
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
keys.push.apply(keys, symbols);
|
|
45
|
-
}
|
|
46
|
-
return keys;
|
|
47
|
-
}
|
|
48
|
-
function _objectSpread(target) {
|
|
49
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
50
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
51
|
-
if (i % 2) {
|
|
52
|
-
ownKeys(Object(source), true).forEach(function(key) {
|
|
53
|
-
_defineProperty(target, key, source[key]);
|
|
54
|
-
});
|
|
55
|
-
} else if (Object.getOwnPropertyDescriptors) {
|
|
56
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
57
|
-
} else {
|
|
58
|
-
ownKeys(Object(source)).forEach(function(key) {
|
|
59
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return target;
|
|
64
|
-
}
|
|
65
|
-
function _defineProperty(obj, key, value) {
|
|
66
|
-
if (key in obj) {
|
|
67
|
-
Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
|
|
68
|
-
} else {
|
|
69
|
-
obj[key] = value;
|
|
70
|
-
}
|
|
71
|
-
return obj;
|
|
72
|
-
}
|
|
73
|
-
var PATTERN_REGEX = /(M|y|d|D|h|H|m|s|S|G|Z|P|a)+/g;
|
|
74
|
-
var ESCAPE_REGEX = /\\"|"((?:\\"|[^"])*)"|(\+)/g;
|
|
75
|
-
var optionNames = {
|
|
76
|
-
y: "year",
|
|
77
|
-
M: "month",
|
|
78
|
-
d: "day",
|
|
79
|
-
D: "weekday",
|
|
80
|
-
S: "fractionalSecondDigits",
|
|
81
|
-
G: "era",
|
|
82
|
-
Z: "timeZoneName",
|
|
83
|
-
P: "dayPeriod",
|
|
84
|
-
a: "hour12",
|
|
85
|
-
h: "hour",
|
|
86
|
-
H: "hour",
|
|
87
|
-
m: "minute",
|
|
88
|
-
s: "second"
|
|
89
|
-
};
|
|
90
|
-
var values = {
|
|
91
|
-
y: ["numeric", "2-digit", void 0, "numeric"],
|
|
92
|
-
M: ["narrow", "2-digit", "short", "long"],
|
|
93
|
-
d: ["numeric", "2-digit"],
|
|
94
|
-
D: ["narrow", "short", "long"],
|
|
95
|
-
S: [1, 2, 3],
|
|
96
|
-
G: ["narrow", "short", "long"],
|
|
97
|
-
Z: ["short", "long"],
|
|
98
|
-
P: ["narrow", "short", "long"],
|
|
99
|
-
a: [true],
|
|
100
|
-
h: ["numeric", "2-digit"],
|
|
101
|
-
H: ["numeric", "2-digit"],
|
|
102
|
-
m: ["numeric", "2-digit"],
|
|
103
|
-
s: ["numeric", "2-digit"]
|
|
104
|
-
};
|
|
105
|
-
function padIf(condition, value, length) {
|
|
106
|
-
return condition && length === 2 && value / 10 < 1 ? "0" + value : value;
|
|
107
|
-
}
|
|
108
|
-
function formatType(date, type, length) {
|
|
109
|
-
var _options;
|
|
110
|
-
var _ref = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {}, locale = _ref.locale, timeZone = _ref.timeZone;
|
|
111
|
-
var option = optionNames[type];
|
|
112
|
-
var value = values[type][length - 1];
|
|
113
|
-
if (!value) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
var options = (_options = {}, _defineProperty(_options, option, value), _defineProperty(_options, "timeZone", timeZone), _options);
|
|
117
|
-
if (type === "a") {
|
|
118
|
-
return Intl.DateTimeFormat(locale, _objectSpread(_objectSpread({}, options), {}, {
|
|
119
|
-
hour: "numeric"
|
|
120
|
-
})).formatToParts(date).pop().value;
|
|
121
|
-
}
|
|
122
|
-
if (type === "G" || type === "Z") {
|
|
123
|
-
return Intl.DateTimeFormat(locale, options).formatToParts(date).pop().value;
|
|
124
|
-
}
|
|
125
|
-
if (type === "H" || type === "h") {
|
|
126
|
-
return Intl.DateTimeFormat("en-GB", _objectSpread(_objectSpread({}, options), {}, {
|
|
127
|
-
hourCycle: type === "H" ? "h23" : "h11"
|
|
128
|
-
})).format(date).toLocaleLowerCase().replace(" am", "").replace(" pm", "");
|
|
129
|
-
}
|
|
130
|
-
return padIf(["m", "s"].includes(type) && value === "2-digit", Intl.DateTimeFormat(locale, options).format(date), 2);
|
|
131
|
-
}
|
|
132
|
-
function format2(date, pattern, config) {
|
|
133
|
-
return pattern.split(ESCAPE_REGEX).filter(function(sub) {
|
|
134
|
-
return sub !== void 0;
|
|
135
|
-
}).map(function(sub, index) {
|
|
136
|
-
if (index % 2 !== 0) {
|
|
137
|
-
return sub;
|
|
138
|
-
}
|
|
139
|
-
return sub.replace(PATTERN_REGEX, function(match) {
|
|
140
|
-
var type = match.charAt(0);
|
|
141
|
-
return formatType(date, type, match.length, config) || match;
|
|
142
|
-
});
|
|
143
|
-
}).join("");
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// ../../node_modules/.pnpm/small-date@2.0.1/node_modules/small-date/lib/index.js
|
|
149
|
-
var require_lib = __commonJS({
|
|
150
|
-
"../../node_modules/.pnpm/small-date@2.0.1/node_modules/small-date/lib/index.js"(exports) {
|
|
151
|
-
"use strict";
|
|
152
|
-
Object.defineProperty(exports, "__esModule", {
|
|
153
|
-
value: true
|
|
154
|
-
});
|
|
155
|
-
exports.format = void 0;
|
|
156
|
-
var _format = require_format();
|
|
157
|
-
var _format2 = _interopRequireDefault(_format);
|
|
158
|
-
function _interopRequireDefault(obj) {
|
|
159
|
-
return obj && obj.__esModule ? obj : { "default": obj };
|
|
160
|
-
}
|
|
161
|
-
exports.format = _format2["default"];
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
|
|
165
1
|
// src/async/Deferred.ts
|
|
166
2
|
var DeferredCanceledError = class extends Error {
|
|
167
3
|
constructor() {
|
|
@@ -1531,9 +1367,6 @@ var LazyDictionary = class _LazyDictionary {
|
|
|
1531
1367
|
}
|
|
1532
1368
|
};
|
|
1533
1369
|
|
|
1534
|
-
// src/time/TimeInstant.ts
|
|
1535
|
-
var import_small_date = __toESM(require_lib());
|
|
1536
|
-
|
|
1537
1370
|
// src/time/TimeUnit.ts
|
|
1538
1371
|
var TimeUnit = class _TimeUnit {
|
|
1539
1372
|
constructor(multiplier) {
|
|
@@ -1700,13 +1533,13 @@ var TimeDuration = class _TimeDuration extends TimeBase {
|
|
|
1700
1533
|
* @returns the duration, with only the most significant units displayed as a human readable string, eg: 1d 20h, 10m 30s.
|
|
1701
1534
|
*/
|
|
1702
1535
|
get formatted() {
|
|
1703
|
-
const
|
|
1536
|
+
const format = (x, u1, y, u2) => `${x}${u1} ${pad(y.toString(), 2, "0")}${u2}`;
|
|
1704
1537
|
const units = this.toUnits();
|
|
1705
|
-
if (units.days >= 1) return
|
|
1538
|
+
if (units.days >= 1) return format(units.days, "d", units.hours, "h");
|
|
1706
1539
|
else if (units.hours >= 1)
|
|
1707
|
-
return
|
|
1540
|
+
return format(units.hours, "h", units.minutes, "m");
|
|
1708
1541
|
else if (units.minutes >= 1)
|
|
1709
|
-
return
|
|
1542
|
+
return format(units.minutes, "m", units.seconds, "s");
|
|
1710
1543
|
else if (units.seconds >= 0)
|
|
1711
1544
|
return units.seconds.toString() + "s";
|
|
1712
1545
|
else if (units.seconds > -60)
|
|
@@ -1952,7 +1785,7 @@ var timeInstantResolveValue = (getFromDate2, referenceDate, x) => {
|
|
|
1952
1785
|
};
|
|
1953
1786
|
var getFromDate = {
|
|
1954
1787
|
year: (d) => d.getFullYear(),
|
|
1955
|
-
month: (d) => d.getMonth(),
|
|
1788
|
+
month: (d) => d.getMonth() + 1,
|
|
1956
1789
|
date: (d) => d.getDate(),
|
|
1957
1790
|
hours: (d) => d.getHours(),
|
|
1958
1791
|
minutes: (d) => d.getMinutes(),
|
|
@@ -1969,7 +1802,8 @@ function createTimeInstantFromParameters(aParameters, aReferenceDate = TimeInsta
|
|
|
1969
1802
|
const referenceDate = toReferenceDate(aReferenceDate);
|
|
1970
1803
|
const timestamp2 = Date.UTC(
|
|
1971
1804
|
timeInstantResolveValue(getFromDate.year, referenceDate, year),
|
|
1972
|
-
timeInstantResolveValue(getFromDate.month, referenceDate, month),
|
|
1805
|
+
timeInstantResolveValue(getFromDate.month, referenceDate, month) - 1,
|
|
1806
|
+
// Convert from 1-based to 0-based for Date.UTC()
|
|
1973
1807
|
timeInstantResolveValue(getFromDate.date, referenceDate, date),
|
|
1974
1808
|
timeInstantResolveValue(getFromDate.hours, referenceDate, hours),
|
|
1975
1809
|
timeInstantResolveValue(getFromDate.minutes, referenceDate, minutes),
|
|
@@ -2057,13 +1891,18 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2057
1891
|
return TimeDuration_default.fromMs(Math.max(distance, 0));
|
|
2058
1892
|
}
|
|
2059
1893
|
distanceFromStartOfDay() {
|
|
2060
|
-
|
|
1894
|
+
const params = this.toParameters();
|
|
1895
|
+
const msInDay = params.hours * 36e5 + params.minutes * 6e4 + params.seconds * 1e3 + params.milliseconds;
|
|
1896
|
+
return TimeDuration_default.fromMs(msInDay);
|
|
2061
1897
|
}
|
|
2062
1898
|
atStartOfDay() {
|
|
2063
1899
|
return this.atTime({ hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
|
|
2064
1900
|
}
|
|
2065
1901
|
distanceFromEndOfDay() {
|
|
2066
|
-
|
|
1902
|
+
const params = this.toParameters();
|
|
1903
|
+
const msInDay = params.hours * 36e5 + params.minutes * 6e4 + params.seconds * 1e3 + params.milliseconds;
|
|
1904
|
+
const msUntilEndOfDay = 86399999 - msInDay;
|
|
1905
|
+
return TimeDuration_default.fromMs(msUntilEndOfDay);
|
|
2067
1906
|
}
|
|
2068
1907
|
atEndOfDay() {
|
|
2069
1908
|
return this.atTime({ hours: 23, minutes: 59, seconds: 59, milliseconds: 999 });
|
|
@@ -2089,11 +1928,13 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2089
1928
|
throw new Error("Instant is not in the past");
|
|
2090
1929
|
}
|
|
2091
1930
|
isToday() {
|
|
2092
|
-
return Math.floor(this.
|
|
1931
|
+
return Math.floor(this.ms / 864e5) === Math.floor(Date.now() / 864e5);
|
|
2093
1932
|
}
|
|
2094
1933
|
/**
|
|
2095
1934
|
* Formats this instant using the given pattern. The pattern can contain the following tokens:
|
|
2096
1935
|
*
|
|
1936
|
+
* Note: Implementation inspired by the small-date library (https://github.com/robinweser/small-date).
|
|
1937
|
+
*
|
|
2097
1938
|
* | Token | Description | Example |
|
|
2098
1939
|
* |:------|:--------------------------------|:------------------------------|
|
|
2099
1940
|
* | D | Weekday, 1 letter | W |
|
|
@@ -2133,7 +1974,32 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2133
1974
|
* @returns a string, formatted using the given pattern, at the given timeZone with the given locale.
|
|
2134
1975
|
*/
|
|
2135
1976
|
format(pattern, config = {}) {
|
|
2136
|
-
return (
|
|
1977
|
+
return formatTimeInstant(this, pattern, config);
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* Parses a date string using the given pattern and creates a TimeInstant.
|
|
1981
|
+
* This method is the inverse of format() - parsing a formatted string should recreate the original instant.
|
|
1982
|
+
*
|
|
1983
|
+
* For partial patterns (e.g., time-only or date-only), the missing components are taken from the base instant.
|
|
1984
|
+
* For example, parsing "14:30" with base set to yesterday at midnight will result in yesterday at 14:30.
|
|
1985
|
+
*
|
|
1986
|
+
* Note: Currently performs basic validation (e.g., month 1-12, day 1-31) but does not validate
|
|
1987
|
+
* calendar-specific constraints (e.g., February 30th, April 31st). Invalid dates may be
|
|
1988
|
+
* normalized by the underlying Date constructor.
|
|
1989
|
+
*
|
|
1990
|
+
* @param dateString The date string to parse
|
|
1991
|
+
* @param pattern The pattern used to parse the string (same tokens as format())
|
|
1992
|
+
* @param base The base TimeInstant to use for partial patterns (defaults to now)
|
|
1993
|
+
* @param config An optional locale and timeZone definition to use during parsing
|
|
1994
|
+
* @returns A TimeInstant parsed from the string
|
|
1995
|
+
* @throws Error if the string doesn't match the pattern or contains invalid basic values
|
|
1996
|
+
* @todo Add calendar-aware validation to reject dates like February 30th, April 31st
|
|
1997
|
+
*/
|
|
1998
|
+
static fromString(dateString, pattern, base = _TimeInstant.now(), config = {}) {
|
|
1999
|
+
return parseTimeInstant(dateString, pattern, base, config);
|
|
2000
|
+
}
|
|
2001
|
+
static parse(dateString, pattern, config = {}) {
|
|
2002
|
+
return parseTimeInstantComponents(dateString, pattern, config);
|
|
2137
2003
|
}
|
|
2138
2004
|
/**
|
|
2139
2005
|
* @returns this instant, in ISO 8601 format (eg, 2024-11-01T15:49:22.024Z). The format is meant to always be realiable.
|
|
@@ -2153,8 +2019,17 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2153
2019
|
toDate() {
|
|
2154
2020
|
return new Date(this.ms);
|
|
2155
2021
|
}
|
|
2156
|
-
|
|
2157
|
-
|
|
2022
|
+
toParameters() {
|
|
2023
|
+
const d = this.toDate();
|
|
2024
|
+
return {
|
|
2025
|
+
year: d.getUTCFullYear(),
|
|
2026
|
+
month: d.getUTCMonth() + 1,
|
|
2027
|
+
date: d.getUTCDate(),
|
|
2028
|
+
hours: d.getUTCHours(),
|
|
2029
|
+
minutes: d.getUTCMinutes(),
|
|
2030
|
+
seconds: d.getUTCSeconds(),
|
|
2031
|
+
milliseconds: d.getUTCMilliseconds()
|
|
2032
|
+
};
|
|
2158
2033
|
}
|
|
2159
2034
|
/**
|
|
2160
2035
|
* @returns true if the instant is in the past, false otherwise
|
|
@@ -2217,13 +2092,47 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2217
2092
|
return timeInstantBuilder();
|
|
2218
2093
|
}
|
|
2219
2094
|
static fromIso8601(str) {
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2095
|
+
const iso8601Regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})(Z|[+-]\d{2}:\d{2})?$/;
|
|
2096
|
+
const match = str.match(iso8601Regex);
|
|
2097
|
+
if (!match) {
|
|
2098
|
+
throw new Error("Invalid ISO 8601 date format: " + str);
|
|
2099
|
+
}
|
|
2100
|
+
const [, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr, millisecondStr] = match;
|
|
2101
|
+
const year = parseInt(yearStr, 10);
|
|
2102
|
+
const month = parseInt(monthStr, 10);
|
|
2103
|
+
const date = parseInt(dayStr, 10);
|
|
2104
|
+
const hours = parseInt(hourStr, 10);
|
|
2105
|
+
const minutes = parseInt(minuteStr, 10);
|
|
2106
|
+
const seconds = parseInt(secondStr, 10);
|
|
2107
|
+
const milliseconds = parseInt(millisecondStr, 10);
|
|
2108
|
+
if (!isValidYear(year))
|
|
2109
|
+
throw new Error("Invalid year in: " + str);
|
|
2110
|
+
if (!isValidMonth(month))
|
|
2111
|
+
throw new Error("Invalid month in: " + str);
|
|
2112
|
+
if (!isValidDayOfMonth(date))
|
|
2113
|
+
throw new Error("Invalid day in: " + str);
|
|
2114
|
+
if (!isValidHour(hours))
|
|
2115
|
+
throw new Error("Invalid hour in: " + str);
|
|
2116
|
+
if (!isValidMinute(minutes))
|
|
2117
|
+
throw new Error("Invalid minute in: " + str);
|
|
2118
|
+
if (!isValidSecond(seconds))
|
|
2119
|
+
throw new Error("Invalid second in: " + str);
|
|
2120
|
+
if (!isValidMillisecond(milliseconds))
|
|
2121
|
+
throw new Error("Invalid millisecond in: " + str);
|
|
2122
|
+
return _TimeInstant.fromParameters({
|
|
2123
|
+
year,
|
|
2124
|
+
month,
|
|
2125
|
+
date,
|
|
2126
|
+
hours,
|
|
2127
|
+
minutes,
|
|
2128
|
+
seconds,
|
|
2129
|
+
milliseconds
|
|
2130
|
+
});
|
|
2226
2131
|
}
|
|
2132
|
+
/**
|
|
2133
|
+
* @deprecated [2025.10.19]: Use fromIso8601 instead.
|
|
2134
|
+
*/
|
|
2135
|
+
static tryFromIso8601 = this.fromIso8601;
|
|
2227
2136
|
static now() {
|
|
2228
2137
|
return _TimeInstant.fromUnixTimestamp(Date.now());
|
|
2229
2138
|
}
|
|
@@ -2251,30 +2160,21 @@ var TimeInstant = class _TimeInstant extends TimeBase {
|
|
|
2251
2160
|
const timestamp2 = Math.round(this.ms / unitMs) * unitMs;
|
|
2252
2161
|
return _TimeInstant.fromUnixTimestamp(timestamp2);
|
|
2253
2162
|
}
|
|
2254
|
-
get dayOfMonth() {
|
|
2255
|
-
return this.toDate().getDate();
|
|
2256
|
-
}
|
|
2257
|
-
get dayOfWeek() {
|
|
2258
|
-
return this.toDate().getDay() + 1;
|
|
2259
|
-
}
|
|
2260
|
-
get month() {
|
|
2261
|
-
return this.toDate().getMonth() + 1;
|
|
2262
|
-
}
|
|
2263
|
-
get year() {
|
|
2264
|
-
return this.toDate().getFullYear();
|
|
2265
|
-
}
|
|
2266
2163
|
/**
|
|
2267
2164
|
* Returns the week number represented by this instant, according to the ISO 8601 standard.
|
|
2268
|
-
|
|
2165
|
+
* Please note that the instant and the week number could be of two different years, eg the friday 1st january is actually part of week 52 of the previous year.
|
|
2269
2166
|
*/
|
|
2270
2167
|
get weekNumber() {
|
|
2271
2168
|
const date = this.toDate();
|
|
2272
2169
|
const oneDay = 1e3 * 60 * 60 * 24;
|
|
2273
|
-
const
|
|
2274
|
-
const
|
|
2275
|
-
const
|
|
2170
|
+
const dayOfWeek = date.getUTCDay() || 7;
|
|
2171
|
+
const thursdayMs = this.ms + (4 - dayOfWeek) * oneDay;
|
|
2172
|
+
const thursdayDate = new Date(thursdayMs);
|
|
2173
|
+
const thursdayYear = thursdayDate.getUTCFullYear();
|
|
2174
|
+
const firstOfJanMs = Date.UTC(thursdayYear, 0, 1, 12, 0, 0);
|
|
2175
|
+
const dayOfTheYear = Math.round((thursdayMs - firstOfJanMs) / oneDay);
|
|
2276
2176
|
const weekNumber = Math.floor(dayOfTheYear / 7) + 1;
|
|
2277
|
-
return { weekNumber, year:
|
|
2177
|
+
return { weekNumber, year: thursdayYear };
|
|
2278
2178
|
}
|
|
2279
2179
|
/**
|
|
2280
2180
|
* This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
|
|
@@ -2294,6 +2194,297 @@ function isTimeInstant(x) {
|
|
|
2294
2194
|
return x !== null && x !== void 0 && x instanceof TimeInstant;
|
|
2295
2195
|
}
|
|
2296
2196
|
var TimeInstant_default = TimeInstant;
|
|
2197
|
+
function isValidYear(num) {
|
|
2198
|
+
return num >= 0 && num <= 9999;
|
|
2199
|
+
}
|
|
2200
|
+
function isValidMonth(num) {
|
|
2201
|
+
return num >= 1 && num <= 12;
|
|
2202
|
+
}
|
|
2203
|
+
function isValidDayOfMonth(num) {
|
|
2204
|
+
return num >= 1 && num <= 31;
|
|
2205
|
+
}
|
|
2206
|
+
function isValidHour(num) {
|
|
2207
|
+
return num >= 0 && num <= 23;
|
|
2208
|
+
}
|
|
2209
|
+
function isValidMinute(num) {
|
|
2210
|
+
return num >= 0 && num <= 59;
|
|
2211
|
+
}
|
|
2212
|
+
function isValidSecond(num) {
|
|
2213
|
+
return num >= 0 && num <= 59;
|
|
2214
|
+
}
|
|
2215
|
+
function isValidMillisecond(num) {
|
|
2216
|
+
return num >= 0 && num <= 999;
|
|
2217
|
+
}
|
|
2218
|
+
var monthNamesCache = /* @__PURE__ */ new Map();
|
|
2219
|
+
function getMonthNames(locale = "en") {
|
|
2220
|
+
const cached = monthNamesCache.get(locale);
|
|
2221
|
+
if (cached) {
|
|
2222
|
+
return cached;
|
|
2223
|
+
}
|
|
2224
|
+
const shortFormatter = new Intl.DateTimeFormat(locale, { month: "short" });
|
|
2225
|
+
const longFormatter = new Intl.DateTimeFormat(locale, { month: "long" });
|
|
2226
|
+
const short = [];
|
|
2227
|
+
const long = [];
|
|
2228
|
+
for (let month = 0; month < 12; month++) {
|
|
2229
|
+
const date = new Date(2e3, month, 1);
|
|
2230
|
+
short.push(normalizeMonthName(shortFormatter.format(date)));
|
|
2231
|
+
long.push(normalizeMonthName(longFormatter.format(date)));
|
|
2232
|
+
}
|
|
2233
|
+
const result = { short, long };
|
|
2234
|
+
monthNamesCache.set(locale, result);
|
|
2235
|
+
return result;
|
|
2236
|
+
}
|
|
2237
|
+
function normalizeMonthName(name) {
|
|
2238
|
+
return name.replace(/[.,;:!?]/g, "").normalize("NFD").replace(/[\u0300-\u036f]/g, "").trim();
|
|
2239
|
+
}
|
|
2240
|
+
var PATTERN_REGEX = /(M|y|d|D|h|H|m|s|S|G|Z|P|a)+/g;
|
|
2241
|
+
var ESCAPE_REGEX = /\\"|"((?:\\"|[^"])*)"/g;
|
|
2242
|
+
var formatterConfigs = {
|
|
2243
|
+
// Year
|
|
2244
|
+
"y": { options: { year: "numeric" } },
|
|
2245
|
+
"yy": { options: { year: "2-digit" } },
|
|
2246
|
+
"yyyy": { options: { year: "numeric" } },
|
|
2247
|
+
// Month
|
|
2248
|
+
"M": { options: { month: "numeric" } },
|
|
2249
|
+
"MM": { options: { month: "2-digit" } },
|
|
2250
|
+
"MMM": { options: { month: "short" } },
|
|
2251
|
+
"MMMM": { options: { month: "long" } },
|
|
2252
|
+
// Day
|
|
2253
|
+
"d": { options: { day: "numeric" } },
|
|
2254
|
+
"dd": { options: { day: "2-digit" } },
|
|
2255
|
+
// Hours (24-hour) - extract and pad manually due to Intl quirk
|
|
2256
|
+
"H": { options: { hour: "numeric", hour12: false }, extract: { partType: "hour" } },
|
|
2257
|
+
"HH": { options: { hour: "2-digit", hour12: false }, extract: { partType: "hour", transform: (v) => v.padStart(2, "0") } },
|
|
2258
|
+
// Hours (12-hour) - extract and pad manually due to Intl quirk
|
|
2259
|
+
"h": { options: { hour: "numeric", hour12: true }, extract: { partType: "hour" } },
|
|
2260
|
+
"hh": { options: { hour: "2-digit", hour12: true }, extract: { partType: "hour", transform: (v) => v.padStart(2, "0") } },
|
|
2261
|
+
// Minutes - extract and pad manually due to Intl quirk
|
|
2262
|
+
"m": { options: { minute: "numeric" }, extract: { partType: "minute" } },
|
|
2263
|
+
"mm": { options: { minute: "2-digit" }, extract: { partType: "minute", transform: (v) => v.padStart(2, "0") } },
|
|
2264
|
+
// Seconds - extract and pad manually due to Intl quirk
|
|
2265
|
+
"s": { options: { second: "numeric" }, extract: { partType: "second" } },
|
|
2266
|
+
"ss": { options: { second: "2-digit" }, extract: { partType: "second", transform: (v) => v.padStart(2, "0") } },
|
|
2267
|
+
// Milliseconds - extract manually
|
|
2268
|
+
"S": { options: { fractionalSecondDigits: 1 }, extract: { partType: "fractionalSecond" } },
|
|
2269
|
+
"SS": { options: { fractionalSecondDigits: 2 }, extract: { partType: "fractionalSecond" } },
|
|
2270
|
+
"SSS": { options: { fractionalSecondDigits: 3 }, extract: { partType: "fractionalSecond" } },
|
|
2271
|
+
// Weekday
|
|
2272
|
+
"D": { options: { weekday: "narrow" } },
|
|
2273
|
+
"DD": { options: { weekday: "short" } },
|
|
2274
|
+
"DDD": { options: { weekday: "long" } },
|
|
2275
|
+
// Era
|
|
2276
|
+
"G": { options: { era: "narrow" } },
|
|
2277
|
+
"GG": { options: { era: "short" } },
|
|
2278
|
+
"GGG": { options: { era: "long" } },
|
|
2279
|
+
// Timezone
|
|
2280
|
+
"Z": { options: { timeZoneName: "short" } },
|
|
2281
|
+
"ZZ": { options: { timeZoneName: "long" } },
|
|
2282
|
+
// Day period
|
|
2283
|
+
"P": { options: { dayPeriod: "narrow" } },
|
|
2284
|
+
"PP": { options: { dayPeriod: "short" } },
|
|
2285
|
+
"PPP": { options: { dayPeriod: "long" } },
|
|
2286
|
+
// Meridiem - extract dayPeriod and lowercase it
|
|
2287
|
+
"a": { options: { hour: "numeric", hour12: true }, extract: { partType: "dayPeriod", transform: (v) => v.toLowerCase() } }
|
|
2288
|
+
};
|
|
2289
|
+
function formatType(type, length, date, locale = "en", timeZone = "UTC") {
|
|
2290
|
+
const tokenKey = type.repeat(length);
|
|
2291
|
+
const config = formatterConfigs[tokenKey];
|
|
2292
|
+
if (!config) {
|
|
2293
|
+
return void 0;
|
|
2294
|
+
}
|
|
2295
|
+
const formatter = new Intl.DateTimeFormat(locale, { ...config.options, timeZone });
|
|
2296
|
+
if (config.extract) {
|
|
2297
|
+
const part = formatter.formatToParts(date).find((p) => p.type === config.extract.partType);
|
|
2298
|
+
const value = part?.value;
|
|
2299
|
+
if (!value) {
|
|
2300
|
+
return void 0;
|
|
2301
|
+
}
|
|
2302
|
+
return config.extract.transform ? config.extract.transform(value) : value;
|
|
2303
|
+
}
|
|
2304
|
+
return formatter.format(date);
|
|
2305
|
+
}
|
|
2306
|
+
function formatTimeInstant(instant, pattern, config = {}) {
|
|
2307
|
+
const date = instant.toDate();
|
|
2308
|
+
const locale = config.locale || "en";
|
|
2309
|
+
const timeZone = config.timeZone || "UTC";
|
|
2310
|
+
return pattern.split(ESCAPE_REGEX).filter((sub) => sub !== void 0).map((sub, index) => {
|
|
2311
|
+
if (index % 2 !== 0) {
|
|
2312
|
+
return sub;
|
|
2313
|
+
}
|
|
2314
|
+
return sub.replace(PATTERN_REGEX, (match) => {
|
|
2315
|
+
const type = match.charAt(0);
|
|
2316
|
+
const length = match.length;
|
|
2317
|
+
return formatType(type, length, date, locale, timeZone) || match;
|
|
2318
|
+
});
|
|
2319
|
+
}).join("");
|
|
2320
|
+
}
|
|
2321
|
+
function parseTimeInstant(dateString, pattern, base, config = {}) {
|
|
2322
|
+
const parsed = parseTimeInstantComponents(dateString, pattern, config);
|
|
2323
|
+
const params = {
|
|
2324
|
+
...base.toParameters(),
|
|
2325
|
+
...parsed
|
|
2326
|
+
};
|
|
2327
|
+
return TimeInstant.fromParameters(params);
|
|
2328
|
+
}
|
|
2329
|
+
function parseTimeInstantComponents(dateString, pattern, config = {}) {
|
|
2330
|
+
let regexPattern = pattern;
|
|
2331
|
+
const tokens = [];
|
|
2332
|
+
let position = 0;
|
|
2333
|
+
const parts = pattern.split(ESCAPE_REGEX).filter((sub) => sub !== void 0);
|
|
2334
|
+
regexPattern = parts.map((sub, index) => {
|
|
2335
|
+
if (index % 2 !== 0) {
|
|
2336
|
+
return sub.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2337
|
+
}
|
|
2338
|
+
return sub.replace(PATTERN_REGEX, (match) => {
|
|
2339
|
+
const type = match.charAt(0);
|
|
2340
|
+
tokens.push({ type, length: match.length, position: position++ });
|
|
2341
|
+
switch (type) {
|
|
2342
|
+
case "y":
|
|
2343
|
+
return match.length === 2 ? "(\\d{2})" : "(\\d{4})";
|
|
2344
|
+
case "M":
|
|
2345
|
+
if (match.length === 1) return "(\\d{1,2})";
|
|
2346
|
+
if (match.length === 2) return "(\\d{2})";
|
|
2347
|
+
if (match.length === 3) return "([A-Za-z.]{1,7})";
|
|
2348
|
+
return "([A-Za-z]+)";
|
|
2349
|
+
case "d":
|
|
2350
|
+
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
2351
|
+
case "H":
|
|
2352
|
+
case "h":
|
|
2353
|
+
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
2354
|
+
case "m":
|
|
2355
|
+
case "s":
|
|
2356
|
+
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
2357
|
+
case "S":
|
|
2358
|
+
return `(\\d{${match.length}})`;
|
|
2359
|
+
case "a":
|
|
2360
|
+
return "([aApP][mM])";
|
|
2361
|
+
case "D":
|
|
2362
|
+
if (match.length === 1) return "([A-Za-z])";
|
|
2363
|
+
if (match.length === 2) return "([A-Za-z]{3})";
|
|
2364
|
+
return "([A-Za-z]+)";
|
|
2365
|
+
case "G":
|
|
2366
|
+
if (match.length === 1) return "([A-Za-z])";
|
|
2367
|
+
if (match.length === 2) return "([A-Za-z]{2})";
|
|
2368
|
+
return "([A-Za-z\\s]+)";
|
|
2369
|
+
case "Z":
|
|
2370
|
+
return match.length === 1 ? "([A-Za-z0-9+\\-:]+)" : "([A-Za-z\\s]+)";
|
|
2371
|
+
case "P":
|
|
2372
|
+
return "([A-Za-z\\s]+)";
|
|
2373
|
+
default:
|
|
2374
|
+
return match;
|
|
2375
|
+
}
|
|
2376
|
+
});
|
|
2377
|
+
}).join("");
|
|
2378
|
+
const regex = new RegExp("^" + regexPattern + "$");
|
|
2379
|
+
const matches = dateString.match(regex);
|
|
2380
|
+
if (!matches) {
|
|
2381
|
+
throw new Error(`Date string "${dateString}" does not match pattern "${pattern}"`);
|
|
2382
|
+
}
|
|
2383
|
+
const result = {};
|
|
2384
|
+
const locale = config.locale || "en";
|
|
2385
|
+
let isPM = false;
|
|
2386
|
+
let is12Hour = false;
|
|
2387
|
+
let hourValue;
|
|
2388
|
+
tokens.forEach((token, index) => {
|
|
2389
|
+
const value = matches[index + 1];
|
|
2390
|
+
switch (token.type) {
|
|
2391
|
+
case "y":
|
|
2392
|
+
if (token.length === 2) {
|
|
2393
|
+
const shortYear = parseInt(value, 10);
|
|
2394
|
+
result.year = shortYear < 50 ? 2e3 + shortYear : 1900 + shortYear;
|
|
2395
|
+
} else {
|
|
2396
|
+
result.year = parseInt(value, 10);
|
|
2397
|
+
}
|
|
2398
|
+
break;
|
|
2399
|
+
case "M":
|
|
2400
|
+
switch (token.length) {
|
|
2401
|
+
case 1:
|
|
2402
|
+
case 2:
|
|
2403
|
+
result.month = parseInt(value, 10);
|
|
2404
|
+
break;
|
|
2405
|
+
case 3: {
|
|
2406
|
+
const normalizedValue = normalizeMonthName(value);
|
|
2407
|
+
const monthIndex = getMonthNames(locale).short.findIndex(
|
|
2408
|
+
(name) => name.toLowerCase() === normalizedValue.toLowerCase()
|
|
2409
|
+
);
|
|
2410
|
+
if (monthIndex === -1)
|
|
2411
|
+
throw new Error(`Invalid short month name in date string: ${dateString}`);
|
|
2412
|
+
result.month = monthIndex + 1;
|
|
2413
|
+
break;
|
|
2414
|
+
}
|
|
2415
|
+
case 4: {
|
|
2416
|
+
const normalizedValue = normalizeMonthName(value);
|
|
2417
|
+
const monthIndex = getMonthNames(locale).long.findIndex(
|
|
2418
|
+
(name) => name.toLowerCase() === normalizedValue.toLowerCase()
|
|
2419
|
+
);
|
|
2420
|
+
if (monthIndex === -1)
|
|
2421
|
+
throw new Error(`Invalid full month name in date string: ${dateString}`);
|
|
2422
|
+
result.month = monthIndex + 1;
|
|
2423
|
+
break;
|
|
2424
|
+
}
|
|
2425
|
+
default:
|
|
2426
|
+
throw new Error(`Invalid month pattern: ${token}`);
|
|
2427
|
+
}
|
|
2428
|
+
break;
|
|
2429
|
+
case "d":
|
|
2430
|
+
result.date = parseInt(value, 10);
|
|
2431
|
+
break;
|
|
2432
|
+
case "H":
|
|
2433
|
+
result.hours = parseInt(value, 10);
|
|
2434
|
+
break;
|
|
2435
|
+
case "h":
|
|
2436
|
+
hourValue = parseInt(value, 10);
|
|
2437
|
+
is12Hour = true;
|
|
2438
|
+
break;
|
|
2439
|
+
case "m":
|
|
2440
|
+
result.minutes = parseInt(value, 10);
|
|
2441
|
+
break;
|
|
2442
|
+
case "s":
|
|
2443
|
+
result.seconds = parseInt(value, 10);
|
|
2444
|
+
break;
|
|
2445
|
+
case "S":
|
|
2446
|
+
let ms = parseInt(value, 10);
|
|
2447
|
+
if (token.length === 1) ms *= 100;
|
|
2448
|
+
else if (token.length === 2) ms *= 10;
|
|
2449
|
+
result.milliseconds = ms;
|
|
2450
|
+
break;
|
|
2451
|
+
case "a":
|
|
2452
|
+
isPM = value.toLowerCase().includes("p");
|
|
2453
|
+
break;
|
|
2454
|
+
}
|
|
2455
|
+
});
|
|
2456
|
+
if (is12Hour && hourValue !== void 0) {
|
|
2457
|
+
if (isPM && hourValue < 12) {
|
|
2458
|
+
result.hours = hourValue + 12;
|
|
2459
|
+
} else if (!isPM && hourValue === 12) {
|
|
2460
|
+
result.hours = 0;
|
|
2461
|
+
} else {
|
|
2462
|
+
result.hours = hourValue;
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
if (typeof result.year === "number" && !isValidYear(result.year)) {
|
|
2466
|
+
throw new Error(`Invalid year in date string: ${dateString}`);
|
|
2467
|
+
}
|
|
2468
|
+
if (typeof result.month === "number" && !isValidMonth(result.month)) {
|
|
2469
|
+
throw new Error(`Invalid month in date string: ${dateString}`);
|
|
2470
|
+
}
|
|
2471
|
+
if (typeof result.date === "number" && !isValidDayOfMonth(result.date)) {
|
|
2472
|
+
throw new Error(`Invalid day in date string: ${dateString}`);
|
|
2473
|
+
}
|
|
2474
|
+
if (typeof result.hours === "number" && !isValidHour(result.hours)) {
|
|
2475
|
+
throw new Error(`Invalid hour in date string: ${dateString}`);
|
|
2476
|
+
}
|
|
2477
|
+
if (typeof result.minutes === "number" && !isValidMinute(result.minutes)) {
|
|
2478
|
+
throw new Error(`Invalid minute in date string: ${dateString}`);
|
|
2479
|
+
}
|
|
2480
|
+
if (typeof result.seconds === "number" && !isValidSecond(result.seconds)) {
|
|
2481
|
+
throw new Error(`Invalid second in date string: ${dateString}`);
|
|
2482
|
+
}
|
|
2483
|
+
if (typeof result.milliseconds === "number" && !isValidMillisecond(result.milliseconds)) {
|
|
2484
|
+
throw new Error(`Invalid millisecond in date string: ${dateString}`);
|
|
2485
|
+
}
|
|
2486
|
+
return result;
|
|
2487
|
+
}
|
|
2297
2488
|
|
|
2298
2489
|
// src/Logger.ts
|
|
2299
2490
|
var LEVELS = ["trace", "log", "debug", "info", "warn", "error"];
|