StringToBigInt.js 581 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $BigInt = GetIntrinsic('%BigInt%', true);
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  6. // https://262.ecma-international.org/11.0/#sec-stringtobigint
  7. module.exports = function StringToBigInt(argument) {
  8. if (typeof argument !== 'string') {
  9. throw new $TypeError('`argument` must be a string');
  10. }
  11. if (!$BigInt) {
  12. throw new $SyntaxError('BigInts are not supported in this environment');
  13. }
  14. try {
  15. return $BigInt(argument);
  16. } catch (e) {
  17. return NaN;
  18. }
  19. };