UNPKG

6.86 kBTypeScriptView Raw
1// Type definitions for Dot-Object 2.1
2// Project: https://github.com/rhalff/dot-object
3// Definitions by: Niko Kovačič <https://github.com/nkovacic>
4// Rico Sandyca <https://github.com/ricosandyca>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6// TypeScript Version: 2.2
7
8
9declare namespace DotObject {
10 interface DotConstructor extends Dot {
11 new (
12 separator: string,
13 override?: boolean,
14 useArray?: boolean,
15 useBrackets?: boolean
16 ): Dot;
17 }
18
19 interface ModifierFunctionWrapper {
20 (arg: any): any;
21 }
22
23 interface Dot {
24 /**
25 *
26 * Copy a property from one object to another object.
27 *
28 * If the source path does not exist (undefined)
29 * the property on the other object will not be set.
30 *
31 * @param {String} source
32 * @param {String} target
33 * @param {Object} obj1
34 * @param {Object} obj2
35 * @param {Function|Array} mods
36 * @param {Boolean} merge
37 */
38 copy(source: string, target: string, obj1: any, obj2: any, mods?: ModifierFunctionWrapper | Array<ModifierFunctionWrapper>, merge?: boolean): void;
39 /**
40 *
41 * Convert object to dotted-key/value pair
42 *
43 * Usage:
44 * var tgt = {}
45 * dot.dot(obj, tgt)
46 *
47 * @param {Object} obj source object
48 * @param {Object} tgt target object
49 */
50 dot(obj: any, tgt: any): void
51 /**
52 *
53 * Convert object to dotted-key/value pair
54 *
55 * Usage:
56 *
57 * var tgt = dot.dot(obj)
58 * @param {Object} obj source object
59 * @return {Object} result
60 */
61 dot(obj: any): any
62
63 /**
64 *
65 * Remove value from an object using dot notation.
66 *
67 * @param {String | Array<String>} path
68 * @param {Object} obj
69 * @return {Mixed} The removed value
70 */
71 del(path: string | string[], obj: any): any;
72 /**
73 *
74 * Delete value from an object using dot notation.
75 *
76 * @param {String | Array<String>} path
77 * @param {Object} obj
78 * @return {any} The removed value
79 */
80 delete(path: string | string[], obj: any): any;
81 /**
82 *
83 * Keep array
84 *
85 * example:
86 *
87 * var obj = {
88 * "id": "my-id",
89 * "other": [1, 2, 3]
90 * "some": {
91 * "array": ["A", "B"]
92 * }
93 * }
94 *
95 * if the keepArray property is true:
96 *
97 * {
98 * "id": "my-id",
99 * "other": [1, 2, 3],
100 * "some.array": ["A", "B"]
101 * }
102 */
103 keepArray: boolean;
104 /**
105 *
106 * Move a property from one place to the other.
107 *
108 * If the source path does not exist (undefined)
109 * the target property will not be set.
110 *
111 * @param {String} source
112 * @param {String} target
113 * @param {Object} obj
114 * @param {Function|Array} mods
115 * @param {Boolean} merge
116 */
117 move(source: string, target: string, obj: any, mods?: ModifierFunctionWrapper | Array<ModifierFunctionWrapper>, merge?: boolean): void;
118 /**
119 *
120 * Converts an object with dotted-key/value pairs to it's expanded version
121 *
122 * Optionally transformed by a set of modifiers.
123 *
124 * Usage:
125 *
126 * var row = {
127 * 'nr': 200,
128 * 'doc.name': ' My Document '
129 * }
130 *
131 * var mods = {
132 * 'doc.name': [_s.trim, _s.underscored]
133 * }
134 *
135 * dot.object(row, mods)
136 *
137 * @param {Object} obj
138 * @param {Object} mods
139 */
140 object(obj: object, mods?: ModifierFunctionWrapper | Array<ModifierFunctionWrapper>): object;
141 /**
142 *
143 * Pick a value from an object using dot notation.
144 *
145 * Optionally remove the value
146 *
147 * @param {String} path
148 * @param {Object} obj
149 * @param {Boolean} remove
150 */
151 pick(path: string, obj: any, remove?: boolean): any;
152 /**
153 *
154 * Remove value from an object using dot notation.
155 *
156 * @param {String | Array<String>} path
157 * @param {Object} obj
158 * @return {Mixed} The removed value
159 */
160 remove(path: string | string[], obj: any): any;
161 /**
162 *
163 * Replace/create with a string
164 *
165 * @param {String} path dotted path
166 * @param {String} v value to be set
167 * @param {Object} obj object to be modified
168 * @param {Function|Array} mods optional modifier
169 */
170 str(path: string, v: any, obj: object, mods?: ModifierFunctionWrapper | Array<ModifierFunctionWrapper>): void;
171 /**
172 *
173 * Replace/merge an object to an existing object property
174 *
175 * @param {String} path dotted path
176 * @param {Object} v object to be set
177 * @param {Object} obj object to be modified
178 * @param {Boolean} merge optional merge
179 */
180 set(path: string, v: any, obj: object, merge?: boolean): void;
181 /**
182 *
183 * Transfer a property from one object to another object.
184 *
185 * If the source path does not exist (undefined)
186 * the property on the other object will not be set.
187 *
188 * @param {String} source
189 * @param {String} target
190 * @param {Object} obj1
191 * @param {Object} obj2
192 * @param {Function|Array} mods
193 * @param {Boolean} merge
194 */
195 transfer(source: string, target: string, obj1: any, obj2: any, mods?: ModifierFunctionWrapper | Array<ModifierFunctionWrapper>, merge?: boolean): void;
196 /**
197 *
198 * Transform an object
199 *
200 * Usage:
201 *
202 * var obj = {
203 * "id": 1,
204 * "some": {
205 * "thing": "else"
206 * }
207 * }
208 *
209 * var transform = {
210 * "id": "nr",
211 * "some.thing": "name"
212 * }
213 *
214 * var tgt = dot.transform(transform, obj)
215 *
216 * @param {Object} recipe Transform recipe
217 * @param {Object} obj Object to be transformed
218 * @param {Array} mods modifiers for the target
219 */
220 transform(recipe: any, obj: any, mods?: ModifierFunctionWrapper | Array<ModifierFunctionWrapper>): void;
221 }
222}
223
224declare var dot: DotObject.DotConstructor;
225
226declare module 'dot-object' {
227 export = dot;
228}