|
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;
|
|
0 commit comments