1 | import {Get} from 'type-fest';
|
2 |
|
3 | /**
|
4 | Get the value of the property at the given path.
|
5 |
|
6 | @param object - Object or array to get the `path` value.
|
7 | @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
|
8 | @param defaultValue - Default value.
|
9 |
|
10 | @example
|
11 | ```
|
12 | import {getProperty} from 'dot-prop';
|
13 |
|
14 | getProperty({foo: {bar: 'unicorn'}}, 'foo.bar');
|
15 | //=> 'unicorn'
|
16 |
|
17 | getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep');
|
18 | //=> undefined
|
19 |
|
20 | getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
|
21 | //=> 'default value'
|
22 |
|
23 | getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
|
24 | //=> 'unicorn'
|
25 |
|
26 | getProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar');
|
27 | //=> 'unicorn'
|
28 | ```
|
29 | */
|
30 | export function getProperty<ObjectType, PathType extends string, DefaultValue = undefined>(
|
31 | object: ObjectType,
|
32 | path: PathType,
|
33 | defaultValue?: DefaultValue
|
34 | ): ObjectType extends Record<string, unknown> | unknown[] ? (unknown extends Get<ObjectType, PathType> ? DefaultValue : Get<ObjectType, PathType>) : undefined;
|
35 |
|
36 | /**
|
37 | Set the property at the given path to the given value.
|
38 |
|
39 | @param object - Object or array to set the `path` value.
|
40 | @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
|
41 | @param value - Value to set at `path`.
|
42 | @returns The object.
|
43 |
|
44 | @example
|
45 | ```
|
46 | import {setProperty} from 'dot-prop';
|
47 |
|
48 | const object = {foo: {bar: 'a'}};
|
49 | setProperty(object, 'foo.bar', 'b');
|
50 | console.log(object);
|
51 | //=> {foo: {bar: 'b'}}
|
52 |
|
53 | const foo = setProperty({}, 'foo.bar', 'c');
|
54 | console.log(foo);
|
55 | //=> {foo: {bar: 'c'}}
|
56 |
|
57 | setProperty(object, 'foo.baz', 'x');
|
58 | console.log(object);
|
59 | //=> {foo: {bar: 'b', baz: 'x'}}
|
60 |
|
61 | setProperty(object, 'foo.biz[0]', 'a');
|
62 | console.log(object);
|
63 | //=> {foo: {bar: 'b', baz: 'x', biz: ['a']}}
|
64 | ```
|
65 | */
|
66 | export function setProperty<ObjectType extends Record<string, any>>(
|
67 | object: ObjectType,
|
68 | path: string,
|
69 | value: unknown
|
70 | ): ObjectType;
|
71 |
|
72 | /**
|
73 | Check whether the property at the given path exists.
|
74 |
|
75 | @param object - Object or array to test the `path` value.
|
76 | @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
|
77 |
|
78 | @example
|
79 | ```
|
80 | import {hasProperty} from 'dot-prop';
|
81 |
|
82 | hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar');
|
83 | //=> true
|
84 | ```
|
85 | */
|
86 | export function hasProperty(object: Record<string, any> | undefined, path: string): boolean;
|
87 |
|
88 | /**
|
89 | Delete the property at the given path.
|
90 |
|
91 | @param object - Object or array to delete the `path` value.
|
92 | @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
|
93 | @returns A boolean of whether the property existed before being deleted.
|
94 |
|
95 | @example
|
96 | ```
|
97 | import {deleteProperty} from 'dot-prop';
|
98 |
|
99 | const object = {foo: {bar: 'a'}};
|
100 | deleteProperty(object, 'foo.bar');
|
101 | console.log(object);
|
102 | //=> {foo: {}}
|
103 |
|
104 | object.foo.bar = {x: 'y', y: 'x'};
|
105 | deleteProperty(object, 'foo.bar.x');
|
106 | console.log(object);
|
107 | //=> {foo: {bar: {y: 'x'}}}
|
108 | ```
|
109 | */
|
110 | export function deleteProperty(object: Record<string, any>, path: string): boolean;
|
111 |
|
112 | /**
|
113 | Escape special characters in a path. Useful for sanitizing user input.
|
114 |
|
115 | @param path - The dot path to sanitize.
|
116 |
|
117 | @example
|
118 | ```
|
119 | import {getProperty, escapePath} from 'dot-prop';
|
120 |
|
121 | const object = {
|
122 | foo: {
|
123 | bar: 'πΈπ» You found me Mario!',
|
124 | },
|
125 | 'foo.bar' : 'π The princess is in another castle!',
|
126 | };
|
127 | const escapedPath = escapePath('foo.bar');
|
128 |
|
129 | console.log(getProperty(object, escapedPath));
|
130 | //=> 'π The princess is in another castle!'
|
131 | ```
|
132 | */
|
133 | export function escapePath(path: string): string;
|
134 |
|
135 | /**
|
136 | Returns an array of every path. Plain objects are deeply recursed and are not themselves included.
|
137 |
|
138 | This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
|
139 |
|
140 | @param object - The object to iterate through.
|
141 |
|
142 | @example
|
143 | ```
|
144 | import {getProperty, deepKeys} from 'dot-prop';
|
145 |
|
146 | const user = {
|
147 | name: {
|
148 | first: 'Richie',
|
149 | last: 'Bendall',
|
150 | },
|
151 | };
|
152 |
|
153 | for (const property of deepKeys(user)) {
|
154 | console.log(`${property}: ${getProperty(user, property)}`);
|
155 | //=> name.first: Richie
|
156 | //=> name.last: Bendall
|
157 | }
|
158 | ```
|
159 | */
|
160 | export function deepKeys(object: unknown): string[];
|