Created
May 27, 2022 17:14
-
-
Save jordanbtucker/d48b826aa9f4e868f8d406d84fa10526 to your computer and use it in GitHub Desktop.
JSON/JSON5 BigInt Replacer and Reviver
This file contains 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
function bigIntReplacer(key, value) { | |
if (typeof value === 'bigint') { | |
return `${value}n` | |
} | |
return value | |
} |
This file contains 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
function bigIntReviver(name, value) { | |
const bigIntRegExp = /^([-+]?\d+)n$/ | |
if (typeof value === 'string') { | |
const match = bigIntRegExp.exec(value) | |
if (match != null) { | |
return BigInt(match[1]) | |
} | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment