Skip to content

Documentation / @ripl/utilities

@ripl/utilities ​

Shared typed utility functions for Ripl — a unified API for 2D graphics rendering in the browser.

Installation ​

bash
npm install @ripl/utilities

Overview ​

A collection of strictly-typed utility functions used across the Ripl ecosystem. Zero dependencies, fully tree-shakable.

API ​

Type Guards ​

typescript
typeIsArray(value); // value is unknown[]
typeIsFunction(value); // value is AnyFunction
typeIsNil(value); // value is null | undefined
typeIsNumber(value); // value is number
typeIsString(value); // value is string
typeIsObject(value); // value is object
typeIsDate(value); // value is Date
typeIsBoolean(value); // value is boolean

Collection Helpers ​

typescript
arrayJoin(left, right, predicate); // left/inner/right join for data diffing (Map-optimized for key predicates)
arrayMapRange(length, callback); // map over a numeric range
arrayGroup(array, key); // group array items by key or function
arrayIntersection(left, right, predicate); // intersection of two arrays
arrayDifference(left, right, predicate); // difference of two arrays

Note: Generic array wrappers (arrayForEach, arrayMap, arrayFilter, arrayReduce, arrayFind, arrayFlatMap) have been removed in favour of native array methods for better performance.

Object Helpers ​

typescript
objectForEach(object, callback);
objectMap(object, callback);
objectMerge(target, ...sources);

Common Types ​

typescript
OneOrMore<T>; // T | T[]
AnyFunction; // (...args: any[]) => any
AnyObject; // { [key: PropertyKey]: unknown }
Disposable; // { dispose: () => void }
Predicate<L, R>; // (left: L, right: R) => boolean

Value Helpers ​

typescript
valueOr(value, fallback); // value ?? fallback with type narrowing
valueOrDefault(value, fallback); // nullish coalescing with defaults

Documentation ​

Full documentation is available at ripl.rocks.

License ​

MIT

Interfaces ​

InterfaceDescription
ArrayJoinResult of an array join containing unmatched left items, matched pairs, and unmatched right items.
DisposableA resource that can be disposed to release underlying subscriptions or handles.

Type Aliases ​

Type AliasDescription
AnyAsyncFunctionA loosely-typed async function signature.
AnyFunctionA loosely-typed function signature that accepts and returns anything.
AnyObjectA loosely-typed object with string, number, or symbol keys.
ArrayGroupIdentityA property key or indexer function used to derive group identity from array items.
ArrayJoinPredicateA shared key or predicate function used to match items between two arrays in a join.
CachedFunctionA function wrapper that caches its result after the first invocation until explicitly invalidated.
CollectionIterateeCallback invoked for each item in a collection, receiving the value and its index.
GetMutableKeysExtracts the mutable (non-readonly) property keys from an object type.
GetReadonlyKeysExtracts the readonly property keys from an object type.
IfEqualsConditional type that resolves to A if X and Y are identical, otherwise B.
IndexerDerives a grouping key from a value.
IterableObjectAn object whose values are unknown, suitable for generic iteration.
MemoizedFunctionA function wrapper that caches results per unique key, exposing the underlying cache Map.
MemoizeResolverDerives a cache key from the arguments of a memoized function.
MergeMerges two types, with properties in TB overriding those in TA.
ObjectIterateeCallback invoked for each entry in an object, receiving the key and value.
ObjectReducerReducer callback for folding over object entries into an accumulated result.
OneOrMoreRepresents a single value or an array of values.
PredicateA comparison function that tests whether two values match.
UnionToIntersectionConverts a union type to an intersection type.

Functions ​

FunctionDescription
arrayDifferenceReturns items from the left array that have no matching counterpart in the right array.
arrayGroupGroups array items by a property key or indexer function into a keyed record.
arrayIntersectionReturns items from the left array that have a matching counterpart in the right array.
arrayJoinPerforms a full join between two arrays, returning entries (left-only), updates (matched), and exits (right-only).
arrayMapRangeCreates an array of the given length by mapping each index through the iteratee.
comparitorDateDate comparator suitable for sorting dates in ascending chronological order.
comparitorNumericNumeric comparator suitable for sorting numbers in ascending order.
comparitorStringLocale-aware string comparator suitable for alphabetical sorting.
functionCacheWraps a function so its result is computed once and then returned from cache on subsequent calls until invalidate() is called.
functionIdentityReturns the value it receives unchanged — useful as a default transform or placeholder.
functionMemoizeMemoizes a function by caching results keyed by the resolver (defaults to the first argument).
functionProduceWraps a value or factory function into a consistent factory that always returns the value.
noop-
numberGCDComputes the greatest common divisor of two integers using the Euclidean algorithm.
numberNiceRounds a value to a "nice" human-readable number (1, 2, 5, or 10 scaled by the appropriate power of ten).
numberSumComputes the sum of an array of numbers, or of values mapped through an optional iteratee.
objectForEachIterates over the enumerable properties of an object, invoking the iteratee for each key-value pair.
objectFreezeCreates a shallow frozen copy of the given object.
objectMapMaps over the enumerable properties of an object, producing a new object with transformed values.
objectReduceReduces the enumerable properties of an object into a single accumulated value.
predicateIdentityTests strict reference equality between two values.
predicateKeyTests whether two objects share the same value at a given key.
setFilterFilters a Set, returning a new Set containing only values that satisfy the predicate.
setFindSearches a Set for the first value that satisfies the predicate.
setFlatMapFlat-maps over a Set, concatenating the arrays returned by the iteratee into a new Set.
setForEachIterates over each value in a Set, invoking the iteratee with the value and a running index.
setMapMaps over a Set, producing a new Set with each value transformed by the iteratee.
stringEqualsCase-insensitive string equality check.
stringUniqueIdGenerates a cryptographically random hexadecimal string of the specified length.
typeIsArrayChecks whether a value is an array.
typeIsBooleanChecks whether a value is a boolean.
typeIsDateChecks whether a value is a Date instance.
typeIsFunctionChecks whether a value is a function.
typeIsNilChecks whether a value is null or undefined.
typeIsNumberChecks whether a value is a number.
typeIsObjectChecks whether a value is a non-null object.
typeIsStringChecks whether a value is a string.
valueOneOrMoreNormalises a single value or array into a guaranteed array.