From 3da5472d3165742152260530d97f26fe2ed4eef3 Mon Sep 17 00:00:00 2001 From: joao lopes Date: Sun, 27 Oct 2024 14:58:33 -0300 Subject: [PATCH 1/2] fix(jsonify): parse returning undefined instead of the number --- src/jsonify/parse.ts | 10 +++++++--- src/jsonify/parse_test.ts | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/jsonify/parse.ts b/src/jsonify/parse.ts index 66ed83ca89c..03859467228 100644 --- a/src/jsonify/parse.ts +++ b/src/jsonify/parse.ts @@ -30,9 +30,13 @@ function unpack( custom: CustomParser | undefined, ): void { if (idx in hydrated) return; - + const current = arr[idx]; if (typeof current === "number") { + if(idx !== 0) { + hydrated[idx] = current; + return; + } switch (current) { case UNDEFINED: hydrated[idx] = undefined; @@ -149,8 +153,8 @@ function unpack( hydrated[idx] = actual; const keys = Object.keys(current); - for (let i = 0; i < keys.length; i++) { - const key = keys[i]; + for (const element of keys) { + const key = element; // deno-lint-ignore no-explicit-any const ref = (current as any)[key]; if (ref < 0) { diff --git a/src/jsonify/parse_test.ts b/src/jsonify/parse_test.ts index 8e5a1b88df1..42b17f1cbf3 100644 --- a/src/jsonify/parse_test.ts +++ b/src/jsonify/parse_test.ts @@ -97,3 +97,7 @@ Deno.test("parse - circular references", () => { Deno.test("parse - object", () => { expect(parse('[{"foo":1},42]')).toEqual({ foo: 42 }); }); + +Deno.test("parse - object with number negative", () => { + expect(parse('[{"foo":1}, -1]')).toEqual({ foo: -1 }); +}); From f3cc496f221ce05f1e4b70b2132c36f0d224fbf3 Mon Sep 17 00:00:00 2001 From: joao lopes Date: Wed, 30 Oct 2024 09:49:51 -0300 Subject: [PATCH 2/2] fix: review code changes --- src/jsonify/parse.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/jsonify/parse.ts b/src/jsonify/parse.ts index 03859467228..ff8c67581d2 100644 --- a/src/jsonify/parse.ts +++ b/src/jsonify/parse.ts @@ -30,10 +30,10 @@ function unpack( custom: CustomParser | undefined, ): void { if (idx in hydrated) return; - + const current = arr[idx]; if (typeof current === "number") { - if(idx !== 0) { + if (idx !== 0) { hydrated[idx] = current; return; } @@ -153,8 +153,8 @@ function unpack( hydrated[idx] = actual; const keys = Object.keys(current); - for (const element of keys) { - const key = element; + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; // deno-lint-ignore no-explicit-any const ref = (current as any)[key]; if (ref < 0) {