@naturalcycles/js-lib 14.250.0 → 14.252.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/datetime/localDate.js +1 -0
- package/dist/decorators/debounce.decorator.js +2 -2
- package/dist/decorators/retry.decorator.js +1 -1
- package/dist/define.js +1 -1
- package/dist/error/tryCatch.js +1 -1
- package/dist/object/object.util.d.ts +10 -0
- package/dist/object/object.util.js +40 -15
- package/dist/object/sortObject.js +10 -7
- package/dist/object/sortObjectDeep.js +6 -9
- package/dist/promise/pFilter.js +1 -1
- package/dist/promise/pTimeout.js +1 -1
- package/dist/string/safeJsonStringify.js +1 -1
- package/dist-esm/datetime/localDate.js +1 -0
- package/dist-esm/decorators/debounce.decorator.js +2 -2
- package/dist-esm/decorators/retry.decorator.js +1 -1
- package/dist-esm/define.js +1 -1
- package/dist-esm/error/tryCatch.js +1 -1
- package/dist-esm/object/object.util.js +38 -15
- package/dist-esm/object/sortObject.js +10 -7
- package/dist-esm/object/sortObjectDeep.js +6 -9
- package/dist-esm/promise/pFilter.js +1 -1
- package/dist-esm/promise/pTimeout.js +1 -1
- package/dist-esm/string/safeJsonStringify.js +1 -1
- package/package.json +2 -2
- package/src/array/range.ts +1 -1
- package/src/datetime/localDate.ts +1 -0
- package/src/decorators/debounce.decorator.ts +2 -2
- package/src/decorators/retry.decorator.ts +1 -1
- package/src/define.ts +1 -1
- package/src/error/tryCatch.ts +1 -1
- package/src/object/object.util.ts +46 -12
- package/src/object/sortObject.ts +9 -7
- package/src/object/sortObjectDeep.ts +6 -12
- package/src/promise/pFilter.ts +1 -1
- package/src/promise/pTimeout.ts +1 -1
- package/src/string/safeJsonStringify.ts +1 -1
|
@@ -246,6 +246,7 @@ class LocalDate {
|
|
|
246
246
|
return this.plus(-num, 'year');
|
|
247
247
|
}
|
|
248
248
|
plus(num, unit, mutate = false) {
|
|
249
|
+
num = Math.floor(num); // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
|
|
249
250
|
let { day, month, year } = this;
|
|
250
251
|
if (unit === 'week') {
|
|
251
252
|
num *= 7;
|
|
@@ -5,7 +5,7 @@ exports._Throttle = _Throttle;
|
|
|
5
5
|
const debounce_1 = require("./debounce");
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
7
7
|
function _Debounce(wait, opt = {}) {
|
|
8
|
-
return (
|
|
8
|
+
return (_target, _key, descriptor) => {
|
|
9
9
|
const originalFn = descriptor.value;
|
|
10
10
|
descriptor.value = (0, debounce_1._debounce)(originalFn, wait, opt);
|
|
11
11
|
return descriptor;
|
|
@@ -13,7 +13,7 @@ function _Debounce(wait, opt = {}) {
|
|
|
13
13
|
}
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
15
15
|
function _Throttle(wait, opt = {}) {
|
|
16
|
-
return (
|
|
16
|
+
return (_target, _key, descriptor) => {
|
|
17
17
|
const originalFn = descriptor.value;
|
|
18
18
|
descriptor.value = (0, debounce_1._throttle)(originalFn, wait, opt);
|
|
19
19
|
return descriptor;
|
|
@@ -4,7 +4,7 @@ exports._Retry = _Retry;
|
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
6
6
|
function _Retry(opt = {}) {
|
|
7
|
-
return (
|
|
7
|
+
return (_target, _key, descriptor) => {
|
|
8
8
|
const originalFn = descriptor.value;
|
|
9
9
|
descriptor.value = (0, __1.pRetryFn)(originalFn, opt);
|
|
10
10
|
return descriptor;
|
package/dist/define.js
CHANGED
|
@@ -97,7 +97,7 @@ function _defineProperty(obj, prop, pd) {
|
|
|
97
97
|
* See _defineProperty for exact defaults definition.
|
|
98
98
|
*/
|
|
99
99
|
function _defineProps(obj, props) {
|
|
100
|
-
return Object.defineProperties(obj, (0, object_util_1._mapValues)(props, (
|
|
100
|
+
return Object.defineProperties(obj, (0, object_util_1._mapValues)(props, (_k, pd) => ({
|
|
101
101
|
writable: true,
|
|
102
102
|
configurable: true,
|
|
103
103
|
enumerable: true,
|
package/dist/error/tryCatch.js
CHANGED
|
@@ -38,7 +38,7 @@ function _tryCatch(fn, opt = {}) {
|
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
41
|
-
const _TryCatch = (opt = {}) => (
|
|
41
|
+
const _TryCatch = (opt = {}) => (_target, _key, descriptor) => {
|
|
42
42
|
const originalFn = descriptor.value;
|
|
43
43
|
descriptor.value = _tryCatch(originalFn, opt);
|
|
44
44
|
return descriptor;
|
|
@@ -4,11 +4,21 @@ import { AnyObject, KeyValueTuple, ObjectMapper, ObjectPredicate, Reviver, SKIP,
|
|
|
4
4
|
* Opposite of Omit.
|
|
5
5
|
*/
|
|
6
6
|
export declare function _pick<T extends AnyObject, K extends keyof T>(obj: T, props: readonly K[], mutate?: boolean): T;
|
|
7
|
+
/**
|
|
8
|
+
* Sets all properties of an object except passed ones to `undefined`.
|
|
9
|
+
* This is a more performant alternative to `_pick` that does picking/deleting.
|
|
10
|
+
*/
|
|
11
|
+
export declare function _pickWithUndefined<T extends AnyObject, K extends keyof T>(obj: T, props: readonly K[], mutate?: boolean): T;
|
|
7
12
|
/**
|
|
8
13
|
* Returns clone of `obj` with `props` omitted.
|
|
9
14
|
* Opposite of Pick.
|
|
10
15
|
*/
|
|
11
16
|
export declare function _omit<T extends AnyObject, K extends keyof T>(obj: T, props: readonly K[], mutate?: boolean): T;
|
|
17
|
+
/**
|
|
18
|
+
* Sets all passed properties of an object to `undefined`.
|
|
19
|
+
* This is a more performant alternative to `_omit` that does picking/deleting.
|
|
20
|
+
*/
|
|
21
|
+
export declare function _omitWithUndefined<T extends AnyObject, K extends keyof T>(obj: T, props: readonly K[], mutate?: boolean): T;
|
|
12
22
|
/**
|
|
13
23
|
* Returns object with filtered keys from `props` array.
|
|
14
24
|
* E.g:
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._pick = _pick;
|
|
4
|
+
exports._pickWithUndefined = _pickWithUndefined;
|
|
4
5
|
exports._omit = _omit;
|
|
6
|
+
exports._omitWithUndefined = _omitWithUndefined;
|
|
5
7
|
exports._mask = _mask;
|
|
6
8
|
exports._filterFalsyValues = _filterFalsyValues;
|
|
7
9
|
exports._filterNullishValues = _filterNullishValues;
|
|
@@ -35,17 +37,30 @@ const types_1 = require("../types");
|
|
|
35
37
|
function _pick(obj, props, mutate = false) {
|
|
36
38
|
if (mutate) {
|
|
37
39
|
// Start as original object (mutable), DELETE properties that are not whitelisted
|
|
38
|
-
for (const
|
|
39
|
-
if (!props.includes(
|
|
40
|
-
delete obj[
|
|
40
|
+
for (const k of Object.keys(obj)) {
|
|
41
|
+
if (!props.includes(k))
|
|
42
|
+
delete obj[k];
|
|
41
43
|
}
|
|
42
44
|
return obj;
|
|
43
45
|
}
|
|
44
46
|
// Start as empty object, pick/add needed properties
|
|
45
47
|
const r = {};
|
|
46
|
-
for (const
|
|
47
|
-
if (
|
|
48
|
-
r[
|
|
48
|
+
for (const k of props) {
|
|
49
|
+
if (k in obj)
|
|
50
|
+
r[k] = obj[k];
|
|
51
|
+
}
|
|
52
|
+
return r;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Sets all properties of an object except passed ones to `undefined`.
|
|
56
|
+
* This is a more performant alternative to `_pick` that does picking/deleting.
|
|
57
|
+
*/
|
|
58
|
+
function _pickWithUndefined(obj, props, mutate = false) {
|
|
59
|
+
const r = mutate ? obj : { ...obj };
|
|
60
|
+
for (const k of Object.keys(r)) {
|
|
61
|
+
if (!props.includes(k)) {
|
|
62
|
+
r[k] = undefined;
|
|
63
|
+
}
|
|
49
64
|
}
|
|
50
65
|
return r;
|
|
51
66
|
}
|
|
@@ -55,15 +70,26 @@ function _pick(obj, props, mutate = false) {
|
|
|
55
70
|
*/
|
|
56
71
|
function _omit(obj, props, mutate = false) {
|
|
57
72
|
if (mutate) {
|
|
58
|
-
for (const
|
|
59
|
-
delete obj[
|
|
73
|
+
for (const k of props) {
|
|
74
|
+
delete obj[k];
|
|
60
75
|
}
|
|
61
76
|
return obj;
|
|
62
77
|
}
|
|
63
78
|
const r = {};
|
|
64
|
-
for (const
|
|
65
|
-
if (!props.includes(
|
|
66
|
-
r[
|
|
79
|
+
for (const k of Object.keys(obj)) {
|
|
80
|
+
if (!props.includes(k))
|
|
81
|
+
r[k] = obj[k];
|
|
82
|
+
}
|
|
83
|
+
return r;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sets all passed properties of an object to `undefined`.
|
|
87
|
+
* This is a more performant alternative to `_omit` that does picking/deleting.
|
|
88
|
+
*/
|
|
89
|
+
function _omitWithUndefined(obj, props, mutate = false) {
|
|
90
|
+
const r = mutate ? obj : { ...obj };
|
|
91
|
+
for (const k of props) {
|
|
92
|
+
r[k] = undefined;
|
|
67
93
|
}
|
|
68
94
|
return r;
|
|
69
95
|
}
|
|
@@ -77,8 +103,8 @@ function _omit(obj, props, mutate = false) {
|
|
|
77
103
|
*/
|
|
78
104
|
function _mask(obj, props, mutate = false) {
|
|
79
105
|
const r = mutate ? obj : _deepCopy(obj);
|
|
80
|
-
for (const
|
|
81
|
-
_unset(r,
|
|
106
|
+
for (const k of props) {
|
|
107
|
+
_unset(r, k);
|
|
82
108
|
}
|
|
83
109
|
return r;
|
|
84
110
|
}
|
|
@@ -282,7 +308,6 @@ function _unset(obj, prop) {
|
|
|
282
308
|
if (!(0, is_util_1._isObject)(obj)) {
|
|
283
309
|
return;
|
|
284
310
|
}
|
|
285
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
286
311
|
if (obj.hasOwnProperty(prop)) {
|
|
287
312
|
delete obj[prop];
|
|
288
313
|
return;
|
|
@@ -400,7 +425,7 @@ function _has(obj, path) {
|
|
|
400
425
|
function _deepFreeze(o) {
|
|
401
426
|
Object.freeze(o);
|
|
402
427
|
Object.getOwnPropertyNames(o).forEach(prop => {
|
|
403
|
-
if (o.hasOwnProperty(prop) &&
|
|
428
|
+
if (o.hasOwnProperty(prop) &&
|
|
404
429
|
o[prop] !== null &&
|
|
405
430
|
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
|
406
431
|
!Object.isFrozen(o[prop])) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._sortObject = _sortObject;
|
|
4
|
-
const index_1 = require("../index");
|
|
5
4
|
/**
|
|
6
5
|
* Returns new object with keys sorder in the given order.
|
|
7
6
|
* All keys that are not listed in `keyOrder` go last.
|
|
@@ -9,13 +8,17 @@ const index_1 = require("../index");
|
|
|
9
8
|
*/
|
|
10
9
|
function _sortObject(obj, keyOrder) {
|
|
11
10
|
const r = {};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
// First, go over ordered keys
|
|
12
|
+
for (const k of keyOrder) {
|
|
13
|
+
if (k in obj) {
|
|
14
|
+
r[k] = obj[k];
|
|
15
15
|
}
|
|
16
|
-
}
|
|
17
|
-
|
|
16
|
+
}
|
|
17
|
+
// Second, go over all other keys
|
|
18
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
19
|
+
if (keyOrder.includes(k))
|
|
20
|
+
continue;
|
|
18
21
|
r[k] = v;
|
|
19
|
-
}
|
|
22
|
+
}
|
|
20
23
|
return r;
|
|
21
24
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports._sortObjectDeep = _sortObjectDeep;
|
|
4
|
-
const __1 = require("..");
|
|
5
4
|
/**
|
|
6
5
|
* based on: https://github.com/IndigoUnited/js-deep-sort-object
|
|
7
6
|
*/
|
|
@@ -10,14 +9,12 @@ function _sortObjectDeep(o) {
|
|
|
10
9
|
if (Array.isArray(o)) {
|
|
11
10
|
return o.map(_sortObjectDeep);
|
|
12
11
|
}
|
|
13
|
-
if (
|
|
14
|
-
const
|
|
15
|
-
Object.keys(o)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
return out;
|
|
12
|
+
if (o && typeof o === 'object') {
|
|
13
|
+
const r = {};
|
|
14
|
+
for (const k of Object.keys(o).sort((a, b) => a.localeCompare(b))) {
|
|
15
|
+
r[k] = _sortObjectDeep(o[k]);
|
|
16
|
+
}
|
|
17
|
+
return r;
|
|
21
18
|
}
|
|
22
19
|
return o;
|
|
23
20
|
}
|
package/dist/promise/pFilter.js
CHANGED
|
@@ -4,5 +4,5 @@ exports.pFilter = pFilter;
|
|
|
4
4
|
async function pFilter(iterable, filterFn) {
|
|
5
5
|
const items = [...iterable];
|
|
6
6
|
const predicates = await Promise.all(items.map((item, i) => filterFn(item, i)));
|
|
7
|
-
return items.filter((
|
|
7
|
+
return items.filter((_item, i) => predicates[i]);
|
|
8
8
|
}
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -33,7 +33,7 @@ async function pTimeout(fn, opt) {
|
|
|
33
33
|
}
|
|
34
34
|
const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
|
|
35
35
|
const fakeError = opt.fakeError || new Error('TimeoutError');
|
|
36
|
-
//
|
|
36
|
+
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: ok
|
|
37
37
|
return await new Promise(async (resolve, reject) => {
|
|
38
38
|
// Prepare the timeout timer
|
|
39
39
|
const timer = setTimeout(() => {
|
|
@@ -20,7 +20,7 @@ function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
|
|
|
20
20
|
function serializer(replacer, cycleReplacer) {
|
|
21
21
|
const stack = [];
|
|
22
22
|
const keys = [];
|
|
23
|
-
cycleReplacer ??= (
|
|
23
|
+
cycleReplacer ??= (_key, value) => {
|
|
24
24
|
if (stack[0] === value)
|
|
25
25
|
return '[Circular ~]';
|
|
26
26
|
return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
|
|
@@ -243,6 +243,7 @@ export class LocalDate {
|
|
|
243
243
|
return this.plus(-num, 'year');
|
|
244
244
|
}
|
|
245
245
|
plus(num, unit, mutate = false) {
|
|
246
|
+
num = Math.floor(num); // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
|
|
246
247
|
let { day, month, year } = this;
|
|
247
248
|
if (unit === 'week') {
|
|
248
249
|
num *= 7;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _debounce, _throttle } from './debounce';
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3
3
|
export function _Debounce(wait, opt = {}) {
|
|
4
|
-
return (
|
|
4
|
+
return (_target, _key, descriptor) => {
|
|
5
5
|
const originalFn = descriptor.value;
|
|
6
6
|
descriptor.value = _debounce(originalFn, wait, opt);
|
|
7
7
|
return descriptor;
|
|
@@ -9,7 +9,7 @@ export function _Debounce(wait, opt = {}) {
|
|
|
9
9
|
}
|
|
10
10
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
11
11
|
export function _Throttle(wait, opt = {}) {
|
|
12
|
-
return (
|
|
12
|
+
return (_target, _key, descriptor) => {
|
|
13
13
|
const originalFn = descriptor.value;
|
|
14
14
|
descriptor.value = _throttle(originalFn, wait, opt);
|
|
15
15
|
return descriptor;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { pRetryFn } from '..';
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3
3
|
export function _Retry(opt = {}) {
|
|
4
|
-
return (
|
|
4
|
+
return (_target, _key, descriptor) => {
|
|
5
5
|
const originalFn = descriptor.value;
|
|
6
6
|
descriptor.value = pRetryFn(originalFn, opt);
|
|
7
7
|
return descriptor;
|
package/dist-esm/define.js
CHANGED
|
@@ -89,7 +89,7 @@ export function _defineProperty(obj, prop, pd) {
|
|
|
89
89
|
* See _defineProperty for exact defaults definition.
|
|
90
90
|
*/
|
|
91
91
|
export function _defineProps(obj, props) {
|
|
92
|
-
return Object.defineProperties(obj, _mapValues(props, (
|
|
92
|
+
return Object.defineProperties(obj, _mapValues(props, (_k, pd) => ({
|
|
93
93
|
writable: true,
|
|
94
94
|
configurable: true,
|
|
95
95
|
enumerable: true,
|
|
@@ -34,7 +34,7 @@ export function _tryCatch(fn, opt = {}) {
|
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
37
|
-
export const _TryCatch = (opt = {}) => (
|
|
37
|
+
export const _TryCatch = (opt = {}) => (_target, _key, descriptor) => {
|
|
38
38
|
const originalFn = descriptor.value;
|
|
39
39
|
descriptor.value = _tryCatch(originalFn, opt);
|
|
40
40
|
return descriptor;
|
|
@@ -7,17 +7,30 @@ import { _objectEntries, SKIP, } from '../types';
|
|
|
7
7
|
export function _pick(obj, props, mutate = false) {
|
|
8
8
|
if (mutate) {
|
|
9
9
|
// Start as original object (mutable), DELETE properties that are not whitelisted
|
|
10
|
-
for (const
|
|
11
|
-
if (!props.includes(
|
|
12
|
-
delete obj[
|
|
10
|
+
for (const k of Object.keys(obj)) {
|
|
11
|
+
if (!props.includes(k))
|
|
12
|
+
delete obj[k];
|
|
13
13
|
}
|
|
14
14
|
return obj;
|
|
15
15
|
}
|
|
16
16
|
// Start as empty object, pick/add needed properties
|
|
17
17
|
const r = {};
|
|
18
|
-
for (const
|
|
19
|
-
if (
|
|
20
|
-
r[
|
|
18
|
+
for (const k of props) {
|
|
19
|
+
if (k in obj)
|
|
20
|
+
r[k] = obj[k];
|
|
21
|
+
}
|
|
22
|
+
return r;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Sets all properties of an object except passed ones to `undefined`.
|
|
26
|
+
* This is a more performant alternative to `_pick` that does picking/deleting.
|
|
27
|
+
*/
|
|
28
|
+
export function _pickWithUndefined(obj, props, mutate = false) {
|
|
29
|
+
const r = mutate ? obj : { ...obj };
|
|
30
|
+
for (const k of Object.keys(r)) {
|
|
31
|
+
if (!props.includes(k)) {
|
|
32
|
+
r[k] = undefined;
|
|
33
|
+
}
|
|
21
34
|
}
|
|
22
35
|
return r;
|
|
23
36
|
}
|
|
@@ -27,15 +40,26 @@ export function _pick(obj, props, mutate = false) {
|
|
|
27
40
|
*/
|
|
28
41
|
export function _omit(obj, props, mutate = false) {
|
|
29
42
|
if (mutate) {
|
|
30
|
-
for (const
|
|
31
|
-
delete obj[
|
|
43
|
+
for (const k of props) {
|
|
44
|
+
delete obj[k];
|
|
32
45
|
}
|
|
33
46
|
return obj;
|
|
34
47
|
}
|
|
35
48
|
const r = {};
|
|
36
|
-
for (const
|
|
37
|
-
if (!props.includes(
|
|
38
|
-
r[
|
|
49
|
+
for (const k of Object.keys(obj)) {
|
|
50
|
+
if (!props.includes(k))
|
|
51
|
+
r[k] = obj[k];
|
|
52
|
+
}
|
|
53
|
+
return r;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sets all passed properties of an object to `undefined`.
|
|
57
|
+
* This is a more performant alternative to `_omit` that does picking/deleting.
|
|
58
|
+
*/
|
|
59
|
+
export function _omitWithUndefined(obj, props, mutate = false) {
|
|
60
|
+
const r = mutate ? obj : { ...obj };
|
|
61
|
+
for (const k of props) {
|
|
62
|
+
r[k] = undefined;
|
|
39
63
|
}
|
|
40
64
|
return r;
|
|
41
65
|
}
|
|
@@ -49,8 +73,8 @@ export function _omit(obj, props, mutate = false) {
|
|
|
49
73
|
*/
|
|
50
74
|
export function _mask(obj, props, mutate = false) {
|
|
51
75
|
const r = mutate ? obj : _deepCopy(obj);
|
|
52
|
-
for (const
|
|
53
|
-
_unset(r,
|
|
76
|
+
for (const k of props) {
|
|
77
|
+
_unset(r, k);
|
|
54
78
|
}
|
|
55
79
|
return r;
|
|
56
80
|
}
|
|
@@ -255,7 +279,6 @@ export function _unset(obj, prop) {
|
|
|
255
279
|
if (!_isObject(obj)) {
|
|
256
280
|
return;
|
|
257
281
|
}
|
|
258
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
259
282
|
if (obj.hasOwnProperty(prop)) {
|
|
260
283
|
delete obj[prop];
|
|
261
284
|
return;
|
|
@@ -373,7 +396,7 @@ export function _has(obj, path) {
|
|
|
373
396
|
export function _deepFreeze(o) {
|
|
374
397
|
Object.freeze(o);
|
|
375
398
|
Object.getOwnPropertyNames(o).forEach(prop => {
|
|
376
|
-
if (o.hasOwnProperty(prop) &&
|
|
399
|
+
if (o.hasOwnProperty(prop) &&
|
|
377
400
|
o[prop] !== null &&
|
|
378
401
|
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
|
379
402
|
!Object.isFrozen(o[prop])) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { _omit } from '../index';
|
|
2
1
|
/**
|
|
3
2
|
* Returns new object with keys sorder in the given order.
|
|
4
3
|
* All keys that are not listed in `keyOrder` go last.
|
|
@@ -6,13 +5,17 @@ import { _omit } from '../index';
|
|
|
6
5
|
*/
|
|
7
6
|
export function _sortObject(obj, keyOrder) {
|
|
8
7
|
const r = {};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
// First, go over ordered keys
|
|
9
|
+
for (const k of keyOrder) {
|
|
10
|
+
if (k in obj) {
|
|
11
|
+
r[k] = obj[k];
|
|
12
12
|
}
|
|
13
|
-
}
|
|
14
|
-
|
|
13
|
+
}
|
|
14
|
+
// Second, go over all other keys
|
|
15
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
16
|
+
if (keyOrder.includes(k))
|
|
17
|
+
continue;
|
|
15
18
|
r[k] = v;
|
|
16
|
-
}
|
|
19
|
+
}
|
|
17
20
|
return r;
|
|
18
21
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { _isObject } from '..';
|
|
2
1
|
/**
|
|
3
2
|
* based on: https://github.com/IndigoUnited/js-deep-sort-object
|
|
4
3
|
*/
|
|
@@ -7,14 +6,12 @@ export function _sortObjectDeep(o) {
|
|
|
7
6
|
if (Array.isArray(o)) {
|
|
8
7
|
return o.map(_sortObjectDeep);
|
|
9
8
|
}
|
|
10
|
-
if (
|
|
11
|
-
const
|
|
12
|
-
Object.keys(o)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
return out;
|
|
9
|
+
if (o && typeof o === 'object') {
|
|
10
|
+
const r = {};
|
|
11
|
+
for (const k of Object.keys(o).sort((a, b) => a.localeCompare(b))) {
|
|
12
|
+
r[k] = _sortObjectDeep(o[k]);
|
|
13
|
+
}
|
|
14
|
+
return r;
|
|
18
15
|
}
|
|
19
16
|
return o;
|
|
20
17
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export async function pFilter(iterable, filterFn) {
|
|
2
2
|
const items = [...iterable];
|
|
3
3
|
const predicates = await Promise.all(items.map((item, i) => filterFn(item, i)));
|
|
4
|
-
return items.filter((
|
|
4
|
+
return items.filter((_item, i) => predicates[i]);
|
|
5
5
|
}
|
|
@@ -29,7 +29,7 @@ export async function pTimeout(fn, opt) {
|
|
|
29
29
|
}
|
|
30
30
|
const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt;
|
|
31
31
|
const fakeError = opt.fakeError || new Error('TimeoutError');
|
|
32
|
-
//
|
|
32
|
+
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: ok
|
|
33
33
|
return await new Promise(async (resolve, reject) => {
|
|
34
34
|
// Prepare the timeout timer
|
|
35
35
|
const timer = setTimeout(() => {
|
|
@@ -17,7 +17,7 @@ export function _safeJsonStringify(obj, replacer, spaces, cycleReplacer) {
|
|
|
17
17
|
function serializer(replacer, cycleReplacer) {
|
|
18
18
|
const stack = [];
|
|
19
19
|
const keys = [];
|
|
20
|
-
cycleReplacer ?? (cycleReplacer = (
|
|
20
|
+
cycleReplacer ?? (cycleReplacer = (_key, value) => {
|
|
21
21
|
if (stack[0] === value)
|
|
22
22
|
return '[Circular ~]';
|
|
23
23
|
return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.252.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky",
|
|
6
6
|
"build": "dev-lib build-esm-cjs",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"types": "dist/index.d.ts",
|
|
47
47
|
"sideEffects": false,
|
|
48
48
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
49
|
+
"node": ">=20.13.0"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"provenance": true,
|
package/src/array/range.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AsyncIterable2 } from '../iter/asyncIterable2'
|
|
2
2
|
import { Iterable2 } from '../iter/iterable2'
|
|
3
3
|
|
|
4
|
-
/* eslint-disable
|
|
4
|
+
/* eslint-disable unicorn/no-new-array */
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Returns an array with ranges from `from` up to (but not including) `to`.
|
|
@@ -287,6 +287,7 @@ export class LocalDate {
|
|
|
287
287
|
}
|
|
288
288
|
|
|
289
289
|
plus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
290
|
+
num = Math.floor(num) // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
|
|
290
291
|
let { day, month, year } = this
|
|
291
292
|
|
|
292
293
|
if (unit === 'week') {
|
|
@@ -3,7 +3,7 @@ import { _debounce, _throttle } from './debounce'
|
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
5
5
|
export function _Debounce(wait: number, opt: DebounceOptions = {}): MethodDecorator {
|
|
6
|
-
return (
|
|
6
|
+
return (_target, _key, descriptor) => {
|
|
7
7
|
const originalFn = descriptor.value
|
|
8
8
|
descriptor.value = _debounce(originalFn as any, wait, opt)
|
|
9
9
|
return descriptor
|
|
@@ -12,7 +12,7 @@ export function _Debounce(wait: number, opt: DebounceOptions = {}): MethodDecora
|
|
|
12
12
|
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
14
|
export function _Throttle(wait: number, opt: ThrottleOptions = {}): MethodDecorator {
|
|
15
|
-
return (
|
|
15
|
+
return (_target, _key, descriptor) => {
|
|
16
16
|
const originalFn = descriptor.value
|
|
17
17
|
descriptor.value = _throttle(originalFn as any, wait, opt)
|
|
18
18
|
return descriptor
|
|
@@ -3,7 +3,7 @@ import { pRetryFn } from '..'
|
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
5
5
|
export function _Retry(opt: PRetryOptions = {}): MethodDecorator {
|
|
6
|
-
return (
|
|
6
|
+
return (_target, _key, descriptor) => {
|
|
7
7
|
const originalFn = descriptor.value
|
|
8
8
|
descriptor.value = pRetryFn(originalFn as any, opt)
|
|
9
9
|
return descriptor
|
package/src/define.ts
CHANGED
package/src/error/tryCatch.ts
CHANGED
|
@@ -67,7 +67,7 @@ export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {
|
|
|
67
67
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
68
68
|
export const _TryCatch =
|
|
69
69
|
(opt: TryCatchOptions = {}): MethodDecorator =>
|
|
70
|
-
(
|
|
70
|
+
(_target, _key, descriptor) => {
|
|
71
71
|
const originalFn = descriptor.value
|
|
72
72
|
descriptor.value = _tryCatch(originalFn as any, opt)
|
|
73
73
|
return descriptor
|
|
@@ -21,15 +21,33 @@ export function _pick<T extends AnyObject, K extends keyof T>(
|
|
|
21
21
|
): T {
|
|
22
22
|
if (mutate) {
|
|
23
23
|
// Start as original object (mutable), DELETE properties that are not whitelisted
|
|
24
|
-
for (const
|
|
25
|
-
if (!props.includes(
|
|
24
|
+
for (const k of Object.keys(obj)) {
|
|
25
|
+
if (!props.includes(k as K)) delete obj[k]
|
|
26
26
|
}
|
|
27
27
|
return obj
|
|
28
28
|
}
|
|
29
29
|
// Start as empty object, pick/add needed properties
|
|
30
30
|
const r = {} as T
|
|
31
|
-
for (const
|
|
32
|
-
if (
|
|
31
|
+
for (const k of props) {
|
|
32
|
+
if (k in obj) r[k] = obj[k]
|
|
33
|
+
}
|
|
34
|
+
return r
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Sets all properties of an object except passed ones to `undefined`.
|
|
39
|
+
* This is a more performant alternative to `_pick` that does picking/deleting.
|
|
40
|
+
*/
|
|
41
|
+
export function _pickWithUndefined<T extends AnyObject, K extends keyof T>(
|
|
42
|
+
obj: T,
|
|
43
|
+
props: readonly K[],
|
|
44
|
+
mutate = false,
|
|
45
|
+
): T {
|
|
46
|
+
const r: T = mutate ? obj : { ...obj }
|
|
47
|
+
for (const k of Object.keys(r)) {
|
|
48
|
+
if (!props.includes(k as K)) {
|
|
49
|
+
r[k as K] = undefined as any
|
|
50
|
+
}
|
|
33
51
|
}
|
|
34
52
|
return r
|
|
35
53
|
}
|
|
@@ -44,15 +62,31 @@ export function _omit<T extends AnyObject, K extends keyof T>(
|
|
|
44
62
|
mutate = false,
|
|
45
63
|
): T {
|
|
46
64
|
if (mutate) {
|
|
47
|
-
for (const
|
|
48
|
-
delete obj[
|
|
65
|
+
for (const k of props) {
|
|
66
|
+
delete obj[k]
|
|
49
67
|
}
|
|
50
68
|
return obj
|
|
51
69
|
}
|
|
52
70
|
|
|
53
71
|
const r = {} as T
|
|
54
|
-
for (const
|
|
55
|
-
if (!props.includes(
|
|
72
|
+
for (const k of Object.keys(obj)) {
|
|
73
|
+
if (!props.includes(k as K)) r[k as K] = obj[k]
|
|
74
|
+
}
|
|
75
|
+
return r
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sets all passed properties of an object to `undefined`.
|
|
80
|
+
* This is a more performant alternative to `_omit` that does picking/deleting.
|
|
81
|
+
*/
|
|
82
|
+
export function _omitWithUndefined<T extends AnyObject, K extends keyof T>(
|
|
83
|
+
obj: T,
|
|
84
|
+
props: readonly K[],
|
|
85
|
+
mutate = false,
|
|
86
|
+
): T {
|
|
87
|
+
const r: T = mutate ? obj : { ...obj }
|
|
88
|
+
for (const k of props) {
|
|
89
|
+
r[k] = undefined as any
|
|
56
90
|
}
|
|
57
91
|
return r
|
|
58
92
|
}
|
|
@@ -67,8 +101,8 @@ export function _omit<T extends AnyObject, K extends keyof T>(
|
|
|
67
101
|
*/
|
|
68
102
|
export function _mask<T extends AnyObject>(obj: T, props: string[], mutate = false): T {
|
|
69
103
|
const r = mutate ? obj : _deepCopy(obj)
|
|
70
|
-
for (const
|
|
71
|
-
_unset(r,
|
|
104
|
+
for (const k of props) {
|
|
105
|
+
_unset(r, k)
|
|
72
106
|
}
|
|
73
107
|
return r
|
|
74
108
|
}
|
|
@@ -298,7 +332,7 @@ export function _unset<T extends AnyObject>(obj: T, prop: string): void {
|
|
|
298
332
|
if (!_isObject(obj)) {
|
|
299
333
|
return
|
|
300
334
|
}
|
|
301
|
-
|
|
335
|
+
|
|
302
336
|
if (obj.hasOwnProperty(prop)) {
|
|
303
337
|
delete obj[prop]
|
|
304
338
|
return
|
|
@@ -436,7 +470,7 @@ export function _deepFreeze(o: any): void {
|
|
|
436
470
|
|
|
437
471
|
Object.getOwnPropertyNames(o).forEach(prop => {
|
|
438
472
|
if (
|
|
439
|
-
o.hasOwnProperty(prop) &&
|
|
473
|
+
o.hasOwnProperty(prop) &&
|
|
440
474
|
o[prop] !== null &&
|
|
441
475
|
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
|
442
476
|
!Object.isFrozen(o[prop])
|
package/src/object/sortObject.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { AnyObject } from '../index'
|
|
2
|
-
import { _omit } from '../index'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Returns new object with keys sorder in the given order.
|
|
@@ -9,15 +8,18 @@ import { _omit } from '../index'
|
|
|
9
8
|
export function _sortObject<T extends AnyObject>(obj: T, keyOrder: (keyof T)[]): T {
|
|
10
9
|
const r = {} as T
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
// First, go over ordered keys
|
|
12
|
+
for (const k of keyOrder) {
|
|
13
|
+
if (k in obj) {
|
|
14
|
+
r[k] = obj[k]
|
|
15
15
|
}
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
// Second, go over all other keys
|
|
19
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
20
|
+
if (keyOrder.includes(k)) continue
|
|
19
21
|
r[k as keyof T] = v
|
|
20
|
-
}
|
|
22
|
+
}
|
|
21
23
|
|
|
22
24
|
return r
|
|
23
25
|
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { _isObject } from '..'
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* based on: https://github.com/IndigoUnited/js-deep-sort-object
|
|
5
3
|
*/
|
|
@@ -9,16 +7,12 @@ export function _sortObjectDeep<T>(o: T): T {
|
|
|
9
7
|
return o.map(_sortObjectDeep) as any
|
|
10
8
|
}
|
|
11
9
|
|
|
12
|
-
if (
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
out[k as keyof T] = _sortObjectDeep(o[k as keyof T])
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
return out
|
|
10
|
+
if (o && typeof o === 'object') {
|
|
11
|
+
const r = {} as T
|
|
12
|
+
for (const k of Object.keys(o).sort((a, b) => a.localeCompare(b)) as (keyof T)[]) {
|
|
13
|
+
r[k] = _sortObjectDeep(o[k])
|
|
14
|
+
}
|
|
15
|
+
return r
|
|
22
16
|
}
|
|
23
17
|
|
|
24
18
|
return o
|
package/src/promise/pFilter.ts
CHANGED
|
@@ -3,5 +3,5 @@ import type { AsyncPredicate } from '../types'
|
|
|
3
3
|
export async function pFilter<T>(iterable: Iterable<T>, filterFn: AsyncPredicate<T>): Promise<T[]> {
|
|
4
4
|
const items = [...iterable]
|
|
5
5
|
const predicates = await Promise.all(items.map((item, i) => filterFn(item, i)))
|
|
6
|
-
return items.filter((
|
|
6
|
+
return items.filter((_item, i) => predicates[i])
|
|
7
7
|
}
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -72,7 +72,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
|
|
|
72
72
|
const { timeout, name = fn.name || 'pTimeout function', onTimeout } = opt
|
|
73
73
|
const fakeError = opt.fakeError || new Error('TimeoutError')
|
|
74
74
|
|
|
75
|
-
//
|
|
75
|
+
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: ok
|
|
76
76
|
return await new Promise(async (resolve, reject) => {
|
|
77
77
|
// Prepare the timeout timer
|
|
78
78
|
const timer = setTimeout(() => {
|
|
@@ -26,7 +26,7 @@ function serializer(replacer?: Reviver, cycleReplacer?: Reviver): Reviver {
|
|
|
26
26
|
const stack: any[] = []
|
|
27
27
|
const keys: string[] = []
|
|
28
28
|
|
|
29
|
-
cycleReplacer ??= (
|
|
29
|
+
cycleReplacer ??= (_key, value) => {
|
|
30
30
|
if (stack[0] === value) return '[Circular ~]'
|
|
31
31
|
return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']'
|
|
32
32
|
}
|