add.js 797 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var isFinite = require('../../helpers/isFinite');
  4. var isNaN = require('../../helpers/isNaN');
  5. // https://262.ecma-international.org/12.0/#sec-numeric-types-number-add
  6. module.exports = function NumberAdd(x, y) {
  7. if (typeof x !== 'number' || typeof y !== 'number') {
  8. throw new $TypeError('Assertion failed: `x` and `y` arguments must be Numbers');
  9. }
  10. if (isNaN(x) || isNaN(y) || (x === Infinity && y === -Infinity) || (x === -Infinity && y === Infinity)) {
  11. return NaN;
  12. }
  13. if (!isFinite(x)) {
  14. return x;
  15. }
  16. if (!isFinite(y)) {
  17. return y;
  18. }
  19. if (x === y && x === 0) { // both zeroes
  20. return Infinity / x === -Infinity && Infinity / y === -Infinity ? -0 : +0;
  21. }
  22. // shortcut for the actual spec mechanics
  23. return x + y;
  24. };