merge-anything 5.1.6 → 5.1.7
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/cjs/index.cjs +83 -0
- package/dist/{types/typeUtils/Iteration.d.ts → cjs/index.d.cts} +164 -5
- package/dist/index.d.ts +416 -0
- package/dist/index.js +78 -0
- package/package.json +18 -12
- package/dist/index.cjs +0 -122
- package/dist/index.es.js +0 -117
- package/dist/types/extensions.d.ts +0 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/merge.d.ts +0 -17
- package/dist/types/typeUtils/Assign.d.ts +0 -50
- package/dist/types/typeUtils/List.d.ts +0 -29
- package/dist/types/typeUtils/MergeDeep.d.ts +0 -57
- package/dist/types/typeUtils/PrettyPrint.d.ts +0 -6
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const isWhat = require('is-what');
|
|
4
|
+
|
|
5
|
+
function concatArrays(originVal, newVal) {
|
|
6
|
+
if (isWhat.isArray(originVal) && isWhat.isArray(newVal)) {
|
|
7
|
+
return originVal.concat(newVal);
|
|
8
|
+
}
|
|
9
|
+
return newVal;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function assignProp(carry, key, newVal, originalObject) {
|
|
13
|
+
const propType = {}.propertyIsEnumerable.call(originalObject, key) ? "enumerable" : "nonenumerable";
|
|
14
|
+
if (propType === "enumerable")
|
|
15
|
+
carry[key] = newVal;
|
|
16
|
+
if (propType === "nonenumerable") {
|
|
17
|
+
Object.defineProperty(carry, key, {
|
|
18
|
+
value: newVal,
|
|
19
|
+
enumerable: false,
|
|
20
|
+
writable: true,
|
|
21
|
+
configurable: true
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function mergeRecursively(origin, newComer, compareFn) {
|
|
26
|
+
if (!isWhat.isPlainObject(newComer))
|
|
27
|
+
return newComer;
|
|
28
|
+
let newObject = {};
|
|
29
|
+
if (isWhat.isPlainObject(origin)) {
|
|
30
|
+
const props2 = Object.getOwnPropertyNames(origin);
|
|
31
|
+
const symbols2 = Object.getOwnPropertySymbols(origin);
|
|
32
|
+
newObject = [...props2, ...symbols2].reduce((carry, key) => {
|
|
33
|
+
const targetVal = origin[key];
|
|
34
|
+
if (!isWhat.isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key) || isWhat.isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key)) {
|
|
35
|
+
assignProp(
|
|
36
|
+
carry,
|
|
37
|
+
key,
|
|
38
|
+
targetVal,
|
|
39
|
+
origin
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return carry;
|
|
43
|
+
}, {});
|
|
44
|
+
}
|
|
45
|
+
const props = Object.getOwnPropertyNames(newComer);
|
|
46
|
+
const symbols = Object.getOwnPropertySymbols(newComer);
|
|
47
|
+
const result = [...props, ...symbols].reduce((carry, key) => {
|
|
48
|
+
let newVal = newComer[key];
|
|
49
|
+
const targetVal = isWhat.isPlainObject(origin) ? origin[key] : void 0;
|
|
50
|
+
if (targetVal !== void 0 && isWhat.isPlainObject(newVal)) {
|
|
51
|
+
newVal = mergeRecursively(targetVal, newVal, compareFn);
|
|
52
|
+
}
|
|
53
|
+
const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
|
|
54
|
+
assignProp(
|
|
55
|
+
carry,
|
|
56
|
+
key,
|
|
57
|
+
propToAssign,
|
|
58
|
+
newComer
|
|
59
|
+
);
|
|
60
|
+
return carry;
|
|
61
|
+
}, newObject);
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
function merge(object, ...otherObjects) {
|
|
65
|
+
return otherObjects.reduce((result, newComer) => {
|
|
66
|
+
return mergeRecursively(result, newComer);
|
|
67
|
+
}, object);
|
|
68
|
+
}
|
|
69
|
+
function mergeAndCompare(compareFn, object, ...otherObjects) {
|
|
70
|
+
return otherObjects.reduce((result, newComer) => {
|
|
71
|
+
return mergeRecursively(result, newComer, compareFn);
|
|
72
|
+
}, object);
|
|
73
|
+
}
|
|
74
|
+
function mergeAndConcat(object, ...otherObjects) {
|
|
75
|
+
return otherObjects.reduce((result, newComer) => {
|
|
76
|
+
return mergeRecursively(result, newComer, concatArrays);
|
|
77
|
+
}, object);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
exports.concatArrays = concatArrays;
|
|
81
|
+
exports.merge = merge;
|
|
82
|
+
exports.mergeAndCompare = mergeAndCompare;
|
|
83
|
+
exports.mergeAndConcat = mergeAndConcat;
|
|
@@ -1,14 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the keys of `O` that are optional
|
|
3
|
+
* @param O
|
|
4
|
+
* @returns [[Key]]
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
type OptionalKeys<O extends object> = O extends unknown ? {
|
|
10
|
+
[K in keyof O]-?: {} extends Pick<O, K> ? K : never;
|
|
11
|
+
}[keyof O] : never;
|
|
12
|
+
/**
|
|
13
|
+
* Get the keys of `O` that are required
|
|
14
|
+
* @param O
|
|
15
|
+
* @returns [[Key]]
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
type RequiredKeys<O extends object> = O extends unknown ? {
|
|
21
|
+
[K in keyof O]-?: {} extends Pick<O, K> ? never : K;
|
|
22
|
+
}[keyof O] : never;
|
|
23
|
+
type MergeObjectDeeply<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = {
|
|
24
|
+
[K in keyof (O & O1)]: K extends RequiredKeys<O1> ? MergeObjectsOrReturnFallback<O[K], O1[K], O1[K]> : K extends OptionalKeys<O1> ? K extends OptionalKeys<O> ? MergeObjectsOrReturnFallback<Exclude<O[K], undefined>, Exclude<O1[K], undefined>, Exclude<O[K], undefined> | Exclude<O1[K], undefined>> : K extends RequiredKeys<O> ? Exclude<O1[K], undefined> extends O[K] ? O[K] : MergeObjectsOrReturnFallback<O[K], Exclude<O1[K], undefined>, O[K] | Exclude<O1[K], undefined>> : O1[K] : O[K];
|
|
25
|
+
};
|
|
26
|
+
type MergeObjectsOrReturnFallback<O, O1, Fallback> = O extends Record<string | number | symbol, unknown> ? O1 extends Record<string | number | symbol, unknown> ? MergeObjectDeeply<O, O1> : Fallback : Fallback;
|
|
27
|
+
/**
|
|
28
|
+
* Accurately merge the fields of `O` with the ones of `O1`. It is
|
|
29
|
+
* equivalent to the spread operator in JavaScript. [[Union]]s and [[Optional]]
|
|
30
|
+
* fields will be handled gracefully.
|
|
31
|
+
*
|
|
32
|
+
* (⚠️ needs `--strictNullChecks` enabled)
|
|
33
|
+
* @param O to complete
|
|
34
|
+
* @param O1 to copy from
|
|
35
|
+
* @returns [[Object]]
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { PrettyPrint } from './PrettyPrint'
|
|
39
|
+
*
|
|
40
|
+
* type A1 = { a: number; b?: number; d?: number; e?: number; x: string; y?: number; z: string; } // prettier-ignore
|
|
41
|
+
* type A2 = { a?: number; c?: number; d?: number; e: number; x: number | undefined; y?: string; z?: number; } // prettier-ignore
|
|
42
|
+
*
|
|
43
|
+
* type Result = PrettyPrint<MergeDeep<A1, A2>>
|
|
44
|
+
* {
|
|
45
|
+
* a: number;
|
|
46
|
+
* b?: number | undefined;
|
|
47
|
+
* c?: number | undefined;
|
|
48
|
+
* d?: number | undefined;
|
|
49
|
+
* e: number;
|
|
50
|
+
* x: number | undefined;
|
|
51
|
+
* y?: string | number | undefined;
|
|
52
|
+
* z: string | number;
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
type MergeDeep<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = O extends unknown ? (O1 extends unknown ? MergeObjectDeeply<O, O1> : never) : never;
|
|
57
|
+
|
|
1
58
|
/**
|
|
2
59
|
* An entry of `IterationMap`
|
|
3
60
|
*/
|
|
4
|
-
|
|
61
|
+
type Iteration = [
|
|
5
62
|
value: number,
|
|
6
63
|
sign: '-' | '0' | '+',
|
|
7
64
|
prev: keyof IterationMap,
|
|
8
65
|
next: keyof IterationMap,
|
|
9
66
|
oppo: keyof IterationMap
|
|
10
67
|
];
|
|
11
|
-
|
|
68
|
+
type IterationMap = {
|
|
12
69
|
'__': [number, '-' | '0' | '+', '__', '__', '__'];
|
|
13
70
|
'-100': [-100, '-', '__', '-99', '100'];
|
|
14
71
|
'-99': [-99, '-', '-100', '-98', '99'];
|
|
@@ -228,7 +285,7 @@ export type IterationMap = {
|
|
|
228
285
|
* type nprev = Pos<prev> // -1
|
|
229
286
|
* ```
|
|
230
287
|
*/
|
|
231
|
-
|
|
288
|
+
type IterationOf<N extends number> = `${N}` extends keyof IterationMap ? IterationMap[`${N}`] : IterationMap['__'];
|
|
232
289
|
/**
|
|
233
290
|
* Get the position of `I` (**number**)
|
|
234
291
|
* @param I to query
|
|
@@ -241,7 +298,7 @@ export type IterationOf<N extends number> = `${N}` extends keyof IterationMap ?
|
|
|
241
298
|
* type test1 = Pos<Next<i>> // 21
|
|
242
299
|
* ```
|
|
243
300
|
*/
|
|
244
|
-
|
|
301
|
+
type Pos<I extends Iteration> = I[0];
|
|
245
302
|
/**
|
|
246
303
|
* Move `I`'s position forward
|
|
247
304
|
* @param I to move
|
|
@@ -254,4 +311,106 @@ export type Pos<I extends Iteration> = I[0];
|
|
|
254
311
|
* type test1 = Pos<Next<i>> // 21
|
|
255
312
|
* ```
|
|
256
313
|
*/
|
|
257
|
-
|
|
314
|
+
type Next<I extends Iteration> = IterationMap[I[3]];
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* A [[List]]
|
|
318
|
+
* @param T its type
|
|
319
|
+
* @returns [[List]]
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* type list0 = [1, 2, 3]
|
|
323
|
+
* type list1 = number[]
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
type List<T = any> = readonly T[];
|
|
327
|
+
/**
|
|
328
|
+
* Get the length of `L`
|
|
329
|
+
* @param L to get length
|
|
330
|
+
* @returns [[String]] or `number`
|
|
331
|
+
* @example
|
|
332
|
+
* ```ts
|
|
333
|
+
* ```
|
|
334
|
+
*/
|
|
335
|
+
type Length<L extends List> = L['length'];
|
|
336
|
+
/**
|
|
337
|
+
* Return the last item out of a [[List]]
|
|
338
|
+
* @param L
|
|
339
|
+
* @returns [[List]]
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
type Pop<L extends List> = L extends readonly [] ? never : L extends [...unknown[], infer Last] ? Last : L extends (infer T)[] ? T : never;
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Ask TS to re-check that `A1` extends `A2`.
|
|
348
|
+
* And if it fails, `A2` will be enforced anyway.
|
|
349
|
+
* Can also be used to add constraints on parameters.
|
|
350
|
+
* @param A1 to check against
|
|
351
|
+
* @param A2 to cast to
|
|
352
|
+
* @returns `A1 | A2`
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* type test0 = Cast<'42', string> // '42'
|
|
356
|
+
* type test1 = Cast<'42', number> // number
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
|
|
360
|
+
/**
|
|
361
|
+
* Check whether `A1` is part of `A2` or not. The difference with
|
|
362
|
+
* `extends` is that it forces a [[Boolean]] return.
|
|
363
|
+
* @param A1
|
|
364
|
+
* @param A2
|
|
365
|
+
* @returns [[Boolean]]
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* type test0 = Extends<'a' | 'b', 'b'> // Boolean
|
|
369
|
+
* type test1 = Extends<'a', 'a' | 'b'> // True
|
|
370
|
+
*
|
|
371
|
+
* type test2 = Extends<{a: string}, {a: any}> // True
|
|
372
|
+
* type test3 = Extends<{a: any}, {a: any, b: any}> // False
|
|
373
|
+
*
|
|
374
|
+
* type test4 = Extends<never, never> // False
|
|
375
|
+
* /// Nothing cannot extend nothing, use `Equals`
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
type Extends<A1, A2> = [A1] extends [never] ? 0 : A1 extends A2 ? 1 : 0;
|
|
379
|
+
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>>;
|
|
380
|
+
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;
|
|
381
|
+
/**
|
|
382
|
+
* Assign a list of [[Object]] into `O` with [[MergeDeep]]. Merges from right to
|
|
383
|
+
* left, first items get overridden by the next ones (last-in overrides).
|
|
384
|
+
* @param O to assign to
|
|
385
|
+
* @param Os to assign
|
|
386
|
+
* @returns [[Object]]
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
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;
|
|
392
|
+
|
|
393
|
+
type Has<U, U1> = [U1] extends [U] ? 1 : 0;
|
|
394
|
+
type If<B extends 0 | 1, Then, Else = never> = B extends 1 ? Then : Else;
|
|
395
|
+
type PrettyPrint<A, Seen = never> = If<Has<Seen, A>, A, A extends Record<string | number | symbol, unknown> ? {
|
|
396
|
+
[K in keyof A]: PrettyPrint<A[K], A | Seen>;
|
|
397
|
+
} & unknown : A>;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* The return type of `merge()`. It reflects the type that is returned by JavaScript.
|
|
401
|
+
*
|
|
402
|
+
* This TS Utility can be used as standalone as well
|
|
403
|
+
*/
|
|
404
|
+
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>;
|
|
405
|
+
/**
|
|
406
|
+
* Merge anything recursively.
|
|
407
|
+
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
408
|
+
* Basic types overwrite objects or other basic types.
|
|
409
|
+
*/
|
|
410
|
+
declare function merge<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
411
|
+
declare function mergeAndCompare<T, Tn extends unknown[]>(compareFn: (prop1: any, prop2: any, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
412
|
+
declare function mergeAndConcat<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
413
|
+
|
|
414
|
+
declare function concatArrays(originVal: any, newVal: any): any | any[];
|
|
415
|
+
|
|
416
|
+
export { Merge, concatArrays, merge, mergeAndCompare, mergeAndConcat };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the keys of `O` that are optional
|
|
3
|
+
* @param O
|
|
4
|
+
* @returns [[Key]]
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
type OptionalKeys<O extends object> = O extends unknown ? {
|
|
10
|
+
[K in keyof O]-?: {} extends Pick<O, K> ? K : never;
|
|
11
|
+
}[keyof O] : never;
|
|
12
|
+
/**
|
|
13
|
+
* Get the keys of `O` that are required
|
|
14
|
+
* @param O
|
|
15
|
+
* @returns [[Key]]
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
type RequiredKeys<O extends object> = O extends unknown ? {
|
|
21
|
+
[K in keyof O]-?: {} extends Pick<O, K> ? never : K;
|
|
22
|
+
}[keyof O] : never;
|
|
23
|
+
type MergeObjectDeeply<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = {
|
|
24
|
+
[K in keyof (O & O1)]: K extends RequiredKeys<O1> ? MergeObjectsOrReturnFallback<O[K], O1[K], O1[K]> : K extends OptionalKeys<O1> ? K extends OptionalKeys<O> ? MergeObjectsOrReturnFallback<Exclude<O[K], undefined>, Exclude<O1[K], undefined>, Exclude<O[K], undefined> | Exclude<O1[K], undefined>> : K extends RequiredKeys<O> ? Exclude<O1[K], undefined> extends O[K] ? O[K] : MergeObjectsOrReturnFallback<O[K], Exclude<O1[K], undefined>, O[K] | Exclude<O1[K], undefined>> : O1[K] : O[K];
|
|
25
|
+
};
|
|
26
|
+
type MergeObjectsOrReturnFallback<O, O1, Fallback> = O extends Record<string | number | symbol, unknown> ? O1 extends Record<string | number | symbol, unknown> ? MergeObjectDeeply<O, O1> : Fallback : Fallback;
|
|
27
|
+
/**
|
|
28
|
+
* Accurately merge the fields of `O` with the ones of `O1`. It is
|
|
29
|
+
* equivalent to the spread operator in JavaScript. [[Union]]s and [[Optional]]
|
|
30
|
+
* fields will be handled gracefully.
|
|
31
|
+
*
|
|
32
|
+
* (⚠️ needs `--strictNullChecks` enabled)
|
|
33
|
+
* @param O to complete
|
|
34
|
+
* @param O1 to copy from
|
|
35
|
+
* @returns [[Object]]
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { PrettyPrint } from './PrettyPrint'
|
|
39
|
+
*
|
|
40
|
+
* type A1 = { a: number; b?: number; d?: number; e?: number; x: string; y?: number; z: string; } // prettier-ignore
|
|
41
|
+
* type A2 = { a?: number; c?: number; d?: number; e: number; x: number | undefined; y?: string; z?: number; } // prettier-ignore
|
|
42
|
+
*
|
|
43
|
+
* type Result = PrettyPrint<MergeDeep<A1, A2>>
|
|
44
|
+
* {
|
|
45
|
+
* a: number;
|
|
46
|
+
* b?: number | undefined;
|
|
47
|
+
* c?: number | undefined;
|
|
48
|
+
* d?: number | undefined;
|
|
49
|
+
* e: number;
|
|
50
|
+
* x: number | undefined;
|
|
51
|
+
* y?: string | number | undefined;
|
|
52
|
+
* z: string | number;
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
type MergeDeep<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = O extends unknown ? (O1 extends unknown ? MergeObjectDeeply<O, O1> : never) : never;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* An entry of `IterationMap`
|
|
60
|
+
*/
|
|
61
|
+
type Iteration = [
|
|
62
|
+
value: number,
|
|
63
|
+
sign: '-' | '0' | '+',
|
|
64
|
+
prev: keyof IterationMap,
|
|
65
|
+
next: keyof IterationMap,
|
|
66
|
+
oppo: keyof IterationMap
|
|
67
|
+
];
|
|
68
|
+
type IterationMap = {
|
|
69
|
+
'__': [number, '-' | '0' | '+', '__', '__', '__'];
|
|
70
|
+
'-100': [-100, '-', '__', '-99', '100'];
|
|
71
|
+
'-99': [-99, '-', '-100', '-98', '99'];
|
|
72
|
+
'-98': [-98, '-', '-99', '-97', '98'];
|
|
73
|
+
'-97': [-97, '-', '-98', '-96', '97'];
|
|
74
|
+
'-96': [-96, '-', '-97', '-95', '96'];
|
|
75
|
+
'-95': [-95, '-', '-96', '-94', '95'];
|
|
76
|
+
'-94': [-94, '-', '-95', '-93', '94'];
|
|
77
|
+
'-93': [-93, '-', '-94', '-92', '93'];
|
|
78
|
+
'-92': [-92, '-', '-93', '-91', '92'];
|
|
79
|
+
'-91': [-91, '-', '-92', '-90', '91'];
|
|
80
|
+
'-90': [-90, '-', '-91', '-89', '90'];
|
|
81
|
+
'-89': [-89, '-', '-90', '-88', '89'];
|
|
82
|
+
'-88': [-88, '-', '-89', '-87', '88'];
|
|
83
|
+
'-87': [-87, '-', '-88', '-86', '87'];
|
|
84
|
+
'-86': [-86, '-', '-87', '-85', '86'];
|
|
85
|
+
'-85': [-85, '-', '-86', '-84', '85'];
|
|
86
|
+
'-84': [-84, '-', '-85', '-83', '84'];
|
|
87
|
+
'-83': [-83, '-', '-84', '-82', '83'];
|
|
88
|
+
'-82': [-82, '-', '-83', '-81', '82'];
|
|
89
|
+
'-81': [-81, '-', '-82', '-80', '81'];
|
|
90
|
+
'-80': [-80, '-', '-81', '-79', '80'];
|
|
91
|
+
'-79': [-79, '-', '-80', '-78', '79'];
|
|
92
|
+
'-78': [-78, '-', '-79', '-77', '78'];
|
|
93
|
+
'-77': [-77, '-', '-78', '-76', '77'];
|
|
94
|
+
'-76': [-76, '-', '-77', '-75', '76'];
|
|
95
|
+
'-75': [-75, '-', '-76', '-74', '75'];
|
|
96
|
+
'-74': [-74, '-', '-75', '-73', '74'];
|
|
97
|
+
'-73': [-73, '-', '-74', '-72', '73'];
|
|
98
|
+
'-72': [-72, '-', '-73', '-71', '72'];
|
|
99
|
+
'-71': [-71, '-', '-72', '-70', '71'];
|
|
100
|
+
'-70': [-70, '-', '-71', '-69', '70'];
|
|
101
|
+
'-69': [-69, '-', '-70', '-68', '69'];
|
|
102
|
+
'-68': [-68, '-', '-69', '-67', '68'];
|
|
103
|
+
'-67': [-67, '-', '-68', '-66', '67'];
|
|
104
|
+
'-66': [-66, '-', '-67', '-65', '66'];
|
|
105
|
+
'-65': [-65, '-', '-66', '-64', '65'];
|
|
106
|
+
'-64': [-64, '-', '-65', '-63', '64'];
|
|
107
|
+
'-63': [-63, '-', '-64', '-62', '63'];
|
|
108
|
+
'-62': [-62, '-', '-63', '-61', '62'];
|
|
109
|
+
'-61': [-61, '-', '-62', '-60', '61'];
|
|
110
|
+
'-60': [-60, '-', '-61', '-59', '60'];
|
|
111
|
+
'-59': [-59, '-', '-60', '-58', '59'];
|
|
112
|
+
'-58': [-58, '-', '-59', '-57', '58'];
|
|
113
|
+
'-57': [-57, '-', '-58', '-56', '57'];
|
|
114
|
+
'-56': [-56, '-', '-57', '-55', '56'];
|
|
115
|
+
'-55': [-55, '-', '-56', '-54', '55'];
|
|
116
|
+
'-54': [-54, '-', '-55', '-53', '54'];
|
|
117
|
+
'-53': [-53, '-', '-54', '-52', '53'];
|
|
118
|
+
'-52': [-52, '-', '-53', '-51', '52'];
|
|
119
|
+
'-51': [-51, '-', '-52', '-50', '51'];
|
|
120
|
+
'-50': [-50, '-', '-51', '-49', '50'];
|
|
121
|
+
'-49': [-49, '-', '-50', '-48', '49'];
|
|
122
|
+
'-48': [-48, '-', '-49', '-47', '48'];
|
|
123
|
+
'-47': [-47, '-', '-48', '-46', '47'];
|
|
124
|
+
'-46': [-46, '-', '-47', '-45', '46'];
|
|
125
|
+
'-45': [-45, '-', '-46', '-44', '45'];
|
|
126
|
+
'-44': [-44, '-', '-45', '-43', '44'];
|
|
127
|
+
'-43': [-43, '-', '-44', '-42', '43'];
|
|
128
|
+
'-42': [-42, '-', '-43', '-41', '42'];
|
|
129
|
+
'-41': [-41, '-', '-42', '-40', '41'];
|
|
130
|
+
'-40': [-40, '-', '-41', '-39', '40'];
|
|
131
|
+
'-39': [-39, '-', '-40', '-38', '39'];
|
|
132
|
+
'-38': [-38, '-', '-39', '-37', '38'];
|
|
133
|
+
'-37': [-37, '-', '-38', '-36', '37'];
|
|
134
|
+
'-36': [-36, '-', '-37', '-35', '36'];
|
|
135
|
+
'-35': [-35, '-', '-36', '-34', '35'];
|
|
136
|
+
'-34': [-34, '-', '-35', '-33', '34'];
|
|
137
|
+
'-33': [-33, '-', '-34', '-32', '33'];
|
|
138
|
+
'-32': [-32, '-', '-33', '-31', '32'];
|
|
139
|
+
'-31': [-31, '-', '-32', '-30', '31'];
|
|
140
|
+
'-30': [-30, '-', '-31', '-29', '30'];
|
|
141
|
+
'-29': [-29, '-', '-30', '-28', '29'];
|
|
142
|
+
'-28': [-28, '-', '-29', '-27', '28'];
|
|
143
|
+
'-27': [-27, '-', '-28', '-26', '27'];
|
|
144
|
+
'-26': [-26, '-', '-27', '-25', '26'];
|
|
145
|
+
'-25': [-25, '-', '-26', '-24', '25'];
|
|
146
|
+
'-24': [-24, '-', '-25', '-23', '24'];
|
|
147
|
+
'-23': [-23, '-', '-24', '-22', '23'];
|
|
148
|
+
'-22': [-22, '-', '-23', '-21', '22'];
|
|
149
|
+
'-21': [-21, '-', '-22', '-20', '21'];
|
|
150
|
+
'-20': [-20, '-', '-21', '-19', '20'];
|
|
151
|
+
'-19': [-19, '-', '-20', '-18', '19'];
|
|
152
|
+
'-18': [-18, '-', '-19', '-17', '18'];
|
|
153
|
+
'-17': [-17, '-', '-18', '-16', '17'];
|
|
154
|
+
'-16': [-16, '-', '-17', '-15', '16'];
|
|
155
|
+
'-15': [-15, '-', '-16', '-14', '15'];
|
|
156
|
+
'-14': [-14, '-', '-15', '-13', '14'];
|
|
157
|
+
'-13': [-13, '-', '-14', '-12', '13'];
|
|
158
|
+
'-12': [-12, '-', '-13', '-11', '12'];
|
|
159
|
+
'-11': [-11, '-', '-12', '-10', '11'];
|
|
160
|
+
'-10': [-10, '-', '-11', '-9', '10'];
|
|
161
|
+
'-9': [-9, '-', '-10', '-8', '9'];
|
|
162
|
+
'-8': [-8, '-', '-9', '-7', '8'];
|
|
163
|
+
'-7': [-7, '-', '-8', '-6', '7'];
|
|
164
|
+
'-6': [-6, '-', '-7', '-5', '6'];
|
|
165
|
+
'-5': [-5, '-', '-6', '-4', '5'];
|
|
166
|
+
'-4': [-4, '-', '-5', '-3', '4'];
|
|
167
|
+
'-3': [-3, '-', '-4', '-2', '3'];
|
|
168
|
+
'-2': [-2, '-', '-3', '-1', '2'];
|
|
169
|
+
'-1': [-1, '-', '-2', '0', '1'];
|
|
170
|
+
'0': [0, '0', '-1', '1', '0'];
|
|
171
|
+
'1': [1, '+', '0', '2', '-1'];
|
|
172
|
+
'2': [2, '+', '1', '3', '-2'];
|
|
173
|
+
'3': [3, '+', '2', '4', '-3'];
|
|
174
|
+
'4': [4, '+', '3', '5', '-4'];
|
|
175
|
+
'5': [5, '+', '4', '6', '-5'];
|
|
176
|
+
'6': [6, '+', '5', '7', '-6'];
|
|
177
|
+
'7': [7, '+', '6', '8', '-7'];
|
|
178
|
+
'8': [8, '+', '7', '9', '-8'];
|
|
179
|
+
'9': [9, '+', '8', '10', '-9'];
|
|
180
|
+
'10': [10, '+', '9', '11', '-10'];
|
|
181
|
+
'11': [11, '+', '10', '12', '-11'];
|
|
182
|
+
'12': [12, '+', '11', '13', '-12'];
|
|
183
|
+
'13': [13, '+', '12', '14', '-13'];
|
|
184
|
+
'14': [14, '+', '13', '15', '-14'];
|
|
185
|
+
'15': [15, '+', '14', '16', '-15'];
|
|
186
|
+
'16': [16, '+', '15', '17', '-16'];
|
|
187
|
+
'17': [17, '+', '16', '18', '-17'];
|
|
188
|
+
'18': [18, '+', '17', '19', '-18'];
|
|
189
|
+
'19': [19, '+', '18', '20', '-19'];
|
|
190
|
+
'20': [20, '+', '19', '21', '-20'];
|
|
191
|
+
'21': [21, '+', '20', '22', '-21'];
|
|
192
|
+
'22': [22, '+', '21', '23', '-22'];
|
|
193
|
+
'23': [23, '+', '22', '24', '-23'];
|
|
194
|
+
'24': [24, '+', '23', '25', '-24'];
|
|
195
|
+
'25': [25, '+', '24', '26', '-25'];
|
|
196
|
+
'26': [26, '+', '25', '27', '-26'];
|
|
197
|
+
'27': [27, '+', '26', '28', '-27'];
|
|
198
|
+
'28': [28, '+', '27', '29', '-28'];
|
|
199
|
+
'29': [29, '+', '28', '30', '-29'];
|
|
200
|
+
'30': [30, '+', '29', '31', '-30'];
|
|
201
|
+
'31': [31, '+', '30', '32', '-31'];
|
|
202
|
+
'32': [32, '+', '31', '33', '-32'];
|
|
203
|
+
'33': [33, '+', '32', '34', '-33'];
|
|
204
|
+
'34': [34, '+', '33', '35', '-34'];
|
|
205
|
+
'35': [35, '+', '34', '36', '-35'];
|
|
206
|
+
'36': [36, '+', '35', '37', '-36'];
|
|
207
|
+
'37': [37, '+', '36', '38', '-37'];
|
|
208
|
+
'38': [38, '+', '37', '39', '-38'];
|
|
209
|
+
'39': [39, '+', '38', '40', '-39'];
|
|
210
|
+
'40': [40, '+', '39', '41', '-40'];
|
|
211
|
+
'41': [41, '+', '40', '42', '-41'];
|
|
212
|
+
'42': [42, '+', '41', '43', '-42'];
|
|
213
|
+
'43': [43, '+', '42', '44', '-43'];
|
|
214
|
+
'44': [44, '+', '43', '45', '-44'];
|
|
215
|
+
'45': [45, '+', '44', '46', '-45'];
|
|
216
|
+
'46': [46, '+', '45', '47', '-46'];
|
|
217
|
+
'47': [47, '+', '46', '48', '-47'];
|
|
218
|
+
'48': [48, '+', '47', '49', '-48'];
|
|
219
|
+
'49': [49, '+', '48', '50', '-49'];
|
|
220
|
+
'50': [50, '+', '49', '51', '-50'];
|
|
221
|
+
'51': [51, '+', '50', '52', '-51'];
|
|
222
|
+
'52': [52, '+', '51', '53', '-52'];
|
|
223
|
+
'53': [53, '+', '52', '54', '-53'];
|
|
224
|
+
'54': [54, '+', '53', '55', '-54'];
|
|
225
|
+
'55': [55, '+', '54', '56', '-55'];
|
|
226
|
+
'56': [56, '+', '55', '57', '-56'];
|
|
227
|
+
'57': [57, '+', '56', '58', '-57'];
|
|
228
|
+
'58': [58, '+', '57', '59', '-58'];
|
|
229
|
+
'59': [59, '+', '58', '60', '-59'];
|
|
230
|
+
'60': [60, '+', '59', '61', '-60'];
|
|
231
|
+
'61': [61, '+', '60', '62', '-61'];
|
|
232
|
+
'62': [62, '+', '61', '63', '-62'];
|
|
233
|
+
'63': [63, '+', '62', '64', '-63'];
|
|
234
|
+
'64': [64, '+', '63', '65', '-64'];
|
|
235
|
+
'65': [65, '+', '64', '66', '-65'];
|
|
236
|
+
'66': [66, '+', '65', '67', '-66'];
|
|
237
|
+
'67': [67, '+', '66', '68', '-67'];
|
|
238
|
+
'68': [68, '+', '67', '69', '-68'];
|
|
239
|
+
'69': [69, '+', '68', '70', '-69'];
|
|
240
|
+
'70': [70, '+', '69', '71', '-70'];
|
|
241
|
+
'71': [71, '+', '70', '72', '-71'];
|
|
242
|
+
'72': [72, '+', '71', '73', '-72'];
|
|
243
|
+
'73': [73, '+', '72', '74', '-73'];
|
|
244
|
+
'74': [74, '+', '73', '75', '-74'];
|
|
245
|
+
'75': [75, '+', '74', '76', '-75'];
|
|
246
|
+
'76': [76, '+', '75', '77', '-76'];
|
|
247
|
+
'77': [77, '+', '76', '78', '-77'];
|
|
248
|
+
'78': [78, '+', '77', '79', '-78'];
|
|
249
|
+
'79': [79, '+', '78', '80', '-79'];
|
|
250
|
+
'80': [80, '+', '79', '81', '-80'];
|
|
251
|
+
'81': [81, '+', '80', '82', '-81'];
|
|
252
|
+
'82': [82, '+', '81', '83', '-82'];
|
|
253
|
+
'83': [83, '+', '82', '84', '-83'];
|
|
254
|
+
'84': [84, '+', '83', '85', '-84'];
|
|
255
|
+
'85': [85, '+', '84', '86', '-85'];
|
|
256
|
+
'86': [86, '+', '85', '87', '-86'];
|
|
257
|
+
'87': [87, '+', '86', '88', '-87'];
|
|
258
|
+
'88': [88, '+', '87', '89', '-88'];
|
|
259
|
+
'89': [89, '+', '88', '90', '-89'];
|
|
260
|
+
'90': [90, '+', '89', '91', '-90'];
|
|
261
|
+
'91': [91, '+', '90', '92', '-91'];
|
|
262
|
+
'92': [92, '+', '91', '93', '-92'];
|
|
263
|
+
'93': [93, '+', '92', '94', '-93'];
|
|
264
|
+
'94': [94, '+', '93', '95', '-94'];
|
|
265
|
+
'95': [95, '+', '94', '96', '-95'];
|
|
266
|
+
'96': [96, '+', '95', '97', '-96'];
|
|
267
|
+
'97': [97, '+', '96', '98', '-97'];
|
|
268
|
+
'98': [98, '+', '97', '99', '-98'];
|
|
269
|
+
'99': [99, '+', '98', '100', '-99'];
|
|
270
|
+
'100': [100, '+', '99', '__', '-100'];
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* Transform a number into an [[Iteration]]
|
|
274
|
+
* (to use [[Prev]], [[Next]], & [[Pos]])
|
|
275
|
+
* @param N to transform
|
|
276
|
+
* @returns [[Iteration]]
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* type i = IterationOf<0> // ["-1", "1", "0", 0, "0"]
|
|
280
|
+
*
|
|
281
|
+
* type next = Next<i> // ["0", "2", "1", 1, "+"]
|
|
282
|
+
* type prev = Prev<i> // ["-2", "0", "-1", -1, "-"]
|
|
283
|
+
*
|
|
284
|
+
* type nnext = Pos<next> // +1
|
|
285
|
+
* type nprev = Pos<prev> // -1
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
type IterationOf<N extends number> = `${N}` extends keyof IterationMap ? IterationMap[`${N}`] : IterationMap['__'];
|
|
289
|
+
/**
|
|
290
|
+
* Get the position of `I` (**number**)
|
|
291
|
+
* @param I to query
|
|
292
|
+
* @returns `number`
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* type i = IterationOf<'20'>
|
|
296
|
+
*
|
|
297
|
+
* type test0 = Pos<i> // 20
|
|
298
|
+
* type test1 = Pos<Next<i>> // 21
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
type Pos<I extends Iteration> = I[0];
|
|
302
|
+
/**
|
|
303
|
+
* Move `I`'s position forward
|
|
304
|
+
* @param I to move
|
|
305
|
+
* @returns [[Iteration]]
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* type i = IterationOf<'20'>
|
|
309
|
+
*
|
|
310
|
+
* type test0 = Pos<i> // 20
|
|
311
|
+
* type test1 = Pos<Next<i>> // 21
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
type Next<I extends Iteration> = IterationMap[I[3]];
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* A [[List]]
|
|
318
|
+
* @param T its type
|
|
319
|
+
* @returns [[List]]
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* type list0 = [1, 2, 3]
|
|
323
|
+
* type list1 = number[]
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
type List<T = any> = readonly T[];
|
|
327
|
+
/**
|
|
328
|
+
* Get the length of `L`
|
|
329
|
+
* @param L to get length
|
|
330
|
+
* @returns [[String]] or `number`
|
|
331
|
+
* @example
|
|
332
|
+
* ```ts
|
|
333
|
+
* ```
|
|
334
|
+
*/
|
|
335
|
+
type Length<L extends List> = L['length'];
|
|
336
|
+
/**
|
|
337
|
+
* Return the last item out of a [[List]]
|
|
338
|
+
* @param L
|
|
339
|
+
* @returns [[List]]
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
type Pop<L extends List> = L extends readonly [] ? never : L extends [...unknown[], infer Last] ? Last : L extends (infer T)[] ? T : never;
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Ask TS to re-check that `A1` extends `A2`.
|
|
348
|
+
* And if it fails, `A2` will be enforced anyway.
|
|
349
|
+
* Can also be used to add constraints on parameters.
|
|
350
|
+
* @param A1 to check against
|
|
351
|
+
* @param A2 to cast to
|
|
352
|
+
* @returns `A1 | A2`
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* type test0 = Cast<'42', string> // '42'
|
|
356
|
+
* type test1 = Cast<'42', number> // number
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
|
|
360
|
+
/**
|
|
361
|
+
* Check whether `A1` is part of `A2` or not. The difference with
|
|
362
|
+
* `extends` is that it forces a [[Boolean]] return.
|
|
363
|
+
* @param A1
|
|
364
|
+
* @param A2
|
|
365
|
+
* @returns [[Boolean]]
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* type test0 = Extends<'a' | 'b', 'b'> // Boolean
|
|
369
|
+
* type test1 = Extends<'a', 'a' | 'b'> // True
|
|
370
|
+
*
|
|
371
|
+
* type test2 = Extends<{a: string}, {a: any}> // True
|
|
372
|
+
* type test3 = Extends<{a: any}, {a: any, b: any}> // False
|
|
373
|
+
*
|
|
374
|
+
* type test4 = Extends<never, never> // False
|
|
375
|
+
* /// Nothing cannot extend nothing, use `Equals`
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
type Extends<A1, A2> = [A1] extends [never] ? 0 : A1 extends A2 ? 1 : 0;
|
|
379
|
+
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>>;
|
|
380
|
+
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;
|
|
381
|
+
/**
|
|
382
|
+
* Assign a list of [[Object]] into `O` with [[MergeDeep]]. Merges from right to
|
|
383
|
+
* left, first items get overridden by the next ones (last-in overrides).
|
|
384
|
+
* @param O to assign to
|
|
385
|
+
* @param Os to assign
|
|
386
|
+
* @returns [[Object]]
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
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;
|
|
392
|
+
|
|
393
|
+
type Has<U, U1> = [U1] extends [U] ? 1 : 0;
|
|
394
|
+
type If<B extends 0 | 1, Then, Else = never> = B extends 1 ? Then : Else;
|
|
395
|
+
type PrettyPrint<A, Seen = never> = If<Has<Seen, A>, A, A extends Record<string | number | symbol, unknown> ? {
|
|
396
|
+
[K in keyof A]: PrettyPrint<A[K], A | Seen>;
|
|
397
|
+
} & unknown : A>;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* The return type of `merge()`. It reflects the type that is returned by JavaScript.
|
|
401
|
+
*
|
|
402
|
+
* This TS Utility can be used as standalone as well
|
|
403
|
+
*/
|
|
404
|
+
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>;
|
|
405
|
+
/**
|
|
406
|
+
* Merge anything recursively.
|
|
407
|
+
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
408
|
+
* Basic types overwrite objects or other basic types.
|
|
409
|
+
*/
|
|
410
|
+
declare function merge<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
411
|
+
declare function mergeAndCompare<T, Tn extends unknown[]>(compareFn: (prop1: any, prop2: any, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
412
|
+
declare function mergeAndConcat<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
413
|
+
|
|
414
|
+
declare function concatArrays(originVal: any, newVal: any): any | any[];
|
|
415
|
+
|
|
416
|
+
export { Merge, concatArrays, merge, mergeAndCompare, mergeAndConcat };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isArray, isPlainObject, isSymbol } from 'is-what';
|
|
2
|
+
|
|
3
|
+
function concatArrays(originVal, newVal) {
|
|
4
|
+
if (isArray(originVal) && isArray(newVal)) {
|
|
5
|
+
return originVal.concat(newVal);
|
|
6
|
+
}
|
|
7
|
+
return newVal;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function assignProp(carry, key, newVal, originalObject) {
|
|
11
|
+
const propType = {}.propertyIsEnumerable.call(originalObject, key) ? "enumerable" : "nonenumerable";
|
|
12
|
+
if (propType === "enumerable")
|
|
13
|
+
carry[key] = newVal;
|
|
14
|
+
if (propType === "nonenumerable") {
|
|
15
|
+
Object.defineProperty(carry, key, {
|
|
16
|
+
value: newVal,
|
|
17
|
+
enumerable: false,
|
|
18
|
+
writable: true,
|
|
19
|
+
configurable: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function mergeRecursively(origin, newComer, compareFn) {
|
|
24
|
+
if (!isPlainObject(newComer))
|
|
25
|
+
return newComer;
|
|
26
|
+
let newObject = {};
|
|
27
|
+
if (isPlainObject(origin)) {
|
|
28
|
+
const props2 = Object.getOwnPropertyNames(origin);
|
|
29
|
+
const symbols2 = Object.getOwnPropertySymbols(origin);
|
|
30
|
+
newObject = [...props2, ...symbols2].reduce((carry, key) => {
|
|
31
|
+
const targetVal = origin[key];
|
|
32
|
+
if (!isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key) || isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key)) {
|
|
33
|
+
assignProp(
|
|
34
|
+
carry,
|
|
35
|
+
key,
|
|
36
|
+
targetVal,
|
|
37
|
+
origin
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return carry;
|
|
41
|
+
}, {});
|
|
42
|
+
}
|
|
43
|
+
const props = Object.getOwnPropertyNames(newComer);
|
|
44
|
+
const symbols = Object.getOwnPropertySymbols(newComer);
|
|
45
|
+
const result = [...props, ...symbols].reduce((carry, key) => {
|
|
46
|
+
let newVal = newComer[key];
|
|
47
|
+
const targetVal = isPlainObject(origin) ? origin[key] : void 0;
|
|
48
|
+
if (targetVal !== void 0 && isPlainObject(newVal)) {
|
|
49
|
+
newVal = mergeRecursively(targetVal, newVal, compareFn);
|
|
50
|
+
}
|
|
51
|
+
const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
|
|
52
|
+
assignProp(
|
|
53
|
+
carry,
|
|
54
|
+
key,
|
|
55
|
+
propToAssign,
|
|
56
|
+
newComer
|
|
57
|
+
);
|
|
58
|
+
return carry;
|
|
59
|
+
}, newObject);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
function merge(object, ...otherObjects) {
|
|
63
|
+
return otherObjects.reduce((result, newComer) => {
|
|
64
|
+
return mergeRecursively(result, newComer);
|
|
65
|
+
}, object);
|
|
66
|
+
}
|
|
67
|
+
function mergeAndCompare(compareFn, object, ...otherObjects) {
|
|
68
|
+
return otherObjects.reduce((result, newComer) => {
|
|
69
|
+
return mergeRecursively(result, newComer, compareFn);
|
|
70
|
+
}, object);
|
|
71
|
+
}
|
|
72
|
+
function mergeAndConcat(object, ...otherObjects) {
|
|
73
|
+
return otherObjects.reduce((result, newComer) => {
|
|
74
|
+
return mergeRecursively(result, newComer, concatArrays);
|
|
75
|
+
}, object);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { concatArrays, merge, mergeAndCompare, mergeAndConcat };
|
package/package.json
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "merge-anything",
|
|
3
|
-
"version": "5.1.
|
|
4
|
-
"sideEffects": false,
|
|
5
|
-
"type": "module",
|
|
3
|
+
"version": "5.1.7",
|
|
6
4
|
"description": "Merge objects & other types recursively. A simple & small integration.",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"types": "./dist/
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./dist/cjs/index.d.cts",
|
|
14
|
+
"default": "./dist/cjs/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
},
|
|
17
22
|
"files": [
|
|
@@ -23,7 +28,7 @@
|
|
|
23
28
|
"scripts": {
|
|
24
29
|
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
25
30
|
"test": "vitest run",
|
|
26
|
-
"build": "rollup
|
|
31
|
+
"build": "rollup -c ./rollup.config.js",
|
|
27
32
|
"release": "npm run lint && del dist && npm run build && np"
|
|
28
33
|
},
|
|
29
34
|
"repository": {
|
|
@@ -71,8 +76,9 @@
|
|
|
71
76
|
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
72
77
|
"np": "^7.7.0",
|
|
73
78
|
"prettier": "^2.8.8",
|
|
74
|
-
"rollup": "^3.
|
|
75
|
-
"rollup-plugin-
|
|
79
|
+
"rollup": "^3.23.0",
|
|
80
|
+
"rollup-plugin-dts": "^5.3.0",
|
|
81
|
+
"rollup-plugin-esbuild": "^5.0.0",
|
|
76
82
|
"typescript": "^5.0.4",
|
|
77
83
|
"vitest": "^0.31.0"
|
|
78
84
|
},
|
package/dist/index.cjs
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isWhat = require('is-what');
|
|
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!!
|
|
11
|
-
}
|
|
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' })
|
|
117
|
-
// type P = Pop<Carguments[]>
|
|
118
|
-
|
|
119
|
-
exports.concatArrays = concatArrays;
|
|
120
|
-
exports.merge = merge;
|
|
121
|
-
exports.mergeAndCompare = mergeAndCompare;
|
|
122
|
-
exports.mergeAndConcat = mergeAndConcat;
|
package/dist/index.es.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { isArray, isPlainObject, isSymbol } from 'is-what';
|
|
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!!
|
|
9
|
-
}
|
|
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' })
|
|
115
|
-
// type P = Pop<Carguments[]>
|
|
116
|
-
|
|
117
|
-
export { concatArrays, merge, mergeAndCompare, mergeAndConcat };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function concatArrays(originVal: any, newVal: any): any | any[];
|
package/dist/types/index.d.ts
DELETED
package/dist/types/merge.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A [[List]]
|
|
3
|
-
* @param T its type
|
|
4
|
-
* @returns [[List]]
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* type list0 = [1, 2, 3]
|
|
8
|
-
* type list1 = number[]
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export type List<T = any> = readonly T[];
|
|
12
|
-
/**
|
|
13
|
-
* Get the length of `L`
|
|
14
|
-
* @param L to get length
|
|
15
|
-
* @returns [[String]] or `number`
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export type Length<L extends List> = L['length'];
|
|
21
|
-
/**
|
|
22
|
-
* Return the last item out of a [[List]]
|
|
23
|
-
* @param L
|
|
24
|
-
* @returns [[List]]
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export type Pop<L extends List> = L extends readonly [] ? never : L extends [...unknown[], infer Last] ? Last : L extends (infer T)[] ? T : never;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get the keys of `O` that are optional
|
|
3
|
-
* @param O
|
|
4
|
-
* @returns [[Key]]
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* ```
|
|
8
|
-
*/
|
|
9
|
-
type OptionalKeys<O extends object> = O extends unknown ? {
|
|
10
|
-
[K in keyof O]-?: {} extends Pick<O, K> ? K : never;
|
|
11
|
-
}[keyof O] : never;
|
|
12
|
-
/**
|
|
13
|
-
* Get the keys of `O` that are required
|
|
14
|
-
* @param O
|
|
15
|
-
* @returns [[Key]]
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
type RequiredKeys<O extends object> = O extends unknown ? {
|
|
21
|
-
[K in keyof O]-?: {} extends Pick<O, K> ? never : K;
|
|
22
|
-
}[keyof O] : never;
|
|
23
|
-
type MergeObjectDeeply<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = {
|
|
24
|
-
[K in keyof (O & O1)]: K extends RequiredKeys<O1> ? MergeObjectsOrReturnFallback<O[K], O1[K], O1[K]> : K extends OptionalKeys<O1> ? K extends OptionalKeys<O> ? MergeObjectsOrReturnFallback<Exclude<O[K], undefined>, Exclude<O1[K], undefined>, Exclude<O[K], undefined> | Exclude<O1[K], undefined>> : K extends RequiredKeys<O> ? Exclude<O1[K], undefined> extends O[K] ? O[K] : MergeObjectsOrReturnFallback<O[K], Exclude<O1[K], undefined>, O[K] | Exclude<O1[K], undefined>> : O1[K] : O[K];
|
|
25
|
-
};
|
|
26
|
-
type MergeObjectsOrReturnFallback<O, O1, Fallback> = O extends Record<string | number | symbol, unknown> ? O1 extends Record<string | number | symbol, unknown> ? MergeObjectDeeply<O, O1> : Fallback : Fallback;
|
|
27
|
-
/**
|
|
28
|
-
* Accurately merge the fields of `O` with the ones of `O1`. It is
|
|
29
|
-
* equivalent to the spread operator in JavaScript. [[Union]]s and [[Optional]]
|
|
30
|
-
* fields will be handled gracefully.
|
|
31
|
-
*
|
|
32
|
-
* (⚠️ needs `--strictNullChecks` enabled)
|
|
33
|
-
* @param O to complete
|
|
34
|
-
* @param O1 to copy from
|
|
35
|
-
* @returns [[Object]]
|
|
36
|
-
* @example
|
|
37
|
-
* ```ts
|
|
38
|
-
* import { PrettyPrint } from './PrettyPrint'
|
|
39
|
-
*
|
|
40
|
-
* type A1 = { a: number; b?: number; d?: number; e?: number; x: string; y?: number; z: string; } // prettier-ignore
|
|
41
|
-
* type A2 = { a?: number; c?: number; d?: number; e: number; x: number | undefined; y?: string; z?: number; } // prettier-ignore
|
|
42
|
-
*
|
|
43
|
-
* type Result = PrettyPrint<MergeDeep<A1, A2>>
|
|
44
|
-
* {
|
|
45
|
-
* a: number;
|
|
46
|
-
* b?: number | undefined;
|
|
47
|
-
* c?: number | undefined;
|
|
48
|
-
* d?: number | undefined;
|
|
49
|
-
* e: number;
|
|
50
|
-
* x: number | undefined;
|
|
51
|
-
* y?: string | number | undefined;
|
|
52
|
-
* z: string | number;
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export type MergeDeep<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = O extends unknown ? (O1 extends unknown ? MergeObjectDeeply<O, O1> : never) : never;
|
|
57
|
-
export {};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
type Has<U, U1> = [U1] extends [U] ? 1 : 0;
|
|
2
|
-
type If<B extends 0 | 1, Then, Else = never> = B extends 1 ? Then : Else;
|
|
3
|
-
export type PrettyPrint<A, Seen = never> = If<Has<Seen, A>, A, A extends Record<string | number | symbol, unknown> ? {
|
|
4
|
-
[K in keyof A]: PrettyPrint<A[K], A | Seen>;
|
|
5
|
-
} & unknown : A>;
|
|
6
|
-
export {};
|