@naturalcycles/js-lib 14.104.0 → 14.105.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/enum.util.d.ts +55 -0
- package/dist/enum.util.js +96 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -0
- package/dist/object/object.util.js +1 -0
- package/dist/promise/pProps.js +2 -0
- package/dist/string/url.util.js +1 -1
- package/dist/types.d.ts +3 -0
- package/dist-esm/datetime/localTime.js +2 -1
- package/dist-esm/decorators/createPromiseDecorator.js +5 -10
- package/dist-esm/decorators/debounce.js +1 -6
- package/dist-esm/enum.util.js +83 -0
- package/dist-esm/error/assert.js +3 -12
- package/dist-esm/error/error.util.js +7 -8
- package/dist-esm/error/tryCatch.js +1 -1
- package/dist-esm/index.js +1 -0
- package/dist-esm/json-schema/jsonSchema.util.js +3 -2
- package/dist-esm/json-schema/jsonSchemaBuilder.js +2 -1
- package/dist-esm/math/math.util.js +1 -1
- package/dist-esm/object/object.util.js +5 -3
- package/dist-esm/promise/pMap.js +27 -15
- package/dist-esm/promise/pProps.js +2 -0
- package/dist-esm/promise/pQueue.js +2 -7
- package/dist-esm/promise/pRetry.js +1 -4
- package/dist-esm/promise/pTimeout.js +1 -4
- 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/string/url.util.js +1 -1
- package/dist-esm/vendor/is.js +8 -7
- package/package.json +1 -5
- package/src/enum.util.ts +98 -0
- package/src/index.ts +7 -0
- package/src/object/object.util.ts +5 -4
- package/src/object/sortObjectDeep.ts +1 -1
- package/src/promise/pProps.ts +5 -1
- package/src/string/url.util.ts +2 -2
- package/src/types.ts +4 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NumberEnum, StringEnum } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Returns all number keys of a number-enum.
|
|
4
|
+
*/
|
|
5
|
+
export declare function _numberEnumKeys(en: NumberEnum): string[];
|
|
6
|
+
/**
|
|
7
|
+
* Returns all number values of a number-enum.
|
|
8
|
+
*/
|
|
9
|
+
export declare function _numberEnumValues(en: NumberEnum): number[];
|
|
10
|
+
/**
|
|
11
|
+
* Returns all string keys of a string-enum.
|
|
12
|
+
*/
|
|
13
|
+
export declare function _stringEnumKeys(en: StringEnum): string[];
|
|
14
|
+
/**
|
|
15
|
+
* Returns all string values of a string-enum.
|
|
16
|
+
*/
|
|
17
|
+
export declare function _stringEnumValues(en: StringEnum): string[];
|
|
18
|
+
/**
|
|
19
|
+
* Returns all number-enum "entries", where entry is a tuple of [key, value],
|
|
20
|
+
* where key is a String key, value is a Number value, typed as Enum itself.
|
|
21
|
+
*
|
|
22
|
+
* Doesn't work on String-enums!
|
|
23
|
+
*/
|
|
24
|
+
export declare function _numberEnumEntries<T extends NumberEnum>(en: T): [k: string, v: T[keyof T]][];
|
|
25
|
+
/**
|
|
26
|
+
* Returns all string-enum "entries", where entry is a tuple of [key, value],
|
|
27
|
+
* where key is a String key, value is a String value, typed as Enum itself.
|
|
28
|
+
*
|
|
29
|
+
* Doesn't work on Number-enums!
|
|
30
|
+
*/
|
|
31
|
+
export declare function _stringEnumEntries<T extends StringEnum>(en: T): [k: string, v: T[keyof T]][];
|
|
32
|
+
/**
|
|
33
|
+
* Allows to return a Number enum value (typed as Enum itself) based on it's String key.
|
|
34
|
+
* e.g:
|
|
35
|
+
* const v = SomeEnum['stringValue']
|
|
36
|
+
* // v is of type SomeEnum, which is of type Number
|
|
37
|
+
*
|
|
38
|
+
* Throws if value is not found!
|
|
39
|
+
*/
|
|
40
|
+
export declare function _numberEnumInverse<T extends NumberEnum>(en: T, v: string): T[keyof T];
|
|
41
|
+
/**
|
|
42
|
+
* _enumInverse, but allows to get/return undefined output.
|
|
43
|
+
*/
|
|
44
|
+
export declare function _numberEnumInverseNullable<T extends NumberEnum>(en: T, v: string | undefined): T[keyof T] | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Takes number or string enum input, returns normalized Enum output.
|
|
47
|
+
* Only works for number enums.
|
|
48
|
+
*
|
|
49
|
+
* Throws if value is not found!
|
|
50
|
+
*/
|
|
51
|
+
export declare function _numberEnumNormalize<T extends NumberEnum>(en: T, v: string | number): T[keyof T];
|
|
52
|
+
/**
|
|
53
|
+
* Same as _enumNormalize, but allows to return undefined values.
|
|
54
|
+
*/
|
|
55
|
+
export declare function _numberEnumNormalizeNullable<T extends NumberEnum>(en: T, v: string | number | undefined): T[keyof T] | undefined;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._numberEnumNormalizeNullable = exports._numberEnumNormalize = exports._numberEnumInverseNullable = exports._numberEnumInverse = exports._stringEnumEntries = exports._numberEnumEntries = exports._stringEnumValues = exports._stringEnumKeys = exports._numberEnumValues = exports._numberEnumKeys = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns all number keys of a number-enum.
|
|
6
|
+
*/
|
|
7
|
+
function _numberEnumKeys(en) {
|
|
8
|
+
return Object.values(en).filter(k => typeof k === 'string');
|
|
9
|
+
}
|
|
10
|
+
exports._numberEnumKeys = _numberEnumKeys;
|
|
11
|
+
/**
|
|
12
|
+
* Returns all number values of a number-enum.
|
|
13
|
+
*/
|
|
14
|
+
function _numberEnumValues(en) {
|
|
15
|
+
return Object.values(en).filter(k => typeof k === 'number');
|
|
16
|
+
}
|
|
17
|
+
exports._numberEnumValues = _numberEnumValues;
|
|
18
|
+
/**
|
|
19
|
+
* Returns all string keys of a string-enum.
|
|
20
|
+
*/
|
|
21
|
+
function _stringEnumKeys(en) {
|
|
22
|
+
return Object.keys(en);
|
|
23
|
+
}
|
|
24
|
+
exports._stringEnumKeys = _stringEnumKeys;
|
|
25
|
+
/**
|
|
26
|
+
* Returns all string values of a string-enum.
|
|
27
|
+
*/
|
|
28
|
+
function _stringEnumValues(en) {
|
|
29
|
+
// filtering here is unnecessary, but works as a safety in case Number-enum is passed
|
|
30
|
+
return Object.values(en).filter(k => typeof k === 'string');
|
|
31
|
+
}
|
|
32
|
+
exports._stringEnumValues = _stringEnumValues;
|
|
33
|
+
/**
|
|
34
|
+
* Returns all number-enum "entries", where entry is a tuple of [key, value],
|
|
35
|
+
* where key is a String key, value is a Number value, typed as Enum itself.
|
|
36
|
+
*
|
|
37
|
+
* Doesn't work on String-enums!
|
|
38
|
+
*/
|
|
39
|
+
function _numberEnumEntries(en) {
|
|
40
|
+
return Object.values(en)
|
|
41
|
+
.filter(k => typeof k === 'string')
|
|
42
|
+
.map(k => [k, en[k]]);
|
|
43
|
+
}
|
|
44
|
+
exports._numberEnumEntries = _numberEnumEntries;
|
|
45
|
+
/**
|
|
46
|
+
* Returns all string-enum "entries", where entry is a tuple of [key, value],
|
|
47
|
+
* where key is a String key, value is a String value, typed as Enum itself.
|
|
48
|
+
*
|
|
49
|
+
* Doesn't work on Number-enums!
|
|
50
|
+
*/
|
|
51
|
+
function _stringEnumEntries(en) {
|
|
52
|
+
return Object.keys(en).map(k => [k, en[k]]);
|
|
53
|
+
}
|
|
54
|
+
exports._stringEnumEntries = _stringEnumEntries;
|
|
55
|
+
/**
|
|
56
|
+
* Allows to return a Number enum value (typed as Enum itself) based on it's String key.
|
|
57
|
+
* e.g:
|
|
58
|
+
* const v = SomeEnum['stringValue']
|
|
59
|
+
* // v is of type SomeEnum, which is of type Number
|
|
60
|
+
*
|
|
61
|
+
* Throws if value is not found!
|
|
62
|
+
*/
|
|
63
|
+
function _numberEnumInverse(en, v) {
|
|
64
|
+
const r = en[v];
|
|
65
|
+
if (!r)
|
|
66
|
+
throw new Error(`enumInverse value not found for: ${v}`);
|
|
67
|
+
return r;
|
|
68
|
+
}
|
|
69
|
+
exports._numberEnumInverse = _numberEnumInverse;
|
|
70
|
+
/**
|
|
71
|
+
* _enumInverse, but allows to get/return undefined output.
|
|
72
|
+
*/
|
|
73
|
+
function _numberEnumInverseNullable(en, v) {
|
|
74
|
+
return en[v];
|
|
75
|
+
}
|
|
76
|
+
exports._numberEnumInverseNullable = _numberEnumInverseNullable;
|
|
77
|
+
/**
|
|
78
|
+
* Takes number or string enum input, returns normalized Enum output.
|
|
79
|
+
* Only works for number enums.
|
|
80
|
+
*
|
|
81
|
+
* Throws if value is not found!
|
|
82
|
+
*/
|
|
83
|
+
function _numberEnumNormalize(en, v) {
|
|
84
|
+
const r = _numberEnumNormalizeNullable(en, v);
|
|
85
|
+
if (!r || !en[r])
|
|
86
|
+
throw new Error(`enumNormalize value not found for: ${v}`);
|
|
87
|
+
return r;
|
|
88
|
+
}
|
|
89
|
+
exports._numberEnumNormalize = _numberEnumNormalize;
|
|
90
|
+
/**
|
|
91
|
+
* Same as _enumNormalize, but allows to return undefined values.
|
|
92
|
+
*/
|
|
93
|
+
function _numberEnumNormalizeNullable(en, v) {
|
|
94
|
+
return typeof v === 'string' ? en[v] : v;
|
|
95
|
+
}
|
|
96
|
+
exports._numberEnumNormalizeNullable = _numberEnumNormalizeNullable;
|
package/dist/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from './decorators/retry.decorator';
|
|
|
16
16
|
export * from './decorators/timeout.decorator';
|
|
17
17
|
export * from './error/app.error';
|
|
18
18
|
export * from './error/assert';
|
|
19
|
+
export * from './enum.util';
|
|
19
20
|
import { Admin401ErrorData, Admin403ErrorData, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse } from './error/error.model';
|
|
20
21
|
export * from './error/error.util';
|
|
21
22
|
import { ErrorMode } from './error/errorMode';
|
|
@@ -52,7 +53,7 @@ import { JsonStringifyFunction, StringifyAnyOptions, _stringifyAny } from './str
|
|
|
52
53
|
export * from './time/time.util';
|
|
53
54
|
export * from './is.util';
|
|
54
55
|
import { Class, ConditionalExcept, ConditionalPick, Merge, Promisable, ReadonlyDeep, Simplify } from './typeFest';
|
|
55
|
-
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, UnsavedId, BatchResult, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestampNumber, UnixTimestampMillisNumber, UnixTimestamp, Integer, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, NullishValue, FalsyValue, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
|
|
56
|
+
import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, UnsavedId, BatchResult, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyEnum, NumberEnum, StringEnum, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestampNumber, UnixTimestampMillisNumber, UnixTimestamp, Integer, ValueOf, ValuesOf, AbortableMapper, AbortableAsyncPredicate, AbortableAsyncMapper, AbortablePredicate, NullishValue, FalsyValue, END, SKIP, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
|
|
56
57
|
export * from './unit/size.util';
|
|
57
58
|
import { is } from './vendor/is';
|
|
58
59
|
import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, CommonLogWithLevelFunction, commonLoggerCreate } from './log/commonLogger';
|
|
@@ -69,5 +70,5 @@ import { LocalDateConfig, LocalDateFormatter, LocalDateUnit, LocalDateUnitStrict
|
|
|
69
70
|
import { LocalTimeConfig, LocalTimeFormatter, LocalTimeUnit, LocalTimeComponents, ISODayOfWeek } from './datetime/localTime';
|
|
70
71
|
import { DateIntervalConfig, DateIntervalString } from './datetime/dateInterval';
|
|
71
72
|
import { TimeIntervalConfig, TimeIntervalString } from './datetime/timeInterval';
|
|
72
|
-
export type { DateIntervalConfig, DateIntervalString, TimeIntervalConfig, TimeIntervalString, LocalDateConfig, LocalDateFormatter, LocalDateUnit, LocalDateUnitStrict, Inclusiveness, LocalTimeConfig, LocalTimeFormatter, LocalTimeUnit, ISODayOfWeek, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, Reviver, FalsyValue, NullishValue, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestampNumber, UnixTimestampMillisNumber, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, UnsavedId, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
|
|
73
|
+
export type { DateIntervalConfig, DateIntervalString, TimeIntervalConfig, TimeIntervalString, LocalDateConfig, LocalDateFormatter, LocalDateUnit, LocalDateUnitStrict, Inclusiveness, LocalTimeConfig, LocalTimeFormatter, LocalTimeUnit, ISODayOfWeek, LocalTimeComponents, AbortableMapper, AbortablePredicate, AbortableAsyncPredicate, AbortableAsyncMapper, PQueueCfg, MemoCache, AsyncMemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyEnum, NumberEnum, StringEnum, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateString, IsoDateTimeString, Reviver, FalsyValue, NullishValue, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestampNumber, UnixTimestampMillisNumber, UnixTimestamp, Integer, BaseDBEntity, SavedDBEntity, Saved, Unsaved, UnsavedId, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
|
|
73
74
|
export { is, _createPromiseDecorator, _stringMapValues, _stringMapEntries, _objectKeys, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, ErrorMode, pDefer, AggregatedError, pRetry, pRetryFn, pTimeout, pTimeoutFn, _tryCatch, _TryCatch, _stringifyAny, jsonSchema, JsonSchemaAnyBuilder, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, PQueue, END, SKIP, };
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ tslib_1.__exportStar(require("./decorators/retry.decorator"), exports);
|
|
|
20
20
|
tslib_1.__exportStar(require("./decorators/timeout.decorator"), exports);
|
|
21
21
|
tslib_1.__exportStar(require("./error/app.error"), exports);
|
|
22
22
|
tslib_1.__exportStar(require("./error/assert"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./enum.util"), exports);
|
|
23
24
|
tslib_1.__exportStar(require("./error/error.util"), exports);
|
|
24
25
|
const errorMode_1 = require("./error/errorMode");
|
|
25
26
|
Object.defineProperty(exports, "ErrorMode", { enumerable: true, get: function () { return errorMode_1.ErrorMode; } });
|
|
@@ -330,6 +330,7 @@ function _set(obj, path, value) {
|
|
|
330
330
|
a[c]
|
|
331
331
|
: // No: create the key. Is the next key a potential array-index?
|
|
332
332
|
(a[c] =
|
|
333
|
+
// @ts-ignore
|
|
333
334
|
// eslint-disable-next-line
|
|
334
335
|
Math.abs(path[i + 1]) >> 0 === +path[i + 1]
|
|
335
336
|
? [] // Yes: assign a new array object
|
package/dist/promise/pProps.js
CHANGED
|
@@ -17,6 +17,8 @@ exports.pProps = void 0;
|
|
|
17
17
|
*/
|
|
18
18
|
async function pProps(input) {
|
|
19
19
|
const keys = Object.keys(input);
|
|
20
|
+
// `as any` here is added to make it compile when `noUncheckedIndexedAccess` is false
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
20
22
|
return Object.fromEntries((await Promise.all(Object.values(input))).map((v, i) => [keys[i], v]));
|
|
21
23
|
}
|
|
22
24
|
exports.pProps = pProps;
|
package/dist/string/url.util.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -20,6 +20,9 @@ export interface PromiseMap {
|
|
|
20
20
|
* Because `object` type is not safe/recommended to be used (e.g discouraged by eslint-typescript due to: https://github.com/microsoft/TypeScript/issues/21732)
|
|
21
21
|
*/
|
|
22
22
|
export declare type AnyObject = Record<string, any>;
|
|
23
|
+
export declare type AnyEnum = NumberEnum;
|
|
24
|
+
export declare type NumberEnum = Record<string, number | string>;
|
|
25
|
+
export declare type StringEnum = Record<string, string>;
|
|
23
26
|
export interface CreatedUpdated {
|
|
24
27
|
created: number;
|
|
25
28
|
updated: number;
|
|
@@ -179,10 +179,11 @@ 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;
|
|
182
183
|
const d = mutate ? this.$date : new Date(this.$date);
|
|
183
184
|
// Year, month and day set all-at-once, to avoid 30/31 (and 28/29) mishap
|
|
184
185
|
if (c.day || c.month !== undefined || c.year !== undefined) {
|
|
185
|
-
d.setFullYear(c.year
|
|
186
|
+
d.setFullYear((_a = c.year) !== null && _a !== void 0 ? _a : d.getFullYear(), c.month ? c.month - 1 : d.getMonth(), c.day || d.getDate());
|
|
186
187
|
}
|
|
187
188
|
if (c.hour !== undefined) {
|
|
188
189
|
d.setHours(c.hour);
|
|
@@ -19,6 +19,7 @@ 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;
|
|
22
23
|
// console.log(`@${cfg.decoratorName} called inside function`)
|
|
23
24
|
const started = Date.now();
|
|
24
25
|
try {
|
|
@@ -46,12 +47,9 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
46
47
|
started,
|
|
47
48
|
};
|
|
48
49
|
if (cfg.thenFn) {
|
|
49
|
-
res = cfg.thenFn({
|
|
50
|
-
...resp,
|
|
51
|
-
res,
|
|
52
|
-
});
|
|
50
|
+
res = cfg.thenFn(Object.assign(Object.assign({}, resp), { res }));
|
|
53
51
|
}
|
|
54
|
-
cfg.finallyFn
|
|
52
|
+
(_a = cfg.finallyFn) === null || _a === void 0 ? void 0 : _a.call(cfg, resp);
|
|
55
53
|
return res;
|
|
56
54
|
}
|
|
57
55
|
catch (err) {
|
|
@@ -66,13 +64,10 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
66
64
|
};
|
|
67
65
|
let handled = false;
|
|
68
66
|
if (cfg.catchFn) {
|
|
69
|
-
cfg.catchFn({
|
|
70
|
-
...resp,
|
|
71
|
-
err,
|
|
72
|
-
});
|
|
67
|
+
cfg.catchFn(Object.assign(Object.assign({}, resp), { err }));
|
|
73
68
|
handled = true;
|
|
74
69
|
}
|
|
75
|
-
cfg.finallyFn
|
|
70
|
+
(_b = cfg.finallyFn) === null || _b === void 0 ? void 0 : _b.call(cfg, resp);
|
|
76
71
|
if (!handled) {
|
|
77
72
|
throw err; // rethrow
|
|
78
73
|
}
|
|
@@ -105,10 +105,5 @@ 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, {
|
|
109
|
-
leading: true,
|
|
110
|
-
trailing: true,
|
|
111
|
-
...opt,
|
|
112
|
-
maxWait: wait,
|
|
113
|
-
});
|
|
108
|
+
return _debounce(func, wait, Object.assign(Object.assign({ leading: true, trailing: true }, opt), { maxWait: wait }));
|
|
114
109
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns all number keys of a number-enum.
|
|
3
|
+
*/
|
|
4
|
+
export function _numberEnumKeys(en) {
|
|
5
|
+
return Object.values(en).filter(k => typeof k === 'string');
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Returns all number values of a number-enum.
|
|
9
|
+
*/
|
|
10
|
+
export function _numberEnumValues(en) {
|
|
11
|
+
return Object.values(en).filter(k => typeof k === 'number');
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns all string keys of a string-enum.
|
|
15
|
+
*/
|
|
16
|
+
export function _stringEnumKeys(en) {
|
|
17
|
+
return Object.keys(en);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns all string values of a string-enum.
|
|
21
|
+
*/
|
|
22
|
+
export function _stringEnumValues(en) {
|
|
23
|
+
// filtering here is unnecessary, but works as a safety in case Number-enum is passed
|
|
24
|
+
return Object.values(en).filter(k => typeof k === 'string');
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns all number-enum "entries", where entry is a tuple of [key, value],
|
|
28
|
+
* where key is a String key, value is a Number value, typed as Enum itself.
|
|
29
|
+
*
|
|
30
|
+
* Doesn't work on String-enums!
|
|
31
|
+
*/
|
|
32
|
+
export function _numberEnumEntries(en) {
|
|
33
|
+
return Object.values(en)
|
|
34
|
+
.filter(k => typeof k === 'string')
|
|
35
|
+
.map(k => [k, en[k]]);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns all string-enum "entries", where entry is a tuple of [key, value],
|
|
39
|
+
* where key is a String key, value is a String value, typed as Enum itself.
|
|
40
|
+
*
|
|
41
|
+
* Doesn't work on Number-enums!
|
|
42
|
+
*/
|
|
43
|
+
export function _stringEnumEntries(en) {
|
|
44
|
+
return Object.keys(en).map(k => [k, en[k]]);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Allows to return a Number enum value (typed as Enum itself) based on it's String key.
|
|
48
|
+
* e.g:
|
|
49
|
+
* const v = SomeEnum['stringValue']
|
|
50
|
+
* // v is of type SomeEnum, which is of type Number
|
|
51
|
+
*
|
|
52
|
+
* Throws if value is not found!
|
|
53
|
+
*/
|
|
54
|
+
export function _numberEnumInverse(en, v) {
|
|
55
|
+
const r = en[v];
|
|
56
|
+
if (!r)
|
|
57
|
+
throw new Error(`enumInverse value not found for: ${v}`);
|
|
58
|
+
return r;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* _enumInverse, but allows to get/return undefined output.
|
|
62
|
+
*/
|
|
63
|
+
export function _numberEnumInverseNullable(en, v) {
|
|
64
|
+
return en[v];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Takes number or string enum input, returns normalized Enum output.
|
|
68
|
+
* Only works for number enums.
|
|
69
|
+
*
|
|
70
|
+
* Throws if value is not found!
|
|
71
|
+
*/
|
|
72
|
+
export function _numberEnumNormalize(en, v) {
|
|
73
|
+
const r = _numberEnumNormalizeNullable(en, v);
|
|
74
|
+
if (!r || !en[r])
|
|
75
|
+
throw new Error(`enumNormalize value not found for: ${v}`);
|
|
76
|
+
return r;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Same as _enumNormalize, but allows to return undefined values.
|
|
80
|
+
*/
|
|
81
|
+
export function _numberEnumNormalizeNullable(en, v) {
|
|
82
|
+
return typeof v === 'string' ? en[v] : v;
|
|
83
|
+
}
|
package/dist-esm/error/assert.js
CHANGED
|
@@ -18,10 +18,7 @@ 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', {
|
|
22
|
-
userFriendly: true,
|
|
23
|
-
...errorData,
|
|
24
|
-
});
|
|
21
|
+
throw new AssertionError(message || 'see stacktrace', Object.assign({ userFriendly: true }, errorData));
|
|
25
22
|
}
|
|
26
23
|
}
|
|
27
24
|
/**
|
|
@@ -39,10 +36,7 @@ export function _assertEquals(actual, expected, message, errorData) {
|
|
|
39
36
|
]
|
|
40
37
|
.filter(Boolean)
|
|
41
38
|
.join('\n');
|
|
42
|
-
throw new AssertionError(msg, {
|
|
43
|
-
userFriendly: true,
|
|
44
|
-
...errorData,
|
|
45
|
-
});
|
|
39
|
+
throw new AssertionError(msg, Object.assign({ userFriendly: true }, errorData));
|
|
46
40
|
}
|
|
47
41
|
}
|
|
48
42
|
/**
|
|
@@ -60,10 +54,7 @@ export function _assertDeepEquals(actual, expected, message, errorData) {
|
|
|
60
54
|
]
|
|
61
55
|
.filter(Boolean)
|
|
62
56
|
.join('\n');
|
|
63
|
-
throw new AssertionError(msg, {
|
|
64
|
-
userFriendly: true,
|
|
65
|
-
...errorData,
|
|
66
|
-
});
|
|
57
|
+
throw new AssertionError(msg, Object.assign({ userFriendly: true }, errorData));
|
|
67
58
|
}
|
|
68
59
|
}
|
|
69
60
|
export function _assertIsError(err, message) {
|
|
@@ -21,8 +21,9 @@ 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;
|
|
24
25
|
if (o instanceof Error) {
|
|
25
|
-
return _errorToErrorObject(o, opt
|
|
26
|
+
return _errorToErrorObject(o, (_a = opt === null || opt === void 0 ? void 0 : opt.includeErrorStack) !== null && _a !== void 0 ? _a : true);
|
|
26
27
|
}
|
|
27
28
|
o = _jsonParseIfPossible(o);
|
|
28
29
|
if (_isHttpErrorResponse(o)) {
|
|
@@ -35,10 +36,7 @@ export function _anyToErrorObject(o, opt) {
|
|
|
35
36
|
// so, fair to return `data: {}` in the end
|
|
36
37
|
// Also we're sure it includes no "error name", e.g no `Error: ...`,
|
|
37
38
|
// so, fair to include `name: 'Error'`
|
|
38
|
-
const message = _stringifyAny(o, {
|
|
39
|
-
includeErrorData: true,
|
|
40
|
-
...opt,
|
|
41
|
-
});
|
|
39
|
+
const message = _stringifyAny(o, Object.assign({ includeErrorData: true }, opt));
|
|
42
40
|
return {
|
|
43
41
|
name: 'Error',
|
|
44
42
|
message,
|
|
@@ -49,7 +47,7 @@ export function _errorToErrorObject(e, includeErrorStack = true) {
|
|
|
49
47
|
const obj = {
|
|
50
48
|
name: e.name,
|
|
51
49
|
message: e.message,
|
|
52
|
-
data: {
|
|
50
|
+
data: Object.assign({}, e.data), // empty by default
|
|
53
51
|
};
|
|
54
52
|
if (includeErrorStack) {
|
|
55
53
|
obj.stack = e.stack;
|
|
@@ -83,13 +81,14 @@ export function _errorObjectToError(o, errorClass = Error) {
|
|
|
83
81
|
return err;
|
|
84
82
|
}
|
|
85
83
|
export function _isHttpErrorResponse(o) {
|
|
86
|
-
return _isHttpErrorObject(o
|
|
84
|
+
return _isHttpErrorObject(o === null || o === void 0 ? void 0 : o.error);
|
|
87
85
|
}
|
|
88
86
|
export function _isHttpErrorObject(o) {
|
|
87
|
+
var _a;
|
|
89
88
|
return (!!o &&
|
|
90
89
|
typeof o.name === 'string' &&
|
|
91
90
|
typeof o.message === 'string' &&
|
|
92
|
-
typeof o.data
|
|
91
|
+
typeof ((_a = o.data) === null || _a === void 0 ? void 0 : _a.httpStatusCode) === 'number');
|
|
93
92
|
}
|
|
94
93
|
/**
|
|
95
94
|
* Note: any instance of AppError is also automatically an ErrorObject
|
package/dist-esm/index.js
CHANGED
|
@@ -15,6 +15,7 @@ export * from './decorators/retry.decorator';
|
|
|
15
15
|
export * from './decorators/timeout.decorator';
|
|
16
16
|
export * from './error/app.error';
|
|
17
17
|
export * from './error/assert';
|
|
18
|
+
export * from './enum.util';
|
|
18
19
|
export * from './error/error.util';
|
|
19
20
|
import { ErrorMode } from './error/errorMode';
|
|
20
21
|
export * from './error/http.error';
|
|
@@ -6,6 +6,7 @@ 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;
|
|
9
10
|
// Merge `properties`
|
|
10
11
|
Object.entries(s2.properties).forEach(([k, v]) => {
|
|
11
12
|
;
|
|
@@ -17,8 +18,8 @@ export function mergeJsonSchemaObjects(s1, s2) {
|
|
|
17
18
|
s1.patternProperties[k] = v;
|
|
18
19
|
});
|
|
19
20
|
s1.propertyNames = s2.propertyNames || s1.propertyNames;
|
|
20
|
-
s1.minProperties = s2.minProperties
|
|
21
|
-
s1.maxProperties = s2.maxProperties
|
|
21
|
+
s1.minProperties = (_a = s2.minProperties) !== null && _a !== void 0 ? _a : s1.minProperties;
|
|
22
|
+
s1.maxProperties = (_b = s2.maxProperties) !== null && _b !== void 0 ? _b : s1.maxProperties;
|
|
22
23
|
// Merge `required`
|
|
23
24
|
s1.required.push(...s2.required);
|
|
24
25
|
s1.required = _uniq(s1.required).sort();
|
|
@@ -250,11 +250,12 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
|
|
|
250
250
|
return this;
|
|
251
251
|
}
|
|
252
252
|
transformModify(t, add) {
|
|
253
|
+
var _a;
|
|
253
254
|
if (add) {
|
|
254
255
|
this.schema.transform = _uniq([...(this.schema.transform || []), t]);
|
|
255
256
|
}
|
|
256
257
|
else {
|
|
257
|
-
this.schema.transform = this.schema.transform
|
|
258
|
+
this.schema.transform = (_a = this.schema.transform) === null || _a === void 0 ? void 0 : _a.filter(s => s !== t);
|
|
258
259
|
}
|
|
259
260
|
return this;
|
|
260
261
|
}
|
|
@@ -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 values
|
|
17
|
+
return (values === null || values === void 0 ? void 0 : 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 : Object.assign({}, 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 : Object.assign({}, obj));
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
82
|
* var users = {
|
|
@@ -137,7 +137,8 @@ export function _mapObject(obj, mapper) {
|
|
|
137
137
|
}, {});
|
|
138
138
|
}
|
|
139
139
|
export function _findKeyByValue(obj, v) {
|
|
140
|
-
|
|
140
|
+
var _a;
|
|
141
|
+
return (_a = Object.entries(obj).find(([_, value]) => value === v)) === null || _a === void 0 ? void 0 : _a[0];
|
|
141
142
|
}
|
|
142
143
|
export function _objectNullValuesToUndefined(obj, mutate = false) {
|
|
143
144
|
return _mapValues(obj, (_k, v) => (v === null ? undefined : v), mutate);
|
|
@@ -305,6 +306,7 @@ export function _set(obj, path, value) {
|
|
|
305
306
|
a[c]
|
|
306
307
|
: // No: create the key. Is the next key a potential array-index?
|
|
307
308
|
(a[c] =
|
|
309
|
+
// @ts-ignore
|
|
308
310
|
// eslint-disable-next-line
|
|
309
311
|
Math.abs(path[i + 1]) >> 0 === +path[i + 1]
|
|
310
312
|
? [] // Yes: assign a new array object
|
package/dist-esm/promise/pMap.js
CHANGED
|
@@ -6,6 +6,7 @@ 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";
|
|
9
10
|
import { END, ErrorMode, SKIP } from '..';
|
|
10
11
|
import { AggregatedError } from './AggregatedError';
|
|
11
12
|
/**
|
|
@@ -35,6 +36,7 @@ import { AggregatedError } from './AggregatedError';
|
|
|
35
36
|
* })();
|
|
36
37
|
*/
|
|
37
38
|
export async function pMap(iterable, mapper, opt = {}) {
|
|
39
|
+
var e_1, _a;
|
|
38
40
|
const ret = [];
|
|
39
41
|
// const iterator = iterable[Symbol.iterator]()
|
|
40
42
|
const items = [...iterable];
|
|
@@ -48,23 +50,33 @@ export async function pMap(iterable, mapper, opt = {}) {
|
|
|
48
50
|
let currentIndex = 0;
|
|
49
51
|
// Special cases that are able to preserve async stack traces
|
|
50
52
|
if (concurrency === 1) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (errorMode === ErrorMode.THROW_IMMEDIATELY)
|
|
62
|
-
throw err;
|
|
63
|
-
if (errorMode === ErrorMode.THROW_AGGREGATED) {
|
|
64
|
-
errors.push(err);
|
|
53
|
+
try {
|
|
54
|
+
// Special case for concurrency == 1
|
|
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);
|
|
65
63
|
}
|
|
66
|
-
|
|
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 {
|
|
76
|
+
try {
|
|
77
|
+
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) await _a.call(items_1);
|
|
67
78
|
}
|
|
79
|
+
finally { if (e_1) throw e_1.error; }
|
|
68
80
|
}
|
|
69
81
|
if (errors.length) {
|
|
70
82
|
throw new AggregatedError(errors, ret);
|
|
@@ -14,5 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
export async function pProps(input) {
|
|
16
16
|
const keys = Object.keys(input);
|
|
17
|
+
// `as any` here is added to make it compile when `noUncheckedIndexedAccess` is false
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
17
19
|
return Object.fromEntries((await Promise.all(Object.values(input))).map((v, i) => [keys[i], v]));
|
|
18
20
|
}
|
|
@@ -13,14 +13,9 @@ export class PQueue {
|
|
|
13
13
|
this.inFlight = 0;
|
|
14
14
|
this.queue = [];
|
|
15
15
|
this.onIdleListeners = [];
|
|
16
|
-
this.cfg = {
|
|
16
|
+
this.cfg = Object.assign({
|
|
17
17
|
// concurrency: Number.MAX_SAFE_INTEGER,
|
|
18
|
-
errorMode: ErrorMode.THROW_IMMEDIATELY,
|
|
19
|
-
logger: console,
|
|
20
|
-
debug: false,
|
|
21
|
-
resolveOn: 'finish',
|
|
22
|
-
...cfg,
|
|
23
|
-
};
|
|
18
|
+
errorMode: ErrorMode.THROW_IMMEDIATELY, logger: console, debug: false, resolveOn: 'finish' }, cfg);
|
|
24
19
|
if (!cfg.debug) {
|
|
25
20
|
this.debug = () => { };
|
|
26
21
|
}
|
|
@@ -32,10 +32,7 @@ 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 = {
|
|
36
|
-
...err.data,
|
|
37
|
-
...opt.errorData,
|
|
38
|
-
};
|
|
35
|
+
err.data = Object.assign(Object.assign({}, err.data), opt.errorData);
|
|
39
36
|
reject(err);
|
|
40
37
|
}
|
|
41
38
|
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 (_a) {
|
|
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) => value
|
|
141
|
+
is.buffer = (value) => { var _a, _b, _c; return (_c = (_b = (_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.isBuffer) === null || _b === void 0 ? void 0 : _b.call(_a, value)) !== null && _c !== void 0 ? _c : 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 === null || value === void 0 ? void 0 : value[Symbol.iterator]);
|
|
145
|
+
is.asyncIterable = (value) => is.function_(value === null || value === void 0 ? void 0 : 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 === null || value === void 0 ? void 0 : value.then) && is.function_(value === null || value === void 0 ? void 0 : 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 (_a) {
|
|
188
188
|
return false;
|
|
189
189
|
}
|
|
190
190
|
};
|
|
@@ -235,13 +235,14 @@ 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;
|
|
238
239
|
if (!value) {
|
|
239
240
|
return false;
|
|
240
241
|
}
|
|
241
|
-
if (value === value[Symbol.observable]
|
|
242
|
+
if (value === ((_b = (_a = value)[Symbol.observable]) === null || _b === void 0 ? void 0 : _b.call(_a))) {
|
|
242
243
|
return true;
|
|
243
244
|
}
|
|
244
|
-
if (value === value['@@observable']
|
|
245
|
+
if (value === ((_d = (_c = value)['@@observable']) === null || _d === void 0 ? void 0 : _d.call(_c))) {
|
|
245
246
|
return true;
|
|
246
247
|
}
|
|
247
248
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.105.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"build-prod": "build-prod-esm-cjs",
|
|
@@ -36,10 +36,6 @@
|
|
|
36
36
|
"module": "dist-esm/index.js",
|
|
37
37
|
"types": "dist/index.d.ts",
|
|
38
38
|
"sideEffects": false,
|
|
39
|
-
"exports": {
|
|
40
|
-
"import": "./dist-esm/index.js",
|
|
41
|
-
"require": "./dist/index.js"
|
|
42
|
-
},
|
|
43
39
|
"engines": {
|
|
44
40
|
"node": ">=14.15.0"
|
|
45
41
|
},
|
package/src/enum.util.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { NumberEnum, StringEnum } from './types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns all number keys of a number-enum.
|
|
5
|
+
*/
|
|
6
|
+
export function _numberEnumKeys(en: NumberEnum): string[] {
|
|
7
|
+
return Object.values(en).filter(k => typeof k === 'string') as string[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns all number values of a number-enum.
|
|
12
|
+
*/
|
|
13
|
+
export function _numberEnumValues(en: NumberEnum): number[] {
|
|
14
|
+
return Object.values(en).filter(k => typeof k === 'number') as number[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns all string keys of a string-enum.
|
|
19
|
+
*/
|
|
20
|
+
export function _stringEnumKeys(en: StringEnum): string[] {
|
|
21
|
+
return Object.keys(en)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns all string values of a string-enum.
|
|
26
|
+
*/
|
|
27
|
+
export function _stringEnumValues(en: StringEnum): string[] {
|
|
28
|
+
// filtering here is unnecessary, but works as a safety in case Number-enum is passed
|
|
29
|
+
return Object.values(en).filter(k => typeof k === 'string')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns all number-enum "entries", where entry is a tuple of [key, value],
|
|
34
|
+
* where key is a String key, value is a Number value, typed as Enum itself.
|
|
35
|
+
*
|
|
36
|
+
* Doesn't work on String-enums!
|
|
37
|
+
*/
|
|
38
|
+
export function _numberEnumEntries<T extends NumberEnum>(en: T): [k: string, v: T[keyof T]][] {
|
|
39
|
+
return Object.values(en)
|
|
40
|
+
.filter(k => typeof k === 'string')
|
|
41
|
+
.map(k => [k, en[k]]) as any
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns all string-enum "entries", where entry is a tuple of [key, value],
|
|
46
|
+
* where key is a String key, value is a String value, typed as Enum itself.
|
|
47
|
+
*
|
|
48
|
+
* Doesn't work on Number-enums!
|
|
49
|
+
*/
|
|
50
|
+
export function _stringEnumEntries<T extends StringEnum>(en: T): [k: string, v: T[keyof T]][] {
|
|
51
|
+
return Object.keys(en).map(k => [k, en[k]]) as any
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Allows to return a Number enum value (typed as Enum itself) based on it's String key.
|
|
56
|
+
* e.g:
|
|
57
|
+
* const v = SomeEnum['stringValue']
|
|
58
|
+
* // v is of type SomeEnum, which is of type Number
|
|
59
|
+
*
|
|
60
|
+
* Throws if value is not found!
|
|
61
|
+
*/
|
|
62
|
+
export function _numberEnumInverse<T extends NumberEnum>(en: T, v: string): T[keyof T] {
|
|
63
|
+
const r = en[v as keyof T] as any
|
|
64
|
+
if (!r) throw new Error(`enumInverse value not found for: ${v}`)
|
|
65
|
+
return r
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* _enumInverse, but allows to get/return undefined output.
|
|
70
|
+
*/
|
|
71
|
+
export function _numberEnumInverseNullable<T extends NumberEnum>(
|
|
72
|
+
en: T,
|
|
73
|
+
v: string | undefined,
|
|
74
|
+
): T[keyof T] | undefined {
|
|
75
|
+
return en[v as keyof T]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Takes number or string enum input, returns normalized Enum output.
|
|
80
|
+
* Only works for number enums.
|
|
81
|
+
*
|
|
82
|
+
* Throws if value is not found!
|
|
83
|
+
*/
|
|
84
|
+
export function _numberEnumNormalize<T extends NumberEnum>(en: T, v: string | number): T[keyof T] {
|
|
85
|
+
const r = _numberEnumNormalizeNullable(en, v)
|
|
86
|
+
if (!r || !en[r as keyof T]) throw new Error(`enumNormalize value not found for: ${v}`)
|
|
87
|
+
return r
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Same as _enumNormalize, but allows to return undefined values.
|
|
92
|
+
*/
|
|
93
|
+
export function _numberEnumNormalizeNullable<T extends NumberEnum>(
|
|
94
|
+
en: T,
|
|
95
|
+
v: string | number | undefined,
|
|
96
|
+
): T[keyof T] | undefined {
|
|
97
|
+
return typeof v === 'string' ? en[v as keyof T] : (v as any)
|
|
98
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ export * from './decorators/retry.decorator'
|
|
|
20
20
|
export * from './decorators/timeout.decorator'
|
|
21
21
|
export * from './error/app.error'
|
|
22
22
|
export * from './error/assert'
|
|
23
|
+
export * from './enum.util'
|
|
23
24
|
import {
|
|
24
25
|
Admin401ErrorData,
|
|
25
26
|
Admin403ErrorData,
|
|
@@ -117,6 +118,9 @@ import {
|
|
|
117
118
|
Predicate,
|
|
118
119
|
PromiseMap,
|
|
119
120
|
AnyObject,
|
|
121
|
+
AnyEnum,
|
|
122
|
+
NumberEnum,
|
|
123
|
+
StringEnum,
|
|
120
124
|
AnyFunction,
|
|
121
125
|
Reviver,
|
|
122
126
|
SavedDBEntity,
|
|
@@ -217,6 +221,9 @@ export type {
|
|
|
217
221
|
StringMap,
|
|
218
222
|
PromiseMap,
|
|
219
223
|
AnyObject,
|
|
224
|
+
AnyEnum,
|
|
225
|
+
NumberEnum,
|
|
226
|
+
StringEnum,
|
|
220
227
|
AnyFunction,
|
|
221
228
|
ValuesOf,
|
|
222
229
|
ValueOf,
|
|
@@ -125,7 +125,7 @@ export function _mapValues<T extends AnyObject, OUT = T>(
|
|
|
125
125
|
mutate = false,
|
|
126
126
|
): OUT {
|
|
127
127
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
128
|
-
map[k] = mapper(k, v, obj)
|
|
128
|
+
map[k as keyof OUT] = mapper(k, v, obj)
|
|
129
129
|
return map
|
|
130
130
|
}, (mutate ? obj : {}) as OUT)
|
|
131
131
|
}
|
|
@@ -142,9 +142,9 @@ export function _mapKeys<T extends AnyObject>(
|
|
|
142
142
|
): StringMap<T[keyof T]> {
|
|
143
143
|
// eslint-disable-next-line unicorn/prefer-object-from-entries
|
|
144
144
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
145
|
-
map[mapper(k, v, obj)] = v
|
|
145
|
+
map[mapper(k, v, obj) as keyof T] = v
|
|
146
146
|
return map
|
|
147
|
-
}, {})
|
|
147
|
+
}, {} as T)
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
/**
|
|
@@ -299,7 +299,7 @@ export function _unset<T extends AnyObject>(obj: T, prop: string): void {
|
|
|
299
299
|
export function _invert<T extends AnyObject>(o: T): { [k in ValueOf<T>]: keyof T | undefined } {
|
|
300
300
|
const inv = {} as { [k in ValueOf<T>]: keyof T }
|
|
301
301
|
Object.keys(o).forEach(k => {
|
|
302
|
-
inv[o[k]] = k
|
|
302
|
+
inv[o[k] as ValueOf<T>] = k
|
|
303
303
|
})
|
|
304
304
|
return inv
|
|
305
305
|
}
|
|
@@ -365,6 +365,7 @@ export function _set<IN extends AnyObject, OUT = IN>(
|
|
|
365
365
|
a[c]
|
|
366
366
|
: // No: create the key. Is the next key a potential array-index?
|
|
367
367
|
(a[c] =
|
|
368
|
+
// @ts-ignore
|
|
368
369
|
// eslint-disable-next-line
|
|
369
370
|
Math.abs(path[i + 1]) >> 0 === +path[i + 1]
|
|
370
371
|
? [] // Yes: assign a new array object
|
package/src/promise/pProps.ts
CHANGED
|
@@ -16,5 +16,9 @@ export async function pProps<T>(input: { [K in keyof T]: T[K] | Promise<T[K]> })
|
|
|
16
16
|
[K in keyof T]: Awaited<T[K]>
|
|
17
17
|
}> {
|
|
18
18
|
const keys = Object.keys(input)
|
|
19
|
-
|
|
19
|
+
// `as any` here is added to make it compile when `noUncheckedIndexedAccess` is false
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
21
|
+
return Object.fromEntries(
|
|
22
|
+
(await Promise.all(Object.values(input))).map((v, i) => [keys[i], v]),
|
|
23
|
+
) as any
|
|
20
24
|
}
|
package/src/string/url.util.ts
CHANGED
|
@@ -14,9 +14,9 @@ import { StringMap } from '../types'
|
|
|
14
14
|
* Goal of this function is to produce exactly same output as URLSearchParams would.
|
|
15
15
|
*/
|
|
16
16
|
export function _parseQueryString(search: string): StringMap {
|
|
17
|
-
const qs = {}
|
|
17
|
+
const qs: StringMap = {}
|
|
18
18
|
search
|
|
19
|
-
.
|
|
19
|
+
.slice(search[0] === '?' ? 1 : 0)
|
|
20
20
|
.split('&')
|
|
21
21
|
.forEach(p => {
|
|
22
22
|
const [k, v] = p.split('=')
|
package/src/types.ts
CHANGED
|
@@ -24,6 +24,10 @@ export interface PromiseMap {
|
|
|
24
24
|
*/
|
|
25
25
|
export type AnyObject = Record<string, any>
|
|
26
26
|
|
|
27
|
+
export type AnyEnum = NumberEnum
|
|
28
|
+
export type NumberEnum = Record<string, number | string>
|
|
29
|
+
export type StringEnum = Record<string, string>
|
|
30
|
+
|
|
27
31
|
export interface CreatedUpdated {
|
|
28
32
|
created: number
|
|
29
33
|
updated: number
|