Skip to content

Commit ae904c5

Browse files
fix stringify not picking up negative zero if a normal zero has appeared before it (#87)
* fix stringify not picking up negative zero if a normal zero has appeared before it * changeset --------- Co-authored-by: Rich Harris <[email protected]>
1 parent e95b87a commit ae904c5

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

.changeset/shaggy-bars-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'devalue': patch
3+
---
4+
5+
fix: correctly differentiate between +0 and -0

src/stringify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export function stringify(value, reducers) {
4848
throw new DevalueError(`Cannot stringify a function`, keys);
4949
}
5050

51-
if (indexes.has(thing)) return indexes.get(thing);
52-
5351
if (thing === undefined) return UNDEFINED;
5452
if (Number.isNaN(thing)) return NAN;
5553
if (thing === Infinity) return POSITIVE_INFINITY;
5654
if (thing === -Infinity) return NEGATIVE_INFINITY;
5755
if (thing === 0 && 1 / thing < 0) return NEGATIVE_ZERO;
5856

57+
if (indexes.has(thing)) return indexes.get(thing);
58+
5959
const index = p++;
6060
indexes.set(thing, index);
6161

test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ const fixtures = {
132132
js: '["a","b","c"]',
133133
json: '[[1,2,3],"a","b","c"]'
134134
},
135+
{
136+
name: 'Array where negative zero appears after normal zero',
137+
value: [0, -0],
138+
js: '[0,-0]',
139+
json: '[[1,-6],0]'
140+
},
135141
{
136142
name: 'Array (empty)',
137143
value: [],

0 commit comments

Comments
 (0)