UNPKG

3.86 kBTypeScriptView Raw
1declare const objectPath: objectPath.ObjectPathStatic & {
2 withInheritedProps: objectPath.ObjectPathStatic;
3 create(options?: objectPath.Options): objectPath.ObjectPathStatic;
4};
5
6declare namespace objectPath {
7 interface Options {
8 includeInheritedProps?: boolean | undefined;
9 }
10
11 type Path = Array<number | string> | number | string;
12
13 interface ObjectPathStatic {
14 /**
15 * Binds an object
16 */
17 <T extends object>(object: T): ObjectPathBound<T>;
18
19 /**
20 * Deletes a member from object or array
21 */
22 del(object: object, path: Path): { [key: string]: any };
23
24 /**
25 * Tests path existence
26 */
27 has(object: object, path: Path): boolean;
28
29 /**
30 * Get a path from an object
31 */
32 get(object: object, path: Path): any;
33 get<TResult>(object: object, path: Path, defaultValue: TResult): TResult;
34
35 /**
36 * Set a path to a value
37 * @return Any existing value on the path if any
38 */
39 set<TResult = any>(
40 object: object,
41 path: Path,
42 value: TResult,
43 doNotReplace?: boolean,
44 ): TResult | undefined;
45
46 /**
47 * Create (if path isn't an array) and push the value to it. Can push unlimited number of values
48 */
49 push(object: object, path: Path, ...items: any[]): void;
50
51 /**
52 * Get the first non undefined property
53 */
54 coalesce<TResult>(object: object, paths: Path | Path[], defaultValue: TResult): TResult;
55 coalesce<TResult = any>(
56 object: object,
57 paths: Path | Path[],
58 defaultValue?: TResult,
59 ): TResult | undefined;
60
61 /**
62 * Empty a path. Arrays are set to length 0, objects have all elements deleted, strings
63 * are set to empty, numbers to 0, everything else is set to null
64 */
65 empty(object: object, path: Path): any;
66
67 /**
68 * Set a value if it doesn't exist, do nothing if it does
69 */
70 ensureExists<TResult>(object: object, path: Path, defaultValue: TResult): TResult;
71 ensureExists<TResult = any>(
72 object: object,
73 path: Path,
74 defaultValue?: TResult,
75 ): TResult | undefined;
76
77 /**
78 * Insert an item in an array path
79 */
80 insert(object: object, path: Path, value: any, at?: number): void;
81 }
82
83 interface ObjectPathBound<T extends object> {
84 /**
85 * @see objectPath.del
86 */
87 del(path: Path): { [key: string]: any };
88
89 /**
90 * @see objectPath.has
91 */
92 has(path: Path): boolean;
93
94 /**
95 * @see objectPath.get
96 */
97 get(path: Path): any;
98 get<TResult>(path: Path, defaultValue: TResult): TResult;
99
100 /**
101 * @see objectPath.set
102 */
103 set<TResult = any>(path: Path, value: TResult, doNotReplace?: boolean): TResult | undefined;
104
105 /**
106 * @see objectPath.push
107 */
108 push(path: Path, ...items: any[]): void;
109
110 /**
111 * @see objectPath.coalesce
112 */
113 coalesce<TResult>(paths: Path | Path[], defaultValue: TResult): TResult;
114 coalesce<TResult = any>(paths: Path | Path[], defaultValue?: TResult): TResult | undefined;
115
116 /**
117 * @see objectPath.empty
118 */
119 empty(path: Path): any;
120
121 /**
122 * @see objectPath.ensureExists
123 */
124 ensureExists<TResult>(path: Path, defaultValue: TResult): TResult;
125 ensureExists<TResult = any>(path: Path, defaultValue?: TResult): TResult | undefined;
126
127 /**
128 * @see objectPath.insert
129 */
130 insert(path: Path, value: any, at?: number): void;
131 }
132}
133
134export = objectPath;