From 30c97eb588e040dcc7a239b26b08034efe74bbfd Mon Sep 17 00:00:00 2001 From: Olle Kelderman Date: Sun, 1 Dec 2024 12:53:16 +0100 Subject: [PATCH 1/2] fix stringify not picking up negative zero if a normal zero has appeared before it --- src/stringify.js | 4 ++-- test/test.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stringify.js b/src/stringify.js index df291fd..288449d 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 ebd4147..a1aa7c8 100644 --- a/test/test.js +++ b/test/test.js @@ -124,6 +124,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: [], From 82f6620e2cecbe0751b6de3ecc8e2c4978f755b4 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Aug 2025 12:01:41 -0400 Subject: [PATCH 2/2] changeset --- .changeset/shaggy-bars-hear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shaggy-bars-hear.md 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