A random collection of various handy JavaScript utilities.
- Returns new array with chunked values from original array.
import { arrayChunk } from "essential-utils";
arrayChunk([1, 2, 3, 4], 2); // [[1, 2], [3, 4]]
- Returns sum of all values from passed array.
import { arraySum } from "essential-utils";
arraySum([1, 2, 3, 4]); // 10;
- Returns provided array with shuffled values.
import { arrayShuffle } from "essential-utils";
arrayShuffle([1, 2, 3, 4]); // e.g. [2, 1, 4, 3]
- Returns range between two values in array.
import { numberRange } from "essential-utils";
numberRange(0, 2); // [0, 1, 2];
numberRange(0, 3, 2); // [0, 2];
- Transforms iterable which has forEach function implemented into object.
import { iterableToObject } from "essential-utils";
iterableToObject([0, 5]); // { 0: 0, 1: 5 }
iterableToObject(new Map([["number", 42]])); // { number: 42 }
- Transforms iterable into JSON.
- Returns either new object or original object with mapped values.
import { objectMap } from "essential-utils";
const object = { first: 2, second: 3 };
objectMap(object, value => value ** 2); // { first: 4, second: 8 }
console.log(object); // { first: 2, second: 3 }
objectMap(object, value => value ** 2, true); // { first: 4, second: 8 }
console.log(object); // { first: 4, second: 8 }
- Parses url and returns object with key and value pairs from query.
import { getQueryParams } from "essential-utils";
getQueryParams("?a=5&b=4&a=6"); // { a: ["5", "6"], b: "4" }
getQueryParams("?a[]=5&b[]=4&a[]=6"); // { a: ["5", "6"], b: ["4"] }
getQueryParams("?a=5,6&b=4"); // { a: ["5", "6"], b: "4" }
getQueryParams("?a[]=5,6&b[]=4"); // { a: ["5", "6"], b: ["4"] }