UNPKG

4.24 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[] ? (unknown extends Get<ObjectType, PathType> ? 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;
111
112/**
113Escape special characters in a path. Useful for sanitizing user input.
114
115@param path - The dot path to sanitize.
116
117@example
118```
119import {getProperty, escapePath} from 'dot-prop';
120
121const object = {
122 foo: {
123 bar: 'πŸ‘ΈπŸ» You found me Mario!',
124 },
125 'foo.bar' : 'πŸ„ The princess is in another castle!',
126};
127const escapedPath = escapePath('foo.bar');
128
129console.log(getProperty(object, escapedPath));
130//=> 'πŸ„ The princess is in another castle!'
131```
132*/
133export function escapePath(path: string): string;
134
135/**
136Returns an array of every path. Plain objects are deeply recursed and are not themselves included.
137
138This 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```
144import {getProperty, deepKeys} from 'dot-prop';
145
146const user = {
147 name: {
148 first: 'Richie',
149 last: 'Bendall',
150 },
151};
152
153for (const property of deepKeys(user)) {
154 console.log(`${property}: ${getProperty(user, property)}`);
155 //=> name.first: Richie
156 //=> name.last: Bendall
157}
158```
159*/
160export function deepKeys(object: unknown): string[];