@m3.code/lotide
v1.0.1
Published
A mini clone of the Lodash library, for educational purposes
Downloads
7
Readme
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.
This project was created and published by me as part of my learnings at Lighthouse Labs.
Usage
Install it:
npm install @m3.code/lotide
Require it:
const _ = require('@m3.code/lotide');
Call it:
const results = _.tail([1, 2, 3]) // => [2, 3]
Documentation
The following functions are currently implemented:
assertArraysEqual(arr1, arr2)
: compares 2 arrays to see if they're equal and makes an assertion with results.assertEqual(actual, expected)
: implementation of our assert, comparing 2 values and see if they're equalassertObjectsEqual(actualObj, expectedObj)
: compares 2 objects to see if they're equal and makes an assertion with results.head(arr)
: Returns the 1st element of an arraytail(arr)
: Returns the array without its head, all the following elements after the first one.middle(arr)
: Returns an array of the middle elements of the array, or the 2 middle elements of an array with an even number size lengthcountLetters(sentence)
: Returns an object with Character key and count value pairs, describing a detailed count of characters inside the sentence.Example:
countLetters('hello') => {h: 1, e: 1, l: 2, o:1}
countOnly(allItems, itemsToCount)
: from anallItems
object, we will count how many times the keys initemToCount
exist in this former object.Example:
countOnly([ "Karl", "Salima", "Agouhanna", "Fang", "Kavith", "Jason", "Salima", "Fang", "Joe" ], {Jason: true}) => 1
eqArrays(arr1, arr2)
: compare 2 arrays and returns true if they're equal.eqObjects(obj1, obj2)
: compare 2 objects and returns true if they're equal.findKey(obj, callback)
: given an object, will return the key whose value meets the condition set in the callbback function.Example:
findKey({ "Blue Hill": { stars: 1 }, "Akaleri": { stars: 3 }, "noma": { stars: 2 }, "elBulli": { stars: 3 }, "Ora": { stars: 2 }, "Akelarre": { stars: 3 } }, x => x.stars === 2); // => "noma"
findKeyByValue(obj, value)
: given an object, will return the key whose value meets the value passed as argumentExample:
findKeyByValue({ // eslint-disable-next-line camelcase sci_fi: "The Expanse", comedy: "Brooklyn Nine-Nine", drama: "The Wire" }, "The Wire") => drama
flatten(arr)
: will "flatten" an array, making it unidimensional if there are nested arrays inside.letterPositions(sentence)
: will return an object with the characters as key and their index positions as array of numbers as values.Example:
letterPositions('hello') => {h: [0], e:[1], l: [2,3], o: [4]}
map(arr, callback)
: our implementation of theArray.map
method. Takes an array and returns a new array given the oprations of the callback function.takeUntil(arr, callback)
: will return a new array from the elements of the passed arr until the condition set in callback is met (true).Example:
takeUntil([1, 2, 5, 7, 2, -1, 2, 4, 5], x => x < 0) => [1, 2, 5, 7, 2]
without(source, toRemove)
: returns a new array from thesource
from which we want to remove the elements present in the 2nd arraytoRemove
.Example:
without([1, 2, 3], [1]) => [2,3]