NumberToBigInt.js 690 B

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