shim.js 899 B

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