forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object-path.d.ts
249 lines (223 loc) · 6.92 KB
/
object-path.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Type definitions for objectPath v0.9.x
// Project: https://github.com/mariocasciaro/object-path
// Definitions by: Paulo Cesar <https://github.com/pocesar/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare var objectPath: ObjectPathGlobal.IObjectPathStatic;
declare module ObjectPathGlobal {
type IPath = Array<number|string>|number|string;
type IMultiArray = Array<IPath>;
interface IObjectPathStatic {
/**
* Binds an object
*/
<T extends {}>(object: T): IObjectPathBound<T>;
/*======== Del =========*/
/**
* Deletes a member from object or array
* @param {object} object
* @param {string[]|string} path
* @return object
*/
del<T extends {}>(object: T, path: IPath): T;
/**
* @see objectPath.del
*/
del<T extends {}>(object: T):T;
/**
* @see objectPath.del
*/
del():void;
/*======== Has =========*/
/**
* Tests path existence
* @param {object} object
* @param {string[]|string} path
* @return object
*/
has<T extends {}>(object: T, path: IPath): boolean;
/**
* @see objectPath.has
*/
has<T extends {}>(object: T): boolean;
/**
* @see objectPath.has
*/
has(): boolean;
/*======== Get =========*/
/**
* Get a path from an object
* @param {object} object
* @param {string|string[]|number|number[]} path
* @param {*} [defaultValue=undefined]
*/
get<T extends {}, TResult>(object: T, path: IPath, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get<T extends {}>(object: T): T;
/**
* @see objectPath.get
*/
get():void;
/*======== Set =========*/
/**
* Set a path to a value
* @param {object} object
* @param {string|string[]|number|number[]} path
* @param {*} value
* @param {boolean} [doNotReplace=false]
* @return Any existing value on the path if any
*/
set<T extends {}, TExisting>(object: T, path: IPath, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set<T extends {}>(object: T): T;
/**
* @see objectPath.set
*/
set():void;
/*======== Push =========*/
/**
* Create (if path isn't an array) and push the value to it. Can push unlimited number of values
* @param {object} object
*/
push<T extends {}>(object: T, path: IPath, ...args:any[]):void;
/**
* @see objectPath.push
*/
push():void;
/*======== Coalesce =========*/
/**
* Get the first non undefined property
* @param {object} object
* @param {string[]|string[][]|number[]|number[][]} paths
* @param {*} defaultValue
* @return {*}
*/
coalesce<T extends {}, TResult>(object: T, paths: IMultiArray, defaultValue?: TResult):TResult;
/*======== Empty =========*/
/**
* Empty a path. Arrays are set to length 0, objects have all elements deleted, strings
* are set to empty, numbers to 0, everything else is set to null
* @param {object} object
* @param {string|string[]|number[]} path
*/
empty<T extends {}, TResult>(object: T, path: IPath):TResult;
/**
* @see objectPath.empty
*/
empty<T extends {}>(object: T):T;
/**
* @see objectPath.empty
*/
empty():void;
/*======== EnsureExists =========*/
/**
* Set a value if it doesn't exist, do nothing if it does
* @param {object} object
* @param {string|string[]|number|number[]} path
*/
ensureExists<T extends {}, TExisting>(object: T, path: IPath, value: any):TExisting;
/**
* @see objectPath.ensureExists
*/
ensureExists<T extends {}>(object: T): T;
/**
* @see objectPath.ensureExists
*/
ensureExists():void;
/*======== Insert =========*/
/**
* Insert an item in an array path
* @param {object} object
* @param {string|string[]|number|number[]} path
* @param {*} value
* @param {number} [at=0]
*/
insert<T extends {}>(object: T, path: IPath, value: any, at?: number):void;
}
interface IObjectPathBound<T extends {}> {
/*======== Del =========*/
/**
* @see objectPath.ensureExists
*/
del(path: IPath): T;
/**
* @see objectPath.del
*/
del(): T;
/*======== Has =========*/
/**
* @see objectPath.ensureExists
*/
has(path: IPath): boolean;
/**
* @see objectPath.has
*/
has(): boolean;
/*======== Get =========*/
/**
* @see objectPath.ensureExists
*/
get<TResult>(path: IPath, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get(): T;
/*======== Set =========*/
/**
* @see objectPath.ensureExists
*/
set<TExisting>(path: IPath, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set(): T;
/*======== Push =========*/
/**
* @see objectPath.ensureExists
*/
push(path: IPath, ...args:any[]):void;
/**
* @see objectPath.push
*/
push():void;
/*======== Coalesce =========*/
/**
* @see objectPath.ensureExists
*/
coalesce<TResult>(paths: IMultiArray, defaultValue?: TResult):TResult;
/*======== Empty =========*/
/**
* @see objectPath.ensureExists
*/
empty(path: IPath):T;
/**
* @see objectPath.empty
*/
empty():T;
/*======== EnsureExists =========*/
/**
* @see objectPath.ensureExists
*/
ensureExists<TExisting>(path: IPath, value: any):TExisting;
/**
* @see objectPath.ensureExists
*/
ensureExists(): T;
/*======== Insert =========*/
/**
* @see objectPath.insert
*/
insert(path: IPath, value: any, at?: number):void;
}
}
// browser version
declare module 'objectPath' {
export = objectPath;
}
// node version
declare module 'object-path' {
export = objectPath;
}