shim.js 1000 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Thanks for hints: https://github.com/paulmillr/es6-shim
  2. "use strict";
  3. var some = Array.prototype.some
  4. , abs = Math.abs
  5. , sqrt = Math.sqrt
  6. , compare = function (val1, val2) { return val2 - val1; }
  7. , divide = function (value) { return value / this; }
  8. , add = function (sum, number) { return sum + number * number; };
  9. // eslint-disable-next-line no-unused-vars
  10. module.exports = function (val1, val2/*, …valn*/) {
  11. var result, numbers;
  12. if (!arguments.length) return 0;
  13. some.call(arguments, function (val) {
  14. if (isNaN(val)) {
  15. result = NaN;
  16. return false;
  17. }
  18. if (!isFinite(val)) {
  19. result = Infinity;
  20. return true;
  21. }
  22. if (result !== undefined) return false;
  23. val = Number(val);
  24. if (val === 0) return false;
  25. if (numbers) numbers.push(abs(val));
  26. else numbers = [abs(val)];
  27. return false;
  28. });
  29. if (result !== undefined) return result;
  30. if (!numbers) return 0;
  31. numbers.sort(compare);
  32. return numbers[0] * sqrt(numbers.map(divide, numbers[0]).reduce(add, 0));
  33. };