immutable-path
Version:
Immutable `get`, `set`, `has`, `unset` deep path operations libraray for object, array and `Map`.
56 lines • 3.14 kB
TypeScript
import { Source, Path, Options, UnsetOptions } from "./util/types";
/**
* Sets the property value of path on object/array/Map immutably without changing input. If a portion of path does not exist it's created.
* Provided `atomicClasses` is used to determine which type of objects are treated as atomic. Atomic classes are
* treated like primitives pretending they don't have attributes. See example below. If `preferMap` is true,
* when new objects are needed, they are created as `Map` instead of object.
*
* @param source is the object/array/map to set.
* @param path is the path of the property to set.
* @param value is the value to set.
* @param __namedParameters are options.
* @returns new object/array/Map.
* @example
* const a = set({ x: new Date() }, "x.y", 3); // 3 is not assigned to atomic class Date. Insted it is replaced: { x: { y: 3 } }
* const b = set({ x: { z: 1 } }, "x.y", 3); // 3 is not assigned to `x.y`: { x: { y: 3, z: 1 } }
* const c = set({ }, "x.y", 3); // 3 is not assigned to `x.y`: { x: { y: 3 } }
* const c = set({ }, "x.y", 3, { preferMap: true }); // Map([[x, new Map([[y, 3]])]]);
*/
export declare function set<S extends Source>(source: S, path: Path, value: any, {
/** Atomic classes are treated like a scalar, because MOST PROBABLY user did not intend to update a property of it. Instead they want to replace it. */
atomicClasses,
/** If an attribute for a non-existing object should be created, whether to create a `Map` instead of `object`. */
preferMap, }?: Options, root?: any): any;
/**
* Removes the property at path of object and returns it. Does not change input value.
*
* @param source is the object/array/map to unset a value.
* @param path is the path of the property to unset.
* @param __namedParameters are options.
* @returns new object/array/Map.
*/
export declare function unset<S extends Source>(source: S, path: Path, {
/** Atomic classes are treated like a scalar, because MOST PROBABLY user did not intend to update a property of it. Instead they want to replace it. */
atomicClasses,
/** After unsetting a key/index if object/array/Map becomes empty, it is also unset in parent. */
unsetEmpty,
/** If an attribute for a non-existing object should be created, whether to create a `Map` instead of `object`. */
preferMap, }?: UnsetOptions): any;
/**
* Gets the property value at path of object/array/Map. If the resolved value is undefined the defaultValue is used in its place.
*
* @param source is the object/array/map to query.
* @param path is the path of the property to get.
* @param defaultValue is the value returned if the resolved value is `undefined`.
* @returns the resolved value.
*/
export declare function get<S extends Source>(source: S, path: Path, defaultValue?: any): any;
/**
* Checks if path is a direct property of object/array/Map.
*
* @param source is the object/array/Map to query.
* @param path is the path to check.
* @returns whether path is a direct property of object/array/Map.
*/
export declare function has<S extends Source>(source: S, path: Path): any;
//# sourceMappingURL=immutable-object-path.d.ts.map