Collection of common JavaScript / TypeScript utilities by @oviirup.
- Type-safe utilities for everyday coding
- Zero dependencies
- Thoroughly tested
Type guards and assertion functions for runtime type checking.
isString,isNumber,isInteger,isFloat: Check value typesisArray,isEmptyArray: Check array valuesisObject,isEmptyObject: Check object valuesisEmpty: Check if value is empty, null, undefined, or empty stringisFunction: Check if value is a functionisRegex: Check if value is a regexisTruthy: Check if value is truthyisBrowser: Check if running in browser environmentnot: Negate an assertion function
Array manipulation utilities.
toArray(value): Converts a given value to an arrayunique(value, matcher?): Create an array with all unique itemsat(array, index),first,last: Get first, and last item of the arrayrange(start, stop, steps): Generate a range array of numberstoFiltered(array, predicate): Filter an array, returning a new array with matching itemsmove(array, fromIndex, toIndex): Move an item in an array to a new positionchunk(array, size): Chunk an array into smaller arrays
Number manipulation utilities.
inRange(value, min, max): Checks if a number is within a rangeclamp(value, min, max): Clamps a number between a minimum and maximum valueabbreviate(value, precision?): Abbreviates a number (e.g., 1000 -> 1K)
Object manipulation utilities.
keyInObject(val, key): Checks if a given object has a specified keypick(object, keys): Picks a set of keys from an objectomit(object, keys): Omits a set of keys from an object
Promise manipulation utilities.
sleep(delay): Delays execution for a specified number of millisecondsretry(fn, retries, delay?): Retries a function until it succeeds or max retries reachedtryCatch(input): Wraps a promise and returns a result tuple [value, error]timeout(fn, duration): Rejects a promise if it doesn't resolve within timeout
String manipulation utilities.
slash: Replace backslash to slashtruncate: Truncates a string to specified lengthtoCamelCase,toSnakeCase,toKebabCase,toPascalCase,toSentenceCase,toTitleCase: Convert string case
Secure ID generation.
nanoid(length?, alphabets?): Generate a secure nanoid string