1
1
# nested-objects-util
2
2
3
- A module to filter and diff complex nested objects having circular references.
3
+ A minimalist inspection module to filter and diff complex nested objects having circular references.
4
4
5
5
It was implemented to filter out some values from inconveniently nested objects with circular references and then diff them.
6
6
7
- It's designed to work both on nodejs and browser without any dependencies.
7
+ It's designed to work both on nodejs and browser without any dependencies or additional polyfills.
8
+
9
+ Performance is currently not the key feature of this module, so please use more optimised libraries for e.g. deep equal.
8
10
9
11
## Installation
10
12
11
13
```
12
14
npm install --save nested-objects-util
15
+
16
+ yarn add nested-objects-util
13
17
```
14
18
19
+ Or just copy ` index.min.js ` into the console and start using ` NestedObjectsUtil ` object.
20
+
15
21
## Usage
16
22
17
23
``` js
18
- const nestedObjectsUtil = require (' nested-objects-util' );
24
+ const nestedObjectsUtil = require (' nested-objects-util' )
19
25
```
20
26
21
- #### nestedObjectsUtil.flatten(Object : object, Boolean: sortKeysFlag = false): Object
27
+ #### nestedObjectsUtil.flatten(object : object, sortKeysFlag = false): object
22
28
23
29
Flatten object by keys composed from own nested properties.
24
30
25
31
``` js
26
32
nestedObjectsUtil .flatten ({
27
33
keyA: {
28
- keyE: [ ' value3' , ' value4' ],
34
+ keyE: [' value3' , ' value4' ],
29
35
keyF: null ,
30
36
keyD: ' value2' ,
31
37
keyB: {
32
38
keyC: ' value'
33
39
}
34
40
}
35
- });
41
+ })
36
42
```
37
43
38
44
returns:
@@ -59,15 +65,15 @@ with sortKeys=true it would return:
59
65
}
60
66
```
61
67
62
- #### nestedObjectsUtil.unflatten(Object : object): Object
68
+ #### nestedObjectsUtil.unflatten(object : object): object
63
69
64
70
Unflatten object by keys composed from own nested properties.
65
71
66
72
``` js
67
73
nestedObjectsUtil .unflatten ({
68
74
' keyA.keyB.keyC' : ' value' ,
69
75
' keyA.keyD' : ' value2'
70
- });
76
+ })
71
77
```
72
78
73
79
returns:
@@ -79,11 +85,11 @@ returns:
79
85
" keyC" : " value"
80
86
},
81
87
" keyD" : " value2"
82
- }
83
88
}
89
+ }
84
90
```
85
91
86
- #### nestedObjectsUtil.accessProperty(String | Array: keys, Object: parent = global | window): Various
92
+ #### nestedObjectsUtil.accessProperty(keys: string|string [ ] , parent = global window): any
87
93
88
94
Access object's nested property.
89
95
@@ -94,8 +100,8 @@ const nestedObject = {
94
100
keyC: ' value'
95
101
}
96
102
}
97
- };
98
- nestedObjectsUtil .accessProperty (' keyA.keyB.keyC' , nestedObject);
103
+ }
104
+ nestedObjectsUtil .accessProperty (' keyA.keyB.keyC' , nestedObject)
99
105
```
100
106
101
107
returns:
@@ -104,15 +110,15 @@ returns:
104
110
" value"
105
111
```
106
112
107
- #### nestedObjectsUtil.discardCircular(Object : object, Boolean: stringifyFlag = false): Object | String
113
+ #### nestedObjectsUtil.discardCircular(object : object, stringifyFlag = false): object|string
108
114
109
115
Discard circular references (to avoid "Converting circular structure to JSON" error).
110
116
111
117
``` js
112
118
const a = {
113
119
b: 1
114
- };
115
- a .c = a;
120
+ }
121
+ a .c = a
116
122
nestedObjectsUtil .discardCircular (a)
117
123
```
118
124
@@ -125,7 +131,7 @@ returns:
125
131
}
126
132
```
127
133
128
- #### nestedObjectsUtil.filterValue(Object : object, Various | Array: query, Boolean: flattenFlag = false): Object
134
+ #### nestedObjectsUtil.filterValue(object : object, query: any|any [ ] , flattenFlag = false): object
129
135
130
136
Filter a nested object by value or values (if array passed). Strict comparison is performed.
131
137
@@ -143,10 +149,10 @@ const a = {
143
149
},
144
150
j: ' str4'
145
151
},
146
- k: [ ' str' , ' str5' ]
147
- };
148
- a .l = a .b ;
149
- nestedObjectsUtil .filterValue (a, ' str' );
152
+ k: [' str' , ' str5' ]
153
+ }
154
+ a .l = a .b
155
+ nestedObjectsUtil .filterValue (a, ' str' )
150
156
```
151
157
152
158
returns:
@@ -162,14 +168,14 @@ returns:
162
168
" h" : " str"
163
169
}
164
170
},
165
- " k" : [ " str" ]
171
+ " k" : [" str" ]
166
172
}
167
173
```
168
174
169
175
or with flattenFlag = true
170
176
171
177
``` js
172
- nestedObjectsUtil .filterValue (a, ' str' , true );
178
+ nestedObjectsUtil .filterValue (a, ' str' , true )
173
179
```
174
180
175
181
returns:
@@ -183,7 +189,7 @@ returns:
183
189
}
184
190
```
185
191
186
- #### nestedObjectsUtil.downloadStringified(Object : object, Number: space = 2): String | undefined
192
+ #### nestedObjectsUtil.downloadStringified(object : object, space = 2): string| undefined
187
193
188
194
On browser with HTML5 download API: stringify, format and download the object.
189
195
@@ -196,11 +202,11 @@ const a = {
196
202
d: 2 ,
197
203
e: 2
198
204
}
199
- };
200
- a .f = a;
201
- a .g = a .f ;
202
- const obj = nestedObjectsUtil .discardCircular (a);
203
- nestedObjectsUtil .downloadStringified (obj);
205
+ }
206
+ a .f = a
207
+ a .g = a .f
208
+ const obj = nestedObjectsUtil .discardCircular (a)
209
+ nestedObjectsUtil .downloadStringified (obj)
204
210
```
205
211
206
212
returns:
@@ -217,9 +223,9 @@ returns:
217
223
}
218
224
```
219
225
220
- #### nestedObjectsUtil.areObjectsEqual(Object: objectA, Object: objectB ): Boolean
226
+ #### nestedObjectsUtil.areObjectsEqual(objectA: object, objectB: object, skipProperties?: string [ ] ): boolean
221
227
222
- Compare two objects against each other (by JSON.stringify) after discarding circular references, flattening and ordering keys.
228
+ Compare two objects against each other after discarding circular references, flattening and ordering keys.
223
229
224
230
``` js
225
231
const objectA = {
@@ -228,21 +234,21 @@ const objectA = {
228
234
keyC: ' value'
229
235
},
230
236
keyD: ' value2' ,
231
- keyE: [ ' value3' , ' value4' ]
237
+ keyE: [' value3' , ' value4' ]
232
238
}
233
- };
234
- objectA .circular = objectA;
239
+ }
240
+ objectA .circular = objectA
235
241
const objectB = {
236
242
keyA: {
237
- keyE: [ ' value3' , ' value4' ],
243
+ keyE: [' value3' , ' value4' ],
238
244
keyD: ' value2' ,
239
245
keyB: {
240
246
keyC: ' value'
241
247
}
242
248
}
243
- };
244
- objectB .circular = objectB;
245
- nestedObjectsUtil .areObjectsEqual (objectA, objectB);
249
+ }
250
+ objectB .circular = objectB
251
+ nestedObjectsUtil .areObjectsEqual (objectA, objectB)
246
252
```
247
253
248
254
returns:
@@ -251,7 +257,39 @@ returns:
251
257
true
252
258
```
253
259
254
- #### nestedObjectsUtil.getObjectsDiff(Object: objectA, Object: objectB, Boolean: sortKeysFlag = false, Boolean: flattenFlag = false): Object
260
+ It is also possible to skip certain properties when comparing the objects by providing an array to string properties.
261
+
262
+ ``` js
263
+ const objectA = {
264
+ keyA: {
265
+ keyB: {
266
+ keyC: ' value'
267
+ },
268
+ keyD: ' value2' ,
269
+ keyE: [' value3' , ' value4' ]
270
+ }
271
+ }
272
+ objectA .circular = objectA
273
+ const objectB = {
274
+ keyA: {
275
+ keyB: {
276
+ keyC: ' DIFFERENT_VALUE'
277
+ },
278
+ keyD: ' value2' ,
279
+ keyE: [' value3' , ' value4' ]
280
+ }
281
+ }
282
+ objectB .circular = objectB
283
+ nestedObjectsUtil .areObjectsEqual (objectA, objectB, [' keyA.keyB.keyC' ])
284
+ ```
285
+
286
+ returns:
287
+
288
+ ``` js
289
+ true
290
+ ```
291
+
292
+ #### nestedObjectsUtil.getObjectsDiff(objectA: object, objectB: object, sortKeysFlag = false, flattenFlag = false): object
255
293
256
294
Get the properties which differ between object A and object B and return only those from object B.
257
295
@@ -262,21 +300,21 @@ const objectA = {
262
300
keyC: ' value'
263
301
},
264
302
keyD: ' value2' ,
265
- keyE: [ ' value3' ]
303
+ keyE: [' value3' ]
266
304
}
267
- };
268
- objectA .circular = objectA;
305
+ }
306
+ objectA .circular = objectA
269
307
const objectB = {
270
308
keyA: {
271
309
keyB: {
272
310
keyC: ' value'
273
311
},
274
312
keyD: ' value2_CHANGED' ,
275
- keyE: [ ' value3_CHANGED' ]
313
+ keyE: [' value3_CHANGED' ]
276
314
}
277
- };
278
- objectB .circular = objectB;
279
- nestedObjectsUtil .getObjectsDiff (objectA, objectB);
315
+ }
316
+ objectB .circular = objectB
317
+ nestedObjectsUtil .getObjectsDiff (objectA, objectB)
280
318
```
281
319
282
320
returns:
@@ -285,7 +323,7 @@ returns:
285
323
{
286
324
" keyA" : {
287
325
" keyD" : " value2_CHANGED" ,
288
- " keyE" : [ " value3_CHANGED" ]
326
+ " keyE" : [" value3_CHANGED" ]
289
327
}
290
328
}
291
329
```
@@ -297,22 +335,24 @@ In the browser, NestedObjectsUtil object is exposed either to window or with AMD
297
335
Filter out 'abcd' value from the flattened object and download stringified JSON via HTML5 API with:
298
336
299
337
``` js
300
- const object = NestedObjectsUtil .filterValue (App .SomeHugeObject , ' abcd' , true );
301
- NestedObjectsUtil .downloadStringified (object);
338
+ const object = NestedObjectsUtil .filterValue (App .SomeHugeObject , ' abcd' , true )
339
+ NestedObjectsUtil .downloadStringified (object)
302
340
```
303
341
304
342
Discar circular references from the object, then flatten and download it.
305
343
306
344
``` js
307
- let object = NestedObjectsUtil .discardCircular (App .SomeHugeObject );
308
- object = NestedObjectsUtil .flatten (object);
309
- NestedObjectsUtil .downloadStringified (object);
345
+ let object = NestedObjectsUtil .discardCircular (App .SomeHugeObject )
346
+ object = NestedObjectsUtil .flatten (object)
347
+ NestedObjectsUtil .downloadStringified (object)
310
348
```
311
349
312
350
## Test
313
351
314
352
```
315
353
npm run test
354
+
355
+ yarn test
316
356
```
317
357
318
358
## License
0 commit comments