Utilities for JavaScript
No genius here - that's just the stuff you toss in your project's utilities file.
$ npm i utilita
Attempts to invoke function, and returns either the result or the default value.
import { attempt } from 'utilita';
attempt(() => JSON.parse('[1, 2, 3]'), []); // [ 1, 2, 3 ]
attempt(() => JSON.parse('[1, 2, ]'), []); // []
Assigns a property in a deep clone of the source object.
import { set } from 'utilita';
set({ name: 'Joe' }, 'age', 22); // { name: 'Joe', age: 22 }
Replaces an element at a specified index in a copy of the source array.
import { replaceAt } from 'utilita';
replaceAt([1, 2, 3], 1, 111); // [ 1, 111, 3 ]
Removes an element at a specified index in a copy of the source array, and returns both the modified array & the removed element.
import { removeAt } from 'utilita';
removeAt([1, 2, 3, 4, 5], 2).array; // [ 1, 2, 4, 5 ]
removeAt([1, 2, 3, 4, 5], 2).element; // 3
Generates a random string of a specified length that contains lowercase Latin letters a–z & Arabic numerals 0–9.
import { randomString } from 'utilita';
randomString(4); // qvyn
randomString(7); // tp3o8ow
Tests for a match between a regular expression and a specified string.
import { testRegex } from 'utilita';
testRegex('ab+', 'a'); // false
testRegex('ab+', 'ab'); // true
Logs prettified JSON to console.
import { logJSON } from 'utilita';
logJSON({ name: 'Joe', age: 22 });
// {
// "name": "Joe",
// "age": 22
// }
MIT License