underscore collections

For educational purpose

by aniketmhatre88

JavaScript

/**
 * Implementation of _js collection for educational use
 */

let _ = {
  each: function(list, func, context) {
    let funcContext = context || {};
    if (!list) {
      func.apply(funcContext, list);
      return;
    }

    let keys = Object.keys(list);
    if (keys.length > 0) {
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        func.call(funcContext, list[key], key, list);
      }
    } else {
      func.call(funcContext, list);
    }
  },
  map: function(list, func, context) {
    let funcContext = context || {},
      result = [],
      funcToRun = func || function(arg) {
        return arg;
      };

    if (list && Object.keys(list).length > 0) {
      let keys = Object.keys(list);
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        result.push(funcToRun.call(funcContext, list[key], key, list));
      }
    }

    return result;
  },
  reduce: function(list, func, memo, context) {
    let funcContext = context || {},
      result = memo,
      startIndex = 0;

    if (list && Object.keys(list).length > 0) {
      let keys = Object.keys(list);
      if (!memo) {
        result = list[keys[0]];
        startIndex = 1;
      }
      for (startIndex; startIndex < keys.length; startIndex++) {
        let key = keys[startIndex];
        result = func.call(funcContext, result, list[key]);
      }
    }

    return result;
  },
  reduceRight: function(list, func, memo, context) {
    let funcContext = context || {},
      result = memo;

    if (list && Object.keys(list).length > 0) {
      let keys = Object.keys(list),
        startIndex = keys.length - 1;
      if (!memo) {
        result = list[keys[keys.length - 1]];
        startIndex = keys.length - 2;
      }
      for (startIndex; startIndex >= 0; startIndex--) {
        let key = keys[startIndex];
        result = func.call(funcContext, result, list[key]);
      }
    }
    return result;
  },
  find: function(list, func, context) {
    let...