|
1 | 1 | # nested-objects-util
|
2 | 2 |
|
3 |
| -A module to work with huge nested objects having circular references. |
| 3 | +A module to filter and diff huge nested objects having circular references. |
4 | 4 |
|
5 |
| -It was implemented to filter out some values from huge nested objects with circular references. |
| 5 | +It was implemented to filter out some values from huge nested objects with circular references and then diff them. |
6 | 6 |
|
7 | 7 | It's designed to work both on nodejs and browser.
|
8 | 8 |
|
@@ -18,30 +18,44 @@ npm install --save nested-objects-util
|
18 | 18 | const nestedObjectsUtil = require('nested-objects-util');
|
19 | 19 | ```
|
20 | 20 |
|
21 |
| -#### nestedObjectsUtil.flatten(Object: object): Object |
| 21 | +#### nestedObjectsUtil.flatten(Object: object, Boolean: sortKeysFlag = false): Object |
22 | 22 |
|
23 | 23 | Flatten object by keys composed from own nested properties.
|
24 | 24 |
|
25 | 25 | ```
|
26 | 26 | nestedObjectsUtil.flatten({
|
27 | 27 | keyA: {
|
| 28 | +keyE: [ 'value3', 'value4' ], |
| 29 | +keyF: null, |
| 30 | +keyD: 'value2', |
28 | 31 | keyB: {
|
29 | 32 | keyC: 'value'
|
30 |
| -}, |
31 |
| -keyD: 'value2' |
32 |
| -}, |
33 |
| -keyE: [ 'value3', 'value4' ] |
34 |
| -}) |
| 33 | +} |
| 34 | +} |
| 35 | +}); |
35 | 36 | ```
|
36 | 37 |
|
37 | 38 | returns:
|
38 | 39 |
|
| 40 | +```js |
| 41 | +{ |
| 42 | +"keyA.keyE.0": "value3", |
| 43 | +"keyA.keyE.1": "value4", |
| 44 | +"keyA.keyF": null, |
| 45 | +"keyA.keyD": "value2", |
| 46 | +"keyA.keyB.keyC": "value" |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +with sortKeys=true it would return: |
| 51 | + |
39 | 52 | ```js
|
40 | 53 | {
|
41 | 54 | "keyA.keyB.keyC": "value",
|
42 | 55 | "keyA.keyD": "value2",
|
43 | 56 | "keyA.keyE.0": "value3",
|
44 |
| -"keyA.keyE.1": "value4" |
| 57 | +"keyA.keyE.1": "value4", |
| 58 | +"keyA.keyF": null |
45 | 59 | }
|
46 | 60 | ```
|
47 | 61 |
|
@@ -74,7 +88,7 @@ returns:
|
74 | 88 | Access object's nested property.
|
75 | 89 |
|
76 | 90 | ```
|
77 |
| -var nestedObject = { |
| 91 | +const nestedObject = { |
78 | 92 | keyA: {
|
79 | 93 | keyB: {
|
80 | 94 | keyC: 'value'
|
@@ -90,12 +104,12 @@ returns:
|
90 | 104 | "value"
|
91 | 105 | ```
|
92 | 106 |
|
93 |
| -#### nestedObjectsUtil.discardCircular(Object: object): Object |
| 107 | +#### nestedObjectsUtil.discardCircular(Object: object, Boolean: stringifyFlag = false): Object | String |
94 | 108 |
|
95 | 109 | Discard circular references (to avoid "Converting circular structure to JSON" error).
|
96 | 110 |
|
97 | 111 | ```
|
98 |
| -var a = { |
| 112 | +const a = { |
99 | 113 | b: 1
|
100 | 114 | };
|
101 | 115 | a.c = a;
|
@@ -111,12 +125,12 @@ returns:
|
111 | 125 | }
|
112 | 126 | ```
|
113 | 127 |
|
114 |
| -#### nestedObjectsUtil.filterValue(Object: object, Various: query, Boolean: flattenFlag = false): Object |
| 128 | +#### nestedObjectsUtil.filterValue(Object: object, Various | Array: query, Boolean: flattenFlag = false): Object |
115 | 129 |
|
116 |
| -Filter a nested object by value (with strict comparison performed). |
| 130 | +Filter a nested object by value or values (if array passed). Strict comparison is performed. |
117 | 131 |
|
118 | 132 | ```
|
119 |
| -var a = { |
| 133 | +const a = { |
120 | 134 | b: {
|
121 | 135 | c: 'str',
|
122 | 136 | d: 'str2'
|
@@ -128,9 +142,10 @@ var a = {
|
128 | 142 | i: 'str3'
|
129 | 143 | },
|
130 | 144 | j: 'str4'
|
131 |
| -} |
| 145 | +}, |
| 146 | +k: [ 'str', 'str5' ] |
132 | 147 | };
|
133 |
| -a.k = a.b; |
| 148 | +a.l = a.b; |
134 | 149 | nestedObjectsUtil.filterValue(a, 'str');
|
135 | 150 | ```
|
136 | 151 |
|
@@ -146,11 +161,12 @@ returns:
|
146 | 161 | "g": {
|
147 | 162 | "h": "str"
|
148 | 163 | }
|
149 |
| -} |
| 164 | +}, |
| 165 | +"k": [ "str" ] |
150 | 166 | }
|
151 | 167 | ```
|
152 | 168 |
|
153 |
| -or |
| 169 | +or with flattenFlag = true |
154 | 170 |
|
155 | 171 | ```js
|
156 | 172 | nestedObjectsUtil.filterValue(a, 'str', true);
|
@@ -162,7 +178,8 @@ returns:
|
162 | 178 | {
|
163 | 179 | "b.c": "str",
|
164 | 180 | "e": "str",
|
165 |
| -"f.g.h": "str" |
| 181 | +"f.g.h": "str", |
| 182 | +"k.0": "str" |
166 | 183 | }
|
167 | 184 | ```
|
168 | 185 |
|
@@ -173,7 +190,7 @@ On browser with HTML5 download API: stringify, format and download the object.
|
173 | 190 | Else: return stringified text.
|
174 | 191 |
|
175 | 192 | ```
|
176 |
| -var a = { |
| 193 | +const a = { |
177 | 194 | b: 1,
|
178 | 195 | c: {
|
179 | 196 | d: 2,
|
@@ -182,7 +199,7 @@ var a = {
|
182 | 199 | };
|
183 | 200 | a.f = a;
|
184 | 201 | a.g = a.f;
|
185 |
| -var obj = nestedObjectsUtil.discardCircular(a); |
| 202 | +const obj = nestedObjectsUtil.discardCircular(a); |
186 | 203 | nestedObjectsUtil.downloadStringified(obj);
|
187 | 204 | ```
|
188 | 205 |
|
@@ -200,9 +217,84 @@ returns:
|
200 | 217 | }
|
201 | 218 | ```
|
202 | 219 |
|
| 220 | +#### nestedObjectsUtil.areObjectsEqual(Object: objectA, Object: objectB): Boolean |
| 221 | + |
| 222 | +Compare two objects against each other (by JSON.stringify) after discarding circular references, flattening and ordering keys. |
| 223 | + |
| 224 | +``` |
| 225 | +const objectA = { |
| 226 | +keyA: { |
| 227 | +keyB: { |
| 228 | +keyC: 'value' |
| 229 | +}, |
| 230 | +keyD: 'value2', |
| 231 | +keyE: [ 'value3', 'value4' ] |
| 232 | +} |
| 233 | +}; |
| 234 | +objectA.circular = objectA; |
| 235 | +const objectB = { |
| 236 | +keyA: { |
| 237 | +keyE: [ 'value3', 'value4' ], |
| 238 | +keyD: 'value2', |
| 239 | +keyB: { |
| 240 | +keyC: 'value' |
| 241 | +} |
| 242 | +} |
| 243 | +}; |
| 244 | +objectB.circular = objectB; |
| 245 | +nestedObjectsUtil.areObjectsEqual(objectA, objectB); |
| 246 | +``` |
| 247 | + |
| 248 | +returns: |
| 249 | + |
| 250 | +```js |
| 251 | +true |
| 252 | +``` |
| 253 | + |
| 254 | +#### nestedObjectsUtil.getObjectsDiff(Object: objectA, Object: objectB, Boolean: sortKeysFlag = false, Boolean: flattenFlag = false): Object |
| 255 | + |
| 256 | +Get the properties which differ between object A and object B and return those from object B. |
| 257 | + |
| 258 | +``` |
| 259 | +const objectA = { |
| 260 | +keyA: { |
| 261 | +keyB: { |
| 262 | +keyC: 'value' |
| 263 | +}, |
| 264 | +keyD: 'value2', |
| 265 | +keyE: [ 'value3' ] |
| 266 | +} |
| 267 | +}; |
| 268 | +objectA.circular = objectA; |
| 269 | +const objectB = { |
| 270 | +keyA: { |
| 271 | +keyB: { |
| 272 | +keyC: 'value' |
| 273 | +}, |
| 274 | +keyD: 'value2_CHANGED', |
| 275 | +keyE: [ 'value3_CHANGED' ] |
| 276 | +} |
| 277 | +}; |
| 278 | +objectB.circular = objectB; |
| 279 | +nestedObjectsUtil.getObjectsDiff(objectA, objectB); |
| 280 | +``` |
| 281 | + |
| 282 | +returns: |
| 283 | + |
| 284 | +```js |
| 285 | +{ |
| 286 | +"keyA": { |
| 287 | +"keyD": "value2_CHANGED", |
| 288 | +"keyE": [ "value3_CHANGED" ] |
| 289 | +} |
| 290 | +} |
| 291 | +``` |
| 292 | + |
203 | 293 | ## Example browser usage
|
204 | 294 |
|
205 |
| -Filter out 'abcd' value from the flattened object and download stringified json via HTML5 API with: |
| 295 | +In the browser, NestedObjectsUtil object should be exposed to either window or with AMD. |
| 296 | + |
| 297 | +Filter out 'abcd' value from the flattened object and download stringified JSON via HTML5 API with: |
206 | 298 |
|
207 | 299 | ```js
|
208 | 300 | const object = NestedObjectsUtil.filterValue(App.SomeHugeObject, 'abcd', true);
|
|
0 commit comments