NumberToBigInt.js 814 B

1234567891011121314151617181920212223242526
  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 IsInteger = require('./IsInteger');
  8. var Type = require('./Type');
  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. };