toString.js 840 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $SyntaxError;
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var callBound = require('call-bind/callBound');
  6. var $BigIntToString = callBound('BigInt.prototype.toString', true);
  7. var Type = require('../Type');
  8. var isInteger = require('../../helpers/isInteger');
  9. // https://262.ecma-international.org/14.0/#sec-numeric-types-bigint-tostring
  10. module.exports = function BigIntToString(x, radix) {
  11. if (Type(x) !== 'BigInt') {
  12. throw new $TypeError('Assertion failed: `x` must be a BigInt');
  13. }
  14. if (!isInteger(radix) || radix < 2 || radix > 36) {
  15. throw new $TypeError('Assertion failed: `radix` must be an integer >= 2 and <= 36');
  16. }
  17. if (!$BigIntToString) {
  18. throw new $SyntaxError('BigInt is not supported');
  19. }
  20. return $BigIntToString(x, radix); // steps 1 - 12
  21. };