@naturalcycles/js-lib 14.250.0 → 14.251.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/object/object.util.d.ts +10 -0
- package/dist/object/object.util.js +39 -13
- package/dist/object/sortObject.js +10 -7
- package/dist/object/sortObjectDeep.js +6 -9
- package/dist-esm/object/object.util.js +37 -13
- package/dist-esm/object/sortObject.js +10 -7
- package/dist-esm/object/sortObjectDeep.js +6 -9
- package/package.json +1 -1
- package/src/object/object.util.ts +44 -10
- package/src/object/sortObject.ts +9 -7
- package/src/object/sortObjectDeep.ts +6 -12
|
@@ -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
|
}
|
|
@@ -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
|
}
|
|
@@ -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
|
}
|
|
@@ -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
|
}
|
package/package.json
CHANGED
|
@@ -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
|
}
|
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
|