29 | /**
30 | * Returns the library itself; only useful for internal purposes such as testing
31 | * @ignore
32 | * @returns {DottieLib}
33 | */
34 | function lib() {
35 | const {Dottie} = Import;
36 | return Dottie;
37 | }
38 |
39 |
40 | /**
41 | * Sets `value` at the location in `obj` as determined by `path`
42 | * @param {Object} obj
43 | * @param {String} path
44 | * @param {Any} value
45 | * @returns {Object}
46 | * @example
47 | const path = 'path.to.key';
48 | let obj = {};
49 | obj = dottie.set(obj, path, 'value');
50 | Logger.log(obj.path.to.key); // 'value'
51 | */
52 | function set(obj, path, value) {
53 | const {Dottie} = Import;
54 | return Dottie.set({path, value, obj});
55 | }
56 |
57 |
58 | /**
59 | * Returns the value at location indicated by `path` of `obj`
60 | * @param {Object} obj
61 | * @param {String} path
62 | * @return {Any}
63 | * @example
64 | const path = 'path.to.key';
65 | let obj = {};
66 | obj = dottie.set(obj, path, 'value');
67 | const result = dottie.get(obj, path);
68 | Logger.log(result); // 'value'
69 | */
70 | function get(obj, path) {
71 | const {Dottie} = Import;
72 | return Dottie.get({path, obj});
73 | }
74 |
75 |
76 | /**
77 | * Move a property within one object to another location. In-place operation, returns obj. If sourcePath is undefined, nothing changed
78 | * @param {Object} obj
79 | * @param {String} sourcePath
80 | * @param {String} destPath
81 | * @return {Object}
82 | * @example
83 | const path = 'path.to.key';
84 | const diffPath = 'a.different.path.to.key';
85 | let obj = {};
86 | obj = dottie.set(obj, path, 'value');
87 | obj = dottie.move(obj, path, diffPath);
88 | const first = dottie.get(obj, path);
89 | const second = dottie.get(obj, diffPath);
90 | Logger.log(first); // undefined
91 | Logger.log(second); // 'value'
92 | */
93 | function move(obj, sourcePath, destPath) {
94 | const {Dottie} = Import;
95 | return Dottie.move({sourcePath, destPath, obj})
96 | }
97 |
98 |
99 | /**
100 | * Copy property from one object to another object. If sourcePath is undefined, nothing changed. It returns the destination object, but the operation is in-place.
101 | * @param {Object} obj
102 | * @param {String} sourcePath
103 | * @param {Object} target
104 | * @param {String} destPath
105 | * @returns {Object}
106 | * @example
107 | const path = 'path.to.key';
108 | const source = dottie.set({}, path, 'value');
109 | const dest = {};
110 | dottie.copy(source, path, dest, 'new.path.to.key');
111 | const present = dottie.get(source, path);
112 | const value = dottie.get(dest, path);
113 | Logger.log(present); // undefined
114 | Logger.log(value); // 'value'
115 | */
116 | function copy (obj, sourcePath, target, destPath) {
117 | const {Dottie} = Import;
118 | return Dottie.copy({sourcePath, destPath, obj, target});
119 | }
120 |
121 |
122 | /**
123 | * Transfer property from one object to another. Removes from sourceObject. If sourcePath is undefined, nothing happens. Same as copy except does not remove from source.
124 | * @param {Object} obj
125 | * @param {String} sourcePath
126 | * @param {Object} target
127 | * @param {String} destPath
128 | * @return {Object}
129 | * @example
130 | const path = 'path.to.key';
131 | const source = dottie.set({}, path, 'value');
132 | const dest = {};
133 | dottie.copy(source, dest, path, 'new.path.to.key');
134 | const present = dottie.get(source, path);
135 | const value = dottie.get(dest, path);
136 | Logger.log(present); // undefined
137 | Logger.log(value); // 'value'
138 | */
139 | function transfer (obj, sourcePath, target, destPath) {
140 | const {Dottie} = Import;
141 | return Dottie.transfer({sourcePath, destPath, obj, target});
142 | }
143 |
144 |
145 | /**
146 | * Converts an object with dotted-key/value pairs to it's expanded/normal version
147 | * @param {Object} obj
148 | * @return {Object}
149 | */
150 | function expand (obj) {
151 | const {Dottie} = Import;
152 | return Dottie.expand({obj});
153 | }
154 |
155 |
156 | /**
157 | * Remove a value using dot notation (and keep array indexes)
158 | * @param {Object} obj
159 | * @param {String} path
160 | * @return {Any}
161 | */
162 | function remove(obj, path) {
163 | const {Dottie} = Import;
164 | return Dottie.remove({obj, path});
165 | }
166 |
167 |
168 | /**
169 | * Delete a value using dot notation (and adjust array indexes)
170 | * @param {Object} obj
171 | * @param {Object} path
172 | * @return {Object}
173 | */
174 | function delete_(obj, path) {
175 | const {Dottie} = Import;
176 | return Dottie.delete_({obj, path});
177 | }
178 |
179 |
180 | /**
181 | * Transform properties
182 | * @param {Object} obj
183 | * @param {Object} recipe
184 | * return {Object}
185 | */
186 | function transform(obj, recipe) {
187 | const {Dottie} = Import;
188 | return Dottie.transform({recipe, obj});
189 | }
190 |
191 |
192 | /**
193 | * Convert object to dotted-key/value pair
194 | * @param {Object} obj
195 | * @return {Object}
196 | */
197 | function dot(obj) {
198 | const {Dottie} = Import;
199 | return Dottie.dot({obj});
200 | }
201 |
202 |
203 | /**
204 | * Convert an array of jsons to a 2d array that can be used to populate a Google Spreadsheet with unique headers. The retuned object's first element is an array of header values whose string values are derived via path notation, and any nested objects or arrays follow the naming convention provided by dots (for objects) and brackets (for arrays).
205 | * @param {Object[]} jsons - An array of objects, which can contain native values or nested objects with native values
206 | * @param {Array} [priorityHeaders=[]] - A list of headers you want to appear first as the headers. Note, if header does not appear in any row, it will not appear at all even though you've specified it as priority.
207 | * @param {Boolean} [deleteNulls=false] - If true, do not include any columns whose values are null
208 | * @param {Boolean} [deleteEmptyArrays=true] - If true, make sure that columns that have empty arrays are not shown as null fields
209 | * @return {Array[]}
210 | * @example
211 | // simple example
212 | const jsons = [{
213 | obj: {
214 | key: 'value'
215 | },
216 | arr: [1, 2]
217 | }];
218 | const result = dottie.jsonsToRows(jsons);
219 | Logger.log(result);
220 | // [ ['obj.key', 'arr[0]', 'arry[1]'],
221 | // ['value', 1, 2]
222 | * @example
223 | // example with null objects and empty arrays
224 | const jsons = [{
225 | obj: {
226 | key: null
227 | },
228 | arr: []
229 | }, true, true];
230 | const result = dottie.jsonsToRows(jsons);
231 | Logger.log(result);
232 | // [
233 | // ['obj.key'],
234 | // [null]
235 | // ]
236 | */
237 | function jsonsToRows (jsons, priorityHeaders=[], deleteNulls=false, deleteEmptyArrays=true) {
238 | const {Dottie} = Import;
239 | return Dottie.jsonsToRows({jsons, priorityHeaders, deleteNulls, deleteEmptyArrays});
240 | }
241 |
242 |
243 | /**
244 | *
245 | * @param {Array[]} rows - first item is array string headers in dot notation, each subsequent row represents an item whose values coorespond to the header's path
246 | * @return {Object[]}
247 | * @example
248 | const headers = ['one', 'two.value', 'arr[0].name'];
249 | const rows = [
250 | 1, 2, 'Name'
251 | ];
252 | const jsons = dottie.rowsToJsons([headers, ...rows]);
253 | Logger.log(jsons);
254 | // [{
255 | one: 1,
256 | two: {
257 | value: 2
258 | },
259 | arr: [
260 | {
261 | name: 'Name'
262 | }
263 | ]
264 | }]
265 | */
266 |
267 | function rowsToJsons(rows) {
268 | const {Dottie} = Import;
269 | return Dottie.rowsToJsons({rows});
270 | }
271 |
272 |
273 | /**
274 | * Alternative way to utilize library, where you use methods augmented on the Object and Array prototype, and use named parameters on the method calls. Optional "advanced use" mode.
275 | * @param {Object} Object - Pass in `Object`
276 | * @param {Array} Array - Pass in `Array`
277 | * @example
278 | dottie.augment(Object, Array);
279 | const path = 'path.to.key';
280 | // now can use methods on objects and arrays
281 | const obj = {}.dottie.set({path, value: 'value'});
282 | const arr = [].dottie.jsonsToRows({jsons: [{'key': 'value'}]});
283 | Logger.log(obj.dottie.get({path})); // 'value'
284 | */
285 | function augment (Object, Array) {
286 | const {Dottie} = Import;
287 | Dottie.augment(Object, Array);
288 | }
289 |
290 |
291 |