add.js 800 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var isFinite = require('math-intrinsics/isFinite');
  4. var isNaN = require('math-intrinsics/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. };