StringToBigInt.js 588 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $BigInt = GetIntrinsic('%BigInt%', true);
  4. var $TypeError = require('es-errors/type');
  5. var $SyntaxError = require('es-errors/syntax');
  6. // https://262.ecma-international.org/14.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 void undefined;
  18. }
  19. };