memo()
ts
function memo<T>(validator): TMemoizes a validator function—a parser or a guard. Internally, a memoized function uses a WeakMap to prevent memory leaks. While you can wrap any validator function within memo, primitive values will not be memoized.
Type Parameters
| Type Parameter |
|---|
T extends (arg) => unknown |
Parameters
| Parameter | Type | Description |
|---|---|---|
validator | T | A parser or guard function. |
Returns
T
A memoized version of validator.
Examples
ts
const parseUser = memo(object({
id: parseNumber,
name: parseString,
}))It can be used to for nested properties
ts
const parseUser = object({
id: parseNumber,
name: parseString,
address: memo(object({
street: parseString,
city: parseString,
})),
})memoizeValidatorConstructor()
ts
function memoizeValidatorConstructor<T>(validatorConstructor): TGiven a higher order function that constructs a validator, returns a new function that also constructs a validator, but the validator will be memoized.
Example
ts
const objectMemo = memo2(object)
const parseUser = objectMemo({
id: parseNumber,
name: parseString,
})Type Parameters
| Type Parameter |
|---|
T extends (...schema) => Guard<unknown> | Parser<unknown> |
Parameters
| Parameter | Type | Description |
|---|---|---|
validatorConstructor | T | A higher order function that takes a schema and constructs a validator—a parser or a guard. |
Returns
T
A function of the same type of validatorConstructor, but when called, the returned function is memoized.
