truncate.js 461 B

123456789101112131415
  1. 'use strict';
  2. var floor = require('./floor');
  3. var $TypeError = require('es-errors/type');
  4. // https://262.ecma-international.org/14.0/#eqn-truncate
  5. module.exports = function truncate(x) {
  6. if (typeof x !== 'number' && typeof x !== 'bigint') {
  7. throw new $TypeError('argument must be a Number or a BigInt');
  8. }
  9. var result = x < 0 ? -floor(-x) : floor(x);
  10. return result === 0 ? 0 : result; // in the spec, these are math values, so we filter out -0 here
  11. };