ToIntegerOrInfinity.js 542 B

123456789101112131415161718
  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. return $sign(number) * floor(abs(number));
  14. };