Expedite is a JavaScript utility library for common operations with arrays, objects, strings, numbers and more. It is built with ES6 and has full test coverage with Jest. Expedite has 30+ methods including flatten, xor, join, and more.
- Clone library to local computer.
- Run
npm run build
to build the library. - Open Node REPL by running
node
. - Assign the library import to a variable (see code below).
const expedite = require("./expedite.js").default;
- Use any of libraries built-in methods.
> expedite.flatten([[1, 2], [3]])
[ 1, 2, 3 ]
The flatten
function takes an array of any number of dimensions and returns the elements in a one dimensional array.
const flatten = (anyList) => {
let flatList = [];
anyList.forEach((element) => {
if (Array.isArray(element)) {
flatList = flatList.concat(flatten(element));
} else {
flatList.push(element);
}
});
return flatList;
};
The shuffle
function accepts an array and returns a new array in a randomly shuffled order.
const shuffle = (list) => {
const shuffledList = [];
while (list.length > 0) {
const randomIdx = Math.floor(Math.random() * list.length);
shuffledList.push(list[randomIdx]);
list.splice(randomIdx, 1);
}
return shuffledList;
};
The parsedInt
function accepts a string and converts it into an integer in radix 10.
const parsedInt = (str) => {
if (str.length === 0) return str;
const intList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let finalInt = 0;
for (let i = 0; i < str.length; i++) {
const currEle = str[i];
intList.forEach((int) => {
if (`${int}` === currEle) finalInt = finalInt * 10 + int;
});
}
return finalInt;
};