This is a super tiny package which includes a set of well-typed simple utilities, which is just useful for any kind of project or used inside other Reatom packages. A lot of edge cases are missing here, but this is a nice start for any TypeScript project.
included in @reatom/framework
Just a no-operation function with any
types to put it anywhere you want as a plug.
A promise creation function which relies on the passed timeout in milliseconds.
Classic typeof value === 'object' && value !== null
check with a smart types.
Shallow comparison of two values, supports primitives, objects, dates, arrays, maps, and sets.
Recursive comparison of two values, supports primitives, objects, dates, arrays, maps, and sets. Cyclic references supported too.
Object.assign
with fixed types, equal properties replaced instead of changed to a union type.
assign which set an empty object to the first argument.
Object.keys
with fixed types.
Object.entries
with fixed types.
Get a new object only with the properties of the passed keys.
Get a new object without the properties of the passed keys.
Typesafe shortcut to JSON.parse(JSON.stringify(value))
. UsestructuredClone if your environment supports it.
Returns random integer. Parameters (min = 0
and max = Number.MAX_SAFE_INTEGER - 1
) should be integers too.
Asserts a non-nullable type of the passed value, and accepts an optional message as the second argument for an error.
This function converts any kind of data to a string. It is like a hash function, but the length of the resulted string is close to JSON.stringify
output or a unique string. Map
and Set
are supported, but rely on the order (as it is a required property of these data structures in the standard), while keys of the plain object are sorted automatically. If the value is a function, symbol, an object with custom constructor, or an object with cyclic references, it is a nominal value which cannot be represented in a readable string and will be saved as a unique string (a kind + random number). The nominal results are memoized by a WeakMap; you can memoize all objects transformations by the optional immutable
parameter if you think they will never change.
import { toStringKey } from '@reatom/utils'
toStringKey(new Map([[1, {}]]) === toStringKey(new Map([[1, {}]]) /// true
Parse the passed value to DOMException
instance with name = 'AbortError'
.
Accepts an optional AbortController
and throws an error if the signal is aborted. A ponyfil to AbortSignal API: throwIfAborted
Do the check value instanceof Error && value.name === 'AbortError'
Convert string message to abort error, abort a controller if passed, throw the error. Useful for correct type inference.
2 ** 31 - 1
- https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value
Removes named generics and shows just a plain type.
export type Values<T> = T[keyof T]
Omits the object keys for passed values types.
Picks the object keys for passed values types.
The type of assign
The missed type of all common environments.