ToInteger.js 514 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. // http://262.ecma-international.org/5.1/#sec-9.4
  9. module.exports = function ToInteger(value) {
  10. var number = ToNumber(value);
  11. if ($isNaN(number)) { return 0; }
  12. if (number === 0 || !$isFinite(number)) { return number; }
  13. return $sign(number) * floor(abs(number));
  14. };