@jsopen/objects 1.0.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/CHANGELOG.md ADDED
File without changes
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Panates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @jsopen/objects
2
+
3
+ [![NPM Version][npm-image]][npm-url]
4
+ [![NPM Downloads][downloads-image]][downloads-url]
5
+ [![CI Tests][ci-test-image]][ci-test-url]
6
+ [![Test Coverage][coveralls-image]][coveralls-url]
7
+
8
+ A 'swiss army knife' solution for working with javascript objects.
9
+
10
+ ## Installation
11
+
12
+ `$ npm install @jsopen/objects`
13
+
14
+ ## Node Compatibility
15
+
16
+ - node `>= 16`;
17
+
18
+ ### License
19
+ [MIT](LICENSE)
20
+
21
+
22
+ [npm-image]: https://img.shields.io/npm/v/@jsopen/objects
23
+ [npm-url]: https://npmjs.org/package/@jsopen/objects
24
+ [ci-test-image]: https://github.com/panates/jsopen-objects/actions/workflows/test.yml/badge.svg
25
+ [ci-test-url]: https://github.com/panates/jsopen-objects/actions/workflows/test.yml
26
+ [coveralls-image]: https://img.shields.io/coveralls/panates/@jsopen/objects/master.svg
27
+ [coveralls-url]: https://coveralls.io/r/panates/@jsopen/objects
28
+ [downloads-image]: https://img.shields.io/npm/dm/@jsopen/objects.svg
29
+ [downloads-url]: https://npmjs.org/package/@jsopen/objects
30
+
package/cjs/clone.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clone = clone;
4
+ const merge_js_1 = require("./merge.js");
5
+ function clone(obj, options) {
6
+ return (0, merge_js_1.merge)({}, obj, {
7
+ ...options,
8
+ deep: options?.deep ?? true,
9
+ });
10
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./clone.js"), exports);
5
+ tslib_1.__exportStar(require("./is-built-in.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);
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBuiltIn = isBuiltIn;
4
+ function isBuiltIn(v) {
5
+ return ((typeof v === 'object' &&
6
+ (v instanceof Date ||
7
+ v instanceof RegExp ||
8
+ v instanceof Map ||
9
+ v instanceof Set ||
10
+ v instanceof WeakMap ||
11
+ v instanceof WeakSet ||
12
+ v instanceof WeakRef ||
13
+ v instanceof Promise ||
14
+ v instanceof Error ||
15
+ v instanceof ArrayBuffer ||
16
+ v instanceof SharedArrayBuffer ||
17
+ v instanceof Uint8Array ||
18
+ v instanceof Uint8ClampedArray ||
19
+ v instanceof Uint16Array ||
20
+ v instanceof Uint32Array ||
21
+ v instanceof BigUint64Array ||
22
+ v instanceof Int8Array ||
23
+ v instanceof Int16Array ||
24
+ v instanceof Int32Array ||
25
+ Buffer.isBuffer(v))) ||
26
+ Array.isArray(v));
27
+ }
@@ -0,0 +1,23 @@
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 (typeof obj === 'object' &&
11
+ Object.prototype.toString.call(obj) === '[object Object]') {
12
+ const proto = Object.getPrototypeOf(obj);
13
+ /* istanbul ignore next */
14
+ if (!proto)
15
+ return true;
16
+ const ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') &&
17
+ proto.constructor;
18
+ return (typeof ctor === 'function' &&
19
+ ctor instanceof ctor &&
20
+ Function.prototype.toString.call(ctor) === objCtorStr);
21
+ }
22
+ return false;
23
+ }
package/cjs/merge.js ADDED
@@ -0,0 +1,229 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.merge = merge;
4
+ exports.getMergeFunction = getMergeFunction;
5
+ const is_object_js_1 = require("./is-object.js");
6
+ function merge(target, source, options) {
7
+ if (!((0, is_object_js_1.isObject)(target) || typeof target === 'function')) {
8
+ throw new TypeError('"target" argument must be an object');
9
+ }
10
+ source = source || {};
11
+ if (!((0, is_object_js_1.isObject)(source) || typeof target === 'function')) {
12
+ throw new TypeError('"target" argument must be an object');
13
+ }
14
+ const fn = getMergeFunction(options);
15
+ return fn(target, source, '', options, fn, is_object_js_1.isPlainObject, is_object_js_1.isObject, arrayClone);
16
+ }
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
+ const functionCache = new Map();
19
+ function getMergeFunction(options) {
20
+ const cacheKey = [
21
+ options?.deep,
22
+ options?.moveArrays,
23
+ options?.keepExisting,
24
+ options?.copyDescriptors,
25
+ options?.ignore,
26
+ options?.ignoreUndefined,
27
+ options?.ignoreNulls,
28
+ options?.filter,
29
+ ]
30
+ .map(option => option == null
31
+ ? 'n'
32
+ : typeof option === 'function'
33
+ ? 'f'
34
+ : option
35
+ ? '1'
36
+ : '0')
37
+ .join();
38
+ let fn = functionCache.get(cacheKey);
39
+ if (!fn) {
40
+ fn = buildMerge(options);
41
+ functionCache.set(cacheKey, fn);
42
+ }
43
+ return fn;
44
+ }
45
+ function buildMerge(options) {
46
+ const args = [
47
+ 'target',
48
+ 'source',
49
+ 'curPath',
50
+ 'options',
51
+ 'mergeFunction',
52
+ 'isPlainObject',
53
+ 'isObject',
54
+ 'arrayClone',
55
+ ];
56
+ const scriptL0 = [
57
+ `
58
+ const _merge = (_trgVal, _srcVal, _curPath) =>
59
+ mergeFunction(_trgVal, _srcVal, _curPath, options, mergeFunction, isPlainObject, isObject, arrayClone);
60
+ const keys = Object.getOwnPropertyNames(source);
61
+ keys.push(...Object.getOwnPropertySymbols(source));
62
+ let key;
63
+ let descriptor;
64
+ let srcVal;
65
+ let trgVal;
66
+ `,
67
+ ];
68
+ if (options?.deep) {
69
+ scriptL0.push(`let subPath;`, `let _isPlain;`, `let _isArray;`);
70
+ if (typeof options?.deep === 'function') {
71
+ scriptL0.push(`const deepCallback = options.deep;`);
72
+ }
73
+ }
74
+ if (typeof options?.ignore === 'function') {
75
+ scriptL0.push('const ignoreCallback = options.ignore;');
76
+ }
77
+ if (typeof options?.filter === 'function') {
78
+ scriptL0.push('const filterCallback = options.filter;');
79
+ }
80
+ if (typeof options?.copyDescriptors === 'function') {
81
+ scriptL0.push(`const copyDescriptorsCallback = options.copyDescriptors;`);
82
+ }
83
+ if (typeof options?.moveArrays === 'function') {
84
+ scriptL0.push(`const moveArraysCallback = options.moveArrays;`);
85
+ }
86
+ scriptL0.push(`
87
+ let i = 0;
88
+ const len = keys.length;
89
+ for (i = 0; i < len; i++) {
90
+ key = keys[i];
91
+ /** Should not overwrite __proto__ and constructor properties */
92
+ if (key === '__proto__' || key === 'constructor') continue;
93
+ `);
94
+ const scriptL1For = [];
95
+ scriptL0.push(scriptL1For);
96
+ scriptL0.push('}');
97
+ /** ************* filter *****************/
98
+ if (options?.filter) {
99
+ scriptL1For.push(`
100
+ if (!filterCallback(key, curPath, target, source)) {
101
+ delete target[key];
102
+ continue;
103
+ }`);
104
+ }
105
+ /** ************* ignore *****************/
106
+ if (typeof options?.ignore === 'function') {
107
+ scriptL1For.push(`
108
+ if (
109
+ Object.prototype.hasOwnProperty.call(target, key) &&
110
+ ignoreCallback(key, curPath, target, source)
111
+ ) continue;
112
+ `);
113
+ }
114
+ // scriptL1For.push(
115
+ // `descriptor = { ...Object.getOwnPropertyDescriptor(source, key) };`,
116
+ // );
117
+ /** ************* copyDescriptors *****************/
118
+ if (options?.copyDescriptors) {
119
+ let scriptL2Descriptors = scriptL1For;
120
+ if (typeof options?.copyDescriptors === 'function') {
121
+ scriptL1For.push('if (copyDescriptorsCallback(key, curPath, target, source)) {');
122
+ scriptL2Descriptors = [];
123
+ scriptL1For.push(scriptL2Descriptors);
124
+ scriptL1For.push(`} else`);
125
+ scriptL1For.push(` descriptor = {enumerable: true, configurable: true, writable: true}`);
126
+ }
127
+ scriptL2Descriptors.push(`
128
+ descriptor = { ...Object.getOwnPropertyDescriptor(source, key) }
129
+ if ((descriptor.get || descriptor.set)) {
130
+ Object.defineProperty(target, key, descriptor);
131
+ continue;
132
+ }
133
+ srcVal = source[key];`);
134
+ }
135
+ else {
136
+ scriptL1For.push(`descriptor = {enumerable: true, configurable: true, writable: true}`, `srcVal = source[key];`);
137
+ }
138
+ /** ************* keepExisting *****************/
139
+ if (options?.keepExisting) {
140
+ scriptL1For.push(`if (hasOwnProperty.call(target, key)) continue;`);
141
+ }
142
+ /** ************* ignoreUndefined *****************/
143
+ if (options?.ignoreUndefined ?? true) {
144
+ scriptL1For.push(`if (srcVal === undefined) continue;`);
145
+ }
146
+ /** ************* ignoreNulls *****************/
147
+ if (options?.ignoreNulls) {
148
+ scriptL1For.push(`if (srcVal === null) continue;`);
149
+ }
150
+ /** ************* deep *****************/
151
+ if (options?.deep) {
152
+ scriptL1For.push(`
153
+ _isPlain = isPlainObject(srcVal);
154
+ _isArray = Array.isArray(srcVal);
155
+ if (_isPlain || _isArray) {`);
156
+ const scriptL2Deep = [];
157
+ scriptL1For.push(scriptL2Deep);
158
+ scriptL1For.push('}');
159
+ let scriptL3Deep = scriptL2Deep;
160
+ if (typeof options?.deep === 'function') {
161
+ scriptL2Deep.push(`
162
+ subPath = curPath + (curPath ? '.' : '') + key;
163
+ if (deepCallback(key, subPath, target, source)) {`);
164
+ scriptL3Deep = [];
165
+ scriptL2Deep.push(scriptL3Deep);
166
+ scriptL2Deep.push('}');
167
+ }
168
+ /** ************* _isPlain *****************/
169
+ scriptL3Deep.push(`
170
+ if (_isPlain) {
171
+ trgVal = target[key];
172
+ if (!isObject(trgVal)) {
173
+ descriptor.value = trgVal = {};
174
+ Object.defineProperty(target, key, descriptor);
175
+ }
176
+ _merge(trgVal, srcVal, subPath, options);
177
+ continue;
178
+ }`);
179
+ /** ************* moveArrays *****************/
180
+ if (!options?.moveArrays || typeof options?.moveArrays === 'function') {
181
+ scriptL3Deep.push(`if (_isArray) {`);
182
+ const scriptL4IsArray = [];
183
+ scriptL3Deep.push(scriptL4IsArray);
184
+ scriptL3Deep.push('}');
185
+ let scriptL5CloneArrays = scriptL4IsArray;
186
+ if (typeof options?.moveArrays === 'function') {
187
+ scriptL4IsArray.push(`if (!moveArraysCallback(key, subPath, target, source)) {`);
188
+ scriptL5CloneArrays = [];
189
+ scriptL4IsArray.push(scriptL5CloneArrays);
190
+ scriptL4IsArray.push('}');
191
+ }
192
+ scriptL5CloneArrays.push(`
193
+ descriptor.value = arrayClone(srcVal, _merge, subPath);
194
+ Object.defineProperty(target, key, descriptor);
195
+ continue;
196
+ `);
197
+ }
198
+ }
199
+ /** ************* finalize *****************/
200
+ scriptL1For.push(`
201
+ descriptor.value = srcVal;
202
+ Object.defineProperty(target, key, descriptor);`);
203
+ scriptL0.push('return target;');
204
+ const script = _flattenText(scriptL0);
205
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval,prefer-const
206
+ return Function(...args, script);
207
+ }
208
+ function arrayClone(arr, _merge, curPath) {
209
+ return arr.map((x) => {
210
+ if (Array.isArray(x))
211
+ return arrayClone(x, _merge, curPath);
212
+ if ((0, is_object_js_1.isPlainObject)(x))
213
+ return _merge({}, x, curPath);
214
+ return x;
215
+ });
216
+ }
217
+ function _flattenText(arr, level = 0) {
218
+ const indent = ' '.repeat(level);
219
+ return arr
220
+ .map(v => {
221
+ if (Array.isArray(v))
222
+ return _flattenText(v, level + 1);
223
+ return (indent +
224
+ String(v)
225
+ .trim()
226
+ .replace(/\n/g, '\n' + indent));
227
+ })
228
+ .join('\n');
229
+ }
package/cjs/omit.js ADDED
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.omitUndefined = omitUndefined;
4
+ exports.omitNull = omitNull;
5
+ exports.omitNullish = omitNullish;
6
+ const is_object_js_1 = require("./is-object.js");
7
+ function omitUndefined(obj, deep) {
8
+ /* istanbul ignore next */
9
+ if (!(obj && typeof obj === 'object'))
10
+ return obj;
11
+ let v;
12
+ for (const k of Object.keys(obj)) {
13
+ v = obj[k];
14
+ if (v === undefined)
15
+ delete obj[k];
16
+ else if (deep) {
17
+ if (Array.isArray(v))
18
+ v.forEach(x => omitUndefined(x, deep));
19
+ else if ((0, is_object_js_1.isPlainObject)(v))
20
+ omitUndefined(obj[k], deep);
21
+ }
22
+ }
23
+ return obj;
24
+ }
25
+ function omitNull(obj, deep) {
26
+ /* istanbul ignore next */
27
+ if (!(obj && typeof obj === 'object'))
28
+ return obj;
29
+ let v;
30
+ for (const k of Object.keys(obj)) {
31
+ v = obj[k];
32
+ if (v === null)
33
+ delete obj[k];
34
+ else if (deep) {
35
+ if (Array.isArray(v))
36
+ v.forEach(x => omitNull(x, deep));
37
+ else if ((0, is_object_js_1.isPlainObject)(v))
38
+ omitNull(obj[k], deep);
39
+ }
40
+ }
41
+ return obj;
42
+ }
43
+ function omitNullish(obj, deep) {
44
+ /* istanbul ignore next */
45
+ if (!(obj && typeof obj === 'object'))
46
+ return obj;
47
+ let v;
48
+ for (const k of Object.keys(obj)) {
49
+ v = obj[k];
50
+ if (v == null)
51
+ delete obj[k];
52
+ else if (deep) {
53
+ if (Array.isArray(v))
54
+ v.forEach(x => omitNullish(x, deep));
55
+ else if ((0, is_object_js_1.isPlainObject)(v))
56
+ omitNullish(obj[k], deep);
57
+ }
58
+ }
59
+ return obj;
60
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1 @@
1
+ {"root":["../../src/clone.ts","../../src/index.ts","../../src/is-built-in.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts"],"version":"5.6.3"}
package/esm/clone.js ADDED
@@ -0,0 +1,7 @@
1
+ import { merge } from './merge.js';
2
+ export function clone(obj, options) {
3
+ return merge({}, obj, {
4
+ ...options,
5
+ deep: options?.deep ?? true,
6
+ });
7
+ }
package/esm/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './clone.js';
2
+ export * from './is-built-in.js';
3
+ export * from './is-object.js';
4
+ export * from './merge.js';
5
+ export * from './omit.js';
@@ -0,0 +1,24 @@
1
+ export function isBuiltIn(v) {
2
+ return ((typeof v === 'object' &&
3
+ (v instanceof Date ||
4
+ v instanceof RegExp ||
5
+ v instanceof Map ||
6
+ v instanceof Set ||
7
+ v instanceof WeakMap ||
8
+ v instanceof WeakSet ||
9
+ v instanceof WeakRef ||
10
+ v instanceof Promise ||
11
+ v instanceof Error ||
12
+ v instanceof ArrayBuffer ||
13
+ v instanceof SharedArrayBuffer ||
14
+ v instanceof Uint8Array ||
15
+ v instanceof Uint8ClampedArray ||
16
+ v instanceof Uint16Array ||
17
+ v instanceof Uint32Array ||
18
+ v instanceof BigUint64Array ||
19
+ v instanceof Int8Array ||
20
+ v instanceof Int16Array ||
21
+ v instanceof Int32Array ||
22
+ Buffer.isBuffer(v))) ||
23
+ Array.isArray(v));
24
+ }
@@ -0,0 +1,19 @@
1
+ const objCtorStr = Function.prototype.toString.call(Object);
2
+ export function isObject(v) {
3
+ return v && typeof v === 'object' && !Array.isArray(v);
4
+ }
5
+ export function isPlainObject(obj) {
6
+ if (typeof obj === 'object' &&
7
+ Object.prototype.toString.call(obj) === '[object Object]') {
8
+ const proto = Object.getPrototypeOf(obj);
9
+ /* istanbul ignore next */
10
+ if (!proto)
11
+ return true;
12
+ const ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') &&
13
+ proto.constructor;
14
+ return (typeof ctor === 'function' &&
15
+ ctor instanceof ctor &&
16
+ Function.prototype.toString.call(ctor) === objCtorStr);
17
+ }
18
+ return false;
19
+ }
package/esm/merge.js ADDED
@@ -0,0 +1,225 @@
1
+ import { isObject, isPlainObject } from './is-object.js';
2
+ export function merge(target, source, options) {
3
+ if (!(isObject(target) || typeof target === 'function')) {
4
+ throw new TypeError('"target" argument must be an object');
5
+ }
6
+ source = source || {};
7
+ if (!(isObject(source) || typeof target === 'function')) {
8
+ throw new TypeError('"target" argument must be an object');
9
+ }
10
+ const fn = getMergeFunction(options);
11
+ return fn(target, source, '', options, fn, isPlainObject, isObject, arrayClone);
12
+ }
13
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
+ const functionCache = new Map();
15
+ export function getMergeFunction(options) {
16
+ const cacheKey = [
17
+ options?.deep,
18
+ options?.moveArrays,
19
+ options?.keepExisting,
20
+ options?.copyDescriptors,
21
+ options?.ignore,
22
+ options?.ignoreUndefined,
23
+ options?.ignoreNulls,
24
+ options?.filter,
25
+ ]
26
+ .map(option => option == null
27
+ ? 'n'
28
+ : typeof option === 'function'
29
+ ? 'f'
30
+ : option
31
+ ? '1'
32
+ : '0')
33
+ .join();
34
+ let fn = functionCache.get(cacheKey);
35
+ if (!fn) {
36
+ fn = buildMerge(options);
37
+ functionCache.set(cacheKey, fn);
38
+ }
39
+ return fn;
40
+ }
41
+ function buildMerge(options) {
42
+ const args = [
43
+ 'target',
44
+ 'source',
45
+ 'curPath',
46
+ 'options',
47
+ 'mergeFunction',
48
+ 'isPlainObject',
49
+ 'isObject',
50
+ 'arrayClone',
51
+ ];
52
+ const scriptL0 = [
53
+ `
54
+ const _merge = (_trgVal, _srcVal, _curPath) =>
55
+ mergeFunction(_trgVal, _srcVal, _curPath, options, mergeFunction, isPlainObject, isObject, arrayClone);
56
+ const keys = Object.getOwnPropertyNames(source);
57
+ keys.push(...Object.getOwnPropertySymbols(source));
58
+ let key;
59
+ let descriptor;
60
+ let srcVal;
61
+ let trgVal;
62
+ `,
63
+ ];
64
+ if (options?.deep) {
65
+ scriptL0.push(`let subPath;`, `let _isPlain;`, `let _isArray;`);
66
+ if (typeof options?.deep === 'function') {
67
+ scriptL0.push(`const deepCallback = options.deep;`);
68
+ }
69
+ }
70
+ if (typeof options?.ignore === 'function') {
71
+ scriptL0.push('const ignoreCallback = options.ignore;');
72
+ }
73
+ if (typeof options?.filter === 'function') {
74
+ scriptL0.push('const filterCallback = options.filter;');
75
+ }
76
+ if (typeof options?.copyDescriptors === 'function') {
77
+ scriptL0.push(`const copyDescriptorsCallback = options.copyDescriptors;`);
78
+ }
79
+ if (typeof options?.moveArrays === 'function') {
80
+ scriptL0.push(`const moveArraysCallback = options.moveArrays;`);
81
+ }
82
+ scriptL0.push(`
83
+ let i = 0;
84
+ const len = keys.length;
85
+ for (i = 0; i < len; i++) {
86
+ key = keys[i];
87
+ /** Should not overwrite __proto__ and constructor properties */
88
+ if (key === '__proto__' || key === 'constructor') continue;
89
+ `);
90
+ const scriptL1For = [];
91
+ scriptL0.push(scriptL1For);
92
+ scriptL0.push('}');
93
+ /** ************* filter *****************/
94
+ if (options?.filter) {
95
+ scriptL1For.push(`
96
+ if (!filterCallback(key, curPath, target, source)) {
97
+ delete target[key];
98
+ continue;
99
+ }`);
100
+ }
101
+ /** ************* ignore *****************/
102
+ if (typeof options?.ignore === 'function') {
103
+ scriptL1For.push(`
104
+ if (
105
+ Object.prototype.hasOwnProperty.call(target, key) &&
106
+ ignoreCallback(key, curPath, target, source)
107
+ ) continue;
108
+ `);
109
+ }
110
+ // scriptL1For.push(
111
+ // `descriptor = { ...Object.getOwnPropertyDescriptor(source, key) };`,
112
+ // );
113
+ /** ************* copyDescriptors *****************/
114
+ if (options?.copyDescriptors) {
115
+ let scriptL2Descriptors = scriptL1For;
116
+ if (typeof options?.copyDescriptors === 'function') {
117
+ scriptL1For.push('if (copyDescriptorsCallback(key, curPath, target, source)) {');
118
+ scriptL2Descriptors = [];
119
+ scriptL1For.push(scriptL2Descriptors);
120
+ scriptL1For.push(`} else`);
121
+ scriptL1For.push(` descriptor = {enumerable: true, configurable: true, writable: true}`);
122
+ }
123
+ scriptL2Descriptors.push(`
124
+ descriptor = { ...Object.getOwnPropertyDescriptor(source, key) }
125
+ if ((descriptor.get || descriptor.set)) {
126
+ Object.defineProperty(target, key, descriptor);
127
+ continue;
128
+ }
129
+ srcVal = source[key];`);
130
+ }
131
+ else {
132
+ scriptL1For.push(`descriptor = {enumerable: true, configurable: true, writable: true}`, `srcVal = source[key];`);
133
+ }
134
+ /** ************* keepExisting *****************/
135
+ if (options?.keepExisting) {
136
+ scriptL1For.push(`if (hasOwnProperty.call(target, key)) continue;`);
137
+ }
138
+ /** ************* ignoreUndefined *****************/
139
+ if (options?.ignoreUndefined ?? true) {
140
+ scriptL1For.push(`if (srcVal === undefined) continue;`);
141
+ }
142
+ /** ************* ignoreNulls *****************/
143
+ if (options?.ignoreNulls) {
144
+ scriptL1For.push(`if (srcVal === null) continue;`);
145
+ }
146
+ /** ************* deep *****************/
147
+ if (options?.deep) {
148
+ scriptL1For.push(`
149
+ _isPlain = isPlainObject(srcVal);
150
+ _isArray = Array.isArray(srcVal);
151
+ if (_isPlain || _isArray) {`);
152
+ const scriptL2Deep = [];
153
+ scriptL1For.push(scriptL2Deep);
154
+ scriptL1For.push('}');
155
+ let scriptL3Deep = scriptL2Deep;
156
+ if (typeof options?.deep === 'function') {
157
+ scriptL2Deep.push(`
158
+ subPath = curPath + (curPath ? '.' : '') + key;
159
+ if (deepCallback(key, subPath, target, source)) {`);
160
+ scriptL3Deep = [];
161
+ scriptL2Deep.push(scriptL3Deep);
162
+ scriptL2Deep.push('}');
163
+ }
164
+ /** ************* _isPlain *****************/
165
+ scriptL3Deep.push(`
166
+ if (_isPlain) {
167
+ trgVal = target[key];
168
+ if (!isObject(trgVal)) {
169
+ descriptor.value = trgVal = {};
170
+ Object.defineProperty(target, key, descriptor);
171
+ }
172
+ _merge(trgVal, srcVal, subPath, options);
173
+ continue;
174
+ }`);
175
+ /** ************* moveArrays *****************/
176
+ if (!options?.moveArrays || typeof options?.moveArrays === 'function') {
177
+ scriptL3Deep.push(`if (_isArray) {`);
178
+ const scriptL4IsArray = [];
179
+ scriptL3Deep.push(scriptL4IsArray);
180
+ scriptL3Deep.push('}');
181
+ let scriptL5CloneArrays = scriptL4IsArray;
182
+ if (typeof options?.moveArrays === 'function') {
183
+ scriptL4IsArray.push(`if (!moveArraysCallback(key, subPath, target, source)) {`);
184
+ scriptL5CloneArrays = [];
185
+ scriptL4IsArray.push(scriptL5CloneArrays);
186
+ scriptL4IsArray.push('}');
187
+ }
188
+ scriptL5CloneArrays.push(`
189
+ descriptor.value = arrayClone(srcVal, _merge, subPath);
190
+ Object.defineProperty(target, key, descriptor);
191
+ continue;
192
+ `);
193
+ }
194
+ }
195
+ /** ************* finalize *****************/
196
+ scriptL1For.push(`
197
+ descriptor.value = srcVal;
198
+ Object.defineProperty(target, key, descriptor);`);
199
+ scriptL0.push('return target;');
200
+ const script = _flattenText(scriptL0);
201
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval,prefer-const
202
+ return Function(...args, script);
203
+ }
204
+ function arrayClone(arr, _merge, curPath) {
205
+ return arr.map((x) => {
206
+ if (Array.isArray(x))
207
+ return arrayClone(x, _merge, curPath);
208
+ if (isPlainObject(x))
209
+ return _merge({}, x, curPath);
210
+ return x;
211
+ });
212
+ }
213
+ function _flattenText(arr, level = 0) {
214
+ const indent = ' '.repeat(level);
215
+ return arr
216
+ .map(v => {
217
+ if (Array.isArray(v))
218
+ return _flattenText(v, level + 1);
219
+ return (indent +
220
+ String(v)
221
+ .trim()
222
+ .replace(/\n/g, '\n' + indent));
223
+ })
224
+ .join('\n');
225
+ }
package/esm/omit.js ADDED
@@ -0,0 +1,55 @@
1
+ import { isPlainObject } from './is-object.js';
2
+ export function omitUndefined(obj, deep) {
3
+ /* istanbul ignore next */
4
+ if (!(obj && typeof obj === 'object'))
5
+ return obj;
6
+ let v;
7
+ for (const k of Object.keys(obj)) {
8
+ v = obj[k];
9
+ if (v === undefined)
10
+ delete obj[k];
11
+ else if (deep) {
12
+ if (Array.isArray(v))
13
+ v.forEach(x => omitUndefined(x, deep));
14
+ else if (isPlainObject(v))
15
+ omitUndefined(obj[k], deep);
16
+ }
17
+ }
18
+ return obj;
19
+ }
20
+ export function omitNull(obj, deep) {
21
+ /* istanbul ignore next */
22
+ if (!(obj && typeof obj === 'object'))
23
+ return obj;
24
+ let v;
25
+ for (const k of Object.keys(obj)) {
26
+ v = obj[k];
27
+ if (v === null)
28
+ delete obj[k];
29
+ else if (deep) {
30
+ if (Array.isArray(v))
31
+ v.forEach(x => omitNull(x, deep));
32
+ else if (isPlainObject(v))
33
+ omitNull(obj[k], deep);
34
+ }
35
+ }
36
+ return obj;
37
+ }
38
+ export function omitNullish(obj, deep) {
39
+ /* istanbul ignore next */
40
+ if (!(obj && typeof obj === 'object'))
41
+ return obj;
42
+ let v;
43
+ for (const k of Object.keys(obj)) {
44
+ v = obj[k];
45
+ if (v == null)
46
+ delete obj[k];
47
+ else if (deep) {
48
+ if (Array.isArray(v))
49
+ v.forEach(x => omitNullish(x, deep));
50
+ else if (isPlainObject(v))
51
+ omitNullish(obj[k], deep);
52
+ }
53
+ }
54
+ return obj;
55
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1 @@
1
+ {"root":["../../src/clone.ts","../../src/index.ts","../../src/is-built-in.ts","../../src/is-object.ts","../../src/merge.ts","../../src/omit.ts"],"version":"5.6.3"}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@jsopen/objects",
3
+ "description": "Helper utilities for working with JavaScript objects and arrays",
4
+ "version": "1.0.0",
5
+ "author": "Panates",
6
+ "license": "MIT",
7
+ "dependencies": {
8
+ "tslib": "^2.8.1"
9
+ },
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./types/index.d.ts",
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"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "main": "./cjs/index.js",
26
+ "module": "./esm/index.js",
27
+ "types": "./types/index.d.ts",
28
+ "contributors": [
29
+ "Eray Hanoglu <e.hanoglu@panates.com>"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/panates/jsopen-objects.git"
34
+ },
35
+ "engines": {
36
+ "node": ">= 16.0"
37
+ },
38
+ "files": [
39
+ "cjs/",
40
+ "esm/",
41
+ "types/",
42
+ "LICENSE",
43
+ "README.md",
44
+ "CHANGELOG.md"
45
+ ],
46
+ "keywords": [
47
+ "object",
48
+ "util",
49
+ "utils",
50
+ "merge",
51
+ "clone",
52
+ "deep",
53
+ "omit",
54
+ "is-plain",
55
+ "object-utils"
56
+ ]
57
+ }
@@ -0,0 +1,2 @@
1
+ import { merge } from './merge.js';
2
+ export declare function clone<T extends object>(obj: T, options?: merge.Options): T;
@@ -0,0 +1,5 @@
1
+ export * from './clone.js';
2
+ export * from './is-built-in.js';
3
+ export * from './is-object.js';
4
+ export * from './merge.js';
5
+ export * from './omit.js';
@@ -0,0 +1,5 @@
1
+ export * from './clone.js';
2
+ export * from './is-built-in.js';
3
+ export * from './is-object.js';
4
+ export * from './merge.js';
5
+ export * from './omit.js';
@@ -0,0 +1 @@
1
+ export declare function isBuiltIn(v: any): boolean;
@@ -0,0 +1,2 @@
1
+ export declare function isObject(v: any): boolean;
2
+ export declare function isPlainObject(obj: any): boolean;
@@ -0,0 +1,39 @@
1
+ export declare namespace merge {
2
+ type NodeCallback = (key: string | symbol, path: string, target: any, source: any) => boolean;
3
+ interface Options {
4
+ deep?: boolean | NodeCallback;
5
+ /**
6
+ */
7
+ moveArrays?: boolean | NodeCallback;
8
+ /**
9
+ * Do not overwrite existing properties if set true
10
+ * @default false
11
+ */
12
+ keepExisting?: boolean;
13
+ /**
14
+ * Copy property descriptors
15
+ * @default false
16
+ */
17
+ copyDescriptors?: boolean | NodeCallback;
18
+ /**
19
+ * Do not copy source field if callback returns true
20
+ */
21
+ ignore?: NodeCallback;
22
+ /**
23
+ * Ignore fields which values are "undefined"
24
+ * @default true
25
+ */
26
+ ignoreUndefined?: boolean;
27
+ /**
28
+ * Ignore fields which values are "null"
29
+ * @default false
30
+ */
31
+ ignoreNulls?: boolean;
32
+ /**
33
+ *
34
+ */
35
+ filter?: NodeCallback;
36
+ }
37
+ }
38
+ export declare function merge<A, B>(target: A, source: B, options?: merge.Options): A & B;
39
+ export declare function getMergeFunction(options?: merge.Options): Function;
@@ -0,0 +1,4 @@
1
+ import { DeeperOmitTypes } from 'ts-gems';
2
+ export declare function omitUndefined<T>(obj: T, deep?: boolean): DeeperOmitTypes<T, undefined>;
3
+ export declare function omitNull<T>(obj: T, deep?: boolean): DeeperOmitTypes<T, null>;
4
+ export declare function omitNullish<T>(obj: T, deep?: boolean): DeeperOmitTypes<T, null | undefined>;