@libp2p/utils 6.7.2-e8398d97e → 6.7.2-f0afebfdf
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/index.min.js +2 -2
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/merge-options.d.ts +7 -0
- package/dist/src/merge-options.d.ts.map +1 -0
- package/dist/src/merge-options.js +128 -0
- package/dist/src/merge-options.js.map +1 -0
- package/package.json +6 -5
- package/src/index.ts +1 -0
- package/src/merge-options.ts +161 -0
package/dist/src/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from './is-generator.js';
|
|
|
21
21
|
export * from './is-promise.js';
|
|
22
22
|
export * from './length-prefixed-decoder.js';
|
|
23
23
|
export * from './link-local-ip.js';
|
|
24
|
+
export * from './merge-options.js';
|
|
24
25
|
export * from './mock-muxer.js';
|
|
25
26
|
export * from './mock-stream.js';
|
|
26
27
|
export * from './moving-average.js';
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,kBAAkB,CAAA;AAChC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,oCAAoC,CAAA;AAClD,cAAc,4BAA4B,CAAA;AAC1C,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,+BAA+B,CAAA;AAC7C,cAAc,wBAAwB,CAAA;AACtC,cAAc,2BAA2B,CAAA;AACzC,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cA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AAA;AACjC,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA"}
|
package/dist/src/index.js
CHANGED
|
@@ -21,6 +21,7 @@ export * from './is-generator.js';
|
|
|
21
21
|
export * from './is-promise.js';
|
|
22
22
|
export * from './length-prefixed-decoder.js';
|
|
23
23
|
export * from './link-local-ip.js';
|
|
24
|
+
export * from './merge-options.js';
|
|
24
25
|
export * from './mock-muxer.js';
|
|
25
26
|
export * from './mock-stream.js';
|
|
26
27
|
export * from './moving-average.js';
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,kBAAkB,CAAA;AAChC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,oCAAoC,CAAA;AAClD,cAAc,4BAA4B,CAAA;AAC1C,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,+BAA+B,CAAA;AAC7C,cAAc,wBAAwB,CAAA;AACtC,cAAc,2BAA2B,CAAA;AACzC,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,8BAA8B,CAAA;AAC5C,cAAc,oBAAoB,CAAA;AAClC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,kBAAkB,CAAA;AAChC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,oCAAoC,CAAA;AAClD,cAAc,4BAA4B,CAAA;AAC1C,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,+BAA+B,CAAA;AAC7C,cAAc,wBAAwB,CAAA;AACtC,cAAc,2BAA2B,CAAA;AACzC,cAAc,yBAAyB,CAAA;AACvC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,8BAA8B,CAAA;AAC5C,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-options.d.ts","sourceRoot":"","sources":["../../src/merge-options.ts"],"names":[],"mappings":"AA0IA;;;;GAIG;AACH,wBAAgB,YAAY,CAAE,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE,GAAG,GAAG,CAiB/D"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import isOptionObject from 'is-plain-obj';
|
|
2
|
+
const { hasOwnProperty } = Object.prototype;
|
|
3
|
+
const { propertyIsEnumerable } = Object;
|
|
4
|
+
const defineProperty = (object, name, value) => {
|
|
5
|
+
Object.defineProperty(object, name, {
|
|
6
|
+
value,
|
|
7
|
+
writable: true,
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
const globalThis = this;
|
|
13
|
+
const defaultMergeOptions = {
|
|
14
|
+
concatArrays: false,
|
|
15
|
+
ignoreUndefined: false
|
|
16
|
+
};
|
|
17
|
+
const getEnumerableOwnPropertyKeys = (value) => {
|
|
18
|
+
const keys = [];
|
|
19
|
+
for (const key in value) {
|
|
20
|
+
if (hasOwnProperty.call(value, key)) {
|
|
21
|
+
keys.push(key);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/* istanbul ignore else */
|
|
25
|
+
if (Object.getOwnPropertySymbols) {
|
|
26
|
+
const symbols = Object.getOwnPropertySymbols(value);
|
|
27
|
+
for (const symbol of symbols) {
|
|
28
|
+
if (propertyIsEnumerable.call(value, symbol)) {
|
|
29
|
+
keys.push(symbol);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return keys;
|
|
34
|
+
};
|
|
35
|
+
function clone(value) {
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
return cloneArray(value);
|
|
38
|
+
}
|
|
39
|
+
if (isOptionObject(value)) {
|
|
40
|
+
return cloneOptionObject(value);
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
function cloneArray(array) {
|
|
45
|
+
const result = array.slice(0, 0);
|
|
46
|
+
getEnumerableOwnPropertyKeys(array).forEach(key => {
|
|
47
|
+
defineProperty(result, key, clone(array[key]));
|
|
48
|
+
});
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
function cloneOptionObject(object) {
|
|
52
|
+
const result = Object.getPrototypeOf(object) === null ? Object.create(null) : {};
|
|
53
|
+
getEnumerableOwnPropertyKeys(object).forEach(key => {
|
|
54
|
+
defineProperty(result, key, clone(object[key]));
|
|
55
|
+
});
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
const mergeKeys = (merged, source, keys, config) => {
|
|
59
|
+
keys.forEach(key => {
|
|
60
|
+
if (typeof source[key] === 'undefined' && config.ignoreUndefined) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Do not recurse into prototype chain of merged
|
|
64
|
+
if (key in merged && merged[key] !== Object.getPrototypeOf(merged)) {
|
|
65
|
+
defineProperty(merged, key, merge(merged[key], source[key], config));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
defineProperty(merged, key, clone(source[key]));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return merged;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* see [Array.prototype.concat ( ...arguments )](http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.concat)
|
|
75
|
+
*/
|
|
76
|
+
const concatArrays = (merged, source, config) => {
|
|
77
|
+
let result = merged.slice(0, 0);
|
|
78
|
+
let resultIndex = 0;
|
|
79
|
+
[merged, source].forEach(array => {
|
|
80
|
+
const indices = [];
|
|
81
|
+
// `result.concat(array)` with cloning
|
|
82
|
+
for (let k = 0; k < array.length; k++) {
|
|
83
|
+
if (!hasOwnProperty.call(array, k)) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
indices.push(String(k));
|
|
87
|
+
if (array === merged) {
|
|
88
|
+
// Already cloned
|
|
89
|
+
defineProperty(result, resultIndex++, array[k]);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
defineProperty(result, resultIndex++, clone(array[k]));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Merge non-index keys
|
|
96
|
+
result = mergeKeys(result, array, getEnumerableOwnPropertyKeys(array).filter(key => !indices.includes(key)), config);
|
|
97
|
+
});
|
|
98
|
+
return result;
|
|
99
|
+
};
|
|
100
|
+
function merge(merged, source, config) {
|
|
101
|
+
if (config.concatArrays && Array.isArray(merged) && Array.isArray(source)) {
|
|
102
|
+
return concatArrays(merged, source, config);
|
|
103
|
+
}
|
|
104
|
+
if (!isOptionObject(source) || !isOptionObject(merged)) {
|
|
105
|
+
return clone(source);
|
|
106
|
+
}
|
|
107
|
+
return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), config);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Port of `merge-options` to typescript
|
|
111
|
+
*
|
|
112
|
+
* @see https://github.com/schnittstabil/merge-options/pull/28
|
|
113
|
+
*/
|
|
114
|
+
export function mergeOptions(...options) {
|
|
115
|
+
const config = merge(clone(defaultMergeOptions), (this !== globalThis && this) || {}, defaultMergeOptions);
|
|
116
|
+
let merged = { _: {} };
|
|
117
|
+
for (const option of options) {
|
|
118
|
+
if (option === undefined) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!isOptionObject(option)) {
|
|
122
|
+
throw new TypeError('`' + option + '` is not an Option Object');
|
|
123
|
+
}
|
|
124
|
+
merged = merge(merged, { _: option }, config);
|
|
125
|
+
}
|
|
126
|
+
return merged._;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=merge-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-options.js","sourceRoot":"","sources":["../../src/merge-options.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,cAAc,CAAA;AAEzC,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC,SAAS,CAAA;AAC3C,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,CAAA;AACvC,MAAM,cAAc,GAAG,CAAC,MAAW,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AM,GAAG,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,MAAM,CAAC,CAAA;IAC/C,CAAC;IAED,OAAO,MAAM,CAAC,CAAC,CAAA;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "6.7.2-
|
|
3
|
+
"version": "6.7.2-f0afebfdf",
|
|
4
4
|
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
|
|
@@ -46,15 +46,16 @@
|
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@chainsafe/is-ip": "^2.1.0",
|
|
48
48
|
"@chainsafe/netmask": "^2.0.0",
|
|
49
|
-
"@libp2p/crypto": "5.1.8-
|
|
50
|
-
"@libp2p/interface": "2.11.0-
|
|
51
|
-
"@libp2p/logger": "5.2.0-
|
|
49
|
+
"@libp2p/crypto": "5.1.8-f0afebfdf",
|
|
50
|
+
"@libp2p/interface": "2.11.0-f0afebfdf",
|
|
51
|
+
"@libp2p/logger": "5.2.0-f0afebfdf",
|
|
52
52
|
"@multiformats/multiaddr": "^13.0.1",
|
|
53
53
|
"@sindresorhus/fnv1a": "^3.1.0",
|
|
54
54
|
"any-signal": "^4.1.1",
|
|
55
55
|
"cborg": "^4.2.14",
|
|
56
56
|
"delay": "^6.0.0",
|
|
57
57
|
"is-loopback-addr": "^2.0.2",
|
|
58
|
+
"is-plain-obj": "^4.1.0",
|
|
58
59
|
"it-length-prefixed": "^10.0.1",
|
|
59
60
|
"it-pipe": "^3.0.1",
|
|
60
61
|
"it-pushable": "^3.2.3",
|
|
@@ -69,7 +70,7 @@
|
|
|
69
70
|
"uint8arrays": "^5.1.0"
|
|
70
71
|
},
|
|
71
72
|
"devDependencies": {
|
|
72
|
-
"@libp2p/peer-id": "5.1.9-
|
|
73
|
+
"@libp2p/peer-id": "5.1.9-f0afebfdf",
|
|
73
74
|
"@types/netmask": "^2.0.5",
|
|
74
75
|
"aegir": "^47.0.22",
|
|
75
76
|
"benchmark": "^2.1.4",
|
package/src/index.ts
CHANGED
|
@@ -22,6 +22,7 @@ export * from './is-generator.js'
|
|
|
22
22
|
export * from './is-promise.js'
|
|
23
23
|
export * from './length-prefixed-decoder.js'
|
|
24
24
|
export * from './link-local-ip.js'
|
|
25
|
+
export * from './merge-options.js'
|
|
25
26
|
export * from './mock-muxer.js'
|
|
26
27
|
export * from './mock-stream.js'
|
|
27
28
|
export * from './moving-average.js'
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import isOptionObject from 'is-plain-obj'
|
|
2
|
+
|
|
3
|
+
const { hasOwnProperty } = Object.prototype
|
|
4
|
+
const { propertyIsEnumerable } = Object
|
|
5
|
+
const defineProperty = (object: any, name: any, value: any): void => {
|
|
6
|
+
Object.defineProperty(object, name, {
|
|
7
|
+
value,
|
|
8
|
+
writable: true,
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const globalThis = this
|
|
15
|
+
const defaultMergeOptions = {
|
|
16
|
+
concatArrays: false,
|
|
17
|
+
ignoreUndefined: false
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const getEnumerableOwnPropertyKeys = (value: any): any[] => {
|
|
21
|
+
const keys = []
|
|
22
|
+
|
|
23
|
+
for (const key in value) {
|
|
24
|
+
if (hasOwnProperty.call(value, key)) {
|
|
25
|
+
keys.push(key)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* istanbul ignore else */
|
|
30
|
+
if (Object.getOwnPropertySymbols) {
|
|
31
|
+
const symbols = Object.getOwnPropertySymbols(value)
|
|
32
|
+
|
|
33
|
+
for (const symbol of symbols) {
|
|
34
|
+
if (propertyIsEnumerable.call(value, symbol)) {
|
|
35
|
+
keys.push(symbol)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return keys
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function clone <T> (value: T): T
|
|
44
|
+
function clone <T> (value: T[]): T[]
|
|
45
|
+
function clone (value: any): any {
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
return cloneArray(value)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isOptionObject(value)) {
|
|
51
|
+
return cloneOptionObject(value)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function cloneArray <T> (array: T[]): T[] {
|
|
58
|
+
const result = array.slice(0, 0)
|
|
59
|
+
|
|
60
|
+
getEnumerableOwnPropertyKeys(array).forEach(key => {
|
|
61
|
+
defineProperty(result, key, clone(array[key]))
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return result
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function cloneOptionObject (object: any): any {
|
|
68
|
+
const result = Object.getPrototypeOf(object) === null ? Object.create(null) : {}
|
|
69
|
+
|
|
70
|
+
getEnumerableOwnPropertyKeys(object).forEach(key => {
|
|
71
|
+
defineProperty(result, key, clone(object[key]))
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return result
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const mergeKeys = (merged: any, source: any, keys: any[], config: any): any => {
|
|
78
|
+
keys.forEach(key => {
|
|
79
|
+
if (typeof source[key] === 'undefined' && config.ignoreUndefined) {
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Do not recurse into prototype chain of merged
|
|
84
|
+
if (key in merged && merged[key] !== Object.getPrototypeOf(merged)) {
|
|
85
|
+
defineProperty(merged, key, merge(merged[key], source[key], config))
|
|
86
|
+
} else {
|
|
87
|
+
defineProperty(merged, key, clone(source[key]))
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return merged
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* see [Array.prototype.concat ( ...arguments )](http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.concat)
|
|
96
|
+
*/
|
|
97
|
+
const concatArrays = (merged: any, source: any, config: any): any => {
|
|
98
|
+
let result = merged.slice(0, 0)
|
|
99
|
+
let resultIndex = 0;
|
|
100
|
+
|
|
101
|
+
[merged, source].forEach(array => {
|
|
102
|
+
const indices: any[] = []
|
|
103
|
+
|
|
104
|
+
// `result.concat(array)` with cloning
|
|
105
|
+
for (let k = 0; k < array.length; k++) {
|
|
106
|
+
if (!hasOwnProperty.call(array, k)) {
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
indices.push(String(k))
|
|
111
|
+
|
|
112
|
+
if (array === merged) {
|
|
113
|
+
// Already cloned
|
|
114
|
+
defineProperty(result, resultIndex++, array[k])
|
|
115
|
+
} else {
|
|
116
|
+
defineProperty(result, resultIndex++, clone(array[k]))
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Merge non-index keys
|
|
121
|
+
result = mergeKeys(result, array, getEnumerableOwnPropertyKeys(array).filter(key => !indices.includes(key)), config)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return result
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function merge (merged: any, source: any, config: any): any {
|
|
128
|
+
if (config.concatArrays && Array.isArray(merged) && Array.isArray(source)) {
|
|
129
|
+
return concatArrays(merged, source, config)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!isOptionObject(source) || !isOptionObject(merged)) {
|
|
133
|
+
return clone(source)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), config)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Port of `merge-options` to typescript
|
|
141
|
+
*
|
|
142
|
+
* @see https://github.com/schnittstabil/merge-options/pull/28
|
|
143
|
+
*/
|
|
144
|
+
export function mergeOptions (this: any, ...options: any[]): any {
|
|
145
|
+
const config = merge(clone(defaultMergeOptions), (this !== globalThis && this) || {}, defaultMergeOptions)
|
|
146
|
+
let merged = { _: {} }
|
|
147
|
+
|
|
148
|
+
for (const option of options) {
|
|
149
|
+
if (option === undefined) {
|
|
150
|
+
continue
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!isOptionObject(option)) {
|
|
154
|
+
throw new TypeError('`' + option + '` is not an Option Object')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
merged = merge(merged, { _: option }, config)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return merged._
|
|
161
|
+
}
|