@tai-de/lotide
v1.0.3
Published
Lodash clone for LHL
Downloads
1
Readme
Lotide JS Library
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 @tai-de/lotide
Require it:
const _ = require('@tai-de/lotide');
Call it:
const results = _.tail([1, 2, 3]) // => [2, 3]
********** Equality checks **********
Functions used to compare, returning true or false.
eqArrays(arrayOne, arrayTwo)
- Returns
true
orfalse
after checking if the arrays are equal- Supports nested arrays & array items being objects using recursion
arrayOne
andarrayTwo
can be given any array arguments
eqObjects(object1, object2)
- Returns
true
orfalse
after checking if two objects are equal- Supports arrays as object values & nested objects using recursion
object1
andobject2
can be given any array arguments
********** Assertion functions **********
Functions used to return Assertion Passed
or Assertion Failed
whether two arguments are equal.
assertEqual(actual, expected)
- Returns
Assertion Passed
orAssertion Failed
after checking if the passed arguments are equal. actual
andexpected
can be given any argument (primitive values. see below for Array and Object variants)
assertArraysEqual(actual, expected)
- Returns
Assertion Passed
orAssertion Failed
after checking if the two arrays are equal. actual
andexpected
can be given any two arrays as arguments
assertObjectsEqual(actual, expected)
- Returns
Assertion Passed
orAssertion Failed
after checking if the two objects are equal. actual
andexpected
can be given any two objects as arguments
********** String functions **********
Functions used to manipulate or parse a string
countLetters(string)
- An object will be returned containing counts of all unique letters in the
string
- Spaces are excluded/trimmed from the input
countLetters("Hello")
=>{ h: 1, e: 1, l: 2, o: 1 }
letterPositions(string)
- An object will be returned containing the index positions of all unique letters in the
string
- Spaces are excluded from the output, but do contribute to the positions
letterPositions("hello")
=>{ h: [0], e: [1], l: [2,3], o: [4] }
letterPositions("lighthouse in the house")
=>{ l: [0], i: [1, 11], g: [2], h: [3, 5, 15, 18], t: [4, 14], o: [6, 19], u: [7, 20], s: [8, 21], e: [9, 16, 22], n: [12] }
********** Array functions **********
Functions used to manipulate or parse an array
countOnly(array, object)
- An object will be returned containing counts of everything within the array that the
object
parameter listed astrue
- If the
object
liststrue
for a key that does not exist in the array, include an undefined value in the object output
- If the
countOnly(["Jason", "Jason", "Fang", "Agouhanna"], { "Jason": true, "Karima": true, "Fang": true, "Agouhanna": false })
=>{ Jason: 2, Karima: undefined, Fang: 1 }
flatten(array)
- Returns a new array after doing a flattening of any nested values/arrays
flatten([1, 2, [3, 4], 5, [6]])
=>[1, 2, 3, 4, 5, 6]
flatten([1, 2, [3, 4, [5, 6, [7, 8, [[[[[9, [10]]]]]]]]]])
=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
head(array)
- Returns the head (first value) in a given array
head(["Hello", "Lighthouse", "Labs"])
=>"Hello"
middle(array)
- Returns the middle value of an array
- If array length is less than 2, return empty array
- If array length is odd, return exact middle value
- If array length is even, return middle two values
middle([1])
=>[]
middle([1, 2, 3])
=>[2]
middle([1, 2, 3, 4])
=>[2, 3]
tail(array)
- Returns the tail (array minus head) in a given array
head(["Hello", "Lighthouse", "Labs"])
=>["Lighthouse", "Labs"]
without(array, itemsToRemove)
- Parses
array
and returns a new array that excludes all of the values passed throughitemsToRemove
without([1, 2, 3], [1])
=>[2, 3]
without(["1", "2", "3"], [1, 2, "3"])
=>["1", "2"]
map(array, callBackFn)
- Mimics .map() behavior and applies the
callBackFn
argument against thearray
items map(["never", "gonna", "give", "you", "up"], word => word[0])
=["n", "g", "g", "y", "u"]
takeUntil(array, callBackFn)
- Loops through a given
array
until the condition in thecallBackFn
returns true. A new array is returned with the preceeding values.
const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5];
takeUntil(data1, x => x < 0)
=>[1, 2, 5, 7, 2]
const data2 = ["I've", "been", "to", "Hollywood", ",", "I've", "been", "to", "Redwood""];
takeUntil(data1, x => x < 0)
=>['I've', 'been', 'to', 'Hollywood']
********** Object functions **********
Functions used to manipulate or parse an object
findKey(object, callback)
- Given an
object
of single key/value pairs, this will return the first key where the value is found- If no matching key is found for the value, return
undefined
- If no matching key is found for the value, return
const data = {
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
}
findKey(data, x => x.stars === 2)
=>"noma"
findKeyByValue(object, value)
- Given an
object
of single key/value pairs, this will return the first key where the value is found- If no matching key is found for the value, return
undefined
- If no matching key is found for the value, return
const bestTVShowsByGenre = {
sci_fi: "The Expanse",
comedy: "Brooklyn Nine-Nine",
drama: "The Wire",
space: "The Expanse"
}
findKeyByValue(bestTVShowsByGenre, "Brooklyn Nine-Nine")
=>"comedy"
findKeyByValue(bestTVShowsByGenre, "Scandal")
=>undefined