Skip to content

Commit 0c97105

Browse files
fiskerkeithamus
authored andcommitted
refactor: use pipe and overProperty (#123)
* refactor: `sortProperty` -> `overProperty` * refactor: use `pipe` and `overProperty` * refactor: use reduce
1 parent 4645764 commit 0c97105

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const gitHooks = require('git-hooks-list')
77

88
const hasOwnProperty = (object, property) =>
99
Object.prototype.hasOwnProperty.call(object, property)
10+
const pipe = fns => x => fns.reduce((result, fn) => fn(result), x)
1011
const onArray = fn => x => (Array.isArray(x) ? fn(x) : x)
1112
const uniq = onArray(xs => xs.filter((x, i) => i === xs.indexOf(x)))
1213
const sortArray = onArray(array => [...array].sort())
@@ -174,6 +175,14 @@ const fields = [
174175
]
175176

176177
const defaultSortOrder = fields.map(({ key }) => key)
178+
const overFields = pipe(
179+
fields.reduce((fns, { key, over }) => {
180+
if (over) {
181+
fns.push(overProperty(key, over))
182+
}
183+
return fns
184+
}, []),
185+
)
177186

178187
function editStringJSON(json, over) {
179188
if (typeof json === 'string') {
@@ -218,13 +227,7 @@ function sortPackageJson(jsonIsh, options = {}) {
218227
]
219228
}
220229

221-
const newJson = sortObjectKeys(json, sortOrder)
222-
223-
for (const { key, over } of fields) {
224-
if (over && newJson[key]) newJson[key] = over(newJson[key])
225-
}
226-
227-
return newJson
230+
return overFields(sortObjectKeys(json, sortOrder))
228231
}),
229232
)
230233
}

0 commit comments

Comments
 (0)