@radham/utils
v0.1.2
Published
A small bespoke utility library.
Readme
@radham/utils
A small bespoke utility library.
Install
npm install @radham/utilsUsage
attempt
Executes a function and returns its result, or undefined if it throws.
import { attempt } from '@radham/utils';
attempt(() => JSON.parse('{"a":1}')); // { a: 1 }
attempt(() => JSON.parse('invalid')); // undefinedcapitalize
Capitalizes the first character of a string.
import { capitalize } from '@radham/utils';
capitalize('hello'); // 'Hello'compose
Composes multiple functions into a single function that applies them from left to right.
import { compose } from '@radham/utils';
const addOne = (value: number) => value + 1;
const double = (value: number) => value * 2;
compose(addOne, double)(3); // 8isNumber
Checks whether the given value is a number.
import { isNumber } from '@radham/utils';
isNumber(42); // true
isNumber('42'); // false
isNumber(NaN); // true
isNumber(NaN, { finite: true }); // false
isNumber(Infinity, { finite: true }); // falseisNumberLike
Checks whether the given value is a number or can be coerced to one.
import { isNumberLike } from '@radham/utils';
isNumberLike(42); // true
isNumberLike('42'); // true
isNumberLike('3.14'); // true
isNumberLike('NaN'); // true
isNumberLike('NaN', { finite: true }); // false
isNumberLike('Infinity', { finite: true }); // false
isNumberLike('abc'); // false
isNumberLike(null); // false
isNumberLike(1n); // false
isNumberLike(1n, { bigint: true }); // true
isNumberLike('1n', { bigint: true }); // trueisPlainObject
Checks whether the given value is a plain object.
import { isPlainObject } from '@radham/utils';
isPlainObject({ a: 1 }); // true
isPlainObject([1, 2, 3]); // false
isPlainObject(null); // falseisString
Checks whether the given value is a string.
import { isString } from '@radham/utils';
isString('hello'); // true
isString(42); // falseomit
Creates a new object with the specified keys omitted.
import { omit } from '@radham/utils';
omit({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { b: 2 }
omit({ a: 1, b: 2 }, 'a'); // { b: 2 }range
Creates an array of numbers from start (inclusive) to end (exclusive).
import { range } from '@radham/utils';
range(4); // [0, 1, 2, 3]
range(2, 5); // [2, 3, 4]toString
Converts a value to its string representation.
import { toString } from '@radham/utils';
toString(42); // '42'
toString(null); // 'null'
toString(undefined); // 'undefined'unique
Returns a new array with duplicate values removed.
import { unique } from '@radham/utils';
unique([1, 2, 2, 3, 1]); // [1, 2, 3]License
The BSD 3-Clause License. See the license file for details.
