NumberToBigInt.js 824 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $BigInt = GetIntrinsic('%BigInt%', true);
  4. var $RangeError = GetIntrinsic('%RangeError%');
  5. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  6. var $TypeError = GetIntrinsic('%TypeError%');
  7. var Type = require('./Type');
  8. var isInteger = require('../helpers/isInteger');
  9. // https://262.ecma-international.org/11.0/#sec-numbertobigint
  10. module.exports = function NumberToBigInt(number) {
  11. if (Type(number) !== 'Number') {
  12. throw new $TypeError('Assertion failed: `number` must be a String');
  13. }
  14. if (!isInteger(number)) {
  15. throw new $RangeError('The number ' + number + ' cannot be converted to a BigInt because it is not an integer');
  16. }
  17. if (!$BigInt) {
  18. throw new $SyntaxError('BigInts are not supported in this environment');
  19. }
  20. return $BigInt(number);
  21. };