@@ -7,6 +7,7 @@ const gitHooks = require('git-hooks-list')
77
88const hasOwnProperty = ( object , property ) =>
99 Object . prototype . hasOwnProperty . call ( object , property )
10+ const pipe = fns => x => fns . reduce ( ( result , fn ) => fn ( result ) , x )
1011const onArray = fn => x => ( Array . isArray ( x ) ? fn ( x ) : x )
1112const uniq = onArray ( xs => xs . filter ( ( x , i ) => i === xs . indexOf ( x ) ) )
1213const sortArray = onArray ( array => [ ...array ] . sort ( ) )
@@ -174,6 +175,14 @@ const fields = [
174175]
175176
176177const 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
178187function 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