A mini clone of the Lodash library.
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.
Install it:
npm install @username/lotide
Require it:
const _ = require('@b-rob97/lotide');
Call it:
const results = _.tail([1, 2, 3]) // => [2, 3]
The following functions are currently implemented:
head(arr)
: Returns the first item from an array.tail(arr)
: Returns all items from an array except the first item.middle(arr)
: Returns the middle item(s) from an array.without(source, itemsToRemove)
: Returns a new array with specified items removed.takeUntil(array, callback)
: Returns a slice of an array until a callback condition is met.map(array, callback)
: Creates a new array by applying a callback function to each item.letterPositions(sentence)
: Returns indices of letters in a sentence.findKeyByValue(object, value)
: Finds a key in an object by its corresponding value.findKey(object, callback)
: Finds a key in an object based on a callback condition.eqObjects(object1, object2)
: Compares two objects for deep equality.eqArrays(array1, array2)
: Compares two arrays for equality.countOnly(allItems, itemsToCount)
: Counts occurrences of specified items in an array.countLetters(letters)
: Counts occurrences of letters in an array.assertObjectsEqual(actual, expected)
: Compares and asserts deep equality of objects.assertEqual(actual, expected)
: Compares and asserts equality of values.assertArraysEqual(array1, array2)
: Compares and asserts equality of arrays.
- The head function returns the first item from an array.
- The head function will not return the first element as an array.
- An array with only one element will yield one element as its head.
- An empty array will yield
undefined
as its head.
head(['Hello', 'Lighthouse', 'Labs']) // Returns 'Hello'
- The tail function returns all items from an array except the first item.
- The tail function will return the tail elements as a new array.
- An array with only one element will yield an empty array as its tail.
- An empty array will yield an empty array as its tail.
tail(['Hello', 'Lighthouse', 'Labs']) // Returns ['Lighthouse', 'Labs']
- The middle function returns the middle item(s) from an array.
- If the array has an even number of elements, the middle will be the two middle items.
- An array with less than three elements will yield an empty array as its middle.
- An empty array will yield an empty array as its middle.
middle([1, 2, 3, 4, 5]) // Returns [3]
middle(['a', 'b', 'c', 'd']) // Returns ['b', 'c']
- The without function returns a new array with all the items from the source array except the ones specified in the itemsToRemove array.
- The original source array remains unchanged.
- If an item appears multiple times in the itemsToRemove array, it will be removed from the source array each time.
- If an item to remove is not present in the source array, it is ignored.
- An empty source array will yield an empty array.
without([1, 2, 3, 4, 5], [3, 4]) // Returns [1, 2, 5]
without(['a', 'b', 'c', 'd'], ['b', 'd']) // Returns ['a', 'c']
- The takeUntil function returns a slice of the array with items taken from the beginning until the callback returns a truthy value.
- The original array remains unchanged.
- The callback is applied to each item in the array in order.
- The items are taken from the beginning until the first item for which the callback returns true.
- If no items satisfy the condition, an empty array is returned.
// Example 1: Taking elements until a condition is met
takeUntil(/* Array of numbers */, /* Callback function */) // Returns /* Resulting array */
// Example 2: Taking elements until another condition is met
takeUntil(/* Array of characters */, /* Callback function */) // Returns /* Resulting array */
- The map function creates a new array containing the results of applying the provided callback function to each item in the input array.
- The original array remains unchanged.
- The callback function is applied to each item in the array, in order.
- The results of the callback function are collected into a new array, which is returned as the result.
- The new array will have the same length as the input array.
// Example 1: Applying a transformation to each number
map(/* Array of numbers */, /* Callback function */) // Returns /* Resulting array */
// Example 2: Applying a transformation to each string
map(/* Array of strings */, /* Callback function */) // Returns /* Resulting array */
- The letterPositions function returns an object where each key represents a letter in the sentence and the corresponding value is an array of the indices where that letter appears.
- The spaces in the sentence are ignored.
- The returned object contains arrays of indices for each letter.
- The order of the indices reflects the order in which the letters appear in the sentence.
// Example 1: Finding indices of letters in a sentence
letterPositions(/* Sentence */) // Returns /* Object with letter indices */
// Example 2: Finding indices of letters in another sentence
letterPositions(/* Sentence */) // Returns /* Object with letter indices */
- The findKeyByValue function searches for a key in the given object that corresponds to the specified value.
- The function returns the first key found that matches the value, or
undefined
if no such key is found. - The search is performed by comparing the values associated with each key in the object.
- The function returns the first key found that matches the value, or
// Example 1: Finding a key by its corresponding value
findKeyByValue(/* Object */, /* Value */) // Returns /* Key */
// Example 2: Finding another key by its corresponding value
findKeyByValue(/* Object */, /* Value */) // Returns /* Key */
- The findKey function searches for a key in the given object that satisfies the condition specified by the callback function.
- The function returns the first key found that meets the callback's condition, or
undefined
if no such key is found. - The search is performed by applying the callback to the values associated with each key in the object.
- The function returns the first key found that meets the callback's condition, or
// Example 1: Finding a key based on a condition
findKey(/* Object */, /* Callback function */) // Returns /* Key */
// Example 2: Finding another key based on a different condition
findKey(/* Object */, /* Callback function */) // Returns /* Key */
- The eqObjects function compares two objects for deep equality.
- The function returns
true
if both objects are equal in structure and values, orfalse
otherwise. - The comparison includes nested objects and arrays.
- If any value within the objects is an array, the
eqArrays
function is used to compare them.
- The function returns
// Example 1: Comparing two equal objects
eqObjects(/* Object 1 */, /* Object 2 */) // Returns true
// Example 2: Comparing two different objects
eqObjects(/* Object 1 */, /* Object 2 */) // Returns false
- The eqArrays function compares two arrays for equality.
- The function returns
true
if both arrays have the same length and contain the same elements in the same order, orfalse
otherwise.
- The function returns
// Example 1: Comparing two equal arrays
eqArrays(/* Array 1 */, /* Array 2 */) // Returns true
// Example 2: Comparing two different arrays
eqArrays(/* Array 1 */, /* Array 2 */) // Returns false
- The countOnly function counts the occurrences of items in the
allItems
array that are specified in theitemsToCount
object.- The function returns an object where each key represents an item from
allItems
and the corresponding value is the count of that item in the array. - Only items that are explicitly specified in
itemsToCount
are counted. - If an item's count is zero, it will not be included in the results.
- The function returns an object where each key represents an item from
// Example 1: Counting specified items in an array
countOnly(/* All items */, /* Items to count */) // Returns /* Counted items object */
// Example 2: Counting specified items in another array
countOnly(/* All items */, /* Items to count */) // Returns /* Counted items object */
- The countLetters function counts the occurrences of each letter in the given array.
- The function returns an object where each key represents a lowercase/uppercase letter found in the array and the corresponding value is the count of that letter.
- The counting is case-sensitive.
- Spaces in the array are ignored.
// Example 1: Counting letters in an array
countLetters(/* Array of letters */) // Returns /* Counted letters object */
// Example 2: Counting letters in another array
countLetters(/* Array of letters */) // Returns /* Counted letters object */
- The assertObjectsEqual function compares two objects for deep equality and prints a message indicating whether the comparison passes or fails.
- The function uses the
eqObjects
function to perform the comparison. - If the objects are equal, a success message is printed.
- If the objects are not equal, a failure message is printed.
- The function uses the
// Example 1: Comparing and asserting equal objects
assertObjectsEqual(/* Actual object */, /* Expected object */);
// Example 2: Comparing and asserting different objects
assertObjectsEqual(/* Actual object */, /* Expected object */);
- The assertEqual function compares two values for equality and prints a message indicating whether the comparison passes or fails.
- If the values are equal, a success message is printed.
- If the values are not equal, a failure message is printed.
// Example 1: Comparing and asserting equal values
assertEqual(/* Actual value */, /* Expected value */);
// Example 2: Comparing and asserting different values
assertEqual(/* Actual value */, /* Expected value */);
- The assertArraysEqual function compares two arrays for equality and prints a message indicating whether the comparison passes or fails.
- The function uses the
eqArrays
function to perform the comparison. - If the arrays are equal, a success message is printed.
- If the arrays are not equal, a failure message is printed.
- The function uses the
// Example 1: Comparing and asserting equal arrays
assertArraysEqual(/* Array 1 */, /* Array 2 */);
// Example 2: Comparing and asserting different arrays
assertArraysEqual(/* Array 1 */, /* Array 2 */);