merge-options 1.0.0 → 1.0.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.
- package/index.js +18 -10
- package/license +1 -1
- package/package.json +5 -5
package/index.js
CHANGED
@@ -3,6 +3,13 @@ const isOptionObject = require('is-plain-obj');
|
|
3
3
|
|
4
4
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
5
5
|
const propIsEnumerable = Object.propertyIsEnumerable;
|
6
|
+
const defineProperty = (obj, name, value) => Object.defineProperty(obj, name, {
|
7
|
+
value,
|
8
|
+
writable: true,
|
9
|
+
enumerable: true,
|
10
|
+
configurable: true
|
11
|
+
});
|
12
|
+
|
6
13
|
const globalThis = this;
|
7
14
|
const defaultMergeOpts = {
|
8
15
|
concatArrays: false
|
@@ -47,7 +54,7 @@ function cloneArray(array) {
|
|
47
54
|
const result = array.slice(0, 0);
|
48
55
|
|
49
56
|
getEnumerableOwnPropertyKeys(array).forEach(key => {
|
50
|
-
result
|
57
|
+
defineProperty(result, key, clone(array[key]));
|
51
58
|
});
|
52
59
|
|
53
60
|
return result;
|
@@ -57,7 +64,7 @@ function cloneOptionObject(obj) {
|
|
57
64
|
const result = Object.getPrototypeOf(obj) === null ? Object.create(null) : {};
|
58
65
|
|
59
66
|
getEnumerableOwnPropertyKeys(obj).forEach(key => {
|
60
|
-
result
|
67
|
+
defineProperty(result, key, clone(obj[key]));
|
61
68
|
});
|
62
69
|
|
63
70
|
return result;
|
@@ -69,10 +76,11 @@ function cloneOptionObject(obj) {
|
|
69
76
|
*/
|
70
77
|
const mergeKeys = (merged, source, keys, mergeOpts) => {
|
71
78
|
keys.forEach(key => {
|
72
|
-
|
73
|
-
|
79
|
+
// Do not recurse into prototype chain of merged
|
80
|
+
if (key in merged && merged[key] !== Object.getPrototypeOf(merged)) {
|
81
|
+
defineProperty(merged, key, merge(merged[key], source[key], mergeOpts));
|
74
82
|
} else {
|
75
|
-
merged
|
83
|
+
defineProperty(merged, key, clone(source[key]));
|
76
84
|
}
|
77
85
|
});
|
78
86
|
|
@@ -102,9 +110,9 @@ const concatArrays = (merged, source, mergeOpts) => {
|
|
102
110
|
|
103
111
|
if (array === merged) {
|
104
112
|
// Already cloned
|
105
|
-
result
|
113
|
+
defineProperty(result, resultIndex++, array[k]);
|
106
114
|
} else {
|
107
|
-
result
|
115
|
+
defineProperty(result, resultIndex++, clone(array[k]));
|
108
116
|
}
|
109
117
|
}
|
110
118
|
|
@@ -135,7 +143,7 @@ function merge(merged, source, mergeOpts) {
|
|
135
143
|
|
136
144
|
module.exports = function () {
|
137
145
|
const mergeOpts = merge(clone(defaultMergeOpts), (this !== globalThis && this) || {}, defaultMergeOpts);
|
138
|
-
let merged = {};
|
146
|
+
let merged = {foobar: {}};
|
139
147
|
|
140
148
|
for (let i = 0; i < arguments.length; i++) {
|
141
149
|
const option = arguments[i];
|
@@ -148,8 +156,8 @@ module.exports = function () {
|
|
148
156
|
throw new TypeError('`' + option + '` is not an Option Object');
|
149
157
|
}
|
150
158
|
|
151
|
-
merged = merge(merged, option, mergeOpts);
|
159
|
+
merged = merge(merged, {foobar: option}, mergeOpts);
|
152
160
|
}
|
153
161
|
|
154
|
-
return merged;
|
162
|
+
return merged.foobar;
|
155
163
|
};
|
package/license
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "merge-options",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.1",
|
4
4
|
"description": "Merge Option Objects",
|
5
5
|
"license": "MIT",
|
6
6
|
"repository": "schnittstabil/merge-options",
|
@@ -30,11 +30,11 @@
|
|
30
30
|
"clone"
|
31
31
|
],
|
32
32
|
"devDependencies": {
|
33
|
-
"ava": "^0.
|
34
|
-
"coveralls": "^
|
35
|
-
"nyc": "^
|
33
|
+
"ava": "^0.25",
|
34
|
+
"coveralls": "^3.0",
|
35
|
+
"nyc": "^11.7",
|
36
36
|
"rimraf": "^2.5",
|
37
|
-
"xo": "^0.
|
37
|
+
"xo": "^0.20"
|
38
38
|
},
|
39
39
|
"dependencies": {
|
40
40
|
"is-plain-obj": "^1.1"
|