UNPKG

3.11 kBTypeScriptView Raw
1import {Get} from 'type-fest';
2
3/**
4Get 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```
12import {getProperty} from 'dot-prop';
13
14getProperty({foo: {bar: 'unicorn'}}, 'foo.bar');
15//=> 'unicorn'
16
17getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep');
18//=> undefined
19
20getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
21//=> 'default value'
22
23getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
24//=> 'unicorn'
25
26getProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar');
27//=> 'unicorn'
28```
29*/
30export 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/**
37Set 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```
46import {setProperty} from 'dot-prop';
47
48const object = {foo: {bar: 'a'}};
49setProperty(object, 'foo.bar', 'b');
50console.log(object);
51//=> {foo: {bar: 'b'}}
52
53const foo = setProperty({}, 'foo.bar', 'c');
54console.log(foo);
55//=> {foo: {bar: 'c'}}
56
57setProperty(object, 'foo.baz', 'x');
58console.log(object);
59//=> {foo: {bar: 'b', baz: 'x'}}
60
61setProperty(object, 'foo.biz[0]', 'a');
62console.log(object);
63//=> {foo: {bar: 'b', baz: 'x', biz: ['a']}}
64```
65*/
66export function setProperty<ObjectType extends Record<string, any>>(
67 object: ObjectType,
68 path: string,
69 value: unknown
70): ObjectType;
71
72/**
73Check 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```
80import {hasProperty} from 'dot-prop';
81
82hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar');
83//=> true
84```
85*/
86export function hasProperty(object: Record<string, any> | undefined, path: string): boolean;
87
88/**
89Delete 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```
97import {deleteProperty} from 'dot-prop';
98
99const object = {foo: {bar: 'a'}};
100deleteProperty(object, 'foo.bar');
101console.log(object);
102//=> {foo: {}}
103
104object.foo.bar = {x: 'y', y: 'x'};
105deleteProperty(object, 'foo.bar.x');
106console.log(object);
107//=> {foo: {bar: {y: 'x'}}}
108```
109*/
110export function deleteProperty(object: Record<string, any>, path: string): boolean;