@naturalcycles/js-lib 14.103.0 → 14.104.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-esm/datetime/localTime.js +1 -2
- package/dist-esm/decorators/createPromiseDecorator.js +10 -5
- package/dist-esm/decorators/debounce.js +6 -1
- package/dist-esm/error/assert.js +12 -3
- package/dist-esm/error/error.util.js +8 -7
- package/dist-esm/error/tryCatch.js +1 -1
- 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 +3 -4
- package/dist-esm/promise/pMap.js +15 -27
- package/dist-esm/promise/pQueue.js +7 -2
- package/dist-esm/promise/pRetry.js +4 -1
- package/dist-esm/promise/pTimeout.js +4 -1
- package/dist-esm/string/json.util.js +1 -1
- package/dist-esm/string/safeJsonStringify.js +1 -1
- package/dist-esm/string/stringifyAny.js +1 -1
- package/dist-esm/vendor/is.js +7 -8
- package/package.json +1 -1
|
@@ -179,11 +179,10 @@ export class LocalTime {
|
|
|
179
179
|
return v === undefined ? this.get('second') : this.set('second', v);
|
|
180
180
|
}
|
|
181
181
|
setComponents(c, mutate = false) {
|
|
182
|
-
var _a;
|
|
183
182
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
184
183
|
// Year, month and day set all-at-once, to avoid 30/31 (and 28/29) mishap
|
|
185
184
|
if (c.day || c.month !== undefined || c.year !== undefined) {
|
|
186
|
-
d.setFullYear(
|
|
185
|
+
d.setFullYear(c.year ?? d.getFullYear(), c.month ? c.month - 1 : d.getMonth(), c.day || d.getDate());
|
|
187
186
|
}
|
|
188
187
|
if (c.hour !== undefined) {
|
|
189
188
|
d.setHours(c.hour);
|
|
@@ -19,7 +19,6 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
19
19
|
const key = String(propertyKey);
|
|
20
20
|
const methodSignature = _getTargetMethodSignature(target, key);
|
|
21
21
|
pd.value = async function (...args) {
|
|
22
|
-
var _a, _b;
|
|
23
22
|
// console.log(`@${cfg.decoratorName} called inside function`)
|
|
24
23
|
const started = Date.now();
|
|
25
24
|
try {
|
|
@@ -47,9 +46,12 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
47
46
|
started,
|
|
48
47
|
};
|
|
49
48
|
if (cfg.thenFn) {
|
|
50
|
-
res = cfg.thenFn(
|
|
49
|
+
res = cfg.thenFn({
|
|
50
|
+
...resp,
|
|
51
|
+
res,
|
|
52
|
+
});
|
|
51
53
|
}
|
|
52
|
-
|
|
54
|
+
cfg.finallyFn?.(resp);
|
|
53
55
|
return res;
|
|
54
56
|
}
|
|
55
57
|
catch (err) {
|
|
@@ -64,10 +66,13 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
64
66
|
};
|
|
65
67
|
let handled = false;
|
|
66
68
|
if (cfg.catchFn) {
|
|
67
|
-
cfg.catchFn(
|
|
69
|
+
cfg.catchFn({
|
|
70
|
+
...resp,
|
|
71
|
+
err,
|
|
72
|
+
});
|
|
68
73
|
handled = true;
|
|
69
74
|
}
|
|
70
|
-
|
|
75
|
+
cfg.finallyFn?.(resp);
|
|
71
76
|
if (!handled) {
|
|
72
77
|
throw err; // rethrow
|
|
73
78
|
}
|
|
@@ -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
|
}
|
package/dist-esm/error/assert.js
CHANGED
|
@@ -18,7 +18,10 @@ import { AppError } from './app.error';
|
|
|
18
18
|
export function _assert(condition, // will be evaluated as Boolean
|
|
19
19
|
message, errorData) {
|
|
20
20
|
if (!condition) {
|
|
21
|
-
throw new AssertionError(message || 'see stacktrace',
|
|
21
|
+
throw new AssertionError(message || 'see stacktrace', {
|
|
22
|
+
userFriendly: true,
|
|
23
|
+
...errorData,
|
|
24
|
+
});
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
/**
|
|
@@ -36,7 +39,10 @@ export function _assertEquals(actual, expected, message, errorData) {
|
|
|
36
39
|
]
|
|
37
40
|
.filter(Boolean)
|
|
38
41
|
.join('\n');
|
|
39
|
-
throw new AssertionError(msg,
|
|
42
|
+
throw new AssertionError(msg, {
|
|
43
|
+
userFriendly: true,
|
|
44
|
+
...errorData,
|
|
45
|
+
});
|
|
40
46
|
}
|
|
41
47
|
}
|
|
42
48
|
/**
|
|
@@ -54,7 +60,10 @@ export function _assertDeepEquals(actual, expected, message, errorData) {
|
|
|
54
60
|
]
|
|
55
61
|
.filter(Boolean)
|
|
56
62
|
.join('\n');
|
|
57
|
-
throw new AssertionError(msg,
|
|
63
|
+
throw new AssertionError(msg, {
|
|
64
|
+
userFriendly: true,
|
|
65
|
+
...errorData,
|
|
66
|
+
});
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
export function _assertIsError(err, message) {
|
|
@@ -21,9 +21,8 @@ export function _anyToError(o, errorClass = Error, opt) {
|
|
|
21
21
|
* Objects (not Errors) get converted to prettified JSON string (via `_stringifyAny`).
|
|
22
22
|
*/
|
|
23
23
|
export function _anyToErrorObject(o, opt) {
|
|
24
|
-
var _a;
|
|
25
24
|
if (o instanceof Error) {
|
|
26
|
-
return _errorToErrorObject(o,
|
|
25
|
+
return _errorToErrorObject(o, opt?.includeErrorStack ?? true);
|
|
27
26
|
}
|
|
28
27
|
o = _jsonParseIfPossible(o);
|
|
29
28
|
if (_isHttpErrorResponse(o)) {
|
|
@@ -36,7 +35,10 @@ export function _anyToErrorObject(o, opt) {
|
|
|
36
35
|
// so, fair to return `data: {}` in the end
|
|
37
36
|
// Also we're sure it includes no "error name", e.g no `Error: ...`,
|
|
38
37
|
// so, fair to include `name: 'Error'`
|
|
39
|
-
const message = _stringifyAny(o,
|
|
38
|
+
const message = _stringifyAny(o, {
|
|
39
|
+
includeErrorData: true,
|
|
40
|
+
...opt,
|
|
41
|
+
});
|
|
40
42
|
return {
|
|
41
43
|
name: 'Error',
|
|
42
44
|
message,
|
|
@@ -47,7 +49,7 @@ export function _errorToErrorObject(e, includeErrorStack = true) {
|
|
|
47
49
|
const obj = {
|
|
48
50
|
name: e.name,
|
|
49
51
|
message: e.message,
|
|
50
|
-
data:
|
|
52
|
+
data: { ...e.data }, // empty by default
|
|
51
53
|
};
|
|
52
54
|
if (includeErrorStack) {
|
|
53
55
|
obj.stack = e.stack;
|
|
@@ -81,14 +83,13 @@ export function _errorObjectToError(o, errorClass = Error) {
|
|
|
81
83
|
return err;
|
|
82
84
|
}
|
|
83
85
|
export function _isHttpErrorResponse(o) {
|
|
84
|
-
return _isHttpErrorObject(o
|
|
86
|
+
return _isHttpErrorObject(o?.error);
|
|
85
87
|
}
|
|
86
88
|
export function _isHttpErrorObject(o) {
|
|
87
|
-
var _a;
|
|
88
89
|
return (!!o &&
|
|
89
90
|
typeof o.name === 'string' &&
|
|
90
91
|
typeof o.message === 'string' &&
|
|
91
|
-
typeof
|
|
92
|
+
typeof o.data?.httpStatusCode === 'number');
|
|
92
93
|
}
|
|
93
94
|
/**
|
|
94
95
|
* Note: any instance of AppError is also automatically an ErrorObject
|
|
@@ -6,7 +6,6 @@ import { _filterNullishValues } from '../object/object.util';
|
|
|
6
6
|
* API similar to Object.assign(s1, s2)
|
|
7
7
|
*/
|
|
8
8
|
export function mergeJsonSchemaObjects(s1, s2) {
|
|
9
|
-
var _a, _b;
|
|
10
9
|
// Merge `properties`
|
|
11
10
|
Object.entries(s2.properties).forEach(([k, v]) => {
|
|
12
11
|
;
|
|
@@ -18,8 +17,8 @@ export function mergeJsonSchemaObjects(s1, s2) {
|
|
|
18
17
|
s1.patternProperties[k] = v;
|
|
19
18
|
});
|
|
20
19
|
s1.propertyNames = s2.propertyNames || s1.propertyNames;
|
|
21
|
-
s1.minProperties =
|
|
22
|
-
s1.maxProperties =
|
|
20
|
+
s1.minProperties = s2.minProperties ?? s1.minProperties;
|
|
21
|
+
s1.maxProperties = s2.maxProperties ?? s1.maxProperties;
|
|
23
22
|
// Merge `required`
|
|
24
23
|
s1.required.push(...s2.required);
|
|
25
24
|
s1.required = _uniq(s1.required).sort();
|
|
@@ -250,12 +250,11 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
|
|
|
250
250
|
return this;
|
|
251
251
|
}
|
|
252
252
|
transformModify(t, add) {
|
|
253
|
-
var _a;
|
|
254
253
|
if (add) {
|
|
255
254
|
this.schema.transform = _uniq([...(this.schema.transform || []), t]);
|
|
256
255
|
}
|
|
257
256
|
else {
|
|
258
|
-
this.schema.transform =
|
|
257
|
+
this.schema.transform = this.schema.transform?.filter(s => s !== t);
|
|
259
258
|
}
|
|
260
259
|
return this;
|
|
261
260
|
}
|
|
@@ -14,7 +14,7 @@ export function _average(values) {
|
|
|
14
14
|
* Same as _average, but safely returns null if input array is empty or nullish.
|
|
15
15
|
*/
|
|
16
16
|
export function _averageOrNull(values) {
|
|
17
|
-
return
|
|
17
|
+
return values?.length ? values.reduce((a, b) => a + b) / values.length : null;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* valuesArray and weightsArray length is expected to be the same.
|
|
@@ -29,7 +29,7 @@ export function _omit(obj, props, mutate = false) {
|
|
|
29
29
|
return props.reduce((r, prop) => {
|
|
30
30
|
delete r[prop];
|
|
31
31
|
return r;
|
|
32
|
-
}, mutate ? obj :
|
|
32
|
+
}, mutate ? obj : { ...obj });
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Returns object with filtered keys from `props` array.
|
|
@@ -76,7 +76,7 @@ export function _filterObject(obj, predicate, mutate = false) {
|
|
|
76
76
|
if (!predicate(k, r[k], obj))
|
|
77
77
|
delete r[k];
|
|
78
78
|
return r;
|
|
79
|
-
}, mutate ? obj :
|
|
79
|
+
}, mutate ? obj : { ...obj });
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
82
|
* var users = {
|
|
@@ -137,8 +137,7 @@ export function _mapObject(obj, mapper) {
|
|
|
137
137
|
}, {});
|
|
138
138
|
}
|
|
139
139
|
export function _findKeyByValue(obj, v) {
|
|
140
|
-
|
|
141
|
-
return (_a = Object.entries(obj).find(([_, value]) => value === v)) === null || _a === void 0 ? void 0 : _a[0];
|
|
140
|
+
return Object.entries(obj).find(([_, value]) => value === v)?.[0];
|
|
142
141
|
}
|
|
143
142
|
export function _objectNullValuesToUndefined(obj, mutate = false) {
|
|
144
143
|
return _mapValues(obj, (_k, v) => (v === null ? undefined : v), mutate);
|
package/dist-esm/promise/pMap.js
CHANGED
|
@@ -6,7 +6,6 @@ Improvements:
|
|
|
6
6
|
- Included Typescript typings (no need for @types/p-map)
|
|
7
7
|
- Compatible with pProps (that had typings issues)
|
|
8
8
|
*/
|
|
9
|
-
import { __asyncValues } from "tslib";
|
|
10
9
|
import { END, ErrorMode, SKIP } from '..';
|
|
11
10
|
import { AggregatedError } from './AggregatedError';
|
|
12
11
|
/**
|
|
@@ -36,7 +35,6 @@ import { AggregatedError } from './AggregatedError';
|
|
|
36
35
|
* })();
|
|
37
36
|
*/
|
|
38
37
|
export async function pMap(iterable, mapper, opt = {}) {
|
|
39
|
-
var e_1, _a;
|
|
40
38
|
const ret = [];
|
|
41
39
|
// const iterator = iterable[Symbol.iterator]()
|
|
42
40
|
const items = [...iterable];
|
|
@@ -50,33 +48,23 @@ export async function pMap(iterable, mapper, opt = {}) {
|
|
|
50
48
|
let currentIndex = 0;
|
|
51
49
|
// Special cases that are able to preserve async stack traces
|
|
52
50
|
if (concurrency === 1) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
for (var items_1 = __asyncValues(items), items_1_1; items_1_1 = await items_1.next(), !items_1_1.done;) {
|
|
56
|
-
const item = items_1_1.value;
|
|
57
|
-
try {
|
|
58
|
-
const r = await mapper(item, currentIndex++);
|
|
59
|
-
if (r === END)
|
|
60
|
-
break;
|
|
61
|
-
if (r !== SKIP)
|
|
62
|
-
ret.push(r);
|
|
63
|
-
}
|
|
64
|
-
catch (err) {
|
|
65
|
-
if (errorMode === ErrorMode.THROW_IMMEDIATELY)
|
|
66
|
-
throw err;
|
|
67
|
-
if (errorMode === ErrorMode.THROW_AGGREGATED) {
|
|
68
|
-
errors.push(err);
|
|
69
|
-
}
|
|
70
|
-
// otherwise, suppress completely
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
75
|
-
finally {
|
|
51
|
+
// Special case for concurrency == 1
|
|
52
|
+
for await (const item of items) {
|
|
76
53
|
try {
|
|
77
|
-
|
|
54
|
+
const r = await mapper(item, currentIndex++);
|
|
55
|
+
if (r === END)
|
|
56
|
+
break;
|
|
57
|
+
if (r !== SKIP)
|
|
58
|
+
ret.push(r);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
if (errorMode === ErrorMode.THROW_IMMEDIATELY)
|
|
62
|
+
throw err;
|
|
63
|
+
if (errorMode === ErrorMode.THROW_AGGREGATED) {
|
|
64
|
+
errors.push(err);
|
|
65
|
+
}
|
|
66
|
+
// otherwise, suppress completely
|
|
78
67
|
}
|
|
79
|
-
finally { if (e_1) throw e_1.error; }
|
|
80
68
|
}
|
|
81
69
|
if (errors.length) {
|
|
82
70
|
throw new AggregatedError(errors, ret);
|
|
@@ -13,9 +13,14 @@ export class PQueue {
|
|
|
13
13
|
this.inFlight = 0;
|
|
14
14
|
this.queue = [];
|
|
15
15
|
this.onIdleListeners = [];
|
|
16
|
-
this.cfg =
|
|
16
|
+
this.cfg = {
|
|
17
17
|
// concurrency: Number.MAX_SAFE_INTEGER,
|
|
18
|
-
errorMode: ErrorMode.THROW_IMMEDIATELY,
|
|
18
|
+
errorMode: ErrorMode.THROW_IMMEDIATELY,
|
|
19
|
+
logger: console,
|
|
20
|
+
debug: false,
|
|
21
|
+
resolveOn: 'finish',
|
|
22
|
+
...cfg,
|
|
23
|
+
};
|
|
19
24
|
if (!cfg.debug) {
|
|
20
25
|
this.debug = () => { };
|
|
21
26
|
}
|
|
@@ -32,7 +32,10 @@ export async function pTimeout(promise, opt) {
|
|
|
32
32
|
catch (err) {
|
|
33
33
|
if (fakeError)
|
|
34
34
|
err.stack = fakeError.stack; // keep original stack
|
|
35
|
-
err.data =
|
|
35
|
+
err.data = {
|
|
36
|
+
...err.data,
|
|
37
|
+
...opt.errorData,
|
|
38
|
+
};
|
|
36
39
|
reject(err);
|
|
37
40
|
}
|
|
38
41
|
return;
|
|
@@ -8,7 +8,7 @@ export function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
|
|
|
8
8
|
// Try native first (as it's ~3 times faster)
|
|
9
9
|
return JSON.stringify(obj, replacer, spaces);
|
|
10
10
|
}
|
|
11
|
-
catch
|
|
11
|
+
catch {
|
|
12
12
|
// Native failed - resort to the "safe" serializer
|
|
13
13
|
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
|
|
14
14
|
}
|
package/dist-esm/vendor/is.js
CHANGED
|
@@ -138,15 +138,15 @@ is.array = (value, assertion) => {
|
|
|
138
138
|
}
|
|
139
139
|
return value.every(assertion);
|
|
140
140
|
};
|
|
141
|
-
is.buffer = (value) =>
|
|
141
|
+
is.buffer = (value) => value?.constructor?.isBuffer?.(value) ?? false;
|
|
142
142
|
is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
|
|
143
143
|
is.object = (value) => !is.null_(value) && (typeof value === 'object' || is.function_(value));
|
|
144
|
-
is.iterable = (value) => is.function_(value
|
|
145
|
-
is.asyncIterable = (value) => is.function_(value
|
|
144
|
+
is.iterable = (value) => is.function_(value?.[Symbol.iterator]);
|
|
145
|
+
is.asyncIterable = (value) => is.function_(value?.[Symbol.asyncIterator]);
|
|
146
146
|
is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
|
|
147
147
|
is.asyncGenerator = (value) => is.asyncIterable(value) && is.function_(value.next) && is.function_(value.throw);
|
|
148
148
|
is.nativePromise = (value) => isObjectOfType('Promise')(value);
|
|
149
|
-
const hasPromiseAPI = (value) => is.function_(value
|
|
149
|
+
const hasPromiseAPI = (value) => is.function_(value?.then) && is.function_(value?.catch);
|
|
150
150
|
is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
|
|
151
151
|
is.generatorFunction = isObjectOfType('GeneratorFunction');
|
|
152
152
|
is.asyncGeneratorFunction = (value) => getObjectType(value) === 'AsyncGeneratorFunction';
|
|
@@ -184,7 +184,7 @@ is.urlString = (value) => {
|
|
|
184
184
|
new URL(value); // eslint-disable-line no-new
|
|
185
185
|
return true;
|
|
186
186
|
}
|
|
187
|
-
catch
|
|
187
|
+
catch {
|
|
188
188
|
return false;
|
|
189
189
|
}
|
|
190
190
|
};
|
|
@@ -235,14 +235,13 @@ is.domElement = (value) => {
|
|
|
235
235
|
DOM_PROPERTIES_TO_CHECK.every(property => property in value));
|
|
236
236
|
};
|
|
237
237
|
is.observable = (value) => {
|
|
238
|
-
var _a, _b, _c, _d;
|
|
239
238
|
if (!value) {
|
|
240
239
|
return false;
|
|
241
240
|
}
|
|
242
|
-
if (value ===
|
|
241
|
+
if (value === value[Symbol.observable]?.()) {
|
|
243
242
|
return true;
|
|
244
243
|
}
|
|
245
|
-
if (value ===
|
|
244
|
+
if (value === value['@@observable']?.()) {
|
|
246
245
|
return true;
|
|
247
246
|
}
|
|
248
247
|
return false;
|