UNPKG

@rimbu/deep

Version:

Tools to use handle plain JS objects as immutable objects

211 lines 8.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withType = exports.selectAtWith = exports.selectAt = exports.selectWith = exports.matchAtWith = exports.matchAt = exports.matchWith = exports.patchAtWith = exports.patchWith = exports.getAtWith = exports.protect = exports.select = exports.patchAt = exports.getAt = exports.patch = exports.match = void 0; var internal_cjs_1 = require("./internal.cjs"); var match_cjs_1 = require("./match.cjs"); Object.defineProperty(exports, "match", { enumerable: true, get: function () { return match_cjs_1.match; } }); var patch_cjs_1 = require("./patch.cjs"); Object.defineProperty(exports, "patch", { enumerable: true, get: function () { return patch_cjs_1.patch; } }); var path_cjs_1 = require("./path.cjs"); Object.defineProperty(exports, "getAt", { enumerable: true, get: function () { return path_cjs_1.getAt; } }); Object.defineProperty(exports, "patchAt", { enumerable: true, get: function () { return path_cjs_1.patchAt; } }); var selector_cjs_1 = require("./selector.cjs"); Object.defineProperty(exports, "select", { enumerable: true, get: function () { return selector_cjs_1.select; } }); /** * 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 * ``` */ function protect(source) { return source; } exports.protect = protect; /** * 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'] * ``` */ function getAtWith(path) { return function (source) { return internal_cjs_1.Deep.getAt(source, path); }; } exports.getAtWith = getAtWith; /** * 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' }] * ``` */ function patchWith(patchItem) { return function (source) { return internal_cjs_1.Deep.patch(source, patchItem); }; } exports.patchWith = patchWith; /** * 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' } }] * ``` */ function patchAtWith(path, patchItem) { return function (source) { return internal_cjs_1.Deep.patchAt(source, path, patchItem); }; } exports.patchAtWith = patchAtWith; /** * 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' }] * ``` */ function matchWith(matcher) { return function (source) { return internal_cjs_1.Deep.match(source, matcher); }; } exports.matchWith = matchWith; /** * 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 * ``` */ function matchAt(source, path, matcher) { return internal_cjs_1.Deep.match(internal_cjs_1.Deep.getAt(source, path), matcher); } exports.matchAt = matchAt; /** * 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' }] * ``` */ function matchAtWith(path, matcher) { return function (source) { return internal_cjs_1.Deep.matchAt(source, path, matcher); }; } exports.matchAtWith = matchAtWith; /** * 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] }] * ``` */ function selectWith(selector) { return function (source) { return internal_cjs_1.Deep.select(source, selector); }; } exports.selectWith = selectWith; /** * 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] } * ``` */ function selectAt(source, path, selector) { return internal_cjs_1.Deep.select(internal_cjs_1.Deep.getAt(source, path), selector); } exports.selectAt = selectAt; /** * 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] }] * ``` */ function selectAtWith(path, selector) { return function (source) { return internal_cjs_1.Deep.selectAt(source, path, selector); }; } exports.selectAtWith = selectAtWith; /** * 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 }} * ``` */ function withType() { return internal_cjs_1.Deep; } exports.withType = withType; //# sourceMappingURL=deep.cjs.map