Created
October 7, 2019 20:26
-
-
Save vitaly-t/0a520f0bffd8ffcbba906986f0a19aee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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