shim.js 1004 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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) {
  7. return val2 - val1;
  8. }
  9. , divide = function (value) {
  10. return value / this;
  11. }
  12. , add = function (sum, number) {
  13. return sum + number * number;
  14. };
  15. // eslint-disable-next-line no-unused-vars
  16. module.exports = function (val1, val2 /*, …valn*/) {
  17. var result, numbers;
  18. if (!arguments.length) return 0;
  19. some.call(arguments, function (val) {
  20. if (isNaN(val)) {
  21. result = NaN;
  22. return false;
  23. }
  24. if (!isFinite(val)) {
  25. result = Infinity;
  26. return true;
  27. }
  28. if (result !== undefined) return false;
  29. val = Number(val);
  30. if (val === 0) return false;
  31. if (numbers) numbers.push(abs(val));
  32. else numbers = [abs(val)];
  33. return false;
  34. });
  35. if (result !== undefined) return result;
  36. if (!numbers) return 0;
  37. numbers.sort(compare);
  38. return numbers[0] * sqrt(numbers.map(divide, numbers[0]).reduce(add, 0));
  39. };