@@ -56,7 +56,122 @@ npm install @hypernym/merge
56
56
``` ts
57
57
import { merge } from ' @hypernym/merge'
58
58
59
- const obj = merge ([a , b , c ])
59
+ const A = {
60
+ a: true ,
61
+ b: {
62
+ c: {
63
+ d: [1 , 2 , 3 ],
64
+ },
65
+ e: {
66
+ f: true ,
67
+ },
68
+ },
69
+ }
70
+
71
+ const B = {
72
+ a: ' merge' ,
73
+ b: {
74
+ c: {
75
+ d: [' 4' , ' 5' , ' 6' ],
76
+ },
77
+ e: {
78
+ f: {
79
+ g: 33 ,
80
+ },
81
+ },
82
+ h: [23 , 33 ],
83
+ },
84
+ }
85
+
86
+ const C = {
87
+ i: {
88
+ j: 77 ,
89
+ k: 99 ,
90
+ },
91
+ }
92
+
93
+ const D = {
94
+ i: {
95
+ j: undefined ,
96
+ k: null ,
97
+ },
98
+ }
99
+
100
+ const result = merge ([A , B , C , D ])
101
+
102
+ const resultRules = merge ([A , B , C , D ], {
103
+ rules: { array: ' override' , undefined: ' skip' , null: ' skip' },
104
+ })
105
+ ```
106
+
107
+ ** Output: result**
108
+
109
+ ``` ts
110
+ // Merged Result
111
+ {
112
+ a : ' merge' ,
113
+ b : {
114
+ c : { d : [ 1 , 2 , 3 , ' 4' , ' 5' , ' 6' ] },
115
+ e : { f : { g : 33 } },
116
+ h : [ 23 , 33 ]
117
+ },
118
+ i : { j : undefined , k : null }
119
+ }
120
+ ```
121
+
122
+ ``` ts
123
+ // Automatically Infered Types
124
+ {
125
+ a : string
126
+ b : {
127
+ c : {
128
+ d : (string | number )[]
129
+ }
130
+ e : {
131
+ f : {
132
+ g : number
133
+ }
134
+ }
135
+ h : number []
136
+ }
137
+ i : {
138
+ j : undefined
139
+ k : null
140
+ }
141
+ }
142
+ ```
143
+
144
+ ** Output: resultRules**
145
+
146
+ ``` ts
147
+ // Merged Result With Custom Rules
148
+ {
149
+ a : ' merge' ,
150
+ b : { c : { d : [ ' 4' , ' 5' , ' 6' ] }, e : { f : { g : 33 } }, h : [ 23 , 33 ] },
151
+ i : { j : 77 , k : 99 }
152
+ }
153
+ ```
154
+
155
+ ``` ts
156
+ // Automatically Infered Types
157
+ {
158
+ a : string
159
+ b : {
160
+ c : {
161
+ d : string []
162
+ }
163
+ e : {
164
+ f : {
165
+ g : number
166
+ }
167
+ }
168
+ h : number []
169
+ }
170
+ i : {
171
+ j : number
172
+ k : number
173
+ }
174
+ }
60
175
```
61
176
62
177
## API
@@ -92,10 +207,25 @@ merge([{ a: 1 }, { a: null }], { rules: { null: 'skip' } }) // => { a: 1 }
92
207
``` ts
93
208
import type { Merge } from ' @hypernym/merge'
94
209
95
- type A = { a ? : string }
96
- type B = { b : string }
210
+ Merge < sources , options >
211
+ ```
97
212
98
- type Result = Merge <[A , B ], { rules: { undefined: ' skip' } }> // => { a: string, b: string }
213
+ #### sources
214
+
215
+ - Type: ` object[] `
216
+ - Required: ` true `
217
+
218
+ ``` ts
219
+ Merge < [{ a: number }, { b: number }, { c: number }]> // => { a: number, b: number, c: number }
220
+ ```
221
+
222
+ #### options
223
+
224
+ - Type: ` object `
225
+ - Default: ` undefined `
226
+
227
+ ``` ts
228
+ Merge < [{ a: number }, { a: null }], { rules: { null: ' skip' } }> // => { a: number }
99
229
```
100
230
101
231
## Options
@@ -116,15 +246,15 @@ Defines how merging behaves for the specified types.
116
246
117
247
Specifies the merge strategy for ` array ` types.
118
248
119
- - ` combine ` — combines all values from all sources into a final result.
120
- - ` override ` — value from the last source overrides the others in the final result.
249
+ - ` combine ` — Combines all values from all sources into a final result, meaning that the right sources will merge the properties with the left sources and combine their values .
250
+ - ` override ` — Value from the last source overrides the others in the final result, meaning that the right sources will merge the properties with the left sources and overwrite their values .
121
251
122
252
``` ts
123
- const a = { a: [1 , 2 ] }
124
- const b = { a: [3 , 4 ] }
253
+ const A = { a: [1 , 2 ] }
254
+ const B = { a: [3 , 4 ] }
125
255
126
- const o1 = merge ([a , b ], { rules: { array: ' combine' } }) // => { a: [1, 2, 3, 4] }
127
- const o2 = merge ([a , b ], { rules: { array: ' override' } }) // => { a: [3, 4] }
256
+ const resultCombine = merge ([A , B ], { rules: { array: ' combine' } }) // => { a: [1, 2, 3, 4] }
257
+ const resultOverride = merge ([A , B ], { rules: { array: ' override' } }) // => { a: [3, 4] }
128
258
```
129
259
130
260
#### undefined
@@ -134,15 +264,15 @@ const o2 = merge([a, b], { rules: { array: 'override' } }) // => { a: [3, 4] }
134
264
135
265
Specifies the merge strategy for the ` undefined ` type.
136
266
137
- - ` override ` — explicitly defined value from the last source overrides the others in the final result.
138
- - ` skip ` — skips the explicitly defined value from the last source and uses the defined one if any .
267
+ - ` override ` — Explicitly defined value from the last source overrides the others in the final result.
268
+ - ` skip ` — Skips the explicitly defined value from the last source and uses the defined one.
139
269
140
270
``` ts
141
- const a = { a: ' hello' }
142
- const b = { a: undefined }
271
+ const A = { a: ' hello' }
272
+ const B = { a: undefined }
143
273
144
- const o1 = merge ([a , b ], { rules: { undefined: ' override' } }) // => { a: undefined }
145
- const o2 = merge ([a , b ], { rules: { undefined: ' skip' } }) // => { a: 'hello' }
274
+ const resultOverride = merge ([A , B ], { rules: { undefined: ' override' } }) // => { a: undefined }
275
+ const resultSkip = merge ([A , B ], { rules: { undefined: ' skip' } }) // => { a: 'hello' }
146
276
```
147
277
148
278
#### null
@@ -152,15 +282,15 @@ const o2 = merge([a, b], { rules: { undefined: 'skip' } }) // => { a: 'hello' }
152
282
153
283
Specifies the merge strategy for the ` null ` type.
154
284
155
- - ` override ` — explicitly defined value from the last source overrides the others in the final result.
156
- - ` skip ` — skips the explicitly defined value from the last source and uses the defined one if any .
285
+ - ` override ` — Explicitly defined value from the last source overrides the others in the final result.
286
+ - ` skip ` — Skips the explicitly defined value from the last source and uses the defined one.
157
287
158
288
``` ts
159
- const a = { a: ' hello' }
160
- const b = { a: null }
289
+ const A = { a: ' hello' }
290
+ const B = { a: null }
161
291
162
- const o1 = merge ([a , b ], { rules: { null: ' override' } }) // => { a: null }
163
- const o2 = merge ([a , b ], { rules: { null: ' skip' } }) // => { a: 'hello' }
292
+ const resultOverride = merge ([A , B ], { rules: { null: ' override' } }) // => { a: null }
293
+ const resultSkip = merge ([A , B ], { rules: { null: ' skip' } }) // => { a: 'hello' }
164
294
```
165
295
166
296
## Community
0 commit comments