ToBigInt.js 631 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $BigInt = GetIntrinsic('%BigInt%', true);
  4. var $asIntN = GetIntrinsic('%BigInt.asIntN%', true);
  5. var $Number = GetIntrinsic('%Number%');
  6. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  7. var ToPrimitive = require('./ToPrimitive');
  8. // https://262.ecma-international.org/11.0/#sec-tobigint
  9. module.exports = function ToBigInt(argument) {
  10. if (!$BigInt) {
  11. throw new $SyntaxError('BigInts are not supported in this environment');
  12. }
  13. var prim = ToPrimitive(argument, $Number);
  14. if (typeof prim === 'number') {
  15. return $asIntN(0, prim);
  16. }
  17. return $BigInt(prim);
  18. };