Skip to content

Instantly share code, notes, and snippets.

@vitaly-t
Created October 7, 2019 20:26
Show Gist options
  • Save vitaly-t/5bef7010c7d61d7b05338ce3b7c71e67 to your computer and use it in GitHub Desktop.
Save vitaly-t/5bef7010c7d61d7b05338ce3b7c71e67 to your computer and use it in GitHub Desktop.
// Works the same as JSON.parse, but also handles BigInt type,
// by returning BigInt for each string that uses 123n format:
function parse(text) {
return JSON.parse(text, (_, value) => {
if (typeof value === 'string') {
const m = value.match(/(-?\d+)n/);
if (m && m[0] === value) {
value = BigInt(m[1]);
}
}
return value;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment