Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array-reduce.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var aCallable = require('../internals/a-callable');
  3. var toObject = require('../internals/to-object');
  4. var IndexedObject = require('../internals/indexed-object');
  5. var lengthOfArrayLike = require('../internals/length-of-array-like');
  6. var $TypeError = TypeError;
  7. var REDUCE_EMPTY = 'Reduce of empty array with no initial value';
  8. // `Array.prototype.{ reduce, reduceRight }` methods implementation
  9. var createMethod = function (IS_RIGHT) {
  10. return function (that, callbackfn, argumentsLength, memo) {
  11. var O = toObject(that);
  12. var self = IndexedObject(O);
  13. var length = lengthOfArrayLike(O);
  14. aCallable(callbackfn);
  15. if (length === 0 && argumentsLength < 2) throw new $TypeError(REDUCE_EMPTY);
  16. var index = IS_RIGHT ? length - 1 : 0;
  17. var i = IS_RIGHT ? -1 : 1;
  18. if (argumentsLength < 2) while (true) {
  19. if (index in self) {
  20. memo = self[index];
  21. index += i;
  22. break;
  23. }
  24. index += i;
  25. if (IS_RIGHT ? index < 0 : length <= index) {
  26. throw new $TypeError(REDUCE_EMPTY);
  27. }
  28. }
  29. for (;IS_RIGHT ? index >= 0 : length > index; index += i) if (index in self) {
  30. memo = callbackfn(memo, self[index], index, O);
  31. }
  32. return memo;
  33. };
  34. };
  35. module.exports = {
  36. // `Array.prototype.reduce` method
  37. // https://tc39.es/ecma262/#sec-array.prototype.reduce
  38. left: createMethod(false),
  39. // `Array.prototype.reduceRight` method
  40. // https://tc39.es/ecma262/#sec-array.prototype.reduceright
  41. right: createMethod(true)
  42. };