UNPKG

@bbashcode/lotide-flex

Version:

A clone of the lodash JavaScript library to practice creating various types of functions using JS.

56 lines (49 loc) 2.02 kB
/** * eqArrays method is used to compare two arrays for a perfect match * @param {array} array1 The first param * @param {array} array2 The second param * @return {boolean} returns true or false depending on whether both arrays are equal or not */ const eqArrays = (array1, array2) => array1.length === array2.length && array1.every((element, index) => element === array2[index]); /** * assertArraysEqual method is used for asserting that two arrays are equal * @param {array} actual The first param * @param {array} expected The second param * @return {void/undefined} This method does not return anything, rather the side effect is to console log test pass or fail based on comparison */ const assertArraysEqual = function(actual, expected) { if (eqArrays(actual, expected)) { console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`); } else { console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`); } }; /** * takeUntil is a methof that keeps collecting items from a provided array until the callback provided returns a truthy value. * @param {array} array The first param is an array * @param {function} callbacl The second param is a callback function * @return {void/undefined} returns a shallow copy of the array that satisfies the callback condition */ const takeUntil = function(array, callback){ let results = []; for(let item of array){ if(!callback(item)){ results.push(item); } else { break; } } return results; }; //TEST CODE const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5]; // const results1 = takeUntil(data1, x => x < 0); // console.log(results1); // console.log('---'); const data2 = ["I've", "been", "to", "Hollywood", ",", "I've", "been", "to", "Redwood"]; // const results2 = takeUntil(data2, x => x === ','); // console.log(results2); assertArraysEqual([ 1, 2, 5, 7, 2 ], takeUntil(data1, x => x < 0)); assertArraysEqual(takeUntil(data2, x => x === ','),[ 'I\'ve', 'been', 'to', 'Hollywood' ]);