Skip to content

Commit fa7e425

Browse files
committed
modernized code and updated equal functions
Added skipProperties parameter for areObjectsEqual. Added .npmignore file. Added minify to packages scripts. Added more unit tests. Updated getObjectsDiff to handle empty arrays and return an object, even if it’s empty object. Updated readme file.
1 parent 50842ff commit fa7e425

File tree

8 files changed

+742
-728
lines changed

8 files changed

+742
-728
lines changed

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.spec.js
2+
yarn.lock

README.md

+91-51
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
# nested-objects-util
22

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.
44

55
It was implemented to filter out some values from inconveniently nested objects with circular references and then diff them.
66

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.
810

911
## Installation
1012

1113
```
1214
npm install --save nested-objects-util
15+
16+
yarn add nested-objects-util
1317
```
1418

19+
Or just copy `index.min.js` into the console and start using `NestedObjectsUtil` object.
20+
1521
## Usage
1622

1723
```js
18-
const nestedObjectsUtil = require('nested-objects-util');
24+
const nestedObjectsUtil = require('nested-objects-util')
1925
```
2026

21-
#### nestedObjectsUtil.flatten(Object: object, Boolean: sortKeysFlag = false): Object
27+
#### nestedObjectsUtil.flatten(object: object, sortKeysFlag = false): object
2228

2329
Flatten object by keys composed from own nested properties.
2430

2531
```js
2632
nestedObjectsUtil.flatten({
2733
keyA: {
28-
keyE: [ 'value3', 'value4' ],
34+
keyE: ['value3', 'value4'],
2935
keyF: null,
3036
keyD: 'value2',
3137
keyB: {
3238
keyC: 'value'
3339
}
3440
}
35-
});
41+
})
3642
```
3743

3844
returns:
@@ -59,15 +65,15 @@ with sortKeys=true it would return:
5965
}
6066
```
6167

62-
#### nestedObjectsUtil.unflatten(Object: object): Object
68+
#### nestedObjectsUtil.unflatten(object: object): object
6369

6470
Unflatten object by keys composed from own nested properties.
6571

6672
```js
6773
nestedObjectsUtil.unflatten({
6874
'keyA.keyB.keyC': 'value',
6975
'keyA.keyD': 'value2'
70-
});
76+
})
7177
```
7278

7379
returns:
@@ -79,11 +85,11 @@ returns:
7985
"keyC": "value"
8086
},
8187
"keyD": "value2"
82-
}
8388
}
89+
}
8490
```
8591

86-
#### nestedObjectsUtil.accessProperty(String | Array: keys, Object: parent = global | window): Various
92+
#### nestedObjectsUtil.accessProperty(keys: string|string[], parent = global window): any
8793

8894
Access object's nested property.
8995

@@ -94,8 +100,8 @@ const nestedObject = {
94100
keyC: 'value'
95101
}
96102
}
97-
};
98-
nestedObjectsUtil.accessProperty('keyA.keyB.keyC', nestedObject);
103+
}
104+
nestedObjectsUtil.accessProperty('keyA.keyB.keyC', nestedObject)
99105
```
100106

101107
returns:
@@ -104,15 +110,15 @@ returns:
104110
"value"
105111
```
106112

107-
#### nestedObjectsUtil.discardCircular(Object: object, Boolean: stringifyFlag = false): Object | String
113+
#### nestedObjectsUtil.discardCircular(object: object, stringifyFlag = false): object|string
108114

109115
Discard circular references (to avoid "Converting circular structure to JSON" error).
110116

111117
```js
112118
const a = {
113119
b: 1
114-
};
115-
a.c = a;
120+
}
121+
a.c = a
116122
nestedObjectsUtil.discardCircular(a)
117123
```
118124

@@ -125,7 +131,7 @@ returns:
125131
}
126132
```
127133

128-
#### nestedObjectsUtil.filterValue(Object: object, Various | Array: query, Boolean: flattenFlag = false): Object
134+
#### nestedObjectsUtil.filterValue(object: object, query: any|any[], flattenFlag = false): object
129135

130136
Filter a nested object by value or values (if array passed). Strict comparison is performed.
131137

@@ -143,10 +149,10 @@ const a = {
143149
},
144150
j: 'str4'
145151
},
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')
150156
```
151157

152158
returns:
@@ -162,14 +168,14 @@ returns:
162168
"h": "str"
163169
}
164170
},
165-
"k": [ "str" ]
171+
"k": ["str"]
166172
}
167173
```
168174

169175
or with flattenFlag = true
170176

171177
```js
172-
nestedObjectsUtil.filterValue(a, 'str', true);
178+
nestedObjectsUtil.filterValue(a, 'str', true)
173179
```
174180

175181
returns:
@@ -183,7 +189,7 @@ returns:
183189
}
184190
```
185191

186-
#### nestedObjectsUtil.downloadStringified(Object: object, Number: space = 2): String | undefined
192+
#### nestedObjectsUtil.downloadStringified(object: object, space = 2): string|undefined
187193

188194
On browser with HTML5 download API: stringify, format and download the object.
189195

@@ -196,11 +202,11 @@ const a = {
196202
d: 2,
197203
e: 2
198204
}
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)
204210
```
205211

206212
returns:
@@ -217,9 +223,9 @@ returns:
217223
}
218224
```
219225

220-
#### nestedObjectsUtil.areObjectsEqual(Object: objectA, Object: objectB): Boolean
226+
#### nestedObjectsUtil.areObjectsEqual(objectA: object, objectB: object, skipProperties?: string[]): boolean
221227

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.
223229

224230
```js
225231
const objectA = {
@@ -228,21 +234,21 @@ const objectA = {
228234
keyC: 'value'
229235
},
230236
keyD: 'value2',
231-
keyE: [ 'value3', 'value4' ]
237+
keyE: ['value3', 'value4']
232238
}
233-
};
234-
objectA.circular = objectA;
239+
}
240+
objectA.circular = objectA
235241
const objectB = {
236242
keyA: {
237-
keyE: [ 'value3', 'value4' ],
243+
keyE: ['value3', 'value4'],
238244
keyD: 'value2',
239245
keyB: {
240246
keyC: 'value'
241247
}
242248
}
243-
};
244-
objectB.circular = objectB;
245-
nestedObjectsUtil.areObjectsEqual(objectA, objectB);
249+
}
250+
objectB.circular = objectB
251+
nestedObjectsUtil.areObjectsEqual(objectA, objectB)
246252
```
247253

248254
returns:
@@ -251,7 +257,39 @@ returns:
251257
true
252258
```
253259

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
255293

256294
Get the properties which differ between object A and object B and return only those from object B.
257295

@@ -262,21 +300,21 @@ const objectA = {
262300
keyC: 'value'
263301
},
264302
keyD: 'value2',
265-
keyE: [ 'value3' ]
303+
keyE: ['value3']
266304
}
267-
};
268-
objectA.circular = objectA;
305+
}
306+
objectA.circular = objectA
269307
const objectB = {
270308
keyA: {
271309
keyB: {
272310
keyC: 'value'
273311
},
274312
keyD: 'value2_CHANGED',
275-
keyE: [ 'value3_CHANGED' ]
313+
keyE: ['value3_CHANGED']
276314
}
277-
};
278-
objectB.circular = objectB;
279-
nestedObjectsUtil.getObjectsDiff(objectA, objectB);
315+
}
316+
objectB.circular = objectB
317+
nestedObjectsUtil.getObjectsDiff(objectA, objectB)
280318
```
281319

282320
returns:
@@ -285,7 +323,7 @@ returns:
285323
{
286324
"keyA": {
287325
"keyD": "value2_CHANGED",
288-
"keyE": [ "value3_CHANGED" ]
326+
"keyE": ["value3_CHANGED"]
289327
}
290328
}
291329
```
@@ -297,22 +335,24 @@ In the browser, NestedObjectsUtil object is exposed either to window or with AMD
297335
Filter out 'abcd' value from the flattened object and download stringified JSON via HTML5 API with:
298336

299337
```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)
302340
```
303341

304342
Discar circular references from the object, then flatten and download it.
305343

306344
```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)
310348
```
311349

312350
## Test
313351

314352
```
315353
npm run test
354+
355+
yarn test
316356
```
317357

318358
## License

0 commit comments

Comments
 (0)