Kikobeats / finepack

Organizes and maintains readable your JSON files.
MIT License
19 stars 7 forks source link

Update sort-keys-recursive to version 2.0.0 #36

Closed matteocng closed 7 years ago

matteocng commented 7 years ago

Commits:

Fixes #35.

Kikobeats commented 7 years ago

Thanks for that. I added comments 😄

Kikobeats commented 7 years ago

Can I wait for something more or time to merge? 😄

matteocng commented 7 years ago

Sorting a second time using sort-keys-recursive and passing compareFunction works but it doesn't seem ideal, because the function would be applied to all keys recursively, not just the keys at the first level.

What do you think about this instead:

# After sorting, we move some fields in new positions for esthetic effect.
compareFunction = (a, b) -> KEYWORDS.sort.indexOf(a) - KEYWORDS.sort.indexOf(b)
output = Object.keys(input).sort(compareFunction).reduce (acc, key) ->
 acc[key] = input[key]
 acc
, {} # Object, second argument of reduce(), initial value of 'acc'.
Kikobeats commented 7 years ago

@matteocng I like this new approach, can you commit it? want to know if tests are green with this change

matteocng commented 7 years ago

I have updated the commit, also adding a small clarification about the behaviour of the default sort function (and link to MDN) in the README and code comments.