Skip to content

Commit 32ce870

Browse files
committed
docs: improve docs
1 parent 02af1c7 commit 32ce870

File tree

1 file changed

+152
-22
lines changed

1 file changed

+152
-22
lines changed

README.md

Lines changed: 152 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,122 @@ npm install @hypernym/merge
5656
```ts
5757
import { merge } from '@hypernym/merge'
5858

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+
}
60175
```
61176

62177
## API
@@ -92,10 +207,25 @@ merge([{ a: 1 }, { a: null }], { rules: { null: 'skip' } }) // => { a: 1 }
92207
```ts
93208
import type { Merge } from '@hypernym/merge'
94209

95-
type A = { a?: string }
96-
type B = { b: string }
210+
Merge<sources, options>
211+
```
97212

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 }
99229
```
100230

101231
## Options
@@ -116,15 +246,15 @@ Defines how merging behaves for the specified types.
116246

117247
Specifies the merge strategy for `array` types.
118248

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.
121251

122252
```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] }
125255

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] }
128258
```
129259

130260
#### undefined
@@ -134,15 +264,15 @@ const o2 = merge([a, b], { rules: { array: 'override' } }) // => { a: [3, 4] }
134264

135265
Specifies the merge strategy for the `undefined` type.
136266

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.
139269

140270
```ts
141-
const a = { a: 'hello' }
142-
const b = { a: undefined }
271+
const A = { a: 'hello' }
272+
const B = { a: undefined }
143273

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' }
146276
```
147277

148278
#### null
@@ -152,15 +282,15 @@ const o2 = merge([a, b], { rules: { undefined: 'skip' } }) // => { a: 'hello' }
152282

153283
Specifies the merge strategy for the `null` type.
154284

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.
157287

158288
```ts
159-
const a = { a: 'hello' }
160-
const b = { a: null }
289+
const A = { a: 'hello' }
290+
const B = { a: null }
161291

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' }
164294
```
165295

166296
## Community

0 commit comments

Comments
 (0)