UNPKG

@rimbu/deep

Version:

Tools to use handle plain JS objects as immutable objects

192 lines 7.31 kB
import { Deep } from './internal.mjs'; export { match } from './match.mjs'; export { patch } from './patch.mjs'; export { getAt, patchAt } from './path.mjs'; export { select } from './selector.mjs'; /** * Returns the same value wrapped in the `Protected` type. * @typeparam T - the source value type * @param source - the value to wrap * @note does not perform any runtime protection, it is only a utility to easily add the `Protected` * type to a value * @example * ```ts * const obj = Deep.protect({ a: 1, b: { c: true, d: [1] } }) * obj.a = 2 // compiler error: a is readonly * obj.b.c = false // compiler error: c is readonly * obj.b.d.push(2) // compiler error: d is a readonly array * (obj as any).b.d.push(2) // will actually mutate the object * ``` */ export function protect(source) { return source; } /** * Returns a function that gets the value at the given string `path` inside an object. * @typeparam T - the input value type * @typeparam P - the string literal path type in the object * @param path - the string path in the object * @param source - the value from which to extract the path value * @example * ```ts * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }]; * items.map(Deep.getAtWith('a.c')); * // => ['a', 'b'] * ``` */ export function getAtWith(path) { return (source) => Deep.getAt(source, path); } /** * Returns a function that patches a given `source` with the given `patchItems`. * @typeparam T - the patch value type * @typeparam TE - utility type * @typeparam TT - utility type * @param patchItem - the `Patch` definition to update the given value of type `T` with. * @param source - the value to use the given `patchItem` on. * @example * ```ts * const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }]; * items.map(Deep.patchWith([{ a: v => v + 1 }])); * // => [{ a: 2, b: 'a' }, { a: 3, b: 'b' }] * ``` */ export function patchWith(patchItem) { return (source) => Deep.patch(source, patchItem); } /** * Returns a function that patches a given `value` with the given `patchItems` at the given `path`. * @typeparam T - the patch value type * @typeparam P - the string literal path type in the object * @typeparam TE - utility type * @typeparam TT - utility type * @param path - the string path in the object * @param patchItem - the `Patch` definition to update the value at the given `path` in `T` with. * @param source - the value to use the given `patchItem` on at the given `path`. * @example * ```ts * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }]; * items.map(Deep.patchAtWith('a', [{ b: (v) => v + 1 }])); * // => [{ a: { b: 2, c: 'a' } }, { a: { b: 3, c: 'b' } }] * ``` */ export function patchAtWith(path, patchItem) { return (source) => Deep.patchAt(source, path, patchItem); } /** * Returns a function that matches a given `value` with the given `matcher`. * @typeparam T - the patch value type * @param matcher - a matcher object that matches input values. * @param source - the value to use the given `patchItem` on at the given `path`. * @example * ```ts * const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }]; * items.filter(Deep.matchWith({ a: 2 })); * // => [{ a: 2, b: 'b' }] * ``` */ export function matchWith(matcher) { return (source) => Deep.match(source, matcher); } /** * Returns true if the given `value` object matches the given `matcher` at the given `path`, false otherwise. * @typeparam T - the input value type * @typeparam P - the string literal path type in the object * @param source - the input value * @param path - the string path in the object * @param matcher - a matcher object or a function taking the matcher API and returning a match object * @example * ```ts * const input = { a: 1, b: { c: true, d: 'a' } } * Deep.matchAt(input, 'b', { c: true }) * // => true * ``` */ export function matchAt(source, path, matcher) { return Deep.match(Deep.getAt(source, path), matcher); } /** * Returns a function that matches a given `value` with the given `matcher` at the given string `path`. * @typeparam T - the patch value type * @typeparam P - the string literal path type in the object * @typeparam TE - utility type * @param path - the string path in the object * @param matcher - a matcher object that matches input values. * @param source - the value to use the given `matcher` on at the given `path`. * @example * ```ts * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }]; * items.filter(Deep.matchAtWith('a.b', 2)); * // => [{ a: 2, b: 'b' }] * ``` */ export function matchAtWith(path, matcher) { return (source) => Deep.matchAt(source, path, matcher); } /** * Returns a function that selects a certain shape from a given `value` with the given `selector`. * @typeparam T - the patch value type * @typeparam SL - the selector shape type * @param selector - a shape indicating the selection from the source values * @param source - the value to use the given `selector` on. * @example * ```ts * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }]; * items.map(Deep.selectWith({ q: 'a.c', z: ['a.b', v => v.a.b + 1] as const })); * // => [{ q: 'a', z: [1, 2] }, { q: 'b', z: [2, 3] }] * ``` */ export function selectWith(selector) { return (source) => Deep.select(source, selector); } /** * Returns the result of applying the given `selector` shape to the given `source` value. * @typeparam T - the patch value type * @typeparam P - the string literal path type in the object * @typeparam SL - the selector shape type * @param source - the source value to select from * @param path - the string path in the object * @param selector - a shape indicating the selection from the source value at the given path * @example * ```ts * const item = { a: { b: 1, c: 'a' } }; * Deep.selectAt(item, 'a', { q: 'c', z: ['b', v => v.b + 1] as const }); * // => { q: 'a', z: [1, 2] } * ``` */ export function selectAt(source, path, selector) { return Deep.select(Deep.getAt(source, path), selector); } /** * Returns a function that selects a certain shape from a given `value` with the given `selector` at the given string `path`. * @typeparam T - the patch value type * @typeparam P - the string literal path type in the object * @typeparam SL - the selector shape type * @param path - the string path in the object * @param selector - a shape indicating the selection from the source values * @example * ```ts * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }]; * items.map(Deep.selectAtWith('a', { q: 'c', z: ['b', v => v.b + 1] as const })); * // => [{ q: 'a', z: [1, 2] }, { q: 'b', z: [2, 3] }] * ``` */ export function selectAtWith(path, selector) { return (source) => Deep.selectAt(source, path, selector); } /** * Returns a curried API with a known target type. This can be useful for using the methods in * contexts where the target type can be inferred from the usage. * @typeparam T - the target type * @example * ```ts * const s = { a: 1, b: { c: 'a', d: true }} * const upd = Deep.withType<typeof s>().patchWith([{ d: (v) => !v }]) * upd(s) * // => { a: 1, b: { c: 'a', d: false }} * ``` */ export function withType() { return Deep; } //# sourceMappingURL=deep.mjs.map