merge-anything 5.1.4 → 5.1.6
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.cjs +110 -110
- package/dist/index.es.js +110 -110
- package/dist/types/extensions.d.ts +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/merge.d.ts +17 -17
- package/dist/types/typeUtils/Assign.d.ts +50 -50
- package/dist/types/typeUtils/Iteration.d.ts +257 -257
- package/dist/types/typeUtils/List.d.ts +29 -29
- package/dist/types/typeUtils/MergeDeep.d.ts +57 -57
- package/dist/types/typeUtils/PrettyPrint.d.ts +6 -6
- package/package.json +12 -12
package/dist/index.cjs
CHANGED
|
@@ -2,118 +2,118 @@
|
|
|
2
2
|
|
|
3
3
|
var isWhat = require('is-what');
|
|
4
4
|
|
|
5
|
-
function concatArrays(originVal, newVal) {
|
|
6
|
-
if (isWhat.isArray(originVal) && isWhat.isArray(newVal)) {
|
|
7
|
-
// concat logic
|
|
8
|
-
return originVal.concat(newVal);
|
|
9
|
-
}
|
|
10
|
-
return newVal; // always return newVal as fallback!!
|
|
5
|
+
function concatArrays(originVal, newVal) {
|
|
6
|
+
if (isWhat.isArray(originVal) && isWhat.isArray(newVal)) {
|
|
7
|
+
// concat logic
|
|
8
|
+
return originVal.concat(newVal);
|
|
9
|
+
}
|
|
10
|
+
return newVal; // always return newVal as fallback!!
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
function assignProp(carry, key, newVal, originalObject) {
|
|
14
|
-
const propType = {}.propertyIsEnumerable.call(originalObject, key)
|
|
15
|
-
? 'enumerable'
|
|
16
|
-
: 'nonenumerable';
|
|
17
|
-
if (propType === 'enumerable')
|
|
18
|
-
carry[key] = newVal;
|
|
19
|
-
if (propType === 'nonenumerable') {
|
|
20
|
-
Object.defineProperty(carry, key, {
|
|
21
|
-
value: newVal,
|
|
22
|
-
enumerable: false,
|
|
23
|
-
writable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function mergeRecursively(origin, newComer, compareFn) {
|
|
29
|
-
// always return newComer if its not an object
|
|
30
|
-
if (!isWhat.isPlainObject(newComer))
|
|
31
|
-
return newComer;
|
|
32
|
-
// define newObject to merge all values upon
|
|
33
|
-
let newObject = {};
|
|
34
|
-
if (isWhat.isPlainObject(origin)) {
|
|
35
|
-
const props = Object.getOwnPropertyNames(origin);
|
|
36
|
-
const symbols = Object.getOwnPropertySymbols(origin);
|
|
37
|
-
newObject = [...props, ...symbols].reduce((carry, key) => {
|
|
38
|
-
const targetVal = origin[key];
|
|
39
|
-
if ((!isWhat.isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key)) ||
|
|
40
|
-
(isWhat.isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key))) {
|
|
41
|
-
assignProp(carry, key, targetVal, origin);
|
|
42
|
-
}
|
|
43
|
-
return carry;
|
|
44
|
-
}, {});
|
|
45
|
-
}
|
|
46
|
-
// newObject has all properties that newComer hasn't
|
|
47
|
-
const props = Object.getOwnPropertyNames(newComer);
|
|
48
|
-
const symbols = Object.getOwnPropertySymbols(newComer);
|
|
49
|
-
const result = [...props, ...symbols].reduce((carry, key) => {
|
|
50
|
-
// re-define the origin and newComer as targetVal and newVal
|
|
51
|
-
let newVal = newComer[key];
|
|
52
|
-
const targetVal = isWhat.isPlainObject(origin) ? origin[key] : undefined;
|
|
53
|
-
// When newVal is an object do the merge recursively
|
|
54
|
-
if (targetVal !== undefined && isWhat.isPlainObject(newVal)) {
|
|
55
|
-
newVal = mergeRecursively(targetVal, newVal, compareFn);
|
|
56
|
-
}
|
|
57
|
-
const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
|
|
58
|
-
assignProp(carry, key, propToAssign, newComer);
|
|
59
|
-
return carry;
|
|
60
|
-
}, newObject);
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Merge anything recursively.
|
|
65
|
-
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
66
|
-
* Basic types overwrite objects or other basic types.
|
|
67
|
-
*/
|
|
68
|
-
function merge(object, ...otherObjects) {
|
|
69
|
-
return otherObjects.reduce((result, newComer) => {
|
|
70
|
-
return mergeRecursively(result, newComer);
|
|
71
|
-
}, object);
|
|
72
|
-
}
|
|
73
|
-
function mergeAndCompare(compareFn, object, ...otherObjects) {
|
|
74
|
-
return otherObjects.reduce((result, newComer) => {
|
|
75
|
-
return mergeRecursively(result, newComer, compareFn);
|
|
76
|
-
}, object);
|
|
77
|
-
}
|
|
78
|
-
function mergeAndConcat(object, ...otherObjects) {
|
|
79
|
-
return otherObjects.reduce((result, newComer) => {
|
|
80
|
-
return mergeRecursively(result, newComer, concatArrays);
|
|
81
|
-
}, object);
|
|
82
|
-
}
|
|
83
|
-
// import { Timestamp } from '../test/Timestamp'
|
|
84
|
-
// type T1 = { date: Timestamp }
|
|
85
|
-
// type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
|
|
86
|
-
// type TestT = Merge<T1, T2>
|
|
87
|
-
// type A1 = { arr: string[] }
|
|
88
|
-
// type A2 = { arr: number[] }
|
|
89
|
-
// type A3 = { arr: boolean[] }
|
|
90
|
-
// type TestA = Merge<A1, [A2, A3]>
|
|
91
|
-
// interface I1 {
|
|
92
|
-
// date: Timestamp
|
|
93
|
-
// }
|
|
94
|
-
// interface I2 {
|
|
95
|
-
// date: Timestamp
|
|
96
|
-
// }
|
|
97
|
-
// const _a: I2 = { date: '' } as unknown as I2
|
|
98
|
-
// type TestI = Merge<I1, [I2]>
|
|
99
|
-
// // ReturnType<(typeof merge)<I1, I2>>
|
|
100
|
-
// const a = merge(_a, [_a])
|
|
101
|
-
// interface Arguments extends Record<string | number | symbol, unknown> {
|
|
102
|
-
// key: string;
|
|
103
|
-
// }
|
|
104
|
-
// const aa1: Arguments = { key: "value1" }
|
|
105
|
-
// const aa2: Arguments = { key: "value2" }
|
|
106
|
-
// const aa = merge(a1, a2);
|
|
107
|
-
// interface Barguments {
|
|
108
|
-
// key: string
|
|
109
|
-
// }
|
|
110
|
-
// const ba1: Barguments = { key: 'value1' }
|
|
111
|
-
// const ba2: Barguments = { key: 'value2' }
|
|
112
|
-
// const ba = merge(ba1, ba2)
|
|
113
|
-
// interface Carguments {
|
|
114
|
-
// key: string
|
|
115
|
-
// }
|
|
116
|
-
// const ca = merge<Carguments, Carguments[]>({ key: 'value1' }, { key: 'value2' })
|
|
13
|
+
function assignProp(carry, key, newVal, originalObject) {
|
|
14
|
+
const propType = {}.propertyIsEnumerable.call(originalObject, key)
|
|
15
|
+
? 'enumerable'
|
|
16
|
+
: 'nonenumerable';
|
|
17
|
+
if (propType === 'enumerable')
|
|
18
|
+
carry[key] = newVal;
|
|
19
|
+
if (propType === 'nonenumerable') {
|
|
20
|
+
Object.defineProperty(carry, key, {
|
|
21
|
+
value: newVal,
|
|
22
|
+
enumerable: false,
|
|
23
|
+
writable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function mergeRecursively(origin, newComer, compareFn) {
|
|
29
|
+
// always return newComer if its not an object
|
|
30
|
+
if (!isWhat.isPlainObject(newComer))
|
|
31
|
+
return newComer;
|
|
32
|
+
// define newObject to merge all values upon
|
|
33
|
+
let newObject = {};
|
|
34
|
+
if (isWhat.isPlainObject(origin)) {
|
|
35
|
+
const props = Object.getOwnPropertyNames(origin);
|
|
36
|
+
const symbols = Object.getOwnPropertySymbols(origin);
|
|
37
|
+
newObject = [...props, ...symbols].reduce((carry, key) => {
|
|
38
|
+
const targetVal = origin[key];
|
|
39
|
+
if ((!isWhat.isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key)) ||
|
|
40
|
+
(isWhat.isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key))) {
|
|
41
|
+
assignProp(carry, key, targetVal, origin);
|
|
42
|
+
}
|
|
43
|
+
return carry;
|
|
44
|
+
}, {});
|
|
45
|
+
}
|
|
46
|
+
// newObject has all properties that newComer hasn't
|
|
47
|
+
const props = Object.getOwnPropertyNames(newComer);
|
|
48
|
+
const symbols = Object.getOwnPropertySymbols(newComer);
|
|
49
|
+
const result = [...props, ...symbols].reduce((carry, key) => {
|
|
50
|
+
// re-define the origin and newComer as targetVal and newVal
|
|
51
|
+
let newVal = newComer[key];
|
|
52
|
+
const targetVal = isWhat.isPlainObject(origin) ? origin[key] : undefined;
|
|
53
|
+
// When newVal is an object do the merge recursively
|
|
54
|
+
if (targetVal !== undefined && isWhat.isPlainObject(newVal)) {
|
|
55
|
+
newVal = mergeRecursively(targetVal, newVal, compareFn);
|
|
56
|
+
}
|
|
57
|
+
const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
|
|
58
|
+
assignProp(carry, key, propToAssign, newComer);
|
|
59
|
+
return carry;
|
|
60
|
+
}, newObject);
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Merge anything recursively.
|
|
65
|
+
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
66
|
+
* Basic types overwrite objects or other basic types.
|
|
67
|
+
*/
|
|
68
|
+
function merge(object, ...otherObjects) {
|
|
69
|
+
return otherObjects.reduce((result, newComer) => {
|
|
70
|
+
return mergeRecursively(result, newComer);
|
|
71
|
+
}, object);
|
|
72
|
+
}
|
|
73
|
+
function mergeAndCompare(compareFn, object, ...otherObjects) {
|
|
74
|
+
return otherObjects.reduce((result, newComer) => {
|
|
75
|
+
return mergeRecursively(result, newComer, compareFn);
|
|
76
|
+
}, object);
|
|
77
|
+
}
|
|
78
|
+
function mergeAndConcat(object, ...otherObjects) {
|
|
79
|
+
return otherObjects.reduce((result, newComer) => {
|
|
80
|
+
return mergeRecursively(result, newComer, concatArrays);
|
|
81
|
+
}, object);
|
|
82
|
+
}
|
|
83
|
+
// import { Timestamp } from '../test/Timestamp'
|
|
84
|
+
// type T1 = { date: Timestamp }
|
|
85
|
+
// type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
|
|
86
|
+
// type TestT = Merge<T1, T2>
|
|
87
|
+
// type A1 = { arr: string[] }
|
|
88
|
+
// type A2 = { arr: number[] }
|
|
89
|
+
// type A3 = { arr: boolean[] }
|
|
90
|
+
// type TestA = Merge<A1, [A2, A3]>
|
|
91
|
+
// interface I1 {
|
|
92
|
+
// date: Timestamp
|
|
93
|
+
// }
|
|
94
|
+
// interface I2 {
|
|
95
|
+
// date: Timestamp
|
|
96
|
+
// }
|
|
97
|
+
// const _a: I2 = { date: '' } as unknown as I2
|
|
98
|
+
// type TestI = Merge<I1, [I2]>
|
|
99
|
+
// // ReturnType<(typeof merge)<I1, I2>>
|
|
100
|
+
// const a = merge(_a, [_a])
|
|
101
|
+
// interface Arguments extends Record<string | number | symbol, unknown> {
|
|
102
|
+
// key: string;
|
|
103
|
+
// }
|
|
104
|
+
// const aa1: Arguments = { key: "value1" }
|
|
105
|
+
// const aa2: Arguments = { key: "value2" }
|
|
106
|
+
// const aa = merge(a1, a2);
|
|
107
|
+
// interface Barguments {
|
|
108
|
+
// key: string
|
|
109
|
+
// }
|
|
110
|
+
// const ba1: Barguments = { key: 'value1' }
|
|
111
|
+
// const ba2: Barguments = { key: 'value2' }
|
|
112
|
+
// const ba = merge(ba1, ba2)
|
|
113
|
+
// interface Carguments {
|
|
114
|
+
// key: string
|
|
115
|
+
// }
|
|
116
|
+
// const ca = merge<Carguments, Carguments[]>({ key: 'value1' }, { key: 'value2' })
|
|
117
117
|
// type P = Pop<Carguments[]>
|
|
118
118
|
|
|
119
119
|
exports.concatArrays = concatArrays;
|
package/dist/index.es.js
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
1
|
import { isArray, isPlainObject, isSymbol } from 'is-what';
|
|
2
2
|
|
|
3
|
-
function concatArrays(originVal, newVal) {
|
|
4
|
-
if (isArray(originVal) && isArray(newVal)) {
|
|
5
|
-
// concat logic
|
|
6
|
-
return originVal.concat(newVal);
|
|
7
|
-
}
|
|
8
|
-
return newVal; // always return newVal as fallback!!
|
|
3
|
+
function concatArrays(originVal, newVal) {
|
|
4
|
+
if (isArray(originVal) && isArray(newVal)) {
|
|
5
|
+
// concat logic
|
|
6
|
+
return originVal.concat(newVal);
|
|
7
|
+
}
|
|
8
|
+
return newVal; // always return newVal as fallback!!
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
function assignProp(carry, key, newVal, originalObject) {
|
|
12
|
-
const propType = {}.propertyIsEnumerable.call(originalObject, key)
|
|
13
|
-
? 'enumerable'
|
|
14
|
-
: 'nonenumerable';
|
|
15
|
-
if (propType === 'enumerable')
|
|
16
|
-
carry[key] = newVal;
|
|
17
|
-
if (propType === 'nonenumerable') {
|
|
18
|
-
Object.defineProperty(carry, key, {
|
|
19
|
-
value: newVal,
|
|
20
|
-
enumerable: false,
|
|
21
|
-
writable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function mergeRecursively(origin, newComer, compareFn) {
|
|
27
|
-
// always return newComer if its not an object
|
|
28
|
-
if (!isPlainObject(newComer))
|
|
29
|
-
return newComer;
|
|
30
|
-
// define newObject to merge all values upon
|
|
31
|
-
let newObject = {};
|
|
32
|
-
if (isPlainObject(origin)) {
|
|
33
|
-
const props = Object.getOwnPropertyNames(origin);
|
|
34
|
-
const symbols = Object.getOwnPropertySymbols(origin);
|
|
35
|
-
newObject = [...props, ...symbols].reduce((carry, key) => {
|
|
36
|
-
const targetVal = origin[key];
|
|
37
|
-
if ((!isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key)) ||
|
|
38
|
-
(isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key))) {
|
|
39
|
-
assignProp(carry, key, targetVal, origin);
|
|
40
|
-
}
|
|
41
|
-
return carry;
|
|
42
|
-
}, {});
|
|
43
|
-
}
|
|
44
|
-
// newObject has all properties that newComer hasn't
|
|
45
|
-
const props = Object.getOwnPropertyNames(newComer);
|
|
46
|
-
const symbols = Object.getOwnPropertySymbols(newComer);
|
|
47
|
-
const result = [...props, ...symbols].reduce((carry, key) => {
|
|
48
|
-
// re-define the origin and newComer as targetVal and newVal
|
|
49
|
-
let newVal = newComer[key];
|
|
50
|
-
const targetVal = isPlainObject(origin) ? origin[key] : undefined;
|
|
51
|
-
// When newVal is an object do the merge recursively
|
|
52
|
-
if (targetVal !== undefined && isPlainObject(newVal)) {
|
|
53
|
-
newVal = mergeRecursively(targetVal, newVal, compareFn);
|
|
54
|
-
}
|
|
55
|
-
const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
|
|
56
|
-
assignProp(carry, key, propToAssign, newComer);
|
|
57
|
-
return carry;
|
|
58
|
-
}, newObject);
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Merge anything recursively.
|
|
63
|
-
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
64
|
-
* Basic types overwrite objects or other basic types.
|
|
65
|
-
*/
|
|
66
|
-
function merge(object, ...otherObjects) {
|
|
67
|
-
return otherObjects.reduce((result, newComer) => {
|
|
68
|
-
return mergeRecursively(result, newComer);
|
|
69
|
-
}, object);
|
|
70
|
-
}
|
|
71
|
-
function mergeAndCompare(compareFn, object, ...otherObjects) {
|
|
72
|
-
return otherObjects.reduce((result, newComer) => {
|
|
73
|
-
return mergeRecursively(result, newComer, compareFn);
|
|
74
|
-
}, object);
|
|
75
|
-
}
|
|
76
|
-
function mergeAndConcat(object, ...otherObjects) {
|
|
77
|
-
return otherObjects.reduce((result, newComer) => {
|
|
78
|
-
return mergeRecursively(result, newComer, concatArrays);
|
|
79
|
-
}, object);
|
|
80
|
-
}
|
|
81
|
-
// import { Timestamp } from '../test/Timestamp'
|
|
82
|
-
// type T1 = { date: Timestamp }
|
|
83
|
-
// type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
|
|
84
|
-
// type TestT = Merge<T1, T2>
|
|
85
|
-
// type A1 = { arr: string[] }
|
|
86
|
-
// type A2 = { arr: number[] }
|
|
87
|
-
// type A3 = { arr: boolean[] }
|
|
88
|
-
// type TestA = Merge<A1, [A2, A3]>
|
|
89
|
-
// interface I1 {
|
|
90
|
-
// date: Timestamp
|
|
91
|
-
// }
|
|
92
|
-
// interface I2 {
|
|
93
|
-
// date: Timestamp
|
|
94
|
-
// }
|
|
95
|
-
// const _a: I2 = { date: '' } as unknown as I2
|
|
96
|
-
// type TestI = Merge<I1, [I2]>
|
|
97
|
-
// // ReturnType<(typeof merge)<I1, I2>>
|
|
98
|
-
// const a = merge(_a, [_a])
|
|
99
|
-
// interface Arguments extends Record<string | number | symbol, unknown> {
|
|
100
|
-
// key: string;
|
|
101
|
-
// }
|
|
102
|
-
// const aa1: Arguments = { key: "value1" }
|
|
103
|
-
// const aa2: Arguments = { key: "value2" }
|
|
104
|
-
// const aa = merge(a1, a2);
|
|
105
|
-
// interface Barguments {
|
|
106
|
-
// key: string
|
|
107
|
-
// }
|
|
108
|
-
// const ba1: Barguments = { key: 'value1' }
|
|
109
|
-
// const ba2: Barguments = { key: 'value2' }
|
|
110
|
-
// const ba = merge(ba1, ba2)
|
|
111
|
-
// interface Carguments {
|
|
112
|
-
// key: string
|
|
113
|
-
// }
|
|
114
|
-
// const ca = merge<Carguments, Carguments[]>({ key: 'value1' }, { key: 'value2' })
|
|
11
|
+
function assignProp(carry, key, newVal, originalObject) {
|
|
12
|
+
const propType = {}.propertyIsEnumerable.call(originalObject, key)
|
|
13
|
+
? 'enumerable'
|
|
14
|
+
: 'nonenumerable';
|
|
15
|
+
if (propType === 'enumerable')
|
|
16
|
+
carry[key] = newVal;
|
|
17
|
+
if (propType === 'nonenumerable') {
|
|
18
|
+
Object.defineProperty(carry, key, {
|
|
19
|
+
value: newVal,
|
|
20
|
+
enumerable: false,
|
|
21
|
+
writable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function mergeRecursively(origin, newComer, compareFn) {
|
|
27
|
+
// always return newComer if its not an object
|
|
28
|
+
if (!isPlainObject(newComer))
|
|
29
|
+
return newComer;
|
|
30
|
+
// define newObject to merge all values upon
|
|
31
|
+
let newObject = {};
|
|
32
|
+
if (isPlainObject(origin)) {
|
|
33
|
+
const props = Object.getOwnPropertyNames(origin);
|
|
34
|
+
const symbols = Object.getOwnPropertySymbols(origin);
|
|
35
|
+
newObject = [...props, ...symbols].reduce((carry, key) => {
|
|
36
|
+
const targetVal = origin[key];
|
|
37
|
+
if ((!isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key)) ||
|
|
38
|
+
(isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key))) {
|
|
39
|
+
assignProp(carry, key, targetVal, origin);
|
|
40
|
+
}
|
|
41
|
+
return carry;
|
|
42
|
+
}, {});
|
|
43
|
+
}
|
|
44
|
+
// newObject has all properties that newComer hasn't
|
|
45
|
+
const props = Object.getOwnPropertyNames(newComer);
|
|
46
|
+
const symbols = Object.getOwnPropertySymbols(newComer);
|
|
47
|
+
const result = [...props, ...symbols].reduce((carry, key) => {
|
|
48
|
+
// re-define the origin and newComer as targetVal and newVal
|
|
49
|
+
let newVal = newComer[key];
|
|
50
|
+
const targetVal = isPlainObject(origin) ? origin[key] : undefined;
|
|
51
|
+
// When newVal is an object do the merge recursively
|
|
52
|
+
if (targetVal !== undefined && isPlainObject(newVal)) {
|
|
53
|
+
newVal = mergeRecursively(targetVal, newVal, compareFn);
|
|
54
|
+
}
|
|
55
|
+
const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
|
|
56
|
+
assignProp(carry, key, propToAssign, newComer);
|
|
57
|
+
return carry;
|
|
58
|
+
}, newObject);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Merge anything recursively.
|
|
63
|
+
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
64
|
+
* Basic types overwrite objects or other basic types.
|
|
65
|
+
*/
|
|
66
|
+
function merge(object, ...otherObjects) {
|
|
67
|
+
return otherObjects.reduce((result, newComer) => {
|
|
68
|
+
return mergeRecursively(result, newComer);
|
|
69
|
+
}, object);
|
|
70
|
+
}
|
|
71
|
+
function mergeAndCompare(compareFn, object, ...otherObjects) {
|
|
72
|
+
return otherObjects.reduce((result, newComer) => {
|
|
73
|
+
return mergeRecursively(result, newComer, compareFn);
|
|
74
|
+
}, object);
|
|
75
|
+
}
|
|
76
|
+
function mergeAndConcat(object, ...otherObjects) {
|
|
77
|
+
return otherObjects.reduce((result, newComer) => {
|
|
78
|
+
return mergeRecursively(result, newComer, concatArrays);
|
|
79
|
+
}, object);
|
|
80
|
+
}
|
|
81
|
+
// import { Timestamp } from '../test/Timestamp'
|
|
82
|
+
// type T1 = { date: Timestamp }
|
|
83
|
+
// type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
|
|
84
|
+
// type TestT = Merge<T1, T2>
|
|
85
|
+
// type A1 = { arr: string[] }
|
|
86
|
+
// type A2 = { arr: number[] }
|
|
87
|
+
// type A3 = { arr: boolean[] }
|
|
88
|
+
// type TestA = Merge<A1, [A2, A3]>
|
|
89
|
+
// interface I1 {
|
|
90
|
+
// date: Timestamp
|
|
91
|
+
// }
|
|
92
|
+
// interface I2 {
|
|
93
|
+
// date: Timestamp
|
|
94
|
+
// }
|
|
95
|
+
// const _a: I2 = { date: '' } as unknown as I2
|
|
96
|
+
// type TestI = Merge<I1, [I2]>
|
|
97
|
+
// // ReturnType<(typeof merge)<I1, I2>>
|
|
98
|
+
// const a = merge(_a, [_a])
|
|
99
|
+
// interface Arguments extends Record<string | number | symbol, unknown> {
|
|
100
|
+
// key: string;
|
|
101
|
+
// }
|
|
102
|
+
// const aa1: Arguments = { key: "value1" }
|
|
103
|
+
// const aa2: Arguments = { key: "value2" }
|
|
104
|
+
// const aa = merge(a1, a2);
|
|
105
|
+
// interface Barguments {
|
|
106
|
+
// key: string
|
|
107
|
+
// }
|
|
108
|
+
// const ba1: Barguments = { key: 'value1' }
|
|
109
|
+
// const ba2: Barguments = { key: 'value2' }
|
|
110
|
+
// const ba = merge(ba1, ba2)
|
|
111
|
+
// interface Carguments {
|
|
112
|
+
// key: string
|
|
113
|
+
// }
|
|
114
|
+
// const ca = merge<Carguments, Carguments[]>({ key: 'value1' }, { key: 'value2' })
|
|
115
115
|
// type P = Pop<Carguments[]>
|
|
116
116
|
|
|
117
117
|
export { concatArrays, merge, mergeAndCompare, mergeAndConcat };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function concatArrays(originVal: any, newVal: any): any | any[];
|
|
1
|
+
export declare function concatArrays(originVal: any, newVal: any): any | any[];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './merge.js';
|
|
2
|
-
export * from './extensions.js';
|
|
1
|
+
export * from './merge.js';
|
|
2
|
+
export * from './extensions.js';
|
package/dist/types/merge.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type { Assign } from './typeUtils/Assign.js';
|
|
2
|
-
import type { Pop } from './typeUtils/List.js';
|
|
3
|
-
import type { PrettyPrint } from './typeUtils/PrettyPrint.js';
|
|
4
|
-
/**
|
|
5
|
-
* The return type of `merge()`. It reflects the type that is returned by JavaScript.
|
|
6
|
-
*
|
|
7
|
-
* This TS Utility can be used as standalone as well
|
|
8
|
-
*/
|
|
9
|
-
export type Merge<T, Ts extends unknown[]> = T extends Record<string | number | symbol, unknown> ? Ts extends Record<string | number | symbol, unknown>[] ? PrettyPrint<Assign<T, Ts>> : Pop<Ts> : Pop<Ts>;
|
|
10
|
-
/**
|
|
11
|
-
* Merge anything recursively.
|
|
12
|
-
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
13
|
-
* Basic types overwrite objects or other basic types.
|
|
14
|
-
*/
|
|
15
|
-
export declare function merge<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
16
|
-
export declare function mergeAndCompare<T, Tn extends unknown[]>(compareFn: (prop1: any, prop2: any, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
17
|
-
export declare function mergeAndConcat<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
1
|
+
import type { Assign } from './typeUtils/Assign.js';
|
|
2
|
+
import type { Pop } from './typeUtils/List.js';
|
|
3
|
+
import type { PrettyPrint } from './typeUtils/PrettyPrint.js';
|
|
4
|
+
/**
|
|
5
|
+
* The return type of `merge()`. It reflects the type that is returned by JavaScript.
|
|
6
|
+
*
|
|
7
|
+
* This TS Utility can be used as standalone as well
|
|
8
|
+
*/
|
|
9
|
+
export type Merge<T, Ts extends unknown[]> = T extends Record<string | number | symbol, unknown> ? Ts extends Record<string | number | symbol, unknown>[] ? PrettyPrint<Assign<T, Ts>> : Pop<Ts> : Pop<Ts>;
|
|
10
|
+
/**
|
|
11
|
+
* Merge anything recursively.
|
|
12
|
+
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
13
|
+
* Basic types overwrite objects or other basic types.
|
|
14
|
+
*/
|
|
15
|
+
export declare function merge<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
16
|
+
export declare function mergeAndCompare<T, Tn extends unknown[]>(compareFn: (prop1: any, prop2: any, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
17
|
+
export declare function mergeAndConcat<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import type { MergeDeep } from './MergeDeep.js';
|
|
2
|
-
import type { Iteration, IterationOf, Pos, Next } from './Iteration.js';
|
|
3
|
-
import type { Length, List } from './List.js';
|
|
4
|
-
/**
|
|
5
|
-
* Ask TS to re-check that `A1` extends `A2`.
|
|
6
|
-
* And if it fails, `A2` will be enforced anyway.
|
|
7
|
-
* Can also be used to add constraints on parameters.
|
|
8
|
-
* @param A1 to check against
|
|
9
|
-
* @param A2 to cast to
|
|
10
|
-
* @returns `A1 | A2`
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* type test0 = Cast<'42', string> // '42'
|
|
14
|
-
* type test1 = Cast<'42', number> // number
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
|
|
18
|
-
/**
|
|
19
|
-
* Check whether `A1` is part of `A2` or not. The difference with
|
|
20
|
-
* `extends` is that it forces a [[Boolean]] return.
|
|
21
|
-
* @param A1
|
|
22
|
-
* @param A2
|
|
23
|
-
* @returns [[Boolean]]
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* type test0 = Extends<'a' | 'b', 'b'> // Boolean
|
|
27
|
-
* type test1 = Extends<'a', 'a' | 'b'> // True
|
|
28
|
-
*
|
|
29
|
-
* type test2 = Extends<{a: string}, {a: any}> // True
|
|
30
|
-
* type test3 = Extends<{a: any}, {a: any, b: any}> // False
|
|
31
|
-
*
|
|
32
|
-
* type test4 = Extends<never, never> // False
|
|
33
|
-
* /// Nothing cannot extend nothing, use `Equals`
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
type Extends<A1, A2> = [A1] extends [never] ? 0 : A1 extends A2 ? 1 : 0;
|
|
37
|
-
type __Assign<O extends Record<string | number | symbol, unknown>, Os extends List<Record<string | number | symbol, unknown>>, I extends Iteration = IterationOf<0>> = Extends<Pos<I>, Length<Os>> extends 1 ? O : __Assign<MergeDeep<O, Os[Pos<I>]>, Os, Next<I>>;
|
|
38
|
-
type _Assign<O extends Record<string | number | symbol, unknown>, Os extends List<Record<string | number | symbol, unknown>>> = __Assign<O, Os> extends infer X ? Cast<X, Record<string | number | symbol, unknown>> : never;
|
|
39
|
-
/**
|
|
40
|
-
* Assign a list of [[Object]] into `O` with [[MergeDeep]]. Merges from right to
|
|
41
|
-
* left, first items get overridden by the next ones (last-in overrides).
|
|
42
|
-
* @param O to assign to
|
|
43
|
-
* @param Os to assign
|
|
44
|
-
* @returns [[Object]]
|
|
45
|
-
* @example
|
|
46
|
-
* ```ts
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export type Assign<O extends Record<string | number | symbol, unknown>, Os extends List<Record<string | number | symbol, unknown>>> = O extends unknown ? (Os extends unknown ? _Assign<O, Os> : never) : never;
|
|
50
|
-
export {};
|
|
1
|
+
import type { MergeDeep } from './MergeDeep.js';
|
|
2
|
+
import type { Iteration, IterationOf, Pos, Next } from './Iteration.js';
|
|
3
|
+
import type { Length, List } from './List.js';
|
|
4
|
+
/**
|
|
5
|
+
* Ask TS to re-check that `A1` extends `A2`.
|
|
6
|
+
* And if it fails, `A2` will be enforced anyway.
|
|
7
|
+
* Can also be used to add constraints on parameters.
|
|
8
|
+
* @param A1 to check against
|
|
9
|
+
* @param A2 to cast to
|
|
10
|
+
* @returns `A1 | A2`
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type test0 = Cast<'42', string> // '42'
|
|
14
|
+
* type test1 = Cast<'42', number> // number
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
|
|
18
|
+
/**
|
|
19
|
+
* Check whether `A1` is part of `A2` or not. The difference with
|
|
20
|
+
* `extends` is that it forces a [[Boolean]] return.
|
|
21
|
+
* @param A1
|
|
22
|
+
* @param A2
|
|
23
|
+
* @returns [[Boolean]]
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* type test0 = Extends<'a' | 'b', 'b'> // Boolean
|
|
27
|
+
* type test1 = Extends<'a', 'a' | 'b'> // True
|
|
28
|
+
*
|
|
29
|
+
* type test2 = Extends<{a: string}, {a: any}> // True
|
|
30
|
+
* type test3 = Extends<{a: any}, {a: any, b: any}> // False
|
|
31
|
+
*
|
|
32
|
+
* type test4 = Extends<never, never> // False
|
|
33
|
+
* /// Nothing cannot extend nothing, use `Equals`
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type Extends<A1, A2> = [A1] extends [never] ? 0 : A1 extends A2 ? 1 : 0;
|
|
37
|
+
type __Assign<O extends Record<string | number | symbol, unknown>, Os extends List<Record<string | number | symbol, unknown>>, I extends Iteration = IterationOf<0>> = Extends<Pos<I>, Length<Os>> extends 1 ? O : __Assign<MergeDeep<O, Os[Pos<I>]>, Os, Next<I>>;
|
|
38
|
+
type _Assign<O extends Record<string | number | symbol, unknown>, Os extends List<Record<string | number | symbol, unknown>>> = __Assign<O, Os> extends infer X ? Cast<X, Record<string | number | symbol, unknown>> : never;
|
|
39
|
+
/**
|
|
40
|
+
* Assign a list of [[Object]] into `O` with [[MergeDeep]]. Merges from right to
|
|
41
|
+
* left, first items get overridden by the next ones (last-in overrides).
|
|
42
|
+
* @param O to assign to
|
|
43
|
+
* @param Os to assign
|
|
44
|
+
* @returns [[Object]]
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export type Assign<O extends Record<string | number | symbol, unknown>, Os extends List<Record<string | number | symbol, unknown>>> = O extends unknown ? (Os extends unknown ? _Assign<O, Os> : never) : never;
|
|
50
|
+
export {};
|