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.

22 lines
456 B

4 years ago
  1. 'use strict';
  2. module.exports = function reduce(fn, carry) {
  3. var _this = this;
  4. var reduceCarry = null;
  5. if (carry !== undefined) {
  6. reduceCarry = carry;
  7. }
  8. if (Array.isArray(this.items)) {
  9. this.items.forEach(function (item) {
  10. reduceCarry = fn(reduceCarry, item);
  11. });
  12. } else {
  13. Object.keys(this.items).forEach(function (key) {
  14. reduceCarry = fn(reduceCarry, _this.items[key], key);
  15. });
  16. }
  17. return reduceCarry;
  18. };