Skip to content

Commit 664f052

Browse files
authored
replace deepmerge with @fastify/deepmerge (#178)
* replace deepmerge with @fastify/deepmerge * remove newline * increase code coverage * increase test coverage
1 parent 761bf94 commit 664f052

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@
6666
"tsd": "^0.22.0"
6767
},
6868
"dependencies": {
69-
"deepmerge": "^4.2.2"
69+
"@fastify/deepmerge": "^1.1.0"
7070
}
7171
}

src/ObjectSchema.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict'
2-
const merge = require('deepmerge')
32
const { BaseSchema } = require('./BaseSchema')
43
const {
54
omit,
@@ -9,7 +8,7 @@ const {
98
patchIdsWithParentId,
109
appendRequired,
1110
FluentSchemaError,
12-
combineMerge,
11+
combineDeepmerge,
1312
} = require('./utils')
1413

1514
const initialState = {
@@ -322,7 +321,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
322321
throw new FluentSchemaError("Schema isn't FluentSchema type")
323322
}
324323
const src = base._getState()
325-
const extended = merge(src, schema, { arrayMerge: combineMerge })
324+
const extended = combineDeepmerge(src, schema)
326325
const {
327326
valueOf,
328327
isFluentSchema,

src/utils.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const merge = require('deepmerge')
2+
const deepmerge = require('@fastify/deepmerge')
33
const isFluentSchema = obj => obj && obj.isFluentSchema
44

55
const hasCombiningKeywords = attributes =>
@@ -40,23 +40,33 @@ const flat = array =>
4040
}
4141
}, {})
4242

43-
const combineMerge = (target, source, options) => {
44-
const destination = target.slice()
45-
46-
source.forEach((item, index) => {
47-
const prop = target.find(attr => attr.name === item.name)
48-
if (typeof destination[index] === 'undefined') {
49-
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
50-
} else if (options.isMergeableObject(prop)) {
51-
const propIndex = target.findIndex(prop => prop.name === item.name)
52-
destination[propIndex] = merge(prop, item, options)
53-
} else if (target.indexOf(item) === -1) {
54-
destination.push(item)
55-
}
56-
})
57-
return destination
43+
const combineArray = (options) => {
44+
45+
const {
46+
clone,
47+
isMergeableObject,
48+
deepmerge,
49+
} = options
50+
51+
return (target, source) => {
52+
const result = target.slice()
53+
54+
source.forEach((item, index) => {
55+
const prop = target.find(attr => attr.name === item.name)
56+
if (typeof result[index] === 'undefined') {
57+
result[index] = clone(item)
58+
} else if (isMergeableObject(prop)) {
59+
const propIndex = target.findIndex(prop => prop.name === item.name)
60+
result[propIndex] = deepmerge(prop, item)
61+
} else if (target.indexOf(item) === -1) {
62+
result.push(item)
63+
}
64+
})
65+
return result
66+
}
5867
}
5968

69+
const combineDeepmerge = deepmerge({ mergeArray: combineArray })
6070
const toArray = obj =>
6171
obj && Object.entries(obj).map(([key, value]) => ({ name: key, ...value }))
6272

@@ -230,7 +240,7 @@ module.exports = {
230240
setRaw,
231241
setAttribute,
232242
setComposeType,
233-
combineMerge,
243+
combineDeepmerge,
234244
FORMATS,
235245
TYPES,
236246
FLUENT_SCHEMA,

src/utils.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { setRaw } = require('./utils')
1+
const { setRaw, combineDeepmerge } = require('./utils')
22
const { StringSchema } = require('./StringSchema')
33
const { ObjectSchema } = require('./ObjectSchema')
44

@@ -31,3 +31,45 @@ describe('setRaw', () => {
3131
})
3232
})
3333
})
34+
35+
describe('combineDeepmerge', () => {
36+
it('should merge empty arrays', () => {
37+
const result = combineDeepmerge([], []);
38+
expect(result).toEqual([])
39+
})
40+
41+
it('should merge array with primitive values', () => {
42+
const result = combineDeepmerge([1], [2]);
43+
expect(result).toEqual([1, 2])
44+
})
45+
46+
it('should merge arrays with primitive values', () => {
47+
const result = combineDeepmerge([], [1, 2]);
48+
expect(result).toEqual([1, 2])
49+
})
50+
51+
it('should merge arrays with primitive values', () => {
52+
const result = combineDeepmerge([1, 2], [1, 2, 3]);
53+
expect(result).toEqual([1, 2, 3])
54+
})
55+
56+
it('should merge array with simple Schemas', () => {
57+
const result = combineDeepmerge([{ type: 'string' }], [{ type: 'string' }]);
58+
expect(result).toEqual([{ type: 'string' }])
59+
})
60+
61+
it('should merge array with named Schemas', () => {
62+
const result = combineDeepmerge([{ name: 'variant 1', type: 'string' }], [{ name: 'variant 2', type: 'string' }]);
63+
expect(result).toEqual([{ name: 'variant 1', type: 'string' }, { name: 'variant 2', type: 'string' }])
64+
})
65+
66+
it('should merge array with same named Schemas', () => {
67+
const result = combineDeepmerge([{ name: 'variant 2', type: 'string' }], [{ name: 'variant 2', type: 'number' }]);
68+
expect(result).toEqual([{ name: 'variant 2', type: 'number' }])
69+
})
70+
71+
it('should merge array with same named Schemas', () => {
72+
const result = combineDeepmerge([{ name: 'variant 2', type: 'string' }], [{ name: 'variant 2', type: 'number' }, { name: 'variant 1', type: 'string' }]);
73+
expect(result).toEqual([{ name: 'variant 2', type: 'number' }, { name: 'variant 1', type: 'string' }])
74+
})
75+
})

0 commit comments

Comments
 (0)