diff --git a/.changeset/shaggy-bars-hear.md b/.changeset/shaggy-bars-hear.md new file mode 100644 index 0000000..694d6c0 --- /dev/null +++ b/.changeset/shaggy-bars-hear.md @@ -0,0 +1,5 @@ +--- +'devalue': patch +--- + +fix: correctly differentiate between +0 and -0 diff --git a/src/stringify.js b/src/stringify.js index 4cb6fa5..bec2e58 100644 --- a/src/stringify.js +++ b/src/stringify.js @@ -48,14 +48,14 @@ export function stringify(value, reducers) { throw new DevalueError(`Cannot stringify a function`, keys); } - if (indexes.has(thing)) return indexes.get(thing); - if (thing === undefined) return UNDEFINED; if (Number.isNaN(thing)) return NAN; if (thing === Infinity) return POSITIVE_INFINITY; if (thing === -Infinity) return NEGATIVE_INFINITY; if (thing === 0 && 1 / thing < 0) return NEGATIVE_ZERO; + if (indexes.has(thing)) return indexes.get(thing); + const index = p++; indexes.set(thing, index); diff --git a/test/test.js b/test/test.js index dcd4d0f..1440e04 100644 --- a/test/test.js +++ b/test/test.js @@ -132,6 +132,12 @@ const fixtures = { js: '["a","b","c"]', json: '[[1,2,3],"a","b","c"]' }, + { + name: 'Array where negative zero appears after normal zero', + value: [0, -0], + js: '[0,-0]', + json: '[[1,-6],0]' + }, { name: 'Array (empty)', value: [],