@naturalcycles/js-lib 14.160.0 → 14.161.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/{lazy.d.ts → define.d.ts} +26 -0
- package/dist/define.js +118 -0
- package/dist/error/app.error.d.ts +16 -3
- package/dist/error/app.error.js +22 -19
- package/dist/error/assert.d.ts +1 -1
- package/dist/error/assert.js +2 -2
- package/dist/error/error.util.js +21 -20
- package/dist/error/httpRequestError.d.ts +2 -2
- package/dist/error/httpRequestError.js +2 -5
- package/dist/error/jsonParseError.d.ts +2 -2
- package/dist/error/jsonParseError.js +5 -2
- package/dist/error/try.js +3 -1
- package/dist/http/fetcher.js +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/object/object.util.d.ts +16 -11
- package/dist/object/object.util.js +29 -12
- package/dist/promise/pTimeout.d.ts +3 -3
- package/dist/promise/pTimeout.js +2 -2
- package/dist/types.d.ts +9 -2
- package/dist/types.js +9 -2
- package/dist-esm/{lazy.js → define.js} +38 -0
- package/dist-esm/error/app.error.js +22 -19
- package/dist-esm/error/assert.js +2 -2
- package/dist-esm/error/error.util.js +21 -20
- package/dist-esm/error/httpRequestError.js +2 -5
- package/dist-esm/error/jsonParseError.js +5 -2
- package/dist-esm/error/try.js +3 -1
- package/dist-esm/http/fetcher.js +3 -1
- package/dist-esm/index.js +1 -1
- package/dist-esm/object/object.util.js +27 -11
- package/dist-esm/promise/pTimeout.js +2 -2
- package/dist-esm/types.js +8 -1
- package/package.json +1 -1
- package/src/{lazy.ts → define.ts} +68 -0
- package/src/error/app.error.ts +39 -22
- package/src/error/assert.ts +2 -2
- package/src/error/error.util.ts +21 -22
- package/src/error/httpRequestError.ts +3 -6
- package/src/error/jsonParseError.ts +7 -8
- package/src/error/try.ts +7 -1
- package/src/http/fetcher.ts +3 -1
- package/src/index.ts +1 -1
- package/src/object/object.util.ts +51 -30
- package/src/promise/pTimeout.ts +4 -4
- package/src/types.ts +12 -2
- package/dist/lazy.js +0 -65
|
@@ -28,3 +28,29 @@ export declare function _defineLazyProperty<OBJ extends AnyObject>(obj: OBJ, pro
|
|
|
28
28
|
* Like _defineLazyProperty, but allows to define multiple props at once.
|
|
29
29
|
*/
|
|
30
30
|
export declare function _defineLazyProps<OBJ extends AnyObject>(obj: OBJ, props: Partial<Record<keyof OBJ, AnyFunction>>): OBJ;
|
|
31
|
+
/**
|
|
32
|
+
* Same as Object.defineProperty, but with better (least restricting) defaults.
|
|
33
|
+
*
|
|
34
|
+
* Defaults are:
|
|
35
|
+
* writable: true
|
|
36
|
+
* configurable: true
|
|
37
|
+
* enumerable: true
|
|
38
|
+
* value: existing obj[prop] value
|
|
39
|
+
*
|
|
40
|
+
* Original defaults:
|
|
41
|
+
* writable: false
|
|
42
|
+
* configurable: false
|
|
43
|
+
* enumerable: false
|
|
44
|
+
* value: existing obj[prop] value
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
export declare function _defineProperty<T extends AnyObject>(obj: T, prop: keyof T, pd: PropertyDescriptor): T;
|
|
48
|
+
/**
|
|
49
|
+
* Object.defineProperties with better defaults.
|
|
50
|
+
* See _defineProperty for exact defaults definition.
|
|
51
|
+
*/
|
|
52
|
+
export declare function _defineProps<T extends AnyObject>(obj: T, props: Partial<Record<keyof T, PropertyDescriptor>>): T;
|
|
53
|
+
/**
|
|
54
|
+
* Like _defineProps, but skips props with nullish values.
|
|
55
|
+
*/
|
|
56
|
+
export declare function _defineNonNullishProps<T extends AnyObject>(obj: T, props: Partial<Record<keyof T, PropertyDescriptor>>): T;
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._defineNonNullishProps = exports._defineProps = exports._defineProperty = exports._defineLazyProps = exports._defineLazyProperty = exports._lazyValue = void 0;
|
|
4
|
+
const object_util_1 = require("./object/object.util");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
/**
|
|
7
|
+
* const value = lazyValue(() => expensiveComputation())
|
|
8
|
+
*
|
|
9
|
+
* value() // calls expensiveComputation() once
|
|
10
|
+
* value() // returns cached result
|
|
11
|
+
* value() // returns cached result
|
|
12
|
+
*
|
|
13
|
+
* Based on: https://github.com/sindresorhus/lazy-value
|
|
14
|
+
*/
|
|
15
|
+
function _lazyValue(fn) {
|
|
16
|
+
let isCalled = false;
|
|
17
|
+
let result;
|
|
18
|
+
return (() => {
|
|
19
|
+
if (!isCalled) {
|
|
20
|
+
isCalled = true;
|
|
21
|
+
result = fn();
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports._lazyValue = _lazyValue;
|
|
27
|
+
/**
|
|
28
|
+
* interface Obj {
|
|
29
|
+
* v: number
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const obj = {} as Obj
|
|
33
|
+
*
|
|
34
|
+
* _defineLazyProperty(obj, 'v', () => expensiveComputation())
|
|
35
|
+
* obj.v // runs expensiveComputation() once
|
|
36
|
+
* obj.v // cached value
|
|
37
|
+
* obj.v // cached value
|
|
38
|
+
*
|
|
39
|
+
* Based on: https://github.com/sindresorhus/define-lazy-prop
|
|
40
|
+
*/
|
|
41
|
+
function _defineLazyProperty(obj, propertyName, fn) {
|
|
42
|
+
const define = (value) => {
|
|
43
|
+
Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true });
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(obj, propertyName, {
|
|
46
|
+
configurable: true,
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get() {
|
|
49
|
+
const result = fn();
|
|
50
|
+
define(result);
|
|
51
|
+
return result;
|
|
52
|
+
},
|
|
53
|
+
set(value) {
|
|
54
|
+
define(value);
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
return obj;
|
|
58
|
+
}
|
|
59
|
+
exports._defineLazyProperty = _defineLazyProperty;
|
|
60
|
+
/**
|
|
61
|
+
* Like _defineLazyProperty, but allows to define multiple props at once.
|
|
62
|
+
*/
|
|
63
|
+
function _defineLazyProps(obj, props) {
|
|
64
|
+
Object.entries(props).forEach(([k, fn]) => _defineLazyProperty(obj, k, fn));
|
|
65
|
+
return obj;
|
|
66
|
+
}
|
|
67
|
+
exports._defineLazyProps = _defineLazyProps;
|
|
68
|
+
/**
|
|
69
|
+
* Same as Object.defineProperty, but with better (least restricting) defaults.
|
|
70
|
+
*
|
|
71
|
+
* Defaults are:
|
|
72
|
+
* writable: true
|
|
73
|
+
* configurable: true
|
|
74
|
+
* enumerable: true
|
|
75
|
+
* value: existing obj[prop] value
|
|
76
|
+
*
|
|
77
|
+
* Original defaults:
|
|
78
|
+
* writable: false
|
|
79
|
+
* configurable: false
|
|
80
|
+
* enumerable: false
|
|
81
|
+
* value: existing obj[prop] value
|
|
82
|
+
*
|
|
83
|
+
*/
|
|
84
|
+
function _defineProperty(obj, prop, pd) {
|
|
85
|
+
return Object.defineProperty(obj, prop, {
|
|
86
|
+
writable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
enumerable: true,
|
|
89
|
+
// value: obj[prop], // existing value is already kept by default
|
|
90
|
+
...pd,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
exports._defineProperty = _defineProperty;
|
|
94
|
+
/**
|
|
95
|
+
* Object.defineProperties with better defaults.
|
|
96
|
+
* See _defineProperty for exact defaults definition.
|
|
97
|
+
*/
|
|
98
|
+
function _defineProps(obj, props) {
|
|
99
|
+
return Object.defineProperties(obj, (0, object_util_1._mapValues)(props, (k, pd) => ({
|
|
100
|
+
writable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
enumerable: true,
|
|
103
|
+
// value: obj[k], // existing value is already kept by default
|
|
104
|
+
...pd,
|
|
105
|
+
})));
|
|
106
|
+
}
|
|
107
|
+
exports._defineProps = _defineProps;
|
|
108
|
+
/**
|
|
109
|
+
* Like _defineProps, but skips props with nullish values.
|
|
110
|
+
*/
|
|
111
|
+
function _defineNonNullishProps(obj, props) {
|
|
112
|
+
return _defineProps(obj, (0, object_util_1._mapObject)(props, (k, pd) => {
|
|
113
|
+
if (pd.value === null || pd.value === undefined)
|
|
114
|
+
return types_1.SKIP;
|
|
115
|
+
return [k, pd];
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
exports._defineNonNullishProps = _defineNonNullishProps;
|
|
@@ -4,15 +4,28 @@ import type { ErrorData, ErrorObject } from './error.model';
|
|
|
4
4
|
*
|
|
5
5
|
* message - "technical" message. Frontend decides to show it or not.
|
|
6
6
|
* data - optional "any" payload.
|
|
7
|
-
* data.
|
|
7
|
+
* data.userFriendly - if present, will be displayed to the User as is.
|
|
8
8
|
*
|
|
9
9
|
* Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
|
|
10
10
|
*/
|
|
11
11
|
export declare class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
|
|
12
12
|
data: DATA_TYPE;
|
|
13
13
|
/**
|
|
14
|
-
* cause here is normalized to be an ErrorObject
|
|
14
|
+
* `cause` here is normalized to be an ErrorObject
|
|
15
|
+
*/
|
|
16
|
+
cause?: ErrorObject;
|
|
17
|
+
constructor(message: string, data?: DATA_TYPE, opt?: AppErrorOptions);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Extra options for AppError constructor.
|
|
21
|
+
*/
|
|
22
|
+
export interface AppErrorOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Overrides Error.name and Error.constructor.name
|
|
25
|
+
*/
|
|
26
|
+
name?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Sets Error.cause
|
|
15
29
|
*/
|
|
16
30
|
cause?: ErrorObject;
|
|
17
|
-
constructor(message: string, data?: DATA_TYPE, cause?: ErrorObject, name?: string);
|
|
18
31
|
}
|
package/dist/error/app.error.js
CHANGED
|
@@ -6,29 +6,26 @@ exports.AppError = void 0;
|
|
|
6
6
|
*
|
|
7
7
|
* message - "technical" message. Frontend decides to show it or not.
|
|
8
8
|
* data - optional "any" payload.
|
|
9
|
-
* data.
|
|
9
|
+
* data.userFriendly - if present, will be displayed to the User as is.
|
|
10
10
|
*
|
|
11
11
|
* Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
|
|
12
12
|
*/
|
|
13
13
|
class AppError extends Error {
|
|
14
|
-
constructor(message, data = {},
|
|
14
|
+
constructor(message, data = {}, opt = {}) {
|
|
15
15
|
super(message);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
writable: true,
|
|
30
|
-
configurable: true,
|
|
31
|
-
enumerable: false,
|
|
16
|
+
const { name = this.constructor.name, cause } = opt;
|
|
17
|
+
Object.defineProperties(this, {
|
|
18
|
+
name: {
|
|
19
|
+
value: name,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
},
|
|
23
|
+
data: {
|
|
24
|
+
value: data,
|
|
25
|
+
writable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
enumerable: false,
|
|
28
|
+
},
|
|
32
29
|
});
|
|
33
30
|
if (cause) {
|
|
34
31
|
Object.defineProperty(this, 'cause', {
|
|
@@ -36,9 +33,15 @@ class AppError extends Error {
|
|
|
36
33
|
value: cause,
|
|
37
34
|
writable: true,
|
|
38
35
|
configurable: true,
|
|
39
|
-
enumerable:
|
|
36
|
+
enumerable: true, // unlike standard - setting it to true for "visibility"
|
|
40
37
|
});
|
|
41
38
|
}
|
|
39
|
+
// this is to allow changing this.constuctor.name to a non-minified version
|
|
40
|
+
Object.defineProperty(this.constructor, 'name', {
|
|
41
|
+
value: name,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
});
|
|
42
45
|
// todo: check if it's needed at all!
|
|
43
46
|
// if (Error.captureStackTrace) {
|
|
44
47
|
// Error.captureStackTrace(this, this.constructor)
|
package/dist/error/assert.d.ts
CHANGED
|
@@ -44,5 +44,5 @@ export declare function _assertIsString(v: any, message?: string): asserts v is
|
|
|
44
44
|
export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
|
|
45
45
|
export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
|
|
46
46
|
export declare class AssertionError extends AppError {
|
|
47
|
-
constructor(message: string, data?:
|
|
47
|
+
constructor(message: string, data?: ErrorData);
|
|
48
48
|
}
|
package/dist/error/assert.js
CHANGED
|
@@ -127,8 +127,8 @@ function _assertTypeOf(v, expectedType, message) {
|
|
|
127
127
|
}
|
|
128
128
|
exports._assertTypeOf = _assertTypeOf;
|
|
129
129
|
class AssertionError extends app_error_1.AppError {
|
|
130
|
-
constructor(message, data
|
|
131
|
-
super(message, data,
|
|
130
|
+
constructor(message, data) {
|
|
131
|
+
super(message, data, { name: 'AssertionError' });
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
exports.AssertionError = AssertionError;
|
package/dist/error/error.util.js
CHANGED
|
@@ -90,7 +90,8 @@ function _errorObjectToError(o, errorClass = Error) {
|
|
|
90
90
|
// Here we pass constructor values assuming it's AppError or sub-class of it
|
|
91
91
|
// If not - will be checked at the next step
|
|
92
92
|
// We cannot check `if (errorClass instanceof AppError)`, only `err instanceof AppError`
|
|
93
|
-
const
|
|
93
|
+
const { name, cause } = o;
|
|
94
|
+
const err = new errorClass(o.message, o.data, { name, cause });
|
|
94
95
|
// name: err.name, // cannot be assigned to a readonly property like this
|
|
95
96
|
// stack: o.stack, // also readonly e.g in Firefox
|
|
96
97
|
if (o.stack) {
|
|
@@ -100,30 +101,30 @@ function _errorObjectToError(o, errorClass = Error) {
|
|
|
100
101
|
}
|
|
101
102
|
if (!(err instanceof __1.AppError)) {
|
|
102
103
|
// Following actions are only needed for non-AppError-like errors
|
|
103
|
-
Object.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
Object.defineProperties(err, {
|
|
105
|
+
name: {
|
|
106
|
+
value: name,
|
|
107
|
+
configurable: true,
|
|
108
|
+
writable: true,
|
|
109
|
+
},
|
|
110
|
+
data: {
|
|
111
|
+
value: o.data,
|
|
112
|
+
writable: true,
|
|
113
|
+
configurable: true,
|
|
114
|
+
enumerable: false,
|
|
115
|
+
},
|
|
116
|
+
cause: {
|
|
117
|
+
value: cause,
|
|
118
|
+
writable: true,
|
|
119
|
+
configurable: true,
|
|
120
|
+
enumerable: true,
|
|
121
|
+
},
|
|
107
122
|
});
|
|
108
123
|
Object.defineProperty(err.constructor, 'name', {
|
|
109
|
-
value:
|
|
124
|
+
value: name,
|
|
110
125
|
configurable: true,
|
|
111
126
|
writable: true,
|
|
112
127
|
});
|
|
113
|
-
Object.defineProperty(err, 'data', {
|
|
114
|
-
value: o.data,
|
|
115
|
-
writable: true,
|
|
116
|
-
configurable: true,
|
|
117
|
-
enumerable: false,
|
|
118
|
-
});
|
|
119
|
-
if (o.cause) {
|
|
120
|
-
Object.defineProperty(err, 'cause', {
|
|
121
|
-
value: o.cause,
|
|
122
|
-
writable: true,
|
|
123
|
-
configurable: true,
|
|
124
|
-
enumerable: false,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
128
|
}
|
|
128
129
|
return err;
|
|
129
130
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppError } from './app.error';
|
|
1
|
+
import { AppError, AppErrorOptions } from './app.error';
|
|
2
2
|
import type { ErrorObject, HttpRequestErrorData } from './error.model';
|
|
3
3
|
/**
|
|
4
4
|
* Error that is thrown when Http Request was made and returned an error.
|
|
@@ -18,7 +18,7 @@ import type { ErrorObject, HttpRequestErrorData } from './error.model';
|
|
|
18
18
|
* (by default).
|
|
19
19
|
*/
|
|
20
20
|
export declare class HttpRequestError extends AppError<HttpRequestErrorData> {
|
|
21
|
-
constructor(message: string, data: HttpRequestErrorData,
|
|
21
|
+
constructor(message: string, data: HttpRequestErrorData, opt?: AppErrorOptions);
|
|
22
22
|
/**
|
|
23
23
|
* Cause is strictly-defined for HttpRequestError,
|
|
24
24
|
* so it always has a cause.
|
|
@@ -20,16 +20,13 @@ const app_error_1 = require("./app.error");
|
|
|
20
20
|
* (by default).
|
|
21
21
|
*/
|
|
22
22
|
class HttpRequestError extends app_error_1.AppError {
|
|
23
|
-
constructor(message, data,
|
|
23
|
+
constructor(message, data, opt) {
|
|
24
24
|
if (data.response) {
|
|
25
25
|
Object.defineProperty(data, 'response', {
|
|
26
|
-
value: data.response,
|
|
27
|
-
writable: true,
|
|
28
|
-
configurable: true,
|
|
29
26
|
enumerable: false,
|
|
30
27
|
});
|
|
31
28
|
}
|
|
32
|
-
super(message, data,
|
|
29
|
+
super(message, data, { ...opt, name: 'HttpRequestError' });
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
32
|
exports.HttpRequestError = HttpRequestError;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AppError } from './app.error';
|
|
2
|
-
import { ErrorData
|
|
2
|
+
import { ErrorData } from './error.model';
|
|
3
3
|
export interface JsonParseErrorData extends ErrorData {
|
|
4
4
|
/**
|
|
5
5
|
* Original text that failed to get parsed.
|
|
@@ -7,5 +7,5 @@ export interface JsonParseErrorData extends ErrorData {
|
|
|
7
7
|
text?: string;
|
|
8
8
|
}
|
|
9
9
|
export declare class JsonParseError extends AppError<JsonParseErrorData> {
|
|
10
|
-
constructor(data: JsonParseErrorData
|
|
10
|
+
constructor(data: JsonParseErrorData);
|
|
11
11
|
}
|
|
@@ -4,8 +4,11 @@ exports.JsonParseError = void 0;
|
|
|
4
4
|
const string_util_1 = require("../string/string.util");
|
|
5
5
|
const app_error_1 = require("./app.error");
|
|
6
6
|
class JsonParseError extends app_error_1.AppError {
|
|
7
|
-
constructor(data
|
|
8
|
-
|
|
7
|
+
constructor(data) {
|
|
8
|
+
const message = ['Failed to parse', data.text && (0, string_util_1._truncateMiddle)(data.text, 200)]
|
|
9
|
+
.filter(Boolean)
|
|
10
|
+
.join(': ');
|
|
11
|
+
super(message, data, { name: 'JsonParseError' });
|
|
9
12
|
}
|
|
10
13
|
}
|
|
11
14
|
exports.JsonParseError = JsonParseError;
|
package/dist/error/try.js
CHANGED
|
@@ -57,7 +57,9 @@ exports.pTry = pTry;
|
|
|
57
57
|
*/
|
|
58
58
|
class UnexpectedPassError extends app_error_1.AppError {
|
|
59
59
|
constructor() {
|
|
60
|
-
super('expected error was not thrown', {},
|
|
60
|
+
super('expected error was not thrown', {}, {
|
|
61
|
+
name: 'UnexpectedPassError',
|
|
62
|
+
});
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
exports.UnexpectedPassError = UnexpectedPassError;
|
package/dist/http/fetcher.js
CHANGED
|
@@ -321,7 +321,9 @@ class Fetcher {
|
|
|
321
321
|
requestMethod: res.req.init.method,
|
|
322
322
|
requestSignature: res.signature,
|
|
323
323
|
requestDuration: Date.now() - res.req.started,
|
|
324
|
-
}),
|
|
324
|
+
}), {
|
|
325
|
+
cause,
|
|
326
|
+
});
|
|
325
327
|
await this.processRetry(res);
|
|
326
328
|
}
|
|
327
329
|
async processRetry(res) {
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ZodError = exports.ZodSchema = exports.z = exports.is = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
tslib_1.__exportStar(require("./array/array.util"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./
|
|
6
|
+
tslib_1.__exportStar(require("./define"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./string/url.util"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./array/range"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./decorators/createPromiseDecorator"), exports);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PropertyPath } from '../lodash.types';
|
|
2
|
-
import
|
|
2
|
+
import { KeyValueTuple, SKIP } from '../types';
|
|
3
|
+
import type { AnyObject, ObjectMapper, ObjectPredicate, ValueOf } from '../types';
|
|
3
4
|
/**
|
|
4
5
|
* Returns clone of `obj` with only `props` preserved.
|
|
5
6
|
* Opposite of Omit.
|
|
@@ -44,21 +45,21 @@ export declare function _filterObject<T extends AnyObject>(obj: T, predicate: Ob
|
|
|
44
45
|
* 'pebbles': { 'user': 'pebbles', 'age': 1 }
|
|
45
46
|
* }
|
|
46
47
|
*
|
|
47
|
-
*
|
|
48
|
+
* _mapValues(users, (_key, value) => value.age)
|
|
48
49
|
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
|
|
49
50
|
*
|
|
50
|
-
*
|
|
51
|
-
* _.mapValues(users, 'age')
|
|
52
|
-
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
|
|
51
|
+
* To skip some key-value pairs - use _mapObject instead.
|
|
53
52
|
*/
|
|
54
|
-
export declare function _mapValues<T extends AnyObject
|
|
53
|
+
export declare function _mapValues<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, any>, mutate?: boolean): T;
|
|
55
54
|
/**
|
|
56
55
|
* _.mapKeys({ 'a': 1, 'b': 2 }, (key, value) => key + value)
|
|
57
56
|
* // => { 'a1': 1, 'b2': 2 }
|
|
58
57
|
*
|
|
59
58
|
* Does not support `mutate` flag.
|
|
59
|
+
*
|
|
60
|
+
* To skip some key-value pairs - use _mapObject instead.
|
|
60
61
|
*/
|
|
61
|
-
export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>):
|
|
62
|
+
export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>): Record<string, T[keyof T]>;
|
|
62
63
|
/**
|
|
63
64
|
* Maps object through predicate - a function that receives (k, v, obj)
|
|
64
65
|
* k - key
|
|
@@ -71,13 +72,11 @@ export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapp
|
|
|
71
72
|
* 0 - key of returned object (string)
|
|
72
73
|
* 1 - value of returned object (any)
|
|
73
74
|
*
|
|
74
|
-
* If predicate returns
|
|
75
|
+
* If predicate returns SKIP symbol - such key/value pair is ignored (filtered out).
|
|
75
76
|
*
|
|
76
77
|
* Non-string keys are passed via String(...)
|
|
77
78
|
*/
|
|
78
|
-
export declare function _mapObject<
|
|
79
|
-
[P in keyof IN]: OUT;
|
|
80
|
-
};
|
|
79
|
+
export declare function _mapObject<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, KeyValueTuple<string, any> | typeof SKIP>): T;
|
|
81
80
|
export declare function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): keyof T | undefined;
|
|
82
81
|
export declare function _objectNullValuesToUndefined<T extends AnyObject>(obj: T, mutate?: boolean): T;
|
|
83
82
|
/**
|
|
@@ -184,3 +183,9 @@ export declare function _set<T extends AnyObject>(obj: T, path: PropertyPath, va
|
|
|
184
183
|
* // => false
|
|
185
184
|
*/
|
|
186
185
|
export declare function _has<T extends AnyObject>(obj: T, path: string): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Does Object.freeze recursively for given object.
|
|
188
|
+
*
|
|
189
|
+
* Based on: https://github.com/substack/deep-freeze/blob/master/index.js
|
|
190
|
+
*/
|
|
191
|
+
export declare function _deepFreeze(o: any): void;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._has = exports._set = exports._get = exports._invertMap = exports._invert = exports._unset = exports._deepTrim = exports._merge = exports._filterEmptyValues = exports._undefinedIfEmpty = exports._deepCopy = exports._objectNullValuesToUndefined = exports._findKeyByValue = exports._mapObject = exports._mapKeys = exports._mapValues = exports._filterObject = exports._filterEmptyArrays = exports._filterUndefinedValues = exports._filterNullishValues = exports._filterFalsyValues = exports._mask = exports._omit = exports._pick = void 0;
|
|
3
|
+
exports._deepFreeze = exports._has = exports._set = exports._get = exports._invertMap = exports._invert = exports._unset = exports._deepTrim = exports._merge = exports._filterEmptyValues = exports._undefinedIfEmpty = exports._deepCopy = exports._objectNullValuesToUndefined = exports._findKeyByValue = exports._mapObject = exports._mapKeys = exports._mapValues = exports._filterObject = exports._filterEmptyArrays = exports._filterUndefinedValues = exports._filterNullishValues = exports._filterFalsyValues = exports._mask = exports._omit = exports._pick = void 0;
|
|
4
4
|
const is_util_1 = require("../is.util");
|
|
5
|
+
const types_1 = require("../types");
|
|
5
6
|
/**
|
|
6
7
|
* Returns clone of `obj` with only `props` preserved.
|
|
7
8
|
* Opposite of Omit.
|
|
@@ -93,18 +94,16 @@ exports._filterObject = _filterObject;
|
|
|
93
94
|
* 'pebbles': { 'user': 'pebbles', 'age': 1 }
|
|
94
95
|
* }
|
|
95
96
|
*
|
|
96
|
-
*
|
|
97
|
+
* _mapValues(users, (_key, value) => value.age)
|
|
97
98
|
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
|
|
98
99
|
*
|
|
99
|
-
*
|
|
100
|
-
* _.mapValues(users, 'age')
|
|
101
|
-
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
|
|
100
|
+
* To skip some key-value pairs - use _mapObject instead.
|
|
102
101
|
*/
|
|
103
102
|
function _mapValues(obj, mapper, mutate = false) {
|
|
104
|
-
return
|
|
103
|
+
return (0, types_1._objectEntries)(obj).reduce((map, [k, v]) => {
|
|
105
104
|
map[k] = mapper(k, v, obj);
|
|
106
105
|
return map;
|
|
107
|
-
},
|
|
106
|
+
}, mutate ? obj : {});
|
|
108
107
|
}
|
|
109
108
|
exports._mapValues = _mapValues;
|
|
110
109
|
/**
|
|
@@ -112,9 +111,11 @@ exports._mapValues = _mapValues;
|
|
|
112
111
|
* // => { 'a1': 1, 'b2': 2 }
|
|
113
112
|
*
|
|
114
113
|
* Does not support `mutate` flag.
|
|
114
|
+
*
|
|
115
|
+
* To skip some key-value pairs - use _mapObject instead.
|
|
115
116
|
*/
|
|
116
117
|
function _mapKeys(obj, mapper) {
|
|
117
|
-
return
|
|
118
|
+
return (0, types_1._objectEntries)(obj).reduce((map, [k, v]) => {
|
|
118
119
|
map[mapper(k, v, obj)] = v;
|
|
119
120
|
return map;
|
|
120
121
|
}, {});
|
|
@@ -132,15 +133,14 @@ exports._mapKeys = _mapKeys;
|
|
|
132
133
|
* 0 - key of returned object (string)
|
|
133
134
|
* 1 - value of returned object (any)
|
|
134
135
|
*
|
|
135
|
-
* If predicate returns
|
|
136
|
+
* If predicate returns SKIP symbol - such key/value pair is ignored (filtered out).
|
|
136
137
|
*
|
|
137
138
|
* Non-string keys are passed via String(...)
|
|
138
139
|
*/
|
|
139
140
|
function _mapObject(obj, mapper) {
|
|
140
141
|
return Object.entries(obj).reduce((map, [k, v]) => {
|
|
141
|
-
const r = mapper(k, v, obj)
|
|
142
|
-
if (r
|
|
143
|
-
;
|
|
142
|
+
const r = mapper(k, v, obj);
|
|
143
|
+
if (r !== types_1.SKIP) {
|
|
144
144
|
map[r[0]] = r[1];
|
|
145
145
|
}
|
|
146
146
|
return map;
|
|
@@ -367,3 +367,20 @@ function _has(obj, path) {
|
|
|
367
367
|
return v !== undefined && v !== null;
|
|
368
368
|
}
|
|
369
369
|
exports._has = _has;
|
|
370
|
+
/**
|
|
371
|
+
* Does Object.freeze recursively for given object.
|
|
372
|
+
*
|
|
373
|
+
* Based on: https://github.com/substack/deep-freeze/blob/master/index.js
|
|
374
|
+
*/
|
|
375
|
+
function _deepFreeze(o) {
|
|
376
|
+
Object.freeze(o);
|
|
377
|
+
Object.getOwnPropertyNames(o).forEach(prop => {
|
|
378
|
+
if (o.hasOwnProperty(prop) && // eslint-disable-line no-prototype-builtins
|
|
379
|
+
o[prop] !== null &&
|
|
380
|
+
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
|
381
|
+
!Object.isFrozen(o[prop])) {
|
|
382
|
+
_deepFreeze(o[prop]);
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
exports._deepFreeze = _deepFreeze;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AppError } from '../error/app.error';
|
|
2
|
-
import type { ErrorData
|
|
1
|
+
import { AppError, AppErrorOptions } from '../error/app.error';
|
|
2
|
+
import type { ErrorData } from '../error/error.model';
|
|
3
3
|
import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
|
|
4
4
|
export declare class TimeoutError extends AppError {
|
|
5
|
-
constructor(message: string, data?:
|
|
5
|
+
constructor(message: string, data?: ErrorData, opt?: AppErrorOptions);
|
|
6
6
|
}
|
|
7
7
|
export interface PTimeoutOptions {
|
|
8
8
|
/**
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -4,8 +4,8 @@ exports.pTimeout = exports.pTimeoutFn = exports.TimeoutError = void 0;
|
|
|
4
4
|
const app_error_1 = require("../error/app.error");
|
|
5
5
|
const error_util_1 = require("../error/error.util");
|
|
6
6
|
class TimeoutError extends app_error_1.AppError {
|
|
7
|
-
constructor(message, data
|
|
8
|
-
super(message, data,
|
|
7
|
+
constructor(message, data, opt) {
|
|
8
|
+
super(message, data, { ...opt, name: 'TimeoutError' });
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
exports.TimeoutError = TimeoutError;
|
package/dist/types.d.ts
CHANGED
|
@@ -120,7 +120,7 @@ export type ValuesOf<T extends readonly any[]> = T[number];
|
|
|
120
120
|
*/
|
|
121
121
|
export type ValueOf<T> = T[keyof T];
|
|
122
122
|
export type KeyValueTuple<K, V> = [key: K, value: V];
|
|
123
|
-
export type ObjectMapper<OBJ, OUT> = (key:
|
|
123
|
+
export type ObjectMapper<OBJ, OUT> = (key: keyof OBJ, value: Exclude<OBJ[keyof OBJ], undefined>, obj: OBJ) => OUT;
|
|
124
124
|
export type ObjectPredicate<OBJ> = (key: keyof OBJ, value: Exclude<OBJ[keyof OBJ], undefined>, obj: OBJ) => boolean;
|
|
125
125
|
/**
|
|
126
126
|
* Allows to identify instance of Class by `instanceId`.
|
|
@@ -185,10 +185,17 @@ export declare const _stringMapValues: <T>(m: StringMap<T>) => T[];
|
|
|
185
185
|
*/
|
|
186
186
|
export declare const _stringMapEntries: <T>(m: StringMap<T>) => [k: string, v: T][];
|
|
187
187
|
/**
|
|
188
|
-
*
|
|
188
|
+
* Alias of `Object.keys`, but returns keys typed as `keyof T`, not as just `string`.
|
|
189
189
|
* This is how TypeScript should work, actually.
|
|
190
190
|
*/
|
|
191
191
|
export declare const _objectKeys: <T extends AnyObject>(obj: T) => (keyof T)[];
|
|
192
|
+
/**
|
|
193
|
+
* Alias of `Object.entries`, but returns better-typed output.
|
|
194
|
+
*
|
|
195
|
+
* So e.g you can use _objectEntries(obj).map([k, v] => {})
|
|
196
|
+
* and `k` will be `keyof obj` instead of generic `string`.
|
|
197
|
+
*/
|
|
198
|
+
export declare const _objectEntries: <T extends AnyObject>(obj: T) => [k: keyof T, v: T[keyof T]][];
|
|
192
199
|
export type NullishValue = null | undefined;
|
|
193
200
|
export type FalsyValue = false | '' | 0 | null | undefined;
|
|
194
201
|
/**
|
package/dist/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._objectAssign = exports._typeCast = exports._objectKeys = exports._stringMapEntries = exports._stringMapValues = exports._passNothingPredicate = exports._passthroughPredicate = exports._noop = exports._passUndefinedMapper = exports._passthroughMapper = exports.SKIP = exports.END = void 0;
|
|
3
|
+
exports._objectAssign = exports._typeCast = exports._objectEntries = exports._objectKeys = exports._stringMapEntries = exports._stringMapValues = exports._passNothingPredicate = exports._passthroughPredicate = exports._noop = exports._passUndefinedMapper = exports._passthroughMapper = exports.SKIP = exports.END = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Symbol to indicate END of Sequence.
|
|
6
6
|
*/
|
|
@@ -33,10 +33,17 @@ exports._stringMapValues = Object.values;
|
|
|
33
33
|
*/
|
|
34
34
|
exports._stringMapEntries = Object.entries;
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* Alias of `Object.keys`, but returns keys typed as `keyof T`, not as just `string`.
|
|
37
37
|
* This is how TypeScript should work, actually.
|
|
38
38
|
*/
|
|
39
39
|
exports._objectKeys = Object.keys;
|
|
40
|
+
/**
|
|
41
|
+
* Alias of `Object.entries`, but returns better-typed output.
|
|
42
|
+
*
|
|
43
|
+
* So e.g you can use _objectEntries(obj).map([k, v] => {})
|
|
44
|
+
* and `k` will be `keyof obj` instead of generic `string`.
|
|
45
|
+
*/
|
|
46
|
+
exports._objectEntries = Object.entries;
|
|
40
47
|
/**
|
|
41
48
|
* Utility function that helps to cast *existing variable* to needed type T.
|
|
42
49
|
*
|