From 0f2575380f9fb9c3eedefce07cc56109923e90fc Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Tue, 21 Feb 2023 17:51:49 +0100 Subject: [PATCH 1/8] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e38635..f1cb41e 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ const data = devalue.unflatten(JSON.parse(json).data); ## Custom types -You can serialize and serialize custom types by passing a second argument to `stringify` containing an object of types and their _reducers_, and a second argument to `parse` or `unflatten` containing an object of types and their _revivers_: +You can serialize and deserialize custom types by passing a second argument to `stringify` containing an object of types and their _reducers_, and a second argument to `parse` or `unflatten` containing an object of types and their _revivers_: ```js class Vector { From b65ef2456f5c69cd9fe5c854d51090da6683a647 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Tue, 21 Feb 2023 18:08:08 +0100 Subject: [PATCH 2/8] support to stringify invalid dates --- src/stringify.js | 3 ++- test/test.js | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/stringify.js b/src/stringify.js index 8ce427e..fa989c5 100644 --- a/src/stringify.js +++ b/src/stringify.js @@ -81,7 +81,8 @@ export function stringify(value, reducers) { break; case 'Date': - str = `["Date","${thing.toISOString()}"]`; + const isValidDate = !isNaN(thing.getDate()) + str = `["Date","${isValidDate ? thing.toISOString() : ''}"]`; break; case 'RegExp': diff --git a/test/test.js b/test/test.js index 1857c6b..9e77182 100644 --- a/test/test.js +++ b/test/test.js @@ -107,6 +107,15 @@ const fixtures = { js: 'new Date(1000000000000)', json: '[["Date","2001-09-09T01:46:40.000Z"]]' }, + { + name: 'invalid Date', + value: new Date(''), + js: 'new Date(NaN)', + json: '[["Date",""]]', + validate: (value) => { + assert.ok(isNaN(value.valueOf())); + } + }, { name: 'Array', value: ['a', 'b', 'c'], @@ -455,7 +464,6 @@ for (const [name, tests] of Object.entries(fixtures)) { test(t.name, () => { const actual = unflatten(JSON.parse(t.json), t.revivers); const expected = t.value; - if (t.validate) { t.validate(actual); } else { From 6c9729dce9eb037171fd678297f86d711f07df22 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Tue, 21 Feb 2023 18:10:47 +0100 Subject: [PATCH 3/8] revert removing newline --- test/test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test.js b/test/test.js index 9e77182..f01c03f 100644 --- a/test/test.js +++ b/test/test.js @@ -464,6 +464,7 @@ for (const [name, tests] of Object.entries(fixtures)) { test(t.name, () => { const actual = unflatten(JSON.parse(t.json), t.revivers); const expected = t.value; + if (t.validate) { t.validate(actual); } else { From eab3761da600c5c4726b1c2710b1ea2d5b93bef9 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Sun, 16 Apr 2023 19:39:49 +0545 Subject: [PATCH 4/8] fix: wrong path in error if value comes after a map --- src/stringify.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/stringify.js b/src/stringify.js index 8ce427e..64b53bf 100644 --- a/src/stringify.js +++ b/src/stringify.js @@ -128,6 +128,7 @@ export function stringify(value, reducers) { `.get(${is_primitive(key) ? stringify_primitive(key) : '...'})` ); str += `,${flatten(key)},${flatten(value)}`; + keys.pop(); } str += ']'; From d67d05b2bfd9a74793972090e1b993b801027871 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 19 Apr 2024 12:01:47 -0400 Subject: [PATCH 5/8] add node 18/20 to test matrix, fix bad error in node 20 --- .github/workflows/ci.yml | 2 +- test/test.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f421f0..2edff8f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: strategy: fail-fast: false matrix: - node-version: [16] + node-version: [16, 18, 20] os: [ubuntu-latest] steps: - run: git config --global core.autocrlf false diff --git a/test/test.js b/test/test.js index afe3557..8d336a2 100644 --- a/test/test.js +++ b/test/test.js @@ -9,6 +9,8 @@ class Custom { } } +const node_version = +process.versions.node.split('.')[0]; + const fixtures = { basics: [ { @@ -475,7 +477,10 @@ const invalid = [ { name: 'invalid JSON', json: '][', - message: 'Unexpected token ] in JSON at position 0' + message: + node_version >= 20 + ? `Unexpected token ']', "][" is not valid JSON` + : 'Unexpected token ] in JSON at position 0' }, { name: 'hole', @@ -518,7 +523,13 @@ for (const { name, json, message } of invalid) { uvu.test(`parse error: ${name}`, () => { assert.throws( () => parse(json), - (error) => error.message === message + (error) => { + const match = error.message === message; + if (!match) { + console.error(`Expected: ${message}, got: ${error.message}`); + } + return match; + } ); }); } From a306b98a52f3edd0c9ce13ae8b41a73663d98da2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 19 Apr 2024 12:44:10 -0400 Subject: [PATCH 6/8] add test --- test/test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test.js b/test/test.js index 1857c6b..ecc786e 100644 --- a/test/test.js +++ b/test/test.js @@ -560,6 +560,21 @@ for (const fn of [uneval, stringify]) { assert.equal(e.path, '.foo.map.get("key")'); } }); + + uvu.test(`${fn.name} populates error.path after maps (#64)`, () => { + try { + fn({ + map: new Map([['key', 'value']]), + object: { + invalid() {} + } + }); + } catch (e) { + assert.equal(e.name, 'DevalueError'); + assert.equal(e.message, 'Cannot stringify a function'); + assert.equal(e.path, '.object.invalid'); + } + }); } uvu.test('does not create duplicate parameter names', () => { From e33f235b4c1acb5202e57228518c104580dcc19d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 19 Apr 2024 12:48:16 -0400 Subject: [PATCH 7/8] tweak --- src/stringify.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stringify.js b/src/stringify.js index fa989c5..524d639 100644 --- a/src/stringify.js +++ b/src/stringify.js @@ -81,8 +81,8 @@ export function stringify(value, reducers) { break; case 'Date': - const isValidDate = !isNaN(thing.getDate()) - str = `["Date","${isValidDate ? thing.toISOString() : ''}"]`; + const valid = !isNaN(thing.getDate()); + str = `["Date","${valid ? thing.toISOString() : ''}"]`; break; case 'RegExp': From 3ba57a6a8e60b171f624b6343be704b23a34af6e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 19 Apr 2024 13:30:04 -0400 Subject: [PATCH 8/8] -> v4.3.3 --- CHANGELOG.md | 5 +++++ package.json | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4724a8a..ba98edd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # devalue changelog +## 4.3.3 + +- Support invalid dates ([#61](https://github.com/Rich-Harris/devalue/pull/61)) +- Fix incorrect `error.path` when object contains a map ([#64](https://github.com/Rich-Harris/devalue/pull/64)) + ## 4.3.2 - Better type declarations ([#66](https://github.com/Rich-Harris/devalue/pull/66)) diff --git a/package.json b/package.json index cf3aeb1..1f43014 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "devalue", "description": "Gets the job done when JSON.stringify can't", - "version": "4.3.2", + "version": "4.3.3", "repository": "Rich-Harris/devalue", "exports": { ".": { @@ -30,4 +30,4 @@ "license": "MIT", "type": "module", "packageManager": "pnpm@8.5.1" -} \ No newline at end of file +}