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[] ? (Get<ObjectType, PathType> extends unknown ? 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;
|