@naturalcycles/js-lib 14.235.0 → 14.237.0
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/dist/datetime/localDate.d.ts +2 -1
- package/dist/datetime/localDate.js +7 -0
- package/dist/datetime/localTime.d.ts +67 -26
- package/dist/datetime/localTime.js +99 -33
- package/dist/datetime/wallTime.d.ts +33 -0
- package/dist/datetime/wallTime.js +48 -0
- package/dist/index.d.ts +1 -5
- package/dist/index.js +1 -5
- package/dist-esm/array/range.js +4 -7
- package/dist-esm/datetime/localDate.js +7 -0
- package/dist-esm/datetime/localTime.js +105 -35
- package/dist-esm/datetime/wallTime.js +44 -0
- package/dist-esm/decorators/asyncMemo.decorator.js +1 -1
- package/dist-esm/decorators/createPromiseDecorator.js +10 -5
- package/dist-esm/decorators/debounce.js +6 -1
- package/dist-esm/decorators/memo.decorator.js +1 -1
- package/dist-esm/define.js +14 -2
- package/dist-esm/enum.util.js +1 -2
- package/dist-esm/error/assert.js +12 -3
- package/dist-esm/error/error.util.js +13 -8
- package/dist-esm/error/tryCatch.js +1 -1
- package/dist-esm/http/fetcher.js +74 -41
- package/dist-esm/index.js +1 -5
- package/dist-esm/iter/asyncIterable2.js +37 -122
- package/dist-esm/json-schema/jsonSchema.util.js +2 -3
- package/dist-esm/json-schema/jsonSchemaBuilder.js +1 -2
- package/dist-esm/math/math.util.js +1 -1
- package/dist-esm/object/object.util.js +4 -5
- package/dist-esm/polyfill.js +2 -3
- package/dist-esm/promise/abortable.js +1 -2
- package/dist-esm/promise/pMap.js +3 -3
- package/dist-esm/promise/pQueue.js +7 -2
- package/dist-esm/string/json.util.js +3 -3
- package/dist-esm/string/readingTime.js +4 -1
- package/dist-esm/string/safeJsonStringify.js +2 -2
- package/dist-esm/string/stringify.js +1 -1
- package/dist-esm/web.js +2 -4
- package/dist-esm/zod/zod.util.js +1 -2
- package/package.json +1 -1
- package/src/datetime/localDate.ts +9 -1
- package/src/datetime/localTime.ts +110 -37
- package/src/datetime/wallTime.ts +56 -0
- package/src/index.ts +1 -5
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { _assert } from '../error/assert';
|
|
2
2
|
import { _ms } from '../time/time.util';
|
|
3
3
|
import { localDate } from './localDate';
|
|
4
|
+
import { WallTime } from './wallTime';
|
|
4
5
|
export var ISODayOfWeek;
|
|
5
6
|
(function (ISODayOfWeek) {
|
|
6
7
|
ISODayOfWeek[ISODayOfWeek["MONDAY"] = 1] = "MONDAY";
|
|
@@ -35,6 +36,78 @@ export class LocalTime {
|
|
|
35
36
|
local() {
|
|
36
37
|
return new LocalTime(new Date(this.$date.getTime()));
|
|
37
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns [cloned] fake LocalTime that has yyyy-mm-dd hh:mm:ss in the provided timezone.
|
|
41
|
+
* It is a fake LocalTime in a sense that it's timezone is not real.
|
|
42
|
+
* See this ("common errors"): https://stackoverflow.com/a/15171030/4919972
|
|
43
|
+
* Fake also means that unixTimestamp of that new LocalDate is not the same.
|
|
44
|
+
* For that reason we return WallTime, and not a LocalTime.
|
|
45
|
+
* WallTime can be pretty-printed as Date-only, Time-only or DateAndTime.
|
|
46
|
+
*
|
|
47
|
+
* E.g `inTimezone('America/New_York').toISOTime()`
|
|
48
|
+
*
|
|
49
|
+
* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
50
|
+
*
|
|
51
|
+
* @experimental
|
|
52
|
+
*/
|
|
53
|
+
inTimezone(tz) {
|
|
54
|
+
const d = new Date(this.$date.toLocaleString('en-US', { timeZone: tz }));
|
|
55
|
+
return new WallTime({
|
|
56
|
+
year: d.getFullYear(),
|
|
57
|
+
month: d.getMonth() + 1,
|
|
58
|
+
day: d.getDate(),
|
|
59
|
+
hour: d.getHours(),
|
|
60
|
+
minute: d.getMinutes(),
|
|
61
|
+
second: d.getSeconds(),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
|
|
66
|
+
* to the local time to get UTC time.
|
|
67
|
+
*
|
|
68
|
+
* E.g utcOffset for CEST is -120,
|
|
69
|
+
* which means that you need to add -120 minutes to the local time to get UTC time.
|
|
70
|
+
*
|
|
71
|
+
* Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
|
|
72
|
+
*
|
|
73
|
+
* If timezone (tz) is specified, e.g `America/New_York`,
|
|
74
|
+
* it will return the UTC offset for that timezone.
|
|
75
|
+
*
|
|
76
|
+
* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
77
|
+
*/
|
|
78
|
+
getUTCOffsetMinutes(tz) {
|
|
79
|
+
if (tz) {
|
|
80
|
+
// based on: https://stackoverflow.com/a/53652131/4919972
|
|
81
|
+
const nowTime = this.$date.getTime();
|
|
82
|
+
const tzTime = new Date(this.$date.toLocaleString('en-US', { timeZone: tz })).getTime();
|
|
83
|
+
return Math.round((tzTime - nowTime) / 60000) || 0;
|
|
84
|
+
}
|
|
85
|
+
return -this.$date.getTimezoneOffset() || 0;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Same as getUTCOffsetMinutes, but rounded to hours.
|
|
89
|
+
*
|
|
90
|
+
* E.g for CEST it is -2.
|
|
91
|
+
*
|
|
92
|
+
* Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
|
|
93
|
+
*
|
|
94
|
+
* If timezone (tz) is specified, e.g `America/New_York`,
|
|
95
|
+
* it will return the UTC offset for that timezone.
|
|
96
|
+
*/
|
|
97
|
+
getUTCOffsetHours(tz) {
|
|
98
|
+
return Math.round(this.getUTCOffsetMinutes(tz) / 60);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns e.g `-05:00` for New_York winter time.
|
|
102
|
+
*/
|
|
103
|
+
getUTCOffsetString(tz) {
|
|
104
|
+
const minutes = this.getUTCOffsetMinutes(tz);
|
|
105
|
+
const hours = Math.trunc(minutes / 60);
|
|
106
|
+
const sign = hours < 0 ? '-' : '+';
|
|
107
|
+
const h = String(Math.abs(hours)).padStart(2, '0');
|
|
108
|
+
const m = String(minutes % 60).padStart(2, '0');
|
|
109
|
+
return `${sign}${h}:${m}`;
|
|
110
|
+
}
|
|
38
111
|
get(unit) {
|
|
39
112
|
if (unit === 'year') {
|
|
40
113
|
return this.$date.getFullYear();
|
|
@@ -112,11 +185,10 @@ export class LocalTime {
|
|
|
112
185
|
return v === undefined ? this.get('second') : this.set('second', v);
|
|
113
186
|
}
|
|
114
187
|
setComponents(c, mutate = false) {
|
|
115
|
-
var _a;
|
|
116
188
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
117
189
|
// Year, month and day set all-at-once, to avoid 30/31 (and 28/29) mishap
|
|
118
190
|
if (c.day || c.month !== undefined || c.year !== undefined) {
|
|
119
|
-
d.setFullYear(
|
|
191
|
+
d.setFullYear(c.year ?? d.getFullYear(), c.month ? c.month - 1 : d.getMonth(), c.day || d.getDate());
|
|
120
192
|
}
|
|
121
193
|
if (c.hour !== undefined) {
|
|
122
194
|
d.setHours(c.hour);
|
|
@@ -317,13 +389,13 @@ export class LocalTime {
|
|
|
317
389
|
* Third argument allows to override "now".
|
|
318
390
|
*/
|
|
319
391
|
isOlderThan(n, unit, now) {
|
|
320
|
-
return this.isBefore(localTime.of(now
|
|
392
|
+
return this.isBefore(localTime.of(now ?? new Date()).plus(-n, unit));
|
|
321
393
|
}
|
|
322
394
|
/**
|
|
323
395
|
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
324
396
|
*/
|
|
325
397
|
isSameOrOlderThan(n, unit, now) {
|
|
326
|
-
return this.isSameOrBefore(localTime.of(now
|
|
398
|
+
return this.isSameOrBefore(localTime.of(now ?? new Date()).plus(-n, unit));
|
|
327
399
|
}
|
|
328
400
|
/**
|
|
329
401
|
* Checks if this localTime is younger (>) than "now" by X units.
|
|
@@ -335,13 +407,13 @@ export class LocalTime {
|
|
|
335
407
|
* Third argument allows to override "now".
|
|
336
408
|
*/
|
|
337
409
|
isYoungerThan(n, unit, now) {
|
|
338
|
-
return this.isAfter(localTime.of(now
|
|
410
|
+
return this.isAfter(localTime.of(now ?? new Date()).plus(-n, unit));
|
|
339
411
|
}
|
|
340
412
|
/**
|
|
341
413
|
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
342
414
|
*/
|
|
343
415
|
isSameOrYoungerThan(n, unit, now) {
|
|
344
|
-
return this.isSameOrAfter(localTime.of(now
|
|
416
|
+
return this.isSameOrAfter(localTime.of(now ?? new Date()).plus(-n, unit));
|
|
345
417
|
}
|
|
346
418
|
/**
|
|
347
419
|
* Returns 1 if this > d
|
|
@@ -355,17 +427,20 @@ export class LocalTime {
|
|
|
355
427
|
return 0;
|
|
356
428
|
return t1 < t2 ? -1 : 1;
|
|
357
429
|
}
|
|
358
|
-
|
|
359
|
-
return
|
|
430
|
+
getDateTimeObject() {
|
|
431
|
+
return {
|
|
432
|
+
...this.getDateObject(),
|
|
433
|
+
...this.getTimeObject(),
|
|
434
|
+
};
|
|
360
435
|
}
|
|
361
|
-
|
|
436
|
+
getDateObject() {
|
|
362
437
|
return {
|
|
363
438
|
year: this.$date.getFullYear(),
|
|
364
439
|
month: this.$date.getMonth() + 1,
|
|
365
440
|
day: this.$date.getDate(),
|
|
366
441
|
};
|
|
367
442
|
}
|
|
368
|
-
|
|
443
|
+
getTimeObject() {
|
|
369
444
|
return {
|
|
370
445
|
hour: this.$date.getHours(),
|
|
371
446
|
minute: this.$date.getMinutes(),
|
|
@@ -422,7 +497,7 @@ export class LocalTime {
|
|
|
422
497
|
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
423
498
|
*/
|
|
424
499
|
toISODate() {
|
|
425
|
-
const { year, month, day } = this.
|
|
500
|
+
const { year, month, day } = this.getDateObject();
|
|
426
501
|
return [
|
|
427
502
|
String(year).padStart(4, '0'),
|
|
428
503
|
String(month).padStart(2, '0'),
|
|
@@ -435,7 +510,7 @@ export class LocalTime {
|
|
|
435
510
|
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
436
511
|
*/
|
|
437
512
|
toISOTime(seconds = true) {
|
|
438
|
-
const { hour, minute, second } = this.
|
|
513
|
+
const { hour, minute, second } = this.getTimeObject();
|
|
439
514
|
return [
|
|
440
515
|
String(hour).padStart(2, '0'),
|
|
441
516
|
String(minute).padStart(2, '0'),
|
|
@@ -450,7 +525,7 @@ export class LocalTime {
|
|
|
450
525
|
* Returns e.g: `19840621_1705`
|
|
451
526
|
*/
|
|
452
527
|
toStringCompact(seconds = false) {
|
|
453
|
-
const { year, month, day, hour, minute, second } = this.
|
|
528
|
+
const { year, month, day, hour, minute, second } = this.getDateTimeObject();
|
|
454
529
|
return [
|
|
455
530
|
String(year).padStart(4, '0'),
|
|
456
531
|
String(month).padStart(2, '0'),
|
|
@@ -554,6 +629,23 @@ class LocalTimeFactory {
|
|
|
554
629
|
isValid(d) {
|
|
555
630
|
return this.parseOrNull(d) !== null;
|
|
556
631
|
}
|
|
632
|
+
/**
|
|
633
|
+
* Returns the IANA timezone e.g `Europe/Stockholm`.
|
|
634
|
+
* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
635
|
+
*/
|
|
636
|
+
getTimezone() {
|
|
637
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Returns true if passed IANA timezone is valid/supported.
|
|
641
|
+
* E.g `Europe/Stockholm` is valid, but `Europe/Stockholm2` is not.
|
|
642
|
+
*
|
|
643
|
+
* This implementation is not optimized for performance. If you need frequent validation -
|
|
644
|
+
* consider caching the Intl.supportedValuesOf values as Set and reuse that.
|
|
645
|
+
*/
|
|
646
|
+
isTimezoneValid(tz) {
|
|
647
|
+
return Intl.supportedValuesOf('timeZone').includes(tz);
|
|
648
|
+
}
|
|
557
649
|
now() {
|
|
558
650
|
return new LocalTime(new Date());
|
|
559
651
|
}
|
|
@@ -703,25 +795,3 @@ Object.setPrototypeOf(localTime, localTimeFactory);
|
|
|
703
795
|
export function nowUnix() {
|
|
704
796
|
return Math.floor(Date.now() / 1000);
|
|
705
797
|
}
|
|
706
|
-
/**
|
|
707
|
-
* UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
|
|
708
|
-
* to the local time to get UTC time.
|
|
709
|
-
*
|
|
710
|
-
* E.g utcOffset for CEST is -120,
|
|
711
|
-
* which means that you need to add -120 minutes to the local time to get UTC time.
|
|
712
|
-
*
|
|
713
|
-
* Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
|
|
714
|
-
*/
|
|
715
|
-
export function getUTCOffsetMinutes() {
|
|
716
|
-
return -new Date().getTimezoneOffset() || 0;
|
|
717
|
-
}
|
|
718
|
-
/**
|
|
719
|
-
* Same as getUTCOffsetMinutes, but rounded to hours.
|
|
720
|
-
*
|
|
721
|
-
* E.g for CEST it is -2.
|
|
722
|
-
*
|
|
723
|
-
* Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
|
|
724
|
-
*/
|
|
725
|
-
export function getUTCOffsetHours() {
|
|
726
|
-
return Math.round(getUTCOffsetMinutes() / 60);
|
|
727
|
-
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Representation of a "time on the wall clock",
|
|
3
|
+
* which means "local time, regardless of timezone".
|
|
4
|
+
*
|
|
5
|
+
* Experimental simplified container object to hold
|
|
6
|
+
* date and time components as numbers.
|
|
7
|
+
* No math or manipulation is possible here.
|
|
8
|
+
* Can be pretty-printed as Date, Time or DateAndTime.
|
|
9
|
+
*/
|
|
10
|
+
export class WallTime {
|
|
11
|
+
constructor(obj) {
|
|
12
|
+
Object.assign(this, obj);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns e.g: `1984-06-21 17:56:21`
|
|
16
|
+
* or (if seconds=false):
|
|
17
|
+
* `1984-06-21 17:56`
|
|
18
|
+
*/
|
|
19
|
+
toPretty(seconds = true) {
|
|
20
|
+
return this.toISODate() + ' ' + this.toISOTime(seconds);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns e.g: `1984-06-21`, only the date part of DateTime
|
|
24
|
+
*/
|
|
25
|
+
toISODate() {
|
|
26
|
+
return [
|
|
27
|
+
String(this.year).padStart(4, '0'),
|
|
28
|
+
String(this.month).padStart(2, '0'),
|
|
29
|
+
String(this.day).padStart(2, '0'),
|
|
30
|
+
].join('-');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns e.g: `17:03:15` (or `17:03` with seconds=false)
|
|
34
|
+
*/
|
|
35
|
+
toISOTime(seconds = true) {
|
|
36
|
+
return [
|
|
37
|
+
String(this.hour).padStart(2, '0'),
|
|
38
|
+
String(this.minute).padStart(2, '0'),
|
|
39
|
+
seconds && String(this.second).padStart(2, '0'),
|
|
40
|
+
]
|
|
41
|
+
.filter(Boolean)
|
|
42
|
+
.join(':');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -108,6 +108,6 @@ export const _AsyncMemo = (opt) => (target, key, descriptor) => {
|
|
|
108
108
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
109
109
|
*/
|
|
110
110
|
export function _getAsyncMemo(method) {
|
|
111
|
-
_assert(typeof
|
|
111
|
+
_assert(typeof method?.getInstanceCache === 'function', 'method is not an AsyncMemo instance');
|
|
112
112
|
return method;
|
|
113
113
|
}
|
|
@@ -18,7 +18,6 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
18
18
|
const key = String(propertyKey);
|
|
19
19
|
const methodSignature = _getTargetMethodSignature(target, key);
|
|
20
20
|
pd.value = async function (...args) {
|
|
21
|
-
var _a, _b;
|
|
22
21
|
// console.log(`@${cfg.decoratorName} called inside function`)
|
|
23
22
|
const started = Date.now();
|
|
24
23
|
try {
|
|
@@ -46,9 +45,12 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
46
45
|
started,
|
|
47
46
|
};
|
|
48
47
|
if (cfg.thenFn) {
|
|
49
|
-
res = cfg.thenFn(
|
|
48
|
+
res = cfg.thenFn({
|
|
49
|
+
...resp,
|
|
50
|
+
res,
|
|
51
|
+
});
|
|
50
52
|
}
|
|
51
|
-
|
|
53
|
+
cfg.finallyFn?.(resp);
|
|
52
54
|
return res;
|
|
53
55
|
}
|
|
54
56
|
catch (err) {
|
|
@@ -63,10 +65,13 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
63
65
|
};
|
|
64
66
|
let handled = false;
|
|
65
67
|
if (cfg.catchFn) {
|
|
66
|
-
cfg.catchFn(
|
|
68
|
+
cfg.catchFn({
|
|
69
|
+
...resp,
|
|
70
|
+
err,
|
|
71
|
+
});
|
|
67
72
|
handled = true;
|
|
68
73
|
}
|
|
69
|
-
|
|
74
|
+
cfg.finallyFn?.(resp);
|
|
70
75
|
if (!handled) {
|
|
71
76
|
throw err; // rethrow
|
|
72
77
|
}
|
|
@@ -105,5 +105,10 @@ export function _debounce(func, wait, opt = {}) {
|
|
|
105
105
|
return debounced;
|
|
106
106
|
}
|
|
107
107
|
export function _throttle(func, wait, opt = {}) {
|
|
108
|
-
return _debounce(func, wait,
|
|
108
|
+
return _debounce(func, wait, {
|
|
109
|
+
leading: true,
|
|
110
|
+
trailing: true,
|
|
111
|
+
...opt,
|
|
112
|
+
maxWait: wait,
|
|
113
|
+
});
|
|
109
114
|
}
|
|
@@ -77,6 +77,6 @@ export const _Memo = (opt = {}) => (target, key, descriptor) => {
|
|
|
77
77
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
78
78
|
*/
|
|
79
79
|
export function _getMemo(method) {
|
|
80
|
-
_assert(typeof
|
|
80
|
+
_assert(typeof method?.getInstanceCache === 'function', 'method is not a Memo instance');
|
|
81
81
|
return method;
|
|
82
82
|
}
|
package/dist-esm/define.js
CHANGED
|
@@ -76,14 +76,26 @@ export function _defineLazyProps(obj, props) {
|
|
|
76
76
|
*
|
|
77
77
|
*/
|
|
78
78
|
export function _defineProperty(obj, prop, pd) {
|
|
79
|
-
return Object.defineProperty(obj, prop,
|
|
79
|
+
return Object.defineProperty(obj, prop, {
|
|
80
|
+
writable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
enumerable: true,
|
|
83
|
+
// value: obj[prop], // existing value is already kept by default
|
|
84
|
+
...pd,
|
|
85
|
+
});
|
|
80
86
|
}
|
|
81
87
|
/**
|
|
82
88
|
* Object.defineProperties with better defaults.
|
|
83
89
|
* See _defineProperty for exact defaults definition.
|
|
84
90
|
*/
|
|
85
91
|
export function _defineProps(obj, props) {
|
|
86
|
-
return Object.defineProperties(obj, _mapValues(props, (k, pd) => (
|
|
92
|
+
return Object.defineProperties(obj, _mapValues(props, (k, pd) => ({
|
|
93
|
+
writable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
enumerable: true,
|
|
96
|
+
// value: obj[k], // existing value is already kept by default
|
|
97
|
+
...pd,
|
|
98
|
+
})));
|
|
87
99
|
}
|
|
88
100
|
/**
|
|
89
101
|
* Like _defineProps, but skips props with nullish values.
|
package/dist-esm/enum.util.js
CHANGED
|
@@ -147,8 +147,7 @@ export function _numberEnumKey(en, v) {
|
|
|
147
147
|
export function _stringEnumKeyOrUndefined(en,
|
|
148
148
|
// v: T[keyof T] | undefined | null, // cannot make it type-safe :(
|
|
149
149
|
v) {
|
|
150
|
-
|
|
151
|
-
return (_a = Object.entries(en).find(([_, v2]) => v2 === v)) === null || _a === void 0 ? void 0 : _a[0];
|
|
150
|
+
return Object.entries(en).find(([_, v2]) => v2 === v)?.[0];
|
|
152
151
|
}
|
|
153
152
|
export function _stringEnumKey(en, v) {
|
|
154
153
|
const r = _stringEnumKeyOrUndefined(en, v);
|
package/dist-esm/error/assert.js
CHANGED
|
@@ -17,7 +17,10 @@ import { _deepEquals, _isErrorObject, _stringify, AssertionError } from '..';
|
|
|
17
17
|
export function _assert(condition, // will be evaluated as Boolean
|
|
18
18
|
message, errorData) {
|
|
19
19
|
if (!condition) {
|
|
20
|
-
throw new AssertionError(message || 'condition failed',
|
|
20
|
+
throw new AssertionError(message || 'condition failed', {
|
|
21
|
+
userFriendly: true,
|
|
22
|
+
...errorData,
|
|
23
|
+
});
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
26
|
/**
|
|
@@ -32,7 +35,10 @@ export function _assertEquals(actual, expected, message, errorData) {
|
|
|
32
35
|
['not equal', `expected: ${_stringify(expected)}`, `got : ${_stringify(actual)}`]
|
|
33
36
|
.filter(Boolean)
|
|
34
37
|
.join('\n');
|
|
35
|
-
throw new AssertionError(msg,
|
|
38
|
+
throw new AssertionError(msg, {
|
|
39
|
+
userFriendly: true,
|
|
40
|
+
...errorData,
|
|
41
|
+
});
|
|
36
42
|
}
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
@@ -47,7 +53,10 @@ export function _assertDeepEquals(actual, expected, message, errorData) {
|
|
|
47
53
|
[`not deeply equal`, `expected: ${_stringify(expected)}`, `got : ${_stringify(actual)}`]
|
|
48
54
|
.filter(Boolean)
|
|
49
55
|
.join('\n');
|
|
50
|
-
throw new AssertionError(msg,
|
|
56
|
+
throw new AssertionError(msg, {
|
|
57
|
+
userFriendly: true,
|
|
58
|
+
...errorData,
|
|
59
|
+
});
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
export function _assertIsError(err, errorClass = Error) {
|
|
@@ -19,7 +19,10 @@ export function _anyToError(o, errorClass = Error, errorData) {
|
|
|
19
19
|
}
|
|
20
20
|
if (errorData) {
|
|
21
21
|
;
|
|
22
|
-
e.data =
|
|
22
|
+
e.data = {
|
|
23
|
+
...e.data,
|
|
24
|
+
...errorData,
|
|
25
|
+
};
|
|
23
26
|
}
|
|
24
27
|
return e;
|
|
25
28
|
}
|
|
@@ -73,7 +76,7 @@ export function _errorLikeToErrorObject(e) {
|
|
|
73
76
|
const obj = {
|
|
74
77
|
name: e.name,
|
|
75
78
|
message: e.message,
|
|
76
|
-
data:
|
|
79
|
+
data: { ...e.data }, // empty by default
|
|
77
80
|
};
|
|
78
81
|
if (e.stack)
|
|
79
82
|
obj.stack = e.stack;
|
|
@@ -168,11 +171,10 @@ export function _errorSnippet(err, opt = {}) {
|
|
|
168
171
|
}
|
|
169
172
|
}
|
|
170
173
|
export function _isBackendErrorResponseObject(o) {
|
|
171
|
-
return _isErrorObject(o
|
|
174
|
+
return _isErrorObject(o?.error);
|
|
172
175
|
}
|
|
173
176
|
export function _isHttpRequestErrorObject(o) {
|
|
174
|
-
|
|
175
|
-
return !!o && o.name === 'HttpRequestError' && typeof ((_a = o.data) === null || _a === void 0 ? void 0 : _a.requestUrl) === 'string';
|
|
177
|
+
return !!o && o.name === 'HttpRequestError' && typeof o.data?.requestUrl === 'string';
|
|
176
178
|
}
|
|
177
179
|
/**
|
|
178
180
|
* Note: any instance of AppError is also automatically an ErrorObject
|
|
@@ -204,7 +206,10 @@ export function _isErrorLike(o) {
|
|
|
204
206
|
export function _errorDataAppend(err, data) {
|
|
205
207
|
if (!data)
|
|
206
208
|
return err;
|
|
207
|
-
err.data =
|
|
209
|
+
err.data = {
|
|
210
|
+
...err.data,
|
|
211
|
+
...data,
|
|
212
|
+
};
|
|
208
213
|
return err;
|
|
209
214
|
}
|
|
210
215
|
/**
|
|
@@ -294,7 +299,7 @@ export class HttpRequestError extends AppError {
|
|
|
294
299
|
enumerable: false,
|
|
295
300
|
});
|
|
296
301
|
}
|
|
297
|
-
super(message, data,
|
|
302
|
+
super(message, data, { ...opt, name: 'HttpRequestError' });
|
|
298
303
|
}
|
|
299
304
|
}
|
|
300
305
|
export class AssertionError extends AppError {
|
|
@@ -312,7 +317,7 @@ export class JsonParseError extends AppError {
|
|
|
312
317
|
}
|
|
313
318
|
export class TimeoutError extends AppError {
|
|
314
319
|
constructor(message, data, opt) {
|
|
315
|
-
super(message, data,
|
|
320
|
+
super(message, data, { ...opt, name: 'TimeoutError' });
|
|
316
321
|
}
|
|
317
322
|
}
|
|
318
323
|
/**
|