ToNumber.js 809 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $Number = GetIntrinsic('%Number%');
  5. var isPrimitive = require('../helpers/isPrimitive');
  6. var ToPrimitive = require('./ToPrimitive');
  7. var StringToNumber = require('./StringToNumber');
  8. // https://ecma-international.org/ecma-262/13.0/#sec-tonumber
  9. module.exports = function ToNumber(argument) {
  10. var value = isPrimitive(argument) ? argument : ToPrimitive(argument, $Number);
  11. if (typeof value === 'symbol') {
  12. throw new $TypeError('Cannot convert a Symbol value to a number');
  13. }
  14. if (typeof value === 'bigint') {
  15. throw new $TypeError('Conversion from \'BigInt\' to \'number\' is not allowed.');
  16. }
  17. if (typeof value === 'string') {
  18. return StringToNumber(value);
  19. }
  20. return $Number(value);
  21. };