sort keys in db.json
rlidwka opened this issue · comments
now it outputs stuff in random order depending on your internet connection, which messes up git diffs
example implementation: https://github.com/jshttp/mime-db/blob/master/scripts/lib/write-db.js
some amazing overengineering right there :D
hey! the keys being ordered by insertion is not specificed in ECMAScript, just a "defacto" standard :D
why not do something like http://github.com/domenic/sorted-object?
Because it's against the ECMAScript spec. It assumes the defacto standard.
ohhhhhhh sorry spec master
lol. Here is node v0.10.31, clearly not keeping the insertion order with JSON.stringify
:
$ node
> o={}
{}
> o[0]='a'
'a'
> o.s='s'
's'
> o[1]='b'
'b'
> JSON.stringify(o)
'{"0":"a","1":"b","s":"s"}'
>
Looks alphabetical to me... Hehe
lol :) the point is that module is assuming that the object will stringify with the keys in the insertion order, lol
By "sort" I didn't mean "alphabetical sort", just "make sure order is deterministic". So sorted-object
is fine imho.
PS: mime-db implementation only seem to sort one level of keys. Need a bit more amazing over-engineering there?
make sure order is deterministic
it's not determinisitc, still. it is relying on the insertion order to dictate the JSON.stringify order--it does not, as i demonstrated. it's really just as random in the end and you'll have the same problem.
PS: mime-db implementation only seem to sort one level of keys
it only sorts the specific structure that mime-db produces. the second level is all string values, except for extensions, which is an array that we want to keep the order of.
lol. Here is node v0.10.31, clearly not keeping the insertion order with JSON.stringify:
v8 keeps insertion order for anything except numeric values as far as I remember (those get sorted automatically and are always first in the output).
> JSON.stringify({c:1, b:1, a:1, 2:1, 3:1, 1:1})
'{"1":1,"2":1,"3":1,"c":1,"b":1,"a":1}'
If you care about that, my way of doing over-engineering looks nicer:
> require('jju').stringify({c:1, b:1, a:1, 2:1, 3:1, 1:1}, {sort_keys: 1, mode: 'json'})
'{"1": 1, "2": 1, "3": 1, "a": 1, "b": 1, "c": 1}'
But for this I think we'd better stick to old boring solution using sorted-object and json replacer.
haha, @rlidwka you got me :) I specifically know that v8 has that bug with numberic indicies ;) The fact of the matter is it is not even supported in the ECMA-262. The spec specifically calls out that there is no defined order to iterate across properties :)