truncate.js 509 B

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