Skip to content

Commit

Permalink
fix: when string is empty it returns 0
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Jul 17, 2024
1 parent ea85794 commit 6c601e2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/nextjs/app/debug/_components/contract/utilsContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@ const isJsonString = (str: string) => {
}
};

const isBigInt = (str: string) => {
if (str.trim().length === 0) return false;
try {
BigInt(str);
return true;
} catch (e) {
return false;
}
};

// Recursive function to deeply parse JSON strings, correctly handling nested arrays and encoded JSON strings
const deepParseValues = (value: any): any => {
if (typeof value === "string") {
try {
// first try with bigInt because we losse precision with JSON.parse
if (isBigInt(value)) {
return BigInt(value);
} catch (e) {
// It's not a BigInt, continue
}

if (isJsonString(value)) {
const parsed = JSON.parse(value);
return deepParseValues(parsed);
} else {
// It's a string but not a JSON string, return as is
return value;
}

// It's a string but not a JSON string, return as is
return value;
} else if (Array.isArray(value)) {
// If it's an array, recursively parse each element
return value.map(element => deepParseValues(element));
Expand Down

0 comments on commit 6c601e2

Please sign in to comment.