@jsopen/objects 2.1.0 → 2.2.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/README.md +18 -0
- package/{types/merge.d.ts → merge.d.ts} +2 -2
- package/merge.js +191 -0
- package/package.json +5 -21
- package/CHANGELOG.md +0 -7
- package/cjs/clone.js +0 -14
- package/cjs/index.js +0 -11
- package/cjs/is-object.js +0 -24
- package/cjs/merge.js +0 -163
- package/cjs/omit.js +0 -39
- package/cjs/package.json +0 -3
- package/cjs/tsconfig-build-cjs.tsbuildinfo +0 -1
- package/cjs/type-guards.js +0 -44
- package/cjs/update-error-message.js +0 -46
- package/esm/merge.js +0 -160
- package/esm/package.json +0 -3
- package/esm/tsconfig-build-esm.tsbuildinfo +0 -1
- package/types/index.d.cts +0 -6
- /package/{types/clone.d.ts → clone.d.ts} +0 -0
- /package/{esm/clone.js → clone.js} +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
- /package/{types/is-object.d.ts → is-object.d.ts} +0 -0
- /package/{esm/is-object.js → is-object.js} +0 -0
- /package/{types/omit.d.ts → omit.d.ts} +0 -0
- /package/{esm/omit.js → omit.js} +0 -0
- /package/{types/type-guards.d.ts → type-guards.d.ts} +0 -0
- /package/{esm/type-guards.js → type-guards.js} +0 -0
- /package/{types/update-error-message.d.ts → update-error-message.d.ts} +0 -0
- /package/{esm/update-error-message.js → update-error-message.js} +0 -0
package/README.md
CHANGED
|
@@ -7,6 +7,24 @@
|
|
|
7
7
|
|
|
8
8
|
A 'swiss army knife' solution for working with javascript objects.
|
|
9
9
|
|
|
10
|
+
## Functions
|
|
11
|
+
|
|
12
|
+
### [merge](docs/merge.md)
|
|
13
|
+
Is a powerful, flexible tool for merging objects, arrays, and their nested properties.
|
|
14
|
+
|
|
15
|
+
### [clone / deepClone](docs/clone.md)
|
|
16
|
+
Easy ways to create shallow or deep copies of objects and arrays.
|
|
17
|
+
|
|
18
|
+
### [omit / omitUndefined / omitNull / omitNullish](docs/omit.md)
|
|
19
|
+
Easily exclude specific keys or nullish values from objects.
|
|
20
|
+
|
|
21
|
+
### [updateErrorMessage](docs/update-error-message.md)
|
|
22
|
+
Update an Error object's message while correctly refreshing the stack trace.
|
|
23
|
+
|
|
24
|
+
### [Utilities](docs/utils.md)
|
|
25
|
+
Various utility functions for object and type checking.
|
|
26
|
+
|
|
27
|
+
|
|
10
28
|
## Installation
|
|
11
29
|
|
|
12
30
|
`$ npm install @jsopen/objects`
|
|
@@ -18,8 +18,8 @@ export declare namespace merge {
|
|
|
18
18
|
/**
|
|
19
19
|
* Optional variable that determines the depth of an operation or inclusion behavior.
|
|
20
20
|
*
|
|
21
|
-
* - If set to `true`, it enables a deep operation for only
|
|
22
|
-
* - If set to `'full'`, it enables a deep operation for all objects, including classes, excluding built-in objects
|
|
21
|
+
* - If set to `true`, it enables a deep operation for only plain objects and arrays. Non-plain objects (class instances) are assigned by reference.
|
|
22
|
+
* - If set to `'full'`, it enables a deep operation for all objects, including classes, excluding built-in objects.
|
|
23
23
|
* - If assigned a `NodeCallback` function, it provides a custom callback mechanism for handling the operation.
|
|
24
24
|
*
|
|
25
25
|
* This variable can be used to define the level of depth or customization for a given process.
|
package/merge.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { isObject, isPlainObject } from './is-object.js';
|
|
2
|
+
import { isBuiltIn } from './type-guards.js';
|
|
3
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
4
|
+
export function merge(targetObject, sourceObject, options) {
|
|
5
|
+
if (!(isObject(targetObject) || typeof targetObject === 'function')) {
|
|
6
|
+
throw new TypeError('"target" argument must be an object');
|
|
7
|
+
}
|
|
8
|
+
if (sourceObject == null)
|
|
9
|
+
return targetObject;
|
|
10
|
+
if (!(isObject(sourceObject) ||
|
|
11
|
+
typeof sourceObject === 'function' ||
|
|
12
|
+
Array.isArray(sourceObject))) {
|
|
13
|
+
throw new TypeError('"target" argument must be an object or array of objects');
|
|
14
|
+
}
|
|
15
|
+
const optsKeepExisting = !!options?.keepExisting;
|
|
16
|
+
const optsKeepExistingFn = typeof options?.keepExisting === 'function'
|
|
17
|
+
? options?.keepExisting
|
|
18
|
+
: undefined;
|
|
19
|
+
const optsFilterFn = options?.filter;
|
|
20
|
+
const optsIgnoreUndefined = options?.ignoreUndefined ?? true;
|
|
21
|
+
const optsIgnoreNulls = options?.ignoreNulls;
|
|
22
|
+
const optsDeep = options?.deep;
|
|
23
|
+
const optsDeepFull = optsDeep === 'full';
|
|
24
|
+
const optsDeepFn = typeof options?.deep === 'function' ? options?.deep : undefined;
|
|
25
|
+
const optsCopyDescriptors = options?.copyDescriptors;
|
|
26
|
+
const optsMergeArrays = !!options?.mergeArrays;
|
|
27
|
+
const optsMergeArraysUnique = options?.mergeArrays === 'unique';
|
|
28
|
+
const optsMergeArraysFn = typeof options?.mergeArrays === 'function'
|
|
29
|
+
? options?.mergeArrays
|
|
30
|
+
: undefined;
|
|
31
|
+
const _merge = (target, source, parentPath = '') => {
|
|
32
|
+
if (!isObject(source))
|
|
33
|
+
return;
|
|
34
|
+
const keys = Object.getOwnPropertyNames(source);
|
|
35
|
+
if (options?.symbolKeys ?? true)
|
|
36
|
+
keys.push(...Object.getOwnPropertySymbols(source));
|
|
37
|
+
let key;
|
|
38
|
+
let descriptor;
|
|
39
|
+
let srcVal;
|
|
40
|
+
let trgVal;
|
|
41
|
+
let goDeep = false;
|
|
42
|
+
let srcIsPlainObject;
|
|
43
|
+
let srcIsArray;
|
|
44
|
+
let srcIsBuiltIn;
|
|
45
|
+
let trgIsArray;
|
|
46
|
+
let curPath = '';
|
|
47
|
+
let keepExisting = false;
|
|
48
|
+
if (isPlainObject(target))
|
|
49
|
+
Object.setPrototypeOf(target, Object.getPrototypeOf(source));
|
|
50
|
+
const ignoreFn = options?.ignoreSource;
|
|
51
|
+
let i = 0;
|
|
52
|
+
const len = keys.length;
|
|
53
|
+
for (i = 0; i < len; i++) {
|
|
54
|
+
key = keys[i];
|
|
55
|
+
/** Should not overwrite __proto__ and constructor properties */
|
|
56
|
+
if (key === '__proto__' || key === 'constructor')
|
|
57
|
+
continue;
|
|
58
|
+
if (optsCopyDescriptors) {
|
|
59
|
+
descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
60
|
+
if (descriptor?.get || descriptor?.set) {
|
|
61
|
+
Object.defineProperty(target, key, descriptor);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
srcVal = source[key];
|
|
66
|
+
/** Check if the property should be ignored */
|
|
67
|
+
if (ignoreFn?.(srcVal, {
|
|
68
|
+
key,
|
|
69
|
+
source,
|
|
70
|
+
target,
|
|
71
|
+
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
72
|
+
})) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
srcIsPlainObject = isPlainObject(srcVal);
|
|
76
|
+
srcIsArray = Array.isArray(srcVal);
|
|
77
|
+
srcIsBuiltIn = isBuiltIn(srcVal) && !srcIsArray;
|
|
78
|
+
trgVal = target[key];
|
|
79
|
+
trgIsArray = Array.isArray(trgVal);
|
|
80
|
+
curPath = parentPath + (parentPath ? '.' : '') + String(key);
|
|
81
|
+
if (optsFilterFn &&
|
|
82
|
+
!optsFilterFn(srcVal, {
|
|
83
|
+
key,
|
|
84
|
+
source,
|
|
85
|
+
target,
|
|
86
|
+
path: curPath,
|
|
87
|
+
})) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
/** Determine if we should go deeper into the object */
|
|
91
|
+
goDeep = !!(optsDeep &&
|
|
92
|
+
!srcIsBuiltIn &&
|
|
93
|
+
/** Source value should be an object */
|
|
94
|
+
typeof srcVal === 'object' &&
|
|
95
|
+
/** deep full or plain object */
|
|
96
|
+
(optsDeepFull || srcIsPlainObject || srcIsArray));
|
|
97
|
+
keepExisting =
|
|
98
|
+
optsKeepExisting &&
|
|
99
|
+
hasOwnProperty.call(target, key) &&
|
|
100
|
+
(!optsKeepExistingFn ||
|
|
101
|
+
optsKeepExistingFn(srcVal, {
|
|
102
|
+
key,
|
|
103
|
+
source,
|
|
104
|
+
target,
|
|
105
|
+
path: curPath,
|
|
106
|
+
}));
|
|
107
|
+
if (goDeep && optsDeepFn) {
|
|
108
|
+
goDeep = optsDeepFn(srcVal, {
|
|
109
|
+
key,
|
|
110
|
+
source,
|
|
111
|
+
target,
|
|
112
|
+
path: curPath,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (optsIgnoreUndefined && srcVal === undefined) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (optsIgnoreNulls && srcVal === null) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (goDeep) {
|
|
122
|
+
// if (keepExisting) &&
|
|
123
|
+
/** Array */
|
|
124
|
+
if (srcIsArray) {
|
|
125
|
+
/** If the target value is not an array, we do not need a deep merge operation */
|
|
126
|
+
if (!trgIsArray) {
|
|
127
|
+
if (keepExisting)
|
|
128
|
+
continue;
|
|
129
|
+
srcVal = _arrayClone(srcVal, curPath);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
srcVal = _arrayClone(srcVal, curPath);
|
|
133
|
+
if (optsMergeArrays &&
|
|
134
|
+
(!optsMergeArraysFn ||
|
|
135
|
+
optsMergeArraysFn?.(srcVal, {
|
|
136
|
+
key,
|
|
137
|
+
source,
|
|
138
|
+
target,
|
|
139
|
+
path: curPath,
|
|
140
|
+
}))) {
|
|
141
|
+
srcVal = [...trgVal, ...srcVal];
|
|
142
|
+
if (optsMergeArraysUnique)
|
|
143
|
+
target[key] = Array.from(new Set(srcVal));
|
|
144
|
+
else
|
|
145
|
+
target[key] = srcVal;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
if (optsMergeArraysUnique)
|
|
150
|
+
srcVal = Array.from(new Set(srcVal));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
/** Object */
|
|
156
|
+
if (!isObject(target[key])) {
|
|
157
|
+
if (keepExisting)
|
|
158
|
+
continue;
|
|
159
|
+
target[key] = {};
|
|
160
|
+
}
|
|
161
|
+
_merge(target[key], srcVal, curPath);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (keepExisting)
|
|
166
|
+
continue;
|
|
167
|
+
if (optsCopyDescriptors) {
|
|
168
|
+
descriptor = { ...Object.getOwnPropertyDescriptor(source, key) };
|
|
169
|
+
descriptor.value = srcVal;
|
|
170
|
+
Object.defineProperty(target, key, descriptor);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
target[key] = srcVal;
|
|
174
|
+
}
|
|
175
|
+
return target;
|
|
176
|
+
};
|
|
177
|
+
const _arrayClone = (arr, curPath) => {
|
|
178
|
+
return arr.map((x, index) => {
|
|
179
|
+
if (Array.isArray(x))
|
|
180
|
+
return _arrayClone(x, curPath + '[' + index + ']');
|
|
181
|
+
if (typeof x === 'object' && !isBuiltIn(x))
|
|
182
|
+
return _merge({}, x, curPath + '[' + index + ']');
|
|
183
|
+
return x;
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
const sources = Array.isArray(sourceObject) ? sourceObject : [sourceObject];
|
|
187
|
+
for (const src of sources) {
|
|
188
|
+
_merge(targetObject, src);
|
|
189
|
+
}
|
|
190
|
+
return targetObject;
|
|
191
|
+
}
|
package/package.json
CHANGED
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsopen/objects",
|
|
3
3
|
"description": "Helper utilities for working with JavaScript objects and arrays",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"tslib": "^2.8.1"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
|
+
"module": "./index.js",
|
|
12
|
+
"types": "./index.d.ts",
|
|
11
13
|
"exports": {
|
|
12
14
|
".": {
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"default": "./esm/index.js"
|
|
16
|
-
},
|
|
17
|
-
"require": {
|
|
18
|
-
"types": "./types/index.d.cts",
|
|
19
|
-
"default": "./cjs/index.js"
|
|
20
|
-
},
|
|
21
|
-
"default": "./esm/index.js"
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
22
17
|
},
|
|
23
18
|
"./package.json": "./package.json"
|
|
24
19
|
},
|
|
25
|
-
"main": "./cjs/index.js",
|
|
26
|
-
"module": "./esm/index.js",
|
|
27
|
-
"types": "./types/index.d.ts",
|
|
28
20
|
"contributors": [
|
|
29
21
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
30
22
|
],
|
|
@@ -35,14 +27,6 @@
|
|
|
35
27
|
"engines": {
|
|
36
28
|
"node": ">= 16.0"
|
|
37
29
|
},
|
|
38
|
-
"files": [
|
|
39
|
-
"cjs/",
|
|
40
|
-
"esm/",
|
|
41
|
-
"types/",
|
|
42
|
-
"LICENSE",
|
|
43
|
-
"README.md",
|
|
44
|
-
"CHANGELOG.md"
|
|
45
|
-
],
|
|
46
30
|
"keywords": [
|
|
47
31
|
"object",
|
|
48
32
|
"util",
|
package/CHANGELOG.md
DELETED
package/cjs/clone.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.clone = clone;
|
|
4
|
-
exports.deepClone = deepClone;
|
|
5
|
-
const merge_js_1 = require("./merge.js");
|
|
6
|
-
function clone(obj, options) {
|
|
7
|
-
return (0, merge_js_1.merge)({}, obj, {
|
|
8
|
-
...options,
|
|
9
|
-
deep: options?.deep ?? true,
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
function deepClone(obj, options) {
|
|
13
|
-
return clone(obj, { ...options, deep: 'full' });
|
|
14
|
-
}
|
package/cjs/index.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.updateErrorMessage = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("./clone.js"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./is-object.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./merge.js"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./omit.js"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./type-guards.js"), exports);
|
|
10
|
-
var update_error_message_js_1 = require("./update-error-message.js");
|
|
11
|
-
Object.defineProperty(exports, "updateErrorMessage", { enumerable: true, get: function () { return update_error_message_js_1.updateErrorMessage; } });
|
package/cjs/is-object.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isObject = isObject;
|
|
4
|
-
exports.isPlainObject = isPlainObject;
|
|
5
|
-
const objCtorStr = Function.prototype.toString.call(Object);
|
|
6
|
-
function isObject(v) {
|
|
7
|
-
return v && typeof v === 'object' && !Array.isArray(v);
|
|
8
|
-
}
|
|
9
|
-
function isPlainObject(obj) {
|
|
10
|
-
if (obj &&
|
|
11
|
-
typeof obj === 'object' &&
|
|
12
|
-
Object.prototype.toString.call(obj) === '[object Object]') {
|
|
13
|
-
const proto = Object.getPrototypeOf(obj);
|
|
14
|
-
/* istanbul ignore next */
|
|
15
|
-
if (!proto)
|
|
16
|
-
return true;
|
|
17
|
-
const ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') &&
|
|
18
|
-
proto.constructor;
|
|
19
|
-
return (typeof ctor === 'function' &&
|
|
20
|
-
ctor instanceof ctor &&
|
|
21
|
-
Function.prototype.toString.call(ctor) === objCtorStr);
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
}
|
package/cjs/merge.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.merge = merge;
|
|
4
|
-
const is_object_js_1 = require("./is-object.js");
|
|
5
|
-
const type_guards_js_1 = require("./type-guards.js");
|
|
6
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
7
|
-
function merge(targetObject, sourceObject, options) {
|
|
8
|
-
if (!((0, is_object_js_1.isObject)(targetObject) || typeof targetObject === 'function')) {
|
|
9
|
-
throw new TypeError('"target" argument must be an object');
|
|
10
|
-
}
|
|
11
|
-
if (sourceObject == null)
|
|
12
|
-
return targetObject;
|
|
13
|
-
if (!((0, is_object_js_1.isObject)(sourceObject) ||
|
|
14
|
-
typeof sourceObject === 'function' ||
|
|
15
|
-
Array.isArray(sourceObject))) {
|
|
16
|
-
throw new TypeError('"target" argument must be an object or array of objects');
|
|
17
|
-
}
|
|
18
|
-
const keepExisting = options?.keepExisting;
|
|
19
|
-
const keepExistingFn = typeof options?.keepExisting === 'function'
|
|
20
|
-
? options?.keepExisting
|
|
21
|
-
: undefined;
|
|
22
|
-
const filterFn = options?.filter;
|
|
23
|
-
const ignoreUndefined = options?.ignoreUndefined ?? true;
|
|
24
|
-
const ignoreNulls = options?.ignoreNulls;
|
|
25
|
-
const deep = options?.deep;
|
|
26
|
-
const deepFull = deep === 'full';
|
|
27
|
-
const deepFn = typeof options?.deep === 'function' ? options?.deep : undefined;
|
|
28
|
-
const copyDescriptors = options?.copyDescriptors;
|
|
29
|
-
const mergeArrays = options?.mergeArrays;
|
|
30
|
-
const mergeArraysUnique = options?.mergeArrays === 'unique';
|
|
31
|
-
const mergeArraysFn = typeof options?.mergeArrays === 'function'
|
|
32
|
-
? options?.mergeArrays
|
|
33
|
-
: undefined;
|
|
34
|
-
const _merge = (target, source, parentPath = '') => {
|
|
35
|
-
if (!(0, is_object_js_1.isObject)(source))
|
|
36
|
-
return;
|
|
37
|
-
const keys = Object.getOwnPropertyNames(source);
|
|
38
|
-
if (options?.symbolKeys ?? true)
|
|
39
|
-
keys.push(...Object.getOwnPropertySymbols(source));
|
|
40
|
-
let key;
|
|
41
|
-
let descriptor;
|
|
42
|
-
let srcVal;
|
|
43
|
-
let _goDeep = false;
|
|
44
|
-
if ((0, is_object_js_1.isPlainObject)(target))
|
|
45
|
-
Object.setPrototypeOf(target, Object.getPrototypeOf(source));
|
|
46
|
-
const ignoreFn = options?.ignoreSource;
|
|
47
|
-
let i = 0;
|
|
48
|
-
const len = keys.length;
|
|
49
|
-
for (i = 0; i < len; i++) {
|
|
50
|
-
key = keys[i];
|
|
51
|
-
/** Should not overwrite __proto__ and constructor properties */
|
|
52
|
-
if (key === '__proto__' || key === 'constructor')
|
|
53
|
-
continue;
|
|
54
|
-
if (copyDescriptors) {
|
|
55
|
-
descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
56
|
-
if (descriptor?.get || descriptor?.set) {
|
|
57
|
-
Object.defineProperty(target, key, descriptor);
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
srcVal = source[key];
|
|
62
|
-
if (ignoreFn?.(srcVal, {
|
|
63
|
-
key,
|
|
64
|
-
source,
|
|
65
|
-
target,
|
|
66
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
67
|
-
})) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (keepExisting && hasOwnProperty.call(target, key)) {
|
|
71
|
-
if (!keepExistingFn)
|
|
72
|
-
continue;
|
|
73
|
-
if (keepExistingFn(srcVal, {
|
|
74
|
-
key,
|
|
75
|
-
source,
|
|
76
|
-
target,
|
|
77
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
78
|
-
})) {
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (filterFn &&
|
|
83
|
-
!filterFn(srcVal, {
|
|
84
|
-
key,
|
|
85
|
-
source,
|
|
86
|
-
target,
|
|
87
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
88
|
-
})) {
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
if (ignoreUndefined && srcVal === undefined) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (ignoreNulls && srcVal === null) {
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
if (deep &&
|
|
98
|
-
typeof srcVal === 'object' &&
|
|
99
|
-
(!(0, type_guards_js_1.isBuiltIn)(srcVal) || Array.isArray(srcVal))) {
|
|
100
|
-
_goDeep =
|
|
101
|
-
(deepFn &&
|
|
102
|
-
deepFn(srcVal, {
|
|
103
|
-
key,
|
|
104
|
-
source,
|
|
105
|
-
target,
|
|
106
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
107
|
-
})) ||
|
|
108
|
-
(!deepFn &&
|
|
109
|
-
(deepFull || (0, is_object_js_1.isPlainObject)(srcVal) || Array.isArray(srcVal)));
|
|
110
|
-
if (_goDeep) {
|
|
111
|
-
/** Array */
|
|
112
|
-
if (Array.isArray(srcVal)) {
|
|
113
|
-
if (Array.isArray(target[key]) &&
|
|
114
|
-
(mergeArrays ||
|
|
115
|
-
mergeArraysFn?.(srcVal, {
|
|
116
|
-
key,
|
|
117
|
-
source,
|
|
118
|
-
target,
|
|
119
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
120
|
-
}))) {
|
|
121
|
-
target[key] = _arrayClone(target[key], parentPath + (parentPath ? '.' : '') + String(key));
|
|
122
|
-
}
|
|
123
|
-
else
|
|
124
|
-
target[key] = [];
|
|
125
|
-
target[key].push(..._arrayClone(srcVal, parentPath + (parentPath ? '.' : '') + String(key)));
|
|
126
|
-
if (mergeArraysUnique)
|
|
127
|
-
target[key] = Array.from(new Set(target[key]));
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
/** Object */
|
|
132
|
-
if (!(0, is_object_js_1.isObject)(target[key]))
|
|
133
|
-
target[key] = {};
|
|
134
|
-
_merge(target[key], srcVal, parentPath + (parentPath ? '.' : '') + String(key));
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (copyDescriptors) {
|
|
140
|
-
descriptor = { ...Object.getOwnPropertyDescriptor(source, key) };
|
|
141
|
-
descriptor.value = srcVal;
|
|
142
|
-
Object.defineProperty(target, key, descriptor);
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
target[key] = srcVal;
|
|
146
|
-
}
|
|
147
|
-
return target;
|
|
148
|
-
};
|
|
149
|
-
const _arrayClone = (arr, curPath) => {
|
|
150
|
-
return arr.map((x, index) => {
|
|
151
|
-
if (Array.isArray(x))
|
|
152
|
-
return _arrayClone(x, curPath + '[' + index + ']');
|
|
153
|
-
if (typeof x === 'object' && !(0, type_guards_js_1.isBuiltIn)(x))
|
|
154
|
-
return _merge({}, x, curPath + '[' + index + ']');
|
|
155
|
-
return x;
|
|
156
|
-
});
|
|
157
|
-
};
|
|
158
|
-
const sources = Array.isArray(sourceObject) ? sourceObject : [sourceObject];
|
|
159
|
-
for (const src of sources) {
|
|
160
|
-
_merge(targetObject, src);
|
|
161
|
-
}
|
|
162
|
-
return targetObject;
|
|
163
|
-
}
|
package/cjs/omit.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.omit = omit;
|
|
4
|
-
exports.omitUndefined = omitUndefined;
|
|
5
|
-
exports.omitNull = omitNull;
|
|
6
|
-
exports.omitNullish = omitNullish;
|
|
7
|
-
const merge_js_1 = require("./merge.js");
|
|
8
|
-
function omit(obj, keys) {
|
|
9
|
-
const keysSet = new Set(keys);
|
|
10
|
-
return (0, merge_js_1.merge)({}, obj, {
|
|
11
|
-
deep: false,
|
|
12
|
-
filter(_, { key }) {
|
|
13
|
-
return !keysSet.has(key);
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
function omitUndefined(obj, deep) {
|
|
18
|
-
return (0, merge_js_1.merge)({}, obj, {
|
|
19
|
-
deep,
|
|
20
|
-
ignoreUndefined: true,
|
|
21
|
-
copyDescriptors: true,
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
function omitNull(obj, deep) {
|
|
25
|
-
return (0, merge_js_1.merge)({}, obj, {
|
|
26
|
-
deep,
|
|
27
|
-
ignoreNulls: true,
|
|
28
|
-
ignoreUndefined: false,
|
|
29
|
-
copyDescriptors: true,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
function omitNullish(obj, deep) {
|
|
33
|
-
return (0, merge_js_1.merge)({}, obj, {
|
|
34
|
-
deep,
|
|
35
|
-
ignoreNulls: true,
|
|
36
|
-
ignoreUndefined: true,
|
|
37
|
-
copyDescriptors: true,
|
|
38
|
-
});
|
|
39
|
-
}
|
package/cjs/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["../../src/clone.ts","../../src/index.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts","../../src/type-guards.ts","../../src/update-error-message.ts"],"version":"5.9.3"}
|
package/cjs/type-guards.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isBuiltIn = isBuiltIn;
|
|
4
|
-
exports.isConstructor = isConstructor;
|
|
5
|
-
exports.isIterable = isIterable;
|
|
6
|
-
exports.isAsyncIterable = isAsyncIterable;
|
|
7
|
-
function isBuiltIn(v) {
|
|
8
|
-
return ((v &&
|
|
9
|
-
typeof v === 'object' &&
|
|
10
|
-
(v instanceof Date ||
|
|
11
|
-
v instanceof RegExp ||
|
|
12
|
-
v instanceof Map ||
|
|
13
|
-
v instanceof Set ||
|
|
14
|
-
v instanceof WeakMap ||
|
|
15
|
-
v instanceof WeakSet ||
|
|
16
|
-
v instanceof WeakRef ||
|
|
17
|
-
v instanceof Promise ||
|
|
18
|
-
v instanceof Error ||
|
|
19
|
-
v instanceof ArrayBuffer ||
|
|
20
|
-
v instanceof Uint8Array ||
|
|
21
|
-
v instanceof Uint8ClampedArray ||
|
|
22
|
-
v instanceof Uint16Array ||
|
|
23
|
-
v instanceof Uint32Array ||
|
|
24
|
-
v instanceof BigUint64Array ||
|
|
25
|
-
v instanceof Int8Array ||
|
|
26
|
-
v instanceof Int16Array ||
|
|
27
|
-
v instanceof Int32Array ||
|
|
28
|
-
v.constructor.name === 'SharedArrayBuffer' ||
|
|
29
|
-
Buffer.isBuffer(v))) ||
|
|
30
|
-
Array.isArray(v));
|
|
31
|
-
}
|
|
32
|
-
function isConstructor(fn) {
|
|
33
|
-
return (typeof fn === 'function' &&
|
|
34
|
-
fn.prototype &&
|
|
35
|
-
fn.prototype.constructor === fn &&
|
|
36
|
-
fn.prototype.constructor.name !== 'Function' &&
|
|
37
|
-
fn.prototype.constructor.name !== 'embedded');
|
|
38
|
-
}
|
|
39
|
-
function isIterable(x) {
|
|
40
|
-
return Symbol.iterator in x;
|
|
41
|
-
}
|
|
42
|
-
function isAsyncIterable(x) {
|
|
43
|
-
return Symbol.asyncIterator in x;
|
|
44
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.updateErrorMessage = updateErrorMessage;
|
|
4
|
-
exports.updateErrorMessageFallback = updateErrorMessageFallback;
|
|
5
|
-
/**
|
|
6
|
-
* Updates the error message and stack trace at sametime.
|
|
7
|
-
* @param err
|
|
8
|
-
* @param newMessage
|
|
9
|
-
*/
|
|
10
|
-
function updateErrorMessage(err, newMessage) {
|
|
11
|
-
err.message = String(newMessage);
|
|
12
|
-
/** V8 */
|
|
13
|
-
if (typeof Error.captureStackTrace === 'function') {
|
|
14
|
-
Error.captureStackTrace(err);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
/** Other engines */
|
|
18
|
-
return updateErrorMessageFallback(err, newMessage);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Updates the error message and stack trace at sametime.
|
|
22
|
-
* @param err
|
|
23
|
-
* @param newMessage
|
|
24
|
-
*/
|
|
25
|
-
function updateErrorMessageFallback(err, newMessage) {
|
|
26
|
-
err.message = String(newMessage);
|
|
27
|
-
/** Other engines */
|
|
28
|
-
const stack = typeof err.stack === 'string' ? err.stack : null;
|
|
29
|
-
if (!stack)
|
|
30
|
-
return err;
|
|
31
|
-
const name = err.name || 'Error';
|
|
32
|
-
const lines = stack.split('\n');
|
|
33
|
-
const firstFrameIdx = lines.findIndex(l => /^\s*at\s+/.test(l));
|
|
34
|
-
if (firstFrameIdx === -1) {
|
|
35
|
-
const msgLines = String(newMessage).split(/\r?\n/);
|
|
36
|
-
lines[0] = `${name}: ${msgLines[0] ?? ''}`;
|
|
37
|
-
lines.splice(1, 0, ...msgLines.slice(1));
|
|
38
|
-
err.stack = lines.join('\n');
|
|
39
|
-
return err;
|
|
40
|
-
}
|
|
41
|
-
const frameLines = lines.slice(firstFrameIdx);
|
|
42
|
-
const msgLines = String(newMessage).split(/\r?\n/);
|
|
43
|
-
const newHead = [`${name}: ${msgLines[0] ?? ''}`, ...msgLines.slice(1)];
|
|
44
|
-
err.stack = [...newHead, ...frameLines].join('\n');
|
|
45
|
-
return err;
|
|
46
|
-
}
|
package/esm/merge.js
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { isObject, isPlainObject } from './is-object.js';
|
|
2
|
-
import { isBuiltIn } from './type-guards.js';
|
|
3
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
4
|
-
export function merge(targetObject, sourceObject, options) {
|
|
5
|
-
if (!(isObject(targetObject) || typeof targetObject === 'function')) {
|
|
6
|
-
throw new TypeError('"target" argument must be an object');
|
|
7
|
-
}
|
|
8
|
-
if (sourceObject == null)
|
|
9
|
-
return targetObject;
|
|
10
|
-
if (!(isObject(sourceObject) ||
|
|
11
|
-
typeof sourceObject === 'function' ||
|
|
12
|
-
Array.isArray(sourceObject))) {
|
|
13
|
-
throw new TypeError('"target" argument must be an object or array of objects');
|
|
14
|
-
}
|
|
15
|
-
const keepExisting = options?.keepExisting;
|
|
16
|
-
const keepExistingFn = typeof options?.keepExisting === 'function'
|
|
17
|
-
? options?.keepExisting
|
|
18
|
-
: undefined;
|
|
19
|
-
const filterFn = options?.filter;
|
|
20
|
-
const ignoreUndefined = options?.ignoreUndefined ?? true;
|
|
21
|
-
const ignoreNulls = options?.ignoreNulls;
|
|
22
|
-
const deep = options?.deep;
|
|
23
|
-
const deepFull = deep === 'full';
|
|
24
|
-
const deepFn = typeof options?.deep === 'function' ? options?.deep : undefined;
|
|
25
|
-
const copyDescriptors = options?.copyDescriptors;
|
|
26
|
-
const mergeArrays = options?.mergeArrays;
|
|
27
|
-
const mergeArraysUnique = options?.mergeArrays === 'unique';
|
|
28
|
-
const mergeArraysFn = typeof options?.mergeArrays === 'function'
|
|
29
|
-
? options?.mergeArrays
|
|
30
|
-
: undefined;
|
|
31
|
-
const _merge = (target, source, parentPath = '') => {
|
|
32
|
-
if (!isObject(source))
|
|
33
|
-
return;
|
|
34
|
-
const keys = Object.getOwnPropertyNames(source);
|
|
35
|
-
if (options?.symbolKeys ?? true)
|
|
36
|
-
keys.push(...Object.getOwnPropertySymbols(source));
|
|
37
|
-
let key;
|
|
38
|
-
let descriptor;
|
|
39
|
-
let srcVal;
|
|
40
|
-
let _goDeep = false;
|
|
41
|
-
if (isPlainObject(target))
|
|
42
|
-
Object.setPrototypeOf(target, Object.getPrototypeOf(source));
|
|
43
|
-
const ignoreFn = options?.ignoreSource;
|
|
44
|
-
let i = 0;
|
|
45
|
-
const len = keys.length;
|
|
46
|
-
for (i = 0; i < len; i++) {
|
|
47
|
-
key = keys[i];
|
|
48
|
-
/** Should not overwrite __proto__ and constructor properties */
|
|
49
|
-
if (key === '__proto__' || key === 'constructor')
|
|
50
|
-
continue;
|
|
51
|
-
if (copyDescriptors) {
|
|
52
|
-
descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
53
|
-
if (descriptor?.get || descriptor?.set) {
|
|
54
|
-
Object.defineProperty(target, key, descriptor);
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
srcVal = source[key];
|
|
59
|
-
if (ignoreFn?.(srcVal, {
|
|
60
|
-
key,
|
|
61
|
-
source,
|
|
62
|
-
target,
|
|
63
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
64
|
-
})) {
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
if (keepExisting && hasOwnProperty.call(target, key)) {
|
|
68
|
-
if (!keepExistingFn)
|
|
69
|
-
continue;
|
|
70
|
-
if (keepExistingFn(srcVal, {
|
|
71
|
-
key,
|
|
72
|
-
source,
|
|
73
|
-
target,
|
|
74
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
75
|
-
})) {
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (filterFn &&
|
|
80
|
-
!filterFn(srcVal, {
|
|
81
|
-
key,
|
|
82
|
-
source,
|
|
83
|
-
target,
|
|
84
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
85
|
-
})) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (ignoreUndefined && srcVal === undefined) {
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
if (ignoreNulls && srcVal === null) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (deep &&
|
|
95
|
-
typeof srcVal === 'object' &&
|
|
96
|
-
(!isBuiltIn(srcVal) || Array.isArray(srcVal))) {
|
|
97
|
-
_goDeep =
|
|
98
|
-
(deepFn &&
|
|
99
|
-
deepFn(srcVal, {
|
|
100
|
-
key,
|
|
101
|
-
source,
|
|
102
|
-
target,
|
|
103
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
104
|
-
})) ||
|
|
105
|
-
(!deepFn &&
|
|
106
|
-
(deepFull || isPlainObject(srcVal) || Array.isArray(srcVal)));
|
|
107
|
-
if (_goDeep) {
|
|
108
|
-
/** Array */
|
|
109
|
-
if (Array.isArray(srcVal)) {
|
|
110
|
-
if (Array.isArray(target[key]) &&
|
|
111
|
-
(mergeArrays ||
|
|
112
|
-
mergeArraysFn?.(srcVal, {
|
|
113
|
-
key,
|
|
114
|
-
source,
|
|
115
|
-
target,
|
|
116
|
-
path: parentPath + (parentPath ? '.' : '') + String(key),
|
|
117
|
-
}))) {
|
|
118
|
-
target[key] = _arrayClone(target[key], parentPath + (parentPath ? '.' : '') + String(key));
|
|
119
|
-
}
|
|
120
|
-
else
|
|
121
|
-
target[key] = [];
|
|
122
|
-
target[key].push(..._arrayClone(srcVal, parentPath + (parentPath ? '.' : '') + String(key)));
|
|
123
|
-
if (mergeArraysUnique)
|
|
124
|
-
target[key] = Array.from(new Set(target[key]));
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
/** Object */
|
|
129
|
-
if (!isObject(target[key]))
|
|
130
|
-
target[key] = {};
|
|
131
|
-
_merge(target[key], srcVal, parentPath + (parentPath ? '.' : '') + String(key));
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (copyDescriptors) {
|
|
137
|
-
descriptor = { ...Object.getOwnPropertyDescriptor(source, key) };
|
|
138
|
-
descriptor.value = srcVal;
|
|
139
|
-
Object.defineProperty(target, key, descriptor);
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
target[key] = srcVal;
|
|
143
|
-
}
|
|
144
|
-
return target;
|
|
145
|
-
};
|
|
146
|
-
const _arrayClone = (arr, curPath) => {
|
|
147
|
-
return arr.map((x, index) => {
|
|
148
|
-
if (Array.isArray(x))
|
|
149
|
-
return _arrayClone(x, curPath + '[' + index + ']');
|
|
150
|
-
if (typeof x === 'object' && !isBuiltIn(x))
|
|
151
|
-
return _merge({}, x, curPath + '[' + index + ']');
|
|
152
|
-
return x;
|
|
153
|
-
});
|
|
154
|
-
};
|
|
155
|
-
const sources = Array.isArray(sourceObject) ? sourceObject : [sourceObject];
|
|
156
|
-
for (const src of sources) {
|
|
157
|
-
_merge(targetObject, src);
|
|
158
|
-
}
|
|
159
|
-
return targetObject;
|
|
160
|
-
}
|
package/esm/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["../../src/clone.ts","../../src/index.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts","../../src/type-guards.ts","../../src/update-error-message.ts"],"version":"5.9.3"}
|
package/types/index.d.cts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{esm/omit.js → omit.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|