1.0.3 • Published 6 months ago

@leandro316/lotide v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months 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.

This project was created and published by me as part of my learnings at Lighthouse Labs.

Usage

Install it:

npm install @leandro316/lotide

Require it:

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

Call it:

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

Documentation

The following functions are currently implemented:

assertArraysEqual Check if two arrays have the same element. It prints if both of them are equal or not

Arguments - Two array (array1, array2)

Example

assertArraysEqual([1, 2, 3], [1, 2, "3"]); // print Assertion Failed: array1 === array2

assertEqual - Check if two elements are the same, the actual and expected. It prints if both of them are equal or not

Arguments - two values (value, value)

Example

assertArraysEqual("same", "same"); // print Assertion passed: actual === expected

assertObjectsEqual - Check if two objects has the same properties and value, the actual and expected. It prints if both of them are equal or not

Arguments - two objects (object1, object2)

Example

assertObjectsEqual(
  { colors: ["red", "blue"], size: "medium" },
  {
    size: "medium",
    colors: ["red", "blue"],
  }
); // print Assertion passed: actual === expected

countLetters - count the number of letters and return an object

Arguments - string

return - object

Example

countLetters("lighthouse in the house") , // return {
    l: 1,
    i: 2,
    g: 1,
    h: 4,
    t: 2,
    o: 2,
    u: 2,
    s: 2,
    e: 3,
    n: 1,
  } //

countOnly - count the number of Words in array base on the given value in the second arugument of a function.

Arguments - (array, value)

return - object

Example

const firstNames = [
  "Karl",
  "Salima",
  "Agouhanna",
  "Fang",
  "Kavith",
  "Jason",
  "Salima",
  "Fang",
  "Joe",
];
countOnly(firstNames, {
  Jason: true,
  Karima: true,
  Fang: true,
  Agouhanna: false,
}) , // return {Jason: 1, karima: undefined, Fang: 2, Agouhanna: undefined}

eqArrays - check if two arrays has equal values inside

Arguments - (array1, array2)

return - Boolean

Example

eqArrays([1, 2, 3], [1, 2, 3]), // return true

eqObjects - check if two objects has equal key/properties values

Arguments - (object1, object2)

return - Boolean

Example

const multiColorShirtObject = { colors: ["red", "blue"], size: "medium" };
const anotherMultiColorShirtObject = {
  size: "medium",
  colors: ["red", "blue"],
};
eqObjects(multiColorShirtObject, anotherMultiColorShirtObject), // return true

findKey - return the key of an object that meet the requirements in the callback functions

Arguments - (object1, callBackFunction)

return - object key/s

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
); // return "noma"

findKeyByValue - return the key of an object base on the given value

Arguments - (object1, value)

return - object key/s

Example

const bestTVShowsByGenre = {
  sci_fi: "The Expanse",
  comedy: "Brooklyn Nine-Nine",
  drama: "The Wire",
};

findKeyByValue(bestTVShowsByGenre, "The Wire"),// return "drama";

flatten - flatten an array with an array inside

Arguments - (array)

return - array

Example

flatten([1, 2, [3, 4], 5, [6]]); // return [1, 2, 3, 4, 5, 6]

head - get the first element of an array or string

Arguments - (array), (string)

return - value

Example

head([1, 2, 3]); // return 1

letterPositions - get the index of a given letter/s

Arguments - (string)

return - object

Example

letterPositions("hello").l,// return [2, 3]

middle - get the middle value/s of an array

Arguments - (array)

return - array

Example

middle([1, 2, 3]), // return [2]
middle([1, 2, 3, 4, 5, 6]), // return [3,4]

tail - get all the elements of an array except the first element

Arguments - (array)

return - array

Example

tail(["Hello", "Lighthouse", "Labs"]), // return ["Lighthouse", "Labs"]
tail([1, 2, 3]), // return [2,3]

takeUntil - return an array that meet the requirements in the call back functions. It stops to get the elements of an array once he meet the conditions in callback functions

Arguments - (array, function)

return - array

Example

const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5];
const data2 = [
  "I've",
  "been",
  "to",
  "Hollywood",
  ",",
  "I've",
  "been",
  "to",
  "Redwood",
];

const results1 = takeUntil(data1, (x) => x < 0); // return [1, 2, 5, 7, 2]
const results2 = takeUntil(data2, (x) => x === ","); // return  ["I've", "been", "to", "Hollywood"]

takeUntil - Return an array by removing the elements from array one that are also present in array two

Arguments - (arrayOne, arrayTwo)

return - array

Example

const words = ["hello", "world", "lighthouse"];
without(words, ["lighthouse"]); // return ["hello", "world"]

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago