Skip to content

Commit 8714db3

Browse files
authored
perf: replace accumulator spread with assignment (#274)
1 parent 56a523c commit 8714db3

File tree

2 files changed

+40
-53
lines changed

2 files changed

+40
-53
lines changed

src/ObjectSchema.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
139139
"'patternProperties' invalid options. Provide a valid map e.g. { '^fo.*$': S.string() }"
140140
)
141141
}
142-
return {
143-
...memo,
144-
[pattern]: omit(schema.valueOf({ isRoot: false }), ['$schema'])
145-
}
142+
memo[pattern] = omit(schema.valueOf({ isRoot: false }), ['$schema'])
143+
return memo
146144
}, {})
147145
return setAttribute({ schema, ...options }, [
148146
'patternProperties',
@@ -169,12 +167,10 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
169167
"'dependencies' invalid options. Provide a valid map e.g. { 'foo': ['bar'] } or { 'foo': S.string() }"
170168
)
171169
}
172-
return {
173-
...memo,
174-
[prop]: Array.isArray(schema)
175-
? schema
176-
: omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions'])
177-
}
170+
memo[prop] = Array.isArray(schema)
171+
? schema
172+
: omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions'])
173+
return memo
178174
}, {})
179175
return setAttribute({ schema, ...options }, [
180176
'dependencies',
@@ -199,10 +195,8 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
199195
"'dependentRequired' invalid options. Provide a valid array e.g. { 'foo': ['bar'] }"
200196
)
201197
}
202-
return {
203-
...memo,
204-
[prop]: schema
205-
}
198+
memo[prop] = schema
199+
return memo
206200
}, {})
207201

208202
return setAttribute({ schema, ...options }, [
@@ -228,10 +222,8 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
228222
)
229223
}
230224

231-
return {
232-
...memo,
233-
[prop]: omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions'])
234-
}
225+
memo[prop] = omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions'])
226+
return memo
235227
}, {})
236228

237229
return setAttribute({ schema, ...options }, [
@@ -309,12 +301,15 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
309301
// strip undefined values or empty arrays or internals
310302
attributes = Object.entries({ ...attributes, $id, type }).reduce(
311303
(memo, [key, value]) => {
312-
return key === '$schema' ||
313-
key === 'def' ||
314-
value === undefined ||
315-
(Array.isArray(value) && value.length === 0 && key !== 'default')
316-
? memo
317-
: { ...memo, [key]: value }
304+
if (
305+
key !== '$schema' &&
306+
key !== 'def' &&
307+
value !== undefined &&
308+
!(Array.isArray(value) && value.length === 0 && key !== 'default')
309+
) {
310+
memo[key] = value
311+
}
312+
return memo
318313
},
319314
{}
320315
)

src/utils.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const deepmerge = require('@fastify/deepmerge')
3-
const isFluentSchema = (obj) => obj && obj.isFluentSchema
3+
const isFluentSchema = (obj) => obj?.isFluentSchema
44

55
const hasCombiningKeywords = (attributes) =>
66
attributes.allOf || attributes.anyOf || attributes.oneOf || attributes.not
@@ -25,20 +25,17 @@ const isBoolean = (value) => typeof value === 'boolean'
2525

2626
const omit = (obj, props) =>
2727
Object.entries(obj).reduce((memo, [key, value]) => {
28-
if (props.includes(key)) return memo
29-
return {
30-
...memo,
31-
[key]: value
28+
if (!props.includes(key)) {
29+
memo[key] = value
3230
}
31+
return memo
3332
}, {})
3433

3534
const flat = (array) =>
3635
array.reduce((memo, prop) => {
3736
const { name, ...rest } = prop
38-
return {
39-
...memo,
40-
[name]: rest
41-
}
37+
memo[name] = rest
38+
return memo
4239
}, {})
4340

4441
const combineArray = (options) => {
@@ -132,16 +129,14 @@ const patchIdsWithParentId = ({ schema, generateIds, parentId }) => {
132129
...schema,
133130
properties: properties.reduce((memo, [key, props]) => {
134131
const $id = props.$id || (generateIds ? `#properties/${key}` : undefined)
135-
return {
136-
...memo,
137-
[key]: {
138-
...props,
139-
$id:
140-
generateIds && parentId
141-
? `${parentId}/${$id.replace('#', '')}`
142-
: $id // e.g. #properties/foo/properties/bar
143-
}
132+
memo[key] = {
133+
...props,
134+
$id:
135+
generateIds && parentId
136+
? `${parentId}/${$id.replace('#', '')}`
137+
: $id // e.g. #properties/foo/properties/bar
144138
}
139+
return memo
145140
}, {})
146141
}
147142
}
@@ -152,17 +147,14 @@ const appendRequired = ({
152147
}) => {
153148
const { schemaRequired, attributeRequired } = (required || []).reduce(
154149
(memo, item) => {
155-
return item === REQUIRED
156-
? {
157-
...memo,
158-
// append prop name to the schema.required
159-
schemaRequired: [...memo.schemaRequired, name]
160-
}
161-
: {
162-
...memo,
163-
// propagate required attributes
164-
attributeRequired: [...memo.attributeRequired, item]
165-
}
150+
if (item === REQUIRED) {
151+
// Append prop name to the schema.required
152+
memo.schemaRequired.push(name)
153+
} else {
154+
// Propagate required attributes
155+
memo.attributeRequired.push(item)
156+
}
157+
return memo
166158
},
167159
{ schemaRequired: [], attributeRequired: [] }
168160
)

0 commit comments

Comments
 (0)