1.0.0 • Published 3 years ago

@jinwonn/lotide v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Lotide

A mini clone of the Lodash library.

Purpose

BEWARE: This library was published for learning purposes. It is not intended for use in production-grade software.

Usage

Install it:

npm install @jinwonn/lotide

Require it:

const _ = require('@jinwonn/lotide');

Call it:

const results = _.tail([1, 2, 3]) // => [2, 3]

Documentation

The following functions are currently implemented:

  • countLetters(string): return an object of counts of each of the letters from string (excluding spaces).

      _.countLetters('sttr');
      // => {s:1,t:2,r:1}
    
      _.countLetters('s t tr');
      // => {s:1,t:2,r:1});
  • countOnly([allItems], {itemsToCount}): Returns and array of counts for items specified in {itemsToCount} from inputted collection of items (strings) in [allItems].

      const firstNames = ["Karl", "Salima", "Agouhanna", "Fang","Kavith", "Jason", "Salima", "Fang", "Joe"];
    
      const result1 = _.countOnly(firstNames, { "Jason": true, 
                                                "Karima": true, 
                                                "Fang": true, 
                                                "Agouhanna": false 
                                              });
    
      result1["Jason"]; // => 1
    
      result1["Karima"]; // => undefined
    
      result1["Fang"]; // => 2
    
      result1["Agouhanna"]; // => undefined
  • eqArrays([actual], [expected]): Takes in two arrays and returns true or false, based on a perfect match.

      _.eqArrays([1, [[2], 3]], [1, [[2], 3]]);// => true
    
      _.eqArrays([1, 2, [3]], [[3], 2, 1]);// => false
    
      _.eqArrays(["1", "2", [3]], ["1", 2, [3]]); // => false
  • eqObjects({actual}, {expected}) Takes in two objects and returns true or false, based on a perfect match.

      const ab = { a: "1", b: [2, 3], c: {4 ,5} };
      const ba = { b: [2, 3], c: {4 ,5}, a: "1" };
      
      _.eqObjects(ab, ba); // => true
    
      const abc = { a: "1", b: [2, 3], c: {4 ,5}, d: "6" };
      
      _.eqObjects(ab, abc); // => false
  • findKey({input}, cb): Returns the first key from {input} for which the cb returns a truthy value.

      const input = { "Blue Hill": { stars: 1 },
                      "Akaleri":   { stars: 3 },
                      "noma":      { stars: 2 },
                      "elBulli":   { stars: 3 },
                      "Ora":       { stars: 2 },
                      "Akelarre":  { stars: 3 }
                    };
    
      _.findKey(input, x => x.stars === 2); // => "noma"
      
      _.findKey(input, x => x.stars === 4); // => undefined
  • findKeyByValue({data}, searchKey): Returns the first key from {data} which contains the given value searchKey.

        const bestTVShowsByGenre = { sci_fi: "The Expanse",
                                     comedy: "Brooklyn Nine-Nine",
                                     drama:  "The Wire" };
    
        _.findKeyByValue(bestTVShowsByGenre, "The Wire");
        // => drama
    
        _.findKeyByValue(bestTVShowsByGenre, "That '70s Show");
        // => undefined
  • flatten([input]): Returns a flattened single-level array from [input] which contains other arrays inside.

      _.flatten([1,[2,3],'a',[5],true]);
      // => [1,2,3,'a',5,true])
  • head([input]): Returns the first element of [input].

      _.head([1, a, 3]); // => 1
    
      _.head([]); // => undefined
  • letterPositions(string): returns an object of arrays of all the indices (zero-based positions) in string where each character is found (excluding spaces).

      _.letterPositions('lighthouse labs');
      // => {l: [ 0, 11 ],
             i: [ 1 ],
             g: [ 2 ],
             h: [ 3, 5 ],
             t: [ 4 ],
             o: [ 6 ],
             u: [ 7 ],
             s: [ 8, 14 ],
             e: [ 9 ],
             a: [ 12 ],
             b: [ 13 ]}
  • map([input], cb): Returns a new array based on the results of callback function cb on [input].

      const words = ["ground", "control", "to", "major", "tom"];
    
      _.map(words, word => word.length);
      // => [6,7,2,5,3]
  • middle([input]): Return an array with only the middle element(s) of [input].

      _.middle([1]);
      // => []
    
      _.middle([1, 2]);
      // => []
    
      _.middle([1, 2, 3]);
      // => [2]
    
      _.middle([1, 2, 3, 4]);
      // => [2, 3]
  • tail([input]): Gets and returns an array of all but the first element of [input].

      _.tail([1, a, 3]); // => [a, 3]
  • takeUntil([input], cb): return a "slice of [array] with elements taken from the beginning until the cb returns a truthy value.

      const data1 = [1, 2, 5, -1, 2, 4, 5];
      
      _.takeUntil(data1, x => x < 0);
      // => [ 1, 2, 5 ]
    
      const data2 = ["I've", "been", "to", "Hollywood", ";", "also", "Redwood"];
      
      _.takeUntil(data2, x => x === ';');
      // => [ "I've", 'been', 'to', 'Hollywood' ]
  • without([source], [itemsToRemove]): Returns an array of values from [source] excluding all given values in [itemsToRemove].

      _.without([1, 2, 3], [1]);
      // => [2, 3]
    
      _.without(["1", "2", "3"], [1, 2, "3"]);
      // => ["1", "2"]