Skip to content

Commit

Permalink
fix(jsonify): parse returning undefined instead of the number
Browse files Browse the repository at this point in the history
  • Loading branch information
DevJoaoLopes committed Oct 27, 2024
1 parent b5389f6 commit 3da5472
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/jsonify/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/jsonify/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

0 comments on commit 3da5472

Please sign in to comment.