ToIntegerOrInfinity.js 600 B

1234567891011121314151617181920
  1. 'use strict';
  2. var abs = require('./abs');
  3. var floor = require('./floor');
  4. var ToNumber = require('./ToNumber');
  5. var $isNaN = require('../helpers/isNaN');
  6. var $isFinite = require('../helpers/isFinite');
  7. var $sign = require('../helpers/sign');
  8. // https://262.ecma-international.org/12.0/#sec-tointegerorinfinity
  9. module.exports = function ToIntegerOrInfinity(value) {
  10. var number = ToNumber(value);
  11. if ($isNaN(number) || number === 0) { return 0; }
  12. if (!$isFinite(number)) { return number; }
  13. var integer = floor(abs(number));
  14. if (integer === 0) { return 0; }
  15. return $sign(number) * integer;
  16. };