@jsopen/objects 2.0.2 → 2.1.1

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.
@@ -3,3 +3,4 @@ export * from './is-object.js';
3
3
  export * from './merge.js';
4
4
  export * from './omit.js';
5
5
  export * from './type-guards.js';
6
+ export { updateErrorMessage } from './update-error-message.js';
@@ -3,3 +3,4 @@ export * from './is-object.js';
3
3
  export * from './merge.js';
4
4
  export * from './omit.js';
5
5
  export * from './type-guards.js';
6
+ export { updateErrorMessage } from './update-error-message.js';
@@ -64,7 +64,21 @@ export function merge(targetObject, sourceObject, options) {
64
64
  })) {
65
65
  continue;
66
66
  }
67
- if (keepExisting && hasOwnProperty.call(target, key)) {
67
+ _goDeep = !!(deep &&
68
+ typeof srcVal === 'object' &&
69
+ (!isBuiltIn(srcVal) || Array.isArray(srcVal)));
70
+ if (_goDeep) {
71
+ if (deepFn)
72
+ _goDeep = deepFn(srcVal, {
73
+ key,
74
+ source,
75
+ target,
76
+ path: parentPath + (parentPath ? '.' : '') + String(key),
77
+ });
78
+ else
79
+ _goDeep = deepFull || isPlainObject(srcVal) || Array.isArray(srcVal);
80
+ }
81
+ if (!_goDeep && keepExisting && hasOwnProperty.call(target, key)) {
68
82
  if (!keepExistingFn)
69
83
  continue;
70
84
  if (keepExistingFn(srcVal, {
@@ -91,46 +105,32 @@ export function merge(targetObject, sourceObject, options) {
91
105
  if (ignoreNulls && srcVal === null) {
92
106
  continue;
93
107
  }
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;
108
+ if (_goDeep) {
109
+ /** Array */
110
+ if (Array.isArray(srcVal)) {
111
+ if (Array.isArray(target[key]) &&
112
+ (mergeArrays ||
113
+ mergeArraysFn?.(srcVal, {
114
+ key,
115
+ source,
116
+ target,
117
+ path: parentPath + (parentPath ? '.' : '') + String(key),
118
+ }))) {
119
+ target[key] = _arrayClone(target[key], parentPath + (parentPath ? '.' : '') + String(key));
133
120
  }
121
+ else
122
+ target[key] = [];
123
+ target[key].push(..._arrayClone(srcVal, parentPath + (parentPath ? '.' : '') + String(key)));
124
+ if (mergeArraysUnique)
125
+ target[key] = Array.from(new Set(target[key]));
126
+ continue;
127
+ }
128
+ else {
129
+ /** Object */
130
+ if (!isObject(target[key]))
131
+ target[key] = {};
132
+ _merge(target[key], srcVal, parentPath + (parentPath ? '.' : '') + String(key));
133
+ continue;
134
134
  }
135
135
  }
136
136
  if (copyDescriptors) {
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.0.2",
4
+ "version": "2.1.1",
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
- "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"
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",
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Updates the error message and stack trace at sametime.
3
+ * @param err
4
+ * @param newMessage
5
+ */
6
+ export declare function updateErrorMessage(err: Error, newMessage: string): Error | undefined;
7
+ /**
8
+ * Updates the error message and stack trace at sametime.
9
+ * @param err
10
+ * @param newMessage
11
+ */
12
+ export declare function updateErrorMessageFallback(err: Error, newMessage: string): Error;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Updates the error message and stack trace at sametime.
3
+ * @param err
4
+ * @param newMessage
5
+ */
6
+ export function updateErrorMessage(err, newMessage) {
7
+ err.message = String(newMessage);
8
+ /** V8 */
9
+ if (typeof Error.captureStackTrace === 'function') {
10
+ Error.captureStackTrace(err);
11
+ return;
12
+ }
13
+ /** Other engines */
14
+ return updateErrorMessageFallback(err, newMessage);
15
+ }
16
+ /**
17
+ * Updates the error message and stack trace at sametime.
18
+ * @param err
19
+ * @param newMessage
20
+ */
21
+ export function updateErrorMessageFallback(err, newMessage) {
22
+ err.message = String(newMessage);
23
+ /** Other engines */
24
+ const stack = typeof err.stack === 'string' ? err.stack : null;
25
+ if (!stack)
26
+ return err;
27
+ const name = err.name || 'Error';
28
+ const lines = stack.split('\n');
29
+ const firstFrameIdx = lines.findIndex(l => /^\s*at\s+/.test(l));
30
+ if (firstFrameIdx === -1) {
31
+ const msgLines = String(newMessage).split(/\r?\n/);
32
+ lines[0] = `${name}: ${msgLines[0] ?? ''}`;
33
+ lines.splice(1, 0, ...msgLines.slice(1));
34
+ err.stack = lines.join('\n');
35
+ return err;
36
+ }
37
+ const frameLines = lines.slice(firstFrameIdx);
38
+ const msgLines = String(newMessage).split(/\r?\n/);
39
+ const newHead = [`${name}: ${msgLines[0] ?? ''}`, ...msgLines.slice(1)];
40
+ err.stack = [...newHead, ...frameLines].join('\n');
41
+ return err;
42
+ }
package/CHANGELOG.md DELETED
@@ -1,7 +0,0 @@
1
- ## Changelog
2
-
3
- ### [v2.0.2](https://github.com/panates/jsopen-objects/compare/v2.0.1...v2.0.2) -
4
-
5
- #### 🛠 Refactoring and Updates
6
-
7
- - refactor: Minor typing changes @Eray Hanoğlu
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,8 +0,0 @@
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-object.js"), exports);
6
- tslib_1.__exportStar(require("./merge.js"), exports);
7
- tslib_1.__exportStar(require("./omit.js"), exports);
8
- tslib_1.__exportStar(require("./type-guards.js"), exports);
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,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -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"],"version":"5.9.2"}
@@ -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
- }
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
@@ -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"],"version":"5.9.2"}
package/types/index.d.cts DELETED
@@ -1,5 +0,0 @@
1
- export * from './clone.js';
2
- export * from './is-object.js';
3
- export * from './merge.js';
4
- export * from './omit.js';
5
- export * from './type-guards.js';
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes