@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
61 lines (60 loc) • 2.7 kB
TypeScript
/** Full key definition for {@link getByPath} or {@link setByPath} */
export type PathKeyDef = {
/** Key name */
key: string | number;
/** Key represents collection index */
isIndex?: boolean;
/** Key should produce array if not exists */
isIndexed?: boolean;
};
/** Key definition for {@link getByPath} or {@link setByPath} */
export type PathKey = PathKeyDef | string | number;
/** Parses path to full {@link PathKeyDef} array */
export declare const parseKeys: (path: string | PathKey[]) => PathKeyDef[];
/**
* Gets object property using "path" key
*
* Supports three types of key definition
* - full array of {@link PathKeyDef}
* - array of keys (string or number), can be mixed with a full definitions {@link PathKeyDef}
* - string path mode (supports index syntax):
* - `a.b` - simple key access (`{a : {b: val}}`)
* - `a[0]` - index access, creates collection if it's not exists (`{a : [val]}`)
* - `a[]` - pushes to the end of collection (`{a : [..., val]}`)
* - `a[a.b.c]` - escaping: non-numeric indexes uses as a simple keys, delimiters inside square brackets are ignored (`{a : {'a.b.c': val}}`)
*
* @param data - object
* @param path - key path. string or {@link PathKey} array
* @param defaultValue - default
* @returns specified object property
*/
export declare const getByPath: (data: any, path: string | PathKey[], defaultValue?: any) => any;
/**
* Gets object property using "path" with a keys separated by `.`
* @see getByPath
*/
export declare const get: (data: any, path: string, defaultValue?: any) => any;
/**
* Sets object property using "path" key
* Creates empty object if sub-key value is not presented.
*
* Supports three types of key definition
* - full array of {@link PathKeyDef}
* - array of keys (string or number), can be mixed with a full definitions {@link PathKeyDef}
* - string path mode (supports index syntax and collection creation):
* - `a.b` - simple key access (`{a : {b: val}}`)
* - `a[0]` - index access, creates collection if it's not exists (`{a : [val]}`)
* - `a[]` - pushes to the end of collection (`{a : [..., val]}`)
* - `a[a.b.c]` - escaping: non-numeric indexes uses as a simple keys, delimiters inside square brackets are ignored (`{a : {'a.b.c': val}}`)
*
* @param target - object
* @param path - key path. string or {@link PathKey} array
* @param value - value of property
* @returns original object
*/
export declare const setByPath: (target: any, path: string | PathKey[], value: any) => any;
/**
* Sets object property using "path" with a keys separated by `.`
* @see setByPath
*/
export declare const set: (target: any, path: string, value: any) => any;