JamieMason/Jasmine-Matchers

View on GitHub
src/lib/reduce.ts

Summary

Maintainability
A
55 mins
Test Coverage
import { is } from './is';

export const reduce = (collection, fn, memo) => {
  if (is.Array(collection)) {
    for (let i = 0, len = collection.length; i < len; i++) {
      memo = fn(memo, collection[i], i, collection);
    }
  } else {
    for (const key in collection) {
      if ({}.hasOwnProperty.call(collection, key)) {
        memo = fn(memo, collection[key], key, collection);
      }
    }
  }
  return memo;
};