merge-anything 5.0.4 → 5.1.0
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/README.md +23 -6
- package/dist/index.cjs +9 -6
- package/dist/index.es.js +9 -4
- package/dist/types/index.d.ts +2 -2
- package/dist/types/merge.d.ts +12 -10
- package/dist/types/typeUtils/Assign.d.ts +50 -0
- package/dist/types/typeUtils/Iteration.d.ts +257 -0
- package/dist/types/typeUtils/List.d.ts +20 -0
- package/dist/types/typeUtils/MergeDeep.d.ts +67 -0
- package/dist/types/typeUtils/PrettyPrint.d.ts +6 -0
- package/package.json +10 -11
package/README.md
CHANGED
|
@@ -26,15 +26,20 @@ This last one is crucial! In JavaScript almost everything is _an object_, sure,
|
|
|
26
26
|
|
|
27
27
|
merge-anything will merge objects and nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will copy that instance over "as is". ♻️
|
|
28
28
|
|
|
29
|
-
## Meet the family
|
|
29
|
+
## Meet the family (more tiny utils with TS support)
|
|
30
30
|
|
|
31
|
+
- [is-what 🙉](https://github.com/mesqueeb/is-what)
|
|
32
|
+
- [is-where 🙈](https://github.com/mesqueeb/is-where)
|
|
31
33
|
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
|
|
34
|
+
- [check-anything 👁](https://github.com/mesqueeb/check-anything)
|
|
35
|
+
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
|
|
36
|
+
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
|
|
37
|
+
- [map-anything 🗺](https://github.com/mesqueeb/map-anything)
|
|
32
38
|
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
|
|
33
|
-
- [find-and-replace-anything 🎣](https://github.com/mesqueeb/find-and-replace-anything)
|
|
34
|
-
- [compare-anything 🛰](https://github.com/mesqueeb/compare-anything)
|
|
35
39
|
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
|
|
40
|
+
- [case-anything 🐫](https://github.com/mesqueeb/case-anything)
|
|
36
41
|
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
|
|
37
|
-
- [
|
|
42
|
+
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
|
|
38
43
|
|
|
39
44
|
## Usage
|
|
40
45
|
|
|
@@ -63,6 +68,18 @@ In the example above, if you are using TypeScript, and you hover over `evolution
|
|
|
63
68
|
|
|
64
69
|

|
|
65
70
|
|
|
71
|
+
The return type of the `merge()` function is usable as a TypeScript utility as well:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import type { Merge } from 'merge-anything'
|
|
75
|
+
|
|
76
|
+
type A1 = { name: string }
|
|
77
|
+
type A2 = { types: { water: boolean } }
|
|
78
|
+
type A3 = { types: { fighting: boolean } }
|
|
79
|
+
|
|
80
|
+
type Result = Merge<A1, [A2, A3]>
|
|
81
|
+
```
|
|
82
|
+
|
|
66
83
|
## Rules
|
|
67
84
|
|
|
68
85
|
This package will recursively go through plain objects and merge the values onto a new object.
|
|
@@ -124,7 +141,7 @@ For this case we use `mergeAndCompare`. Here is an example with a compare functi
|
|
|
124
141
|
```js
|
|
125
142
|
import { mergeAndCompare } from 'merge-anything'
|
|
126
143
|
|
|
127
|
-
function concatStrings
|
|
144
|
+
function concatStrings(originVal, newVal, key) {
|
|
128
145
|
if (typeof originVal === 'string' && typeof newVal === 'string') {
|
|
129
146
|
// concat logic
|
|
130
147
|
return `${originVal}${newVal}`
|
|
@@ -192,7 +209,7 @@ It is literally just going through an object recursively and assigning the value
|
|
|
192
209
|
```js
|
|
193
210
|
import { isPlainObject } from 'is-what'
|
|
194
211
|
|
|
195
|
-
function mergeRecursively
|
|
212
|
+
function mergeRecursively(origin, newComer) {
|
|
196
213
|
if (!isPlainObject(newComer)) return newComer
|
|
197
214
|
// define newObject to merge all values upon
|
|
198
215
|
const newObject = isPlainObject(origin)
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var isWhat = require('is-what');
|
|
6
4
|
|
|
7
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
8
5
|
function concatArrays(originVal, newVal) {
|
|
9
6
|
if (isWhat.isArray(originVal) && isWhat.isArray(newVal)) {
|
|
10
7
|
// concat logic
|
|
@@ -67,8 +64,6 @@ function mergeRecursively(origin, newComer, compareFn) {
|
|
|
67
64
|
* Merge anything recursively.
|
|
68
65
|
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
69
66
|
* Basic types overwrite objects or other basic types.
|
|
70
|
-
* @param object
|
|
71
|
-
* @param otherObjects
|
|
72
67
|
*/
|
|
73
68
|
function merge(object, ...otherObjects) {
|
|
74
69
|
return otherObjects.reduce((result, newComer) => {
|
|
@@ -84,7 +79,15 @@ function mergeAndConcat(object, ...otherObjects) {
|
|
|
84
79
|
return otherObjects.reduce((result, newComer) => {
|
|
85
80
|
return mergeRecursively(result, newComer, concatArrays);
|
|
86
81
|
}, object);
|
|
87
|
-
}
|
|
82
|
+
}
|
|
83
|
+
// import { Timestamp } from 'firebase/firestore'
|
|
84
|
+
// type T1 = { date: Timestamp }
|
|
85
|
+
// type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
|
|
86
|
+
// type Test = Merge<T1, T2>
|
|
87
|
+
// type A1 = { arr: string[] }
|
|
88
|
+
// type A2 = { arr: number[] }
|
|
89
|
+
// type A3 = { arr: boolean[] }
|
|
90
|
+
// type Test = Merge<A1, [A2, A3]>
|
|
88
91
|
|
|
89
92
|
exports.concatArrays = concatArrays;
|
|
90
93
|
exports.merge = merge;
|
package/dist/index.es.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isArray, isPlainObject, isSymbol } from 'is-what';
|
|
2
2
|
|
|
3
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
4
3
|
function concatArrays(originVal, newVal) {
|
|
5
4
|
if (isArray(originVal) && isArray(newVal)) {
|
|
6
5
|
// concat logic
|
|
@@ -63,8 +62,6 @@ function mergeRecursively(origin, newComer, compareFn) {
|
|
|
63
62
|
* Merge anything recursively.
|
|
64
63
|
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
65
64
|
* Basic types overwrite objects or other basic types.
|
|
66
|
-
* @param object
|
|
67
|
-
* @param otherObjects
|
|
68
65
|
*/
|
|
69
66
|
function merge(object, ...otherObjects) {
|
|
70
67
|
return otherObjects.reduce((result, newComer) => {
|
|
@@ -80,6 +77,14 @@ function mergeAndConcat(object, ...otherObjects) {
|
|
|
80
77
|
return otherObjects.reduce((result, newComer) => {
|
|
81
78
|
return mergeRecursively(result, newComer, concatArrays);
|
|
82
79
|
}, object);
|
|
83
|
-
}
|
|
80
|
+
}
|
|
81
|
+
// import { Timestamp } from 'firebase/firestore'
|
|
82
|
+
// type T1 = { date: Timestamp }
|
|
83
|
+
// type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
|
|
84
|
+
// type Test = Merge<T1, T2>
|
|
85
|
+
// type A1 = { arr: string[] }
|
|
86
|
+
// type A2 = { arr: number[] }
|
|
87
|
+
// type A3 = { arr: boolean[] }
|
|
88
|
+
// type Test = Merge<A1, [A2, A3]>
|
|
84
89
|
|
|
85
90
|
export { concatArrays, merge, mergeAndCompare, mergeAndConcat };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export * from './merge';
|
|
2
|
+
export * from './extensions';
|
package/dist/types/merge.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type { Assign } from './typeUtils/Assign';
|
|
2
|
+
import type { List } from './typeUtils/List';
|
|
3
|
+
import type { PrettyPrint } from './typeUtils/PrettyPrint';
|
|
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 declare type Merge<T extends object, Ts extends List<object>> = PrettyPrint<Assign<T, Ts>>;
|
|
5
10
|
/**
|
|
6
11
|
* Merge anything recursively.
|
|
7
12
|
* Objects get merged, special objects (classes etc.) are re-assigned "as is".
|
|
8
13
|
* Basic types overwrite objects or other basic types.
|
|
9
|
-
* @param object
|
|
10
|
-
* @param otherObjects
|
|
11
14
|
*/
|
|
12
|
-
export declare function merge<T extends Record<string,
|
|
13
|
-
export declare function mergeAndCompare<T extends Record<string,
|
|
14
|
-
export declare function mergeAndConcat<T extends Record<string,
|
|
15
|
-
export {};
|
|
15
|
+
export declare function merge<T extends Record<string | number | symbol, unknown>, Tn extends Record<string | number | symbol, unknown>[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
16
|
+
export declare function mergeAndCompare<T extends Record<string | number | symbol, unknown>, Tn extends Record<string | number | symbol, unknown>[]>(compareFn: (prop1: any, prop2: any, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
17
|
+
export declare function mergeAndConcat<T extends Record<string | number | symbol, unknown>, Tn extends Record<string | number | symbol, unknown>[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { MergeDeep } from './MergeDeep';
|
|
2
|
+
import { Iteration, IterationOf, Pos, Next } from './Iteration';
|
|
3
|
+
import { Length, List } from './List';
|
|
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
|
+
declare 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
|
+
declare type Extends<A1, A2> = [A1] extends [never] ? 0 : A1 extends A2 ? 1 : 0;
|
|
37
|
+
declare type __Assign<O extends object, Os extends List<object>, I extends Iteration = IterationOf<0>> = Extends<Pos<I>, Length<Os>> extends 1 ? O : __Assign<MergeDeep<O, Os[Pos<I>]>, Os, Next<I>>;
|
|
38
|
+
declare type _Assign<O extends object, Os extends List<object>> = __Assign<O, Os> extends infer X ? Cast<X, object> : 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 declare type Assign<O extends object, Os extends List<object>> = O extends unknown ? Os extends unknown ? _Assign<O, Os> : never : never;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An entry of `IterationMap`
|
|
3
|
+
*/
|
|
4
|
+
export declare type Iteration = [
|
|
5
|
+
value: number,
|
|
6
|
+
sign: '-' | '0' | '+',
|
|
7
|
+
prev: keyof IterationMap,
|
|
8
|
+
next: keyof IterationMap,
|
|
9
|
+
oppo: keyof IterationMap
|
|
10
|
+
];
|
|
11
|
+
export declare type IterationMap = {
|
|
12
|
+
'__': [number, '-' | '0' | '+', '__', '__', '__'];
|
|
13
|
+
'-100': [-100, '-', '__', '-99', '100'];
|
|
14
|
+
'-99': [-99, '-', '-100', '-98', '99'];
|
|
15
|
+
'-98': [-98, '-', '-99', '-97', '98'];
|
|
16
|
+
'-97': [-97, '-', '-98', '-96', '97'];
|
|
17
|
+
'-96': [-96, '-', '-97', '-95', '96'];
|
|
18
|
+
'-95': [-95, '-', '-96', '-94', '95'];
|
|
19
|
+
'-94': [-94, '-', '-95', '-93', '94'];
|
|
20
|
+
'-93': [-93, '-', '-94', '-92', '93'];
|
|
21
|
+
'-92': [-92, '-', '-93', '-91', '92'];
|
|
22
|
+
'-91': [-91, '-', '-92', '-90', '91'];
|
|
23
|
+
'-90': [-90, '-', '-91', '-89', '90'];
|
|
24
|
+
'-89': [-89, '-', '-90', '-88', '89'];
|
|
25
|
+
'-88': [-88, '-', '-89', '-87', '88'];
|
|
26
|
+
'-87': [-87, '-', '-88', '-86', '87'];
|
|
27
|
+
'-86': [-86, '-', '-87', '-85', '86'];
|
|
28
|
+
'-85': [-85, '-', '-86', '-84', '85'];
|
|
29
|
+
'-84': [-84, '-', '-85', '-83', '84'];
|
|
30
|
+
'-83': [-83, '-', '-84', '-82', '83'];
|
|
31
|
+
'-82': [-82, '-', '-83', '-81', '82'];
|
|
32
|
+
'-81': [-81, '-', '-82', '-80', '81'];
|
|
33
|
+
'-80': [-80, '-', '-81', '-79', '80'];
|
|
34
|
+
'-79': [-79, '-', '-80', '-78', '79'];
|
|
35
|
+
'-78': [-78, '-', '-79', '-77', '78'];
|
|
36
|
+
'-77': [-77, '-', '-78', '-76', '77'];
|
|
37
|
+
'-76': [-76, '-', '-77', '-75', '76'];
|
|
38
|
+
'-75': [-75, '-', '-76', '-74', '75'];
|
|
39
|
+
'-74': [-74, '-', '-75', '-73', '74'];
|
|
40
|
+
'-73': [-73, '-', '-74', '-72', '73'];
|
|
41
|
+
'-72': [-72, '-', '-73', '-71', '72'];
|
|
42
|
+
'-71': [-71, '-', '-72', '-70', '71'];
|
|
43
|
+
'-70': [-70, '-', '-71', '-69', '70'];
|
|
44
|
+
'-69': [-69, '-', '-70', '-68', '69'];
|
|
45
|
+
'-68': [-68, '-', '-69', '-67', '68'];
|
|
46
|
+
'-67': [-67, '-', '-68', '-66', '67'];
|
|
47
|
+
'-66': [-66, '-', '-67', '-65', '66'];
|
|
48
|
+
'-65': [-65, '-', '-66', '-64', '65'];
|
|
49
|
+
'-64': [-64, '-', '-65', '-63', '64'];
|
|
50
|
+
'-63': [-63, '-', '-64', '-62', '63'];
|
|
51
|
+
'-62': [-62, '-', '-63', '-61', '62'];
|
|
52
|
+
'-61': [-61, '-', '-62', '-60', '61'];
|
|
53
|
+
'-60': [-60, '-', '-61', '-59', '60'];
|
|
54
|
+
'-59': [-59, '-', '-60', '-58', '59'];
|
|
55
|
+
'-58': [-58, '-', '-59', '-57', '58'];
|
|
56
|
+
'-57': [-57, '-', '-58', '-56', '57'];
|
|
57
|
+
'-56': [-56, '-', '-57', '-55', '56'];
|
|
58
|
+
'-55': [-55, '-', '-56', '-54', '55'];
|
|
59
|
+
'-54': [-54, '-', '-55', '-53', '54'];
|
|
60
|
+
'-53': [-53, '-', '-54', '-52', '53'];
|
|
61
|
+
'-52': [-52, '-', '-53', '-51', '52'];
|
|
62
|
+
'-51': [-51, '-', '-52', '-50', '51'];
|
|
63
|
+
'-50': [-50, '-', '-51', '-49', '50'];
|
|
64
|
+
'-49': [-49, '-', '-50', '-48', '49'];
|
|
65
|
+
'-48': [-48, '-', '-49', '-47', '48'];
|
|
66
|
+
'-47': [-47, '-', '-48', '-46', '47'];
|
|
67
|
+
'-46': [-46, '-', '-47', '-45', '46'];
|
|
68
|
+
'-45': [-45, '-', '-46', '-44', '45'];
|
|
69
|
+
'-44': [-44, '-', '-45', '-43', '44'];
|
|
70
|
+
'-43': [-43, '-', '-44', '-42', '43'];
|
|
71
|
+
'-42': [-42, '-', '-43', '-41', '42'];
|
|
72
|
+
'-41': [-41, '-', '-42', '-40', '41'];
|
|
73
|
+
'-40': [-40, '-', '-41', '-39', '40'];
|
|
74
|
+
'-39': [-39, '-', '-40', '-38', '39'];
|
|
75
|
+
'-38': [-38, '-', '-39', '-37', '38'];
|
|
76
|
+
'-37': [-37, '-', '-38', '-36', '37'];
|
|
77
|
+
'-36': [-36, '-', '-37', '-35', '36'];
|
|
78
|
+
'-35': [-35, '-', '-36', '-34', '35'];
|
|
79
|
+
'-34': [-34, '-', '-35', '-33', '34'];
|
|
80
|
+
'-33': [-33, '-', '-34', '-32', '33'];
|
|
81
|
+
'-32': [-32, '-', '-33', '-31', '32'];
|
|
82
|
+
'-31': [-31, '-', '-32', '-30', '31'];
|
|
83
|
+
'-30': [-30, '-', '-31', '-29', '30'];
|
|
84
|
+
'-29': [-29, '-', '-30', '-28', '29'];
|
|
85
|
+
'-28': [-28, '-', '-29', '-27', '28'];
|
|
86
|
+
'-27': [-27, '-', '-28', '-26', '27'];
|
|
87
|
+
'-26': [-26, '-', '-27', '-25', '26'];
|
|
88
|
+
'-25': [-25, '-', '-26', '-24', '25'];
|
|
89
|
+
'-24': [-24, '-', '-25', '-23', '24'];
|
|
90
|
+
'-23': [-23, '-', '-24', '-22', '23'];
|
|
91
|
+
'-22': [-22, '-', '-23', '-21', '22'];
|
|
92
|
+
'-21': [-21, '-', '-22', '-20', '21'];
|
|
93
|
+
'-20': [-20, '-', '-21', '-19', '20'];
|
|
94
|
+
'-19': [-19, '-', '-20', '-18', '19'];
|
|
95
|
+
'-18': [-18, '-', '-19', '-17', '18'];
|
|
96
|
+
'-17': [-17, '-', '-18', '-16', '17'];
|
|
97
|
+
'-16': [-16, '-', '-17', '-15', '16'];
|
|
98
|
+
'-15': [-15, '-', '-16', '-14', '15'];
|
|
99
|
+
'-14': [-14, '-', '-15', '-13', '14'];
|
|
100
|
+
'-13': [-13, '-', '-14', '-12', '13'];
|
|
101
|
+
'-12': [-12, '-', '-13', '-11', '12'];
|
|
102
|
+
'-11': [-11, '-', '-12', '-10', '11'];
|
|
103
|
+
'-10': [-10, '-', '-11', '-9', '10'];
|
|
104
|
+
'-9': [-9, '-', '-10', '-8', '9'];
|
|
105
|
+
'-8': [-8, '-', '-9', '-7', '8'];
|
|
106
|
+
'-7': [-7, '-', '-8', '-6', '7'];
|
|
107
|
+
'-6': [-6, '-', '-7', '-5', '6'];
|
|
108
|
+
'-5': [-5, '-', '-6', '-4', '5'];
|
|
109
|
+
'-4': [-4, '-', '-5', '-3', '4'];
|
|
110
|
+
'-3': [-3, '-', '-4', '-2', '3'];
|
|
111
|
+
'-2': [-2, '-', '-3', '-1', '2'];
|
|
112
|
+
'-1': [-1, '-', '-2', '0', '1'];
|
|
113
|
+
'0': [0, '0', '-1', '1', '0'];
|
|
114
|
+
'1': [1, '+', '0', '2', '-1'];
|
|
115
|
+
'2': [2, '+', '1', '3', '-2'];
|
|
116
|
+
'3': [3, '+', '2', '4', '-3'];
|
|
117
|
+
'4': [4, '+', '3', '5', '-4'];
|
|
118
|
+
'5': [5, '+', '4', '6', '-5'];
|
|
119
|
+
'6': [6, '+', '5', '7', '-6'];
|
|
120
|
+
'7': [7, '+', '6', '8', '-7'];
|
|
121
|
+
'8': [8, '+', '7', '9', '-8'];
|
|
122
|
+
'9': [9, '+', '8', '10', '-9'];
|
|
123
|
+
'10': [10, '+', '9', '11', '-10'];
|
|
124
|
+
'11': [11, '+', '10', '12', '-11'];
|
|
125
|
+
'12': [12, '+', '11', '13', '-12'];
|
|
126
|
+
'13': [13, '+', '12', '14', '-13'];
|
|
127
|
+
'14': [14, '+', '13', '15', '-14'];
|
|
128
|
+
'15': [15, '+', '14', '16', '-15'];
|
|
129
|
+
'16': [16, '+', '15', '17', '-16'];
|
|
130
|
+
'17': [17, '+', '16', '18', '-17'];
|
|
131
|
+
'18': [18, '+', '17', '19', '-18'];
|
|
132
|
+
'19': [19, '+', '18', '20', '-19'];
|
|
133
|
+
'20': [20, '+', '19', '21', '-20'];
|
|
134
|
+
'21': [21, '+', '20', '22', '-21'];
|
|
135
|
+
'22': [22, '+', '21', '23', '-22'];
|
|
136
|
+
'23': [23, '+', '22', '24', '-23'];
|
|
137
|
+
'24': [24, '+', '23', '25', '-24'];
|
|
138
|
+
'25': [25, '+', '24', '26', '-25'];
|
|
139
|
+
'26': [26, '+', '25', '27', '-26'];
|
|
140
|
+
'27': [27, '+', '26', '28', '-27'];
|
|
141
|
+
'28': [28, '+', '27', '29', '-28'];
|
|
142
|
+
'29': [29, '+', '28', '30', '-29'];
|
|
143
|
+
'30': [30, '+', '29', '31', '-30'];
|
|
144
|
+
'31': [31, '+', '30', '32', '-31'];
|
|
145
|
+
'32': [32, '+', '31', '33', '-32'];
|
|
146
|
+
'33': [33, '+', '32', '34', '-33'];
|
|
147
|
+
'34': [34, '+', '33', '35', '-34'];
|
|
148
|
+
'35': [35, '+', '34', '36', '-35'];
|
|
149
|
+
'36': [36, '+', '35', '37', '-36'];
|
|
150
|
+
'37': [37, '+', '36', '38', '-37'];
|
|
151
|
+
'38': [38, '+', '37', '39', '-38'];
|
|
152
|
+
'39': [39, '+', '38', '40', '-39'];
|
|
153
|
+
'40': [40, '+', '39', '41', '-40'];
|
|
154
|
+
'41': [41, '+', '40', '42', '-41'];
|
|
155
|
+
'42': [42, '+', '41', '43', '-42'];
|
|
156
|
+
'43': [43, '+', '42', '44', '-43'];
|
|
157
|
+
'44': [44, '+', '43', '45', '-44'];
|
|
158
|
+
'45': [45, '+', '44', '46', '-45'];
|
|
159
|
+
'46': [46, '+', '45', '47', '-46'];
|
|
160
|
+
'47': [47, '+', '46', '48', '-47'];
|
|
161
|
+
'48': [48, '+', '47', '49', '-48'];
|
|
162
|
+
'49': [49, '+', '48', '50', '-49'];
|
|
163
|
+
'50': [50, '+', '49', '51', '-50'];
|
|
164
|
+
'51': [51, '+', '50', '52', '-51'];
|
|
165
|
+
'52': [52, '+', '51', '53', '-52'];
|
|
166
|
+
'53': [53, '+', '52', '54', '-53'];
|
|
167
|
+
'54': [54, '+', '53', '55', '-54'];
|
|
168
|
+
'55': [55, '+', '54', '56', '-55'];
|
|
169
|
+
'56': [56, '+', '55', '57', '-56'];
|
|
170
|
+
'57': [57, '+', '56', '58', '-57'];
|
|
171
|
+
'58': [58, '+', '57', '59', '-58'];
|
|
172
|
+
'59': [59, '+', '58', '60', '-59'];
|
|
173
|
+
'60': [60, '+', '59', '61', '-60'];
|
|
174
|
+
'61': [61, '+', '60', '62', '-61'];
|
|
175
|
+
'62': [62, '+', '61', '63', '-62'];
|
|
176
|
+
'63': [63, '+', '62', '64', '-63'];
|
|
177
|
+
'64': [64, '+', '63', '65', '-64'];
|
|
178
|
+
'65': [65, '+', '64', '66', '-65'];
|
|
179
|
+
'66': [66, '+', '65', '67', '-66'];
|
|
180
|
+
'67': [67, '+', '66', '68', '-67'];
|
|
181
|
+
'68': [68, '+', '67', '69', '-68'];
|
|
182
|
+
'69': [69, '+', '68', '70', '-69'];
|
|
183
|
+
'70': [70, '+', '69', '71', '-70'];
|
|
184
|
+
'71': [71, '+', '70', '72', '-71'];
|
|
185
|
+
'72': [72, '+', '71', '73', '-72'];
|
|
186
|
+
'73': [73, '+', '72', '74', '-73'];
|
|
187
|
+
'74': [74, '+', '73', '75', '-74'];
|
|
188
|
+
'75': [75, '+', '74', '76', '-75'];
|
|
189
|
+
'76': [76, '+', '75', '77', '-76'];
|
|
190
|
+
'77': [77, '+', '76', '78', '-77'];
|
|
191
|
+
'78': [78, '+', '77', '79', '-78'];
|
|
192
|
+
'79': [79, '+', '78', '80', '-79'];
|
|
193
|
+
'80': [80, '+', '79', '81', '-80'];
|
|
194
|
+
'81': [81, '+', '80', '82', '-81'];
|
|
195
|
+
'82': [82, '+', '81', '83', '-82'];
|
|
196
|
+
'83': [83, '+', '82', '84', '-83'];
|
|
197
|
+
'84': [84, '+', '83', '85', '-84'];
|
|
198
|
+
'85': [85, '+', '84', '86', '-85'];
|
|
199
|
+
'86': [86, '+', '85', '87', '-86'];
|
|
200
|
+
'87': [87, '+', '86', '88', '-87'];
|
|
201
|
+
'88': [88, '+', '87', '89', '-88'];
|
|
202
|
+
'89': [89, '+', '88', '90', '-89'];
|
|
203
|
+
'90': [90, '+', '89', '91', '-90'];
|
|
204
|
+
'91': [91, '+', '90', '92', '-91'];
|
|
205
|
+
'92': [92, '+', '91', '93', '-92'];
|
|
206
|
+
'93': [93, '+', '92', '94', '-93'];
|
|
207
|
+
'94': [94, '+', '93', '95', '-94'];
|
|
208
|
+
'95': [95, '+', '94', '96', '-95'];
|
|
209
|
+
'96': [96, '+', '95', '97', '-96'];
|
|
210
|
+
'97': [97, '+', '96', '98', '-97'];
|
|
211
|
+
'98': [98, '+', '97', '99', '-98'];
|
|
212
|
+
'99': [99, '+', '98', '100', '-99'];
|
|
213
|
+
'100': [100, '+', '99', '__', '-100'];
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Transform a number into an [[Iteration]]
|
|
217
|
+
* (to use [[Prev]], [[Next]], & [[Pos]])
|
|
218
|
+
* @param N to transform
|
|
219
|
+
* @returns [[Iteration]]
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* type i = IterationOf<0> // ["-1", "1", "0", 0, "0"]
|
|
223
|
+
*
|
|
224
|
+
* type next = Next<i> // ["0", "2", "1", 1, "+"]
|
|
225
|
+
* type prev = Prev<i> // ["-2", "0", "-1", -1, "-"]
|
|
226
|
+
*
|
|
227
|
+
* type nnext = Pos<next> // +1
|
|
228
|
+
* type nprev = Pos<prev> // -1
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export declare type IterationOf<N extends number> = `${N}` extends keyof IterationMap ? IterationMap[`${N}`] : IterationMap['__'];
|
|
232
|
+
/**
|
|
233
|
+
* Get the position of `I` (**number**)
|
|
234
|
+
* @param I to query
|
|
235
|
+
* @returns `number`
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* type i = IterationOf<'20'>
|
|
239
|
+
*
|
|
240
|
+
* type test0 = Pos<i> // 20
|
|
241
|
+
* type test1 = Pos<Next<i>> // 21
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
export declare type Pos<I extends Iteration> = I[0];
|
|
245
|
+
/**
|
|
246
|
+
* Move `I`'s position forward
|
|
247
|
+
* @param I to move
|
|
248
|
+
* @returns [[Iteration]]
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* type i = IterationOf<'20'>
|
|
252
|
+
*
|
|
253
|
+
* type test0 = Pos<i> // 20
|
|
254
|
+
* type test1 = Pos<Next<i>> // 21
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
export declare type Next<I extends Iteration> = IterationMap[I[3]];
|
|
@@ -0,0 +1,20 @@
|
|
|
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 declare 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 declare type Length<L extends List> = L['length'];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make an object properties (all) `never`. We use this to intersect `object`s and
|
|
3
|
+
* preserve the combine modifiers like `+readonly` and `?optional`.
|
|
4
|
+
*/
|
|
5
|
+
declare type Anyfy<O extends object> = {
|
|
6
|
+
[K in keyof O]: any;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Get in `O` the type of a field of key `K`
|
|
10
|
+
* @param O to extract from
|
|
11
|
+
* @param K to extract at
|
|
12
|
+
* @returns [[Any]]
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* type User = {
|
|
16
|
+
* info: { name: string; age: number; payment: {} }
|
|
17
|
+
* id: number
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* type test0 = At<User, 'id'> // number
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare type At<A, K extends string | number | symbol> = unknown extends A ? unknown : K extends keyof A ? A[K] : undefined;
|
|
24
|
+
declare type MergeObjectDeeply<O extends Record<string | number | symbol, unknown>, O1 extends Record<string | number | symbol, unknown>> = {
|
|
25
|
+
[K in keyof (Anyfy<O> & O1)]: K extends keyof O1 ? MergeObjectOrReturnValue<At<O, K>, At<O1, K>> : O[K];
|
|
26
|
+
};
|
|
27
|
+
declare type MergeObjectOrReturnValue<OK, O1K> = [O1K] extends [never] ? OK : OK extends Record<string | number | symbol, unknown> ? O1K extends Record<string | number | symbol, unknown> ? MergeObjectDeeply<OK, O1K> : O1K : O1K;
|
|
28
|
+
/**
|
|
29
|
+
* Accurately merge the fields of `O` with the ones of `O1`. It is
|
|
30
|
+
* equivalent to the spread operator in JavaScript. [[Union]]s and [[Optional]]
|
|
31
|
+
* fields will be handled gracefully.
|
|
32
|
+
*
|
|
33
|
+
* (⚠️ needs `--strictNullChecks` enabled)
|
|
34
|
+
* @param O to complete
|
|
35
|
+
* @param O1 to copy from
|
|
36
|
+
* @returns [[Object]]
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* type O = {
|
|
40
|
+
* name?: string
|
|
41
|
+
* age?: number
|
|
42
|
+
* zip?: string
|
|
43
|
+
* pay: { cvv?: number }
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* type O1 = {
|
|
47
|
+
* age: number
|
|
48
|
+
* zip?: number
|
|
49
|
+
* pay: { cvv : number; ccn?: string }
|
|
50
|
+
* city: string
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* type test = MergeDeep<O, O1>
|
|
54
|
+
* // {
|
|
55
|
+
* // name?: string;
|
|
56
|
+
* // age: number;
|
|
57
|
+
* // zip: number | undefined;
|
|
58
|
+
* // pay: {
|
|
59
|
+
* // cvv: number;
|
|
60
|
+
* // ccn?: string;
|
|
61
|
+
* // };
|
|
62
|
+
* // city: string;
|
|
63
|
+
* // }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare type MergeDeep<O extends object, O1 extends object> = O extends unknown ? O1 extends unknown ? MergeObjectOrReturnValue<O, O1> : never : never;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
declare type Has<U, U1> = [U1] extends [U] ? 1 : 0;
|
|
2
|
+
declare type If<B extends 0 | 1, Then, Else = never> = B extends 1 ? Then : Else;
|
|
3
|
+
export declare 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 {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "merge-anything",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Merge objects & other types recursively. A simple & small integration.",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
25
25
|
"test": "vitest run",
|
|
26
|
-
"build": "rollup -c ./scripts/build.js",
|
|
26
|
+
"build": "rollup --bundleConfigAsCjs -c ./scripts/build.js",
|
|
27
27
|
"release": "npm run lint && del dist && npm run build && np"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
@@ -60,22 +60,21 @@
|
|
|
60
60
|
},
|
|
61
61
|
"homepage": "https://github.com/mesqueeb/merge-anything#readme",
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"is-what": "^4.1.7"
|
|
64
|
-
"ts-toolbelt": "^9.6.0"
|
|
63
|
+
"is-what": "^4.1.7"
|
|
65
64
|
},
|
|
66
65
|
"devDependencies": {
|
|
67
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
68
|
-
"@typescript-eslint/parser": "^5.
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^5.42.1",
|
|
67
|
+
"@typescript-eslint/parser": "^5.42.1",
|
|
69
68
|
"del-cli": "^5.0.0",
|
|
70
|
-
"eslint": "^8.
|
|
69
|
+
"eslint": "^8.27.0",
|
|
71
70
|
"eslint-config-prettier": "^8.5.0",
|
|
72
71
|
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
73
72
|
"np": "^7.6.2",
|
|
74
73
|
"prettier": "^2.7.1",
|
|
75
|
-
"rollup": "^
|
|
76
|
-
"rollup-plugin-typescript2": "^0.
|
|
77
|
-
"typescript": "^4.
|
|
78
|
-
"vitest": "^0.
|
|
74
|
+
"rollup": "^3.3.0",
|
|
75
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
76
|
+
"typescript": "^4.8.4",
|
|
77
|
+
"vitest": "^0.25.1"
|
|
79
78
|
},
|
|
80
79
|
"np": {
|
|
81
80
|
"yarn": false,
|