@zelgadis87/utils-core 4.1.1 → 4.2.1

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/esbuild/index.mjs CHANGED
@@ -1,3 +1,167 @@
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
+
1
165
  // src/types/arrays.ts
2
166
  function ensureArray(t) {
3
167
  return t instanceof Array ? t : [t];
@@ -85,6 +249,16 @@ function isFunction(t) {
85
249
  return typeof t === "function";
86
250
  }
87
251
 
252
+ // src/types/json.ts
253
+ function tryToParseJson(jsonContent) {
254
+ try {
255
+ const data = JSON.parse(jsonContent);
256
+ return [data, null];
257
+ } catch (err) {
258
+ return [null, asError(err)];
259
+ }
260
+ }
261
+
88
262
  // src/types/nulls.ts
89
263
  function ensureDefined(v, name = "value") {
90
264
  if (isDefined(v))
@@ -431,6 +605,127 @@ var LazyAsync = class _LazyAsync {
431
605
  }
432
606
  };
433
607
 
608
+ // src/time/TimeInstant.ts
609
+ var import_small_date = __toESM(require_lib());
610
+
611
+ // src/time/TimeUnit.ts
612
+ var TimeUnit = class _TimeUnit {
613
+ constructor(multiplier) {
614
+ this.multiplier = multiplier;
615
+ }
616
+ toUnit(value, unit) {
617
+ return value * (this.multiplier / unit.multiplier);
618
+ }
619
+ toMs(value) {
620
+ return this.toUnit(value, _TimeUnit.MILLISECONDS);
621
+ }
622
+ toSeconds(value) {
623
+ return this.toUnit(value, _TimeUnit.SECONDS);
624
+ }
625
+ toMinutes(value) {
626
+ return this.toUnit(value, _TimeUnit.MINUTES);
627
+ }
628
+ toHours(value) {
629
+ return this.toUnit(value, _TimeUnit.HOURS);
630
+ }
631
+ toDays(value) {
632
+ return this.toUnit(value, _TimeUnit.DAYS);
633
+ }
634
+ toWeeks(value) {
635
+ return this.toUnit(value, _TimeUnit.WEEKS);
636
+ }
637
+ static MILLISECONDS = new _TimeUnit(1);
638
+ static SECONDS = new _TimeUnit(1e3 * _TimeUnit.MILLISECONDS.multiplier);
639
+ static MINUTES = new _TimeUnit(60 * _TimeUnit.SECONDS.multiplier);
640
+ static HOURS = new _TimeUnit(60 * _TimeUnit.MINUTES.multiplier);
641
+ static DAYS = new _TimeUnit(24 * _TimeUnit.HOURS.multiplier);
642
+ static WEEKS = new _TimeUnit(7 * _TimeUnit.DAYS.multiplier);
643
+ };
644
+
645
+ // src/time/TimeBase.ts
646
+ var TimeBase = class {
647
+ _ms;
648
+ constructor(value, unit) {
649
+ this._ms = unit.toMs(value);
650
+ }
651
+ get ms() {
652
+ return Math.floor(this._ms / TimeUnit.MILLISECONDS.multiplier);
653
+ }
654
+ get seconds() {
655
+ return Math.floor(this._ms / TimeUnit.SECONDS.multiplier);
656
+ }
657
+ get minutes() {
658
+ return Math.floor(this._ms / TimeUnit.MINUTES.multiplier);
659
+ }
660
+ get hours() {
661
+ return Math.floor(this._ms / TimeUnit.HOURS.multiplier);
662
+ }
663
+ get days() {
664
+ return Math.floor(this._ms / TimeUnit.DAYS.multiplier);
665
+ }
666
+ addMs(milliseconds) {
667
+ return this.addUnits(milliseconds, TimeUnit.MILLISECONDS);
668
+ }
669
+ addSeconds(seconds) {
670
+ return this.addUnits(seconds, TimeUnit.SECONDS);
671
+ }
672
+ addMinutes(minutes) {
673
+ return this.addUnits(minutes, TimeUnit.MINUTES);
674
+ }
675
+ addHours(hours) {
676
+ return this.addUnits(hours, TimeUnit.HOURS);
677
+ }
678
+ addDays(days) {
679
+ return this.addUnits(days, TimeUnit.DAYS);
680
+ }
681
+ removeDays(days) {
682
+ return this.removeUnits(days, TimeUnit.DAYS);
683
+ }
684
+ addUnits(n, unit) {
685
+ return this.create(this._ms + unit.toMs(n), TimeUnit.MILLISECONDS);
686
+ }
687
+ removeMs(milliseconds) {
688
+ return this.addUnits(-milliseconds, TimeUnit.MILLISECONDS);
689
+ }
690
+ removeSeconds(seconds) {
691
+ return this.addUnits(-seconds, TimeUnit.SECONDS);
692
+ }
693
+ removeMinutes(minutes) {
694
+ return this.addUnits(-minutes, TimeUnit.MINUTES);
695
+ }
696
+ removeHours(hours) {
697
+ return this.addUnits(-hours, TimeUnit.HOURS);
698
+ }
699
+ removeUnits(n, unit) {
700
+ return this.addUnits(-n, unit);
701
+ }
702
+ getUnit(unit) {
703
+ return this._ms / unit.multiplier;
704
+ }
705
+ toUnits() {
706
+ return {
707
+ days: Math.floor(this._ms / TimeUnit.DAYS.multiplier),
708
+ hours: Math.floor(this._ms / TimeUnit.HOURS.multiplier % 24),
709
+ minutes: Math.floor(this._ms / TimeUnit.MINUTES.multiplier % 60),
710
+ seconds: Math.floor(this._ms / TimeUnit.SECONDS.multiplier % 60)
711
+ };
712
+ }
713
+ static toMs(units) {
714
+ if (!units)
715
+ throw new Error("Invalid units given");
716
+ let ms = 0;
717
+ ms += units.ms ?? 0 * TimeUnit.MILLISECONDS.multiplier;
718
+ ms += (units.seconds ?? 0) * TimeUnit.SECONDS.multiplier;
719
+ ms += (units.minutes ?? 0) * TimeUnit.MINUTES.multiplier;
720
+ ms += (units.hours ?? 0) * TimeUnit.HOURS.multiplier;
721
+ ms += (units.days ?? 0) * TimeUnit.DAYS.multiplier;
722
+ return ms;
723
+ }
724
+ toJSON() {
725
+ return this._ms;
726
+ }
727
+ };
728
+
434
729
  // src/async/Deferred.ts
435
730
  var DeferredCanceledError = class extends Error {
436
731
  constructor() {
@@ -516,6 +811,16 @@ var Deferred = class {
516
811
  };
517
812
  var Deferred_default = Deferred;
518
813
 
814
+ // src/random/randomInterval.ts
815
+ function randomInterval(min2, max2) {
816
+ return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
817
+ }
818
+
819
+ // src/random/randomPercentage.ts
820
+ function randomPercentage(min2, max2) {
821
+ return randomInterval(min2, max2) / 100;
822
+ }
823
+
519
824
  // src/utils/bindThis.ts
520
825
  function bindThis(fn, _this) {
521
826
  return fn.bind(_this);
@@ -888,134 +1193,6 @@ function wrap(str, delimiter) {
888
1193
  return delimiter + str + delimiter;
889
1194
  }
890
1195
 
891
- // src/time/TimeUnit.ts
892
- var TimeUnit = class _TimeUnit {
893
- constructor(multiplier) {
894
- this.multiplier = multiplier;
895
- }
896
- toUnit(value, unit) {
897
- return value * (this.multiplier / unit.multiplier);
898
- }
899
- toMs(value) {
900
- return this.toUnit(value, _TimeUnit.MILLISECONDS);
901
- }
902
- toSeconds(value) {
903
- return this.toUnit(value, _TimeUnit.SECONDS);
904
- }
905
- toMinutes(value) {
906
- return this.toUnit(value, _TimeUnit.MINUTES);
907
- }
908
- toHours(value) {
909
- return this.toUnit(value, _TimeUnit.HOURS);
910
- }
911
- toDays(value) {
912
- return this.toUnit(value, _TimeUnit.DAYS);
913
- }
914
- toWeeks(value) {
915
- return this.toUnit(value, _TimeUnit.WEEKS);
916
- }
917
- static MILLISECONDS = new _TimeUnit(1);
918
- static SECONDS = new _TimeUnit(1e3 * _TimeUnit.MILLISECONDS.multiplier);
919
- static MINUTES = new _TimeUnit(60 * _TimeUnit.SECONDS.multiplier);
920
- static HOURS = new _TimeUnit(60 * _TimeUnit.MINUTES.multiplier);
921
- static DAYS = new _TimeUnit(24 * _TimeUnit.HOURS.multiplier);
922
- static WEEKS = new _TimeUnit(7 * _TimeUnit.DAYS.multiplier);
923
- };
924
-
925
- // src/time/TimeBase.ts
926
- var TimeBase = class {
927
- _ms;
928
- constructor(value, unit) {
929
- this._ms = unit.toMs(value);
930
- }
931
- get ms() {
932
- return Math.floor(this._ms / TimeUnit.MILLISECONDS.multiplier);
933
- }
934
- get seconds() {
935
- return Math.floor(this._ms / TimeUnit.SECONDS.multiplier);
936
- }
937
- get minutes() {
938
- return Math.floor(this._ms / TimeUnit.MINUTES.multiplier);
939
- }
940
- get hours() {
941
- return Math.floor(this._ms / TimeUnit.HOURS.multiplier);
942
- }
943
- get days() {
944
- return Math.floor(this._ms / TimeUnit.DAYS.multiplier);
945
- }
946
- addMs(milliseconds) {
947
- return this.addUnits(milliseconds, TimeUnit.MILLISECONDS);
948
- }
949
- addSeconds(seconds) {
950
- return this.addUnits(seconds, TimeUnit.SECONDS);
951
- }
952
- addMinutes(minutes) {
953
- return this.addUnits(minutes, TimeUnit.MINUTES);
954
- }
955
- addHours(hours) {
956
- return this.addUnits(hours, TimeUnit.HOURS);
957
- }
958
- addDays(days) {
959
- return this.addUnits(days, TimeUnit.DAYS);
960
- }
961
- removeDays(days) {
962
- return this.removeUnits(days, TimeUnit.DAYS);
963
- }
964
- addUnits(n, unit) {
965
- return this.create(this._ms + unit.toMs(n), TimeUnit.MILLISECONDS);
966
- }
967
- removeMs(milliseconds) {
968
- return this.addUnits(-milliseconds, TimeUnit.MILLISECONDS);
969
- }
970
- removeSeconds(seconds) {
971
- return this.addUnits(-seconds, TimeUnit.SECONDS);
972
- }
973
- removeMinutes(minutes) {
974
- return this.addUnits(-minutes, TimeUnit.MINUTES);
975
- }
976
- removeHours(hours) {
977
- return this.addUnits(-hours, TimeUnit.HOURS);
978
- }
979
- removeUnits(n, unit) {
980
- return this.addUnits(-n, unit);
981
- }
982
- getUnit(unit) {
983
- return this._ms / unit.multiplier;
984
- }
985
- toUnits() {
986
- return {
987
- days: Math.floor(this._ms / TimeUnit.DAYS.multiplier),
988
- hours: Math.floor(this._ms / TimeUnit.HOURS.multiplier % 24),
989
- minutes: Math.floor(this._ms / TimeUnit.MINUTES.multiplier % 60),
990
- seconds: Math.floor(this._ms / TimeUnit.SECONDS.multiplier % 60)
991
- };
992
- }
993
- static toMs(units) {
994
- if (!units)
995
- throw new Error("Invalid units given");
996
- let ms = 0;
997
- ms += units.ms ?? 0 * TimeUnit.MILLISECONDS.multiplier;
998
- ms += (units.seconds ?? 0) * TimeUnit.SECONDS.multiplier;
999
- ms += (units.minutes ?? 0) * TimeUnit.MINUTES.multiplier;
1000
- ms += (units.hours ?? 0) * TimeUnit.HOURS.multiplier;
1001
- ms += (units.days ?? 0) * TimeUnit.DAYS.multiplier;
1002
- return ms;
1003
- }
1004
- toJSON() {
1005
- return this._ms;
1006
- }
1007
- };
1008
-
1009
- // src/random/randomInterval.ts
1010
- function randomInterval(min2, max2) {
1011
- return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
1012
- }
1013
-
1014
- // src/random/randomPercentage.ts
1015
- function randomPercentage(min2, max2) {
1016
- return randomInterval(min2, max2) / 100;
1017
- }
1018
-
1019
1196
  // src/time/TimeDuration.ts
1020
1197
  var TimeDuration = class _TimeDuration extends TimeBase {
1021
1198
  constructor(value, unit) {
@@ -1056,14 +1233,14 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1056
1233
  * @returns the duration, with only the most significant units displayed as a human readable string, eg: 1d 20h, 10m 30s.
1057
1234
  */
1058
1235
  get formatted() {
1059
- const format = (x, u1, y, u2) => `${x}${u1} ${pad(y.toString(), 2, "0")}${u2}`;
1236
+ const format2 = (x, u1, y, u2) => `${x}${u1} ${pad(y.toString(), 2, "0")}${u2}`;
1060
1237
  const units = this.toUnits();
1061
1238
  if (units.days >= 1)
1062
- return format(units.days, "d", units.hours, "h");
1239
+ return format2(units.days, "d", units.hours, "h");
1063
1240
  else if (units.hours >= 1)
1064
- return format(units.hours, "h", units.minutes, "m");
1241
+ return format2(units.hours, "h", units.minutes, "m");
1065
1242
  else if (units.minutes >= 1)
1066
- return format(units.minutes, "m", units.seconds, "s");
1243
+ return format2(units.minutes, "m", units.seconds, "s");
1067
1244
  else if (units.seconds >= 0)
1068
1245
  return units.seconds.toString() + "s";
1069
1246
  else if (units.seconds > -60)
@@ -1096,8 +1273,10 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1096
1273
  const deferred = this.promise();
1097
1274
  void deferred.then(() => {
1098
1275
  cb();
1099
- }, (_err) => {
1100
- return;
1276
+ }, (err) => {
1277
+ if (err instanceof DeferredCanceledError)
1278
+ return;
1279
+ throw err;
1101
1280
  });
1102
1281
  return { cancel: () => deferred.cancel() };
1103
1282
  }
@@ -1296,7 +1475,7 @@ var timeInstantResolveValue = (getFromDate2, referenceDate, x) => {
1296
1475
  if (relativeTo === void 0 || relativeTo === "now") {
1297
1476
  return getFromDate2(referenceDate) + relative;
1298
1477
  } else {
1299
- return getFromDate2(relativeTo.asDateUTC()) + relative;
1478
+ return getFromDate2(relativeTo.toDate()) + relative;
1300
1479
  }
1301
1480
  } else if ("absolute" in x) {
1302
1481
  return x.absolute;
@@ -1315,7 +1494,7 @@ var getFromDate = {
1315
1494
  };
1316
1495
  var toReferenceDate = (x) => {
1317
1496
  ensureDefined(x);
1318
- if (isTimeInstant(x)) return x.asDateUTC();
1497
+ if (isTimeInstant(x)) return x.toDate();
1319
1498
  return x;
1320
1499
  };
1321
1500
  function createTimeInstantFromParameters(aParameters, aReferenceDate = TimeInstant.now()) {
@@ -1335,7 +1514,7 @@ function createTimeInstantFromParameters(aParameters, aReferenceDate = TimeInsta
1335
1514
  return TimeInstant.fromUnixTimestamp(timestamp2);
1336
1515
  }
1337
1516
  function timeInstantBuilder() {
1338
- let referenceDate = TimeInstant.now().asDateUTC();
1517
+ let referenceDate = TimeInstant.now().toDate();
1339
1518
  const value = { ...defaultTimeInstantCreationParameters };
1340
1519
  const ret = {
1341
1520
  year: (x) => {
@@ -1411,157 +1590,103 @@ var TimeInstant = class _TimeInstant extends TimeBase {
1411
1590
  atEndOfDay() {
1412
1591
  return this.atTime({ hours: 23, minutes: 59, seconds: 59, milliseconds: 999 });
1413
1592
  }
1414
- /** @deprecated[2024-10-24]: Use #promise() instead. */
1415
- timeout(cb) {
1416
- if (!this.isInTheFuture) {
1417
- throw new Error("Instant is in the past");
1418
- } else {
1419
- return this.distanceFromNow().timeout(() => cb());
1420
- }
1421
- }
1422
1593
  promise() {
1423
- const deferred = new Deferred_default();
1424
- if (!this.isInTheFuture) {
1425
- deferred.resolve();
1426
- } else {
1427
- this.distanceFromNow().timeout(() => deferred.resolve());
1428
- }
1429
- return deferred.asCancelablePromise;
1594
+ this.ensureInTheFuture();
1595
+ return this.distanceFromNow().promise();
1430
1596
  }
1431
1597
  delay(cb) {
1432
- const deferred = this.promise();
1433
- void deferred.then(() => {
1434
- cb();
1435
- }, (_err) => {
1436
- return;
1437
- });
1438
- return { cancel: () => deferred.cancel() };
1598
+ this.ensureInTheFuture();
1599
+ return this.distanceFromNow().delay(cb);
1600
+ }
1601
+ ensureInTheFuture() {
1602
+ if (!this.isInTheFuture)
1603
+ throw new Error("Instant is in the past");
1604
+ }
1605
+ ensureInThePast() {
1606
+ if (!this.isInThePast)
1607
+ throw new Error("Instant is in the future");
1439
1608
  }
1440
1609
  isToday() {
1441
1610
  return Math.floor(this.days) === Math.floor(_TimeInstant.now().days);
1442
1611
  }
1612
+ /**
1613
+ * @returns the time as HH:mm:ss
1614
+ * @deprecated [2024-12-11] this method is too ambigous and should be removed.
1615
+ */
1443
1616
  asTimeString() {
1444
- const date = this.asDate();
1445
- return `${this.doGetTwoDigitsHours(date)}:${this.doGetTwoDigitsMinutes(date)}:${this.doGetTwoDigitsSeconds(date)}`;
1617
+ return this.format("HH:mm:ss");
1446
1618
  }
1619
+ /**
1620
+ * @returns the date as dd/MM/yyyy
1621
+ * @deprecated [2024-12-11] this method is too ambigous and should be removed.
1622
+ */
1447
1623
  asDateString() {
1448
- const date = this.asDate();
1449
- return `${this.doGetTwoDigitsDays(date)}/${this.doGetTwoDigitsMonths(date)}/${this.doGetFourDigitsYears(date)}`;
1624
+ return this.format("dd/MM/yyyy");
1450
1625
  }
1451
1626
  /**
1452
- * @returns this instant, in ISO 8601 format (eg, 2024-11-01T15:49:22.024+0200). The format is meant to always be realiable.
1453
- */
1454
- asIso8601() {
1455
- const dash = "-", colon = ".", doublecolon = ":", T = "T", date = this.asDate();
1456
- return this.doGetFourDigitsYears(date) + dash + this.doGetTwoDigitsMonths(date) + dash + this.doGetTwoDigitsDays(date) + T + this.doGetTwoDigitsHours(date) + doublecolon + this.doGetTwoDigitsMinutes(date) + doublecolon + this.doGetTwoDigitsSeconds(date) + colon + this.doGetThreeDigitsMilliseconds(date) + this.doGetFourDigitsTimezoneOffset(date);
1627
+ * Formats this instant using the given pattern. The pattern can contain the following tokens:
1628
+ *
1629
+ * | Token | Description | Example |
1630
+ * |:------|:--------------------------------|:------------------------------|
1631
+ * | D | Weekday, 1 letter | W |
1632
+ * | DD | Weekday, 3 letters | Wed |
1633
+ * | DDD | Weekday, long | Wednesday |
1634
+ * | d | Day of the month, no padding | 3 |
1635
+ * | dd | Day of the month, padded to 2 | 03 |
1636
+ * | M | Month, numeric | 3 |
1637
+ * | MM | Month, 2 digits | 03 |
1638
+ * | MMM | Month, 3 letters | Mar |
1639
+ * | MMMM | Month, long | March |
1640
+ * | y | Year, numeric | 2021 |
1641
+ * | yy | Year, 2 digits | 21 |
1642
+ * | yyyy | Year, numeric | 2021 |
1643
+ * | h | Hours, no padding | 6 |
1644
+ * | hh | Hours, padded to 2 | 06 |
1645
+ * | H | Hours in 24-format, no padding | 18 |
1646
+ * | HH | Hours in 24-format, padded to 2 | 18 |
1647
+ * | m | Minutes, no padding | 7 |
1648
+ * | m | Minutes, padded to 2 | 07 |
1649
+ * | s | Seconds, no padding | 8 |
1650
+ * | ss | Seconds, padded to 2 | 08 |
1651
+ * | S | Milliseconds, no padding | 9 |
1652
+ * | SS | Milliseconds, padded to 2 | 09 |
1653
+ * | SSS | Milliseconds, padded to 3 | 009 |
1654
+ * | G | Era, narrow | A |
1655
+ * | GG | Era, short | AD |
1656
+ * | GGG | Era, long | Anno Domino |
1657
+ * | Z | Time zone, short | GMT+1 |
1658
+ * | ZZ | Time short, long C | entral European Standard Time |
1659
+ * | P | Period of the day, narrow | in the morning |
1660
+ * | PP | Period of the day, short | in the morning |
1661
+ * | PPP | Period of the day, long | in the morning |
1662
+ * | a | Meridiem | pm |
1663
+ * @param pattern The pattern to use. Refer to the token table above for details.
1664
+ * @param config An optional locale and timeZone definition to use during the format.
1665
+ * @returns a string, formatted using the given pattern, at the given timeZone with the given locale.
1666
+ */
1667
+ format(pattern, config = {}) {
1668
+ return (0, import_small_date.format)(this.toDate(), pattern, config);
1457
1669
  }
1458
1670
  /**
1459
- * @returns this instant, in ISO 8601 format and UTC time zone (eg, 2024-11-01T15:49:22.024Z). The format is meant to always be realiable.
1460
- */
1461
- asIso8601UTC() {
1462
- const dash = "-", colon = ".", doublecolon = ":", T = "T", utcDate = this.asDateUTC();
1463
- return this.doGetFourDigitsYears(utcDate) + dash + this.doGetTwoDigitsMonths(utcDate) + dash + this.doGetTwoDigitsDays(utcDate) + T + this.doGetTwoDigitsHours(utcDate) + doublecolon + this.doGetTwoDigitsMinutes(utcDate) + doublecolon + this.doGetTwoDigitsSeconds(utcDate) + colon + this.doGetThreeDigitsMilliseconds(utcDate) + "Z";
1671
+ * @returns this instant, in ISO 8601 format (eg, 2024-11-01T15:49:22.024Z). The format is meant to always be realiable.
1672
+ */
1673
+ asIso8601() {
1674
+ return this.format('yyyy-MM-dd"T"HH:mm:ss.SSS"Z"', { timeZone: "UTC" });
1464
1675
  }
1465
1676
  /**
1466
1677
  * @returns this instant, in a human readable format. The format COULD change in the future, do NOT use this method for consistent outputs.
1678
+ * @deprecated [2024-12-11] this method is too broad and ambigous and should be removed.
1467
1679
  */
1468
1680
  asHumanTimestamp() {
1469
- const dash = "-", doublecolon = ":", space = " ", T = "T", date = this.asDate();
1470
- return this.doGetFourDigitsYears(date) + dash + this.doGetTwoDigitsMonths(date) + dash + this.doGetTwoDigitsDays(date) + space + this.doGetTwoDigitsHours(date) + doublecolon + this.doGetTwoDigitsMinutes(date) + doublecolon + this.doGetTwoDigitsSeconds(date);
1471
- }
1472
- // Please do not use this method internally, as it is not optimized for continued usage.
1473
- get twoDigitsDays() {
1474
- return this.doGetTwoDigitsDays(this.asDate());
1475
- }
1476
- // Please do not use this method internally, as it is not optimized for continued usage.
1477
- get twoDigitsMonths() {
1478
- return this.doGetTwoDigitsMonths(this.asDate());
1479
- }
1480
- // Please do not use this method internally, as it is not optimized for continued usage.
1481
- get fourDigitsYears() {
1482
- return this.doGetFourDigitsYears(this.asDate());
1483
- }
1484
- // Please do not use this method internally, as it is not optimized for continued usage.
1485
- get twoDigitsHours() {
1486
- return this.doGetTwoDigitsHours(this.asDate());
1487
- }
1488
- // Please do not use this method internally, as it is not optimized for continued usage.
1489
- get twoDigitsMinutes() {
1490
- return this.doGetTwoDigitsMinutes(this.asDate());
1491
- }
1492
- // Please do not use this method internally, as it is not optimized for continued usage.
1493
- get twoDigitsSeconds() {
1494
- return this.doGetTwoDigitsSeconds(this.asDate());
1495
- }
1496
- // Please do not use this method internally, as it is not optimized for continued usage.
1497
- get threeDigitsMilliseconds() {
1498
- return this.doGetThreeDigitsMilliseconds(this.asDate());
1499
- }
1500
- // Please do not use this method internally, as it is not optimized for continued usage.
1501
- get twoDigitsDaysUTC() {
1502
- return this.doGetTwoDigitsDays(this.asDateUTC());
1503
- }
1504
- // Please do not use this method internally, as it is not optimized for continued usage.
1505
- get twoDigitsMonthsUTC() {
1506
- return this.doGetTwoDigitsMonths(this.asDateUTC());
1507
- }
1508
- // Please do not use this method internally, as it is not optimized for continued usage.
1509
- get fourDigitsYearsUTC() {
1510
- return this.doGetFourDigitsYears(this.asDateUTC());
1511
- }
1512
- // Please do not use this method internally, as it is not optimized for continued usage.
1513
- get twoDigitsHoursUTC() {
1514
- return this.doGetTwoDigitsHours(this.asDateUTC());
1515
- }
1516
- // Please do not use this method internally, as it is not optimized for continued usage.
1517
- get twoDigitsMinutesUTC() {
1518
- return this.doGetTwoDigitsMinutes(this.asDateUTC());
1519
- }
1520
- // Please do not use this method internally, as it is not optimized for continued usage.
1521
- get twoDigitsSecondsUTC() {
1522
- return this.doGetTwoDigitsSeconds(this.asDateUTC());
1523
- }
1524
- // Please do not use this method internally, as it is not optimized for continued usage.
1525
- get threeDigitsMillisecondsUTC() {
1526
- return this.doGetThreeDigitsMilliseconds(this.asDateUTC());
1527
- }
1528
- // Please do not use this method internally, as it is not optimized for continued usage.
1529
- get fourDigitsTimezoneOffset() {
1530
- const offset = this.asDate().getTimezoneOffset();
1531
- return (offset >= 0 ? "+" : "-") + pad(Math.floor(Math.abs(offset) / 60).toString(), 2, "0") + pad((Math.abs(offset) % 60).toString(), 2, "0");
1532
- }
1533
- doGetTwoDigitsDays(date) {
1534
- return pad(date.getDate().toString(), 2, "0");
1535
- }
1536
- doGetTwoDigitsMonths(date) {
1537
- return pad((date.getMonth() + 1).toString(), 2, "0");
1538
- }
1539
- doGetFourDigitsYears(date) {
1540
- return pad(date.getFullYear().toString(), 4, "0");
1541
- }
1542
- doGetTwoDigitsHours(date) {
1543
- return pad(date.getHours().toString(), 2, "0");
1544
- }
1545
- doGetTwoDigitsMinutes(date) {
1546
- return pad(date.getMinutes().toString(), 2, "0");
1547
- }
1548
- doGetTwoDigitsSeconds(date) {
1549
- return pad(date.getSeconds().toString(), 2, "0");
1550
- }
1551
- doGetThreeDigitsMilliseconds(date) {
1552
- return pad(date.getMilliseconds().toString(), 3, "0");
1553
- }
1554
- doGetFourDigitsTimezoneOffset(date) {
1555
- const offset = date.getTimezoneOffset();
1556
- return (offset >= 0 ? "+" : "-") + pad(Math.floor(Math.abs(offset) / 60).toString(), 2, "0") + pad((Math.abs(offset) % 60).toString(), 2, "0");
1681
+ return this.format("yyyy-MM-dd HH:mm:ss");
1557
1682
  }
1558
- asUnixTimestamp() {
1683
+ toUnixTimestamp() {
1559
1684
  return this.ms;
1560
1685
  }
1561
- asDate() {
1686
+ toDate() {
1562
1687
  return new Date(this.ms);
1563
1688
  }
1564
- asDateUTC() {
1689
+ toDateUTC() {
1565
1690
  return new Date(this.ms + (/* @__PURE__ */ new Date()).getTimezoneOffset() * 60 * 1e3);
1566
1691
  }
1567
1692
  get isInThePast() {
@@ -1682,23 +1807,23 @@ var TimeInstant = class _TimeInstant extends TimeBase {
1682
1807
  return _TimeInstant.fromUnixTimestamp(timestamp2);
1683
1808
  }
1684
1809
  get dayOfMonth() {
1685
- return this.asDate().getDate();
1810
+ return this.toDate().getDate();
1686
1811
  }
1687
1812
  get dayOfWeek() {
1688
- return this.asDate().getDay() + 1;
1813
+ return this.toDate().getDay() + 1;
1689
1814
  }
1690
1815
  get month() {
1691
- return this.asDate().getMonth() + 1;
1816
+ return this.toDate().getMonth() + 1;
1692
1817
  }
1693
1818
  get year() {
1694
- return this.asDate().getFullYear();
1819
+ return this.toDate().getFullYear();
1695
1820
  }
1696
1821
  /**
1697
1822
  * Returns the week number represented by this instant, according to the ISO 8601 standard.
1698
1823
  * 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.
1699
1824
  */
1700
1825
  get weekNumber() {
1701
- const date = this.asDate();
1826
+ const date = this.toDate();
1702
1827
  const oneDay = 1e3 * 60 * 60 * 24;
1703
1828
  const thursdayOfThisWeek = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 4 - (date.getDay() || 7), 14, 0, 0);
1704
1829
  const firstOfJanuary = new Date(thursdayOfThisWeek.getFullYear(), 0, 1, 14, 0, 0);
@@ -2550,6 +2675,7 @@ export {
2550
2675
  sum,
2551
2676
  sumBy,
2552
2677
  throttle,
2678
+ tryToParseJson,
2553
2679
  uniq,
2554
2680
  uniqBy,
2555
2681
  uniqByKey,