NumberToBigInt.js 800 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $BigInt = GetIntrinsic('%BigInt%', true);
  4. var $RangeError = require('es-errors/range');
  5. var $SyntaxError = require('es-errors/syntax');
  6. var $TypeError = require('es-errors/type');
  7. var IsIntegralNumber = require('./IsIntegralNumber');
  8. // https://262.ecma-international.org/12.0/#sec-numbertobigint
  9. module.exports = function NumberToBigInt(number) {
  10. if (typeof 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. if (!$BigInt) {
  17. throw new $SyntaxError('BigInts are not supported in this environment');
  18. }
  19. return $BigInt(number);
  20. };