Skip to content

Commit faf1bd1

Browse files
authored
Add ignore empty arrays option (#20)
* Add `ignoreEmptyArrays` option. * chore(release): 2.5.0
1 parent defc56a commit faf1bd1

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ included in the returned key path list?
8989
```
9090
- ignoreEmptyArraysWhenExpanding = `false` results in: `['features.name', 'rebates']`
9191
- ignoreEmptyArraysWhenExpanding = `true` results in: `['features.name']`
92-
- escapeNestedDots - `Boolean` (Default: `false`) - Should `.` characters that appear in keys be escaped with a preceding `\` character.
92+
- escapeNestedDots - `Boolean` (Default: `false`) - Should `.` characters that appear in keys be escaped with a preceding `\` character?
9393
- Example:
9494
```json
9595
{
@@ -102,6 +102,22 @@ included in the returned key path list?
102102
```
103103
- escapeNestedDots = `false` results in: `['a.a', 'a.b.c', 'a.b.c.d']`
104104
- escapeNestedDots = `true` results in: `['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d']`
105+
- ignoreEmptyArrays - `Boolean` (Default: `false`) - Should key paths for empty arrays be ignored in the generated key list?
106+
- Example:
107+
```json
108+
{
109+
"a": {
110+
"b": [],
111+
"c": {
112+
"f": 4,
113+
"e": []
114+
}
115+
},
116+
"b": []
117+
}
118+
```
119+
- ignoreEmptyArrays = `false` results in `['a.b', 'a.c.f', 'a.c.e', 'b']`
120+
- ignoreEmptyArrays = `true` results in `['a.c.f']`
105121

106122
Returns: `Array[String]`
107123

@@ -159,6 +175,24 @@ included in the returned key path list?
159175
```
160176
- escapeNestedDots = `false` results in: `[ ['a.a', 'a.b.c', 'a.b.c.d'] ]`
161177
- escapeNestedDots = `true` results in: `[ ['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d'] ]`
178+
- ignoreEmptyArrays - `Boolean` (Default: `false`) - Should key paths for empty arrays be ignored in the generated key list?
179+
- Example:
180+
```json
181+
[
182+
{
183+
"a": {
184+
"b": [],
185+
"c": {
186+
"f": 4,
187+
"e": []
188+
}
189+
},
190+
"b": []
191+
}
192+
]
193+
```
194+
- ignoreEmptyArrays = `false` results in `[ ['a.b', 'a.c.f', 'a.c.e', 'b'] ]`
195+
- ignoreEmptyArrays = `true` results in `[ ['a.c.f'] ]`
162196

163197
Returns: `Array[Array[String]]`
164198

@@ -211,8 +245,8 @@ $ npm run coverage
211245

212246
Current Coverage is:
213247
```
214-
Statements : 100% ( 45/45 )
215-
Branches : 100% ( 32/32 )
248+
Statements : 100% ( 47/47 )
249+
Branches : 100% ( 37/37 )
216250
Functions : 100% ( 18/18 )
217-
Lines : 100% ( 44/44 )
251+
Lines : 100% ( 46/46 )
218252
```

lib/deeks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ function generateDeepKeysList(heading, data, options) {
4949
} else if (options.expandArrayObjects && isArrayToRecurOn(data[currentKey])) {
5050
// If we have a nested array that we need to recur on
5151
return processArrayKeys(data[currentKey], keyName, options);
52+
} else if (options.ignoreEmptyArrays && isArrayToRecurOn(data[currentKey]) && !data[currentKey].length) {
53+
return [];
5254
}
5355
// Otherwise return this key name since we don't have a sub document
5456
return keyName;
@@ -137,6 +139,7 @@ function mergeOptions(options) {
137139
expandArrayObjects: false,
138140
ignoreEmptyArraysWhenExpanding: false,
139141
escapeNestedDots: false,
142+
ignoreEmptyArrays: false,
140143
...options || {}
141144
};
142145
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deeks",
3-
"version": "2.4.1",
3+
"version": "2.5.0",
44
"description": "Retrieve all keys and nested keys from objects and arrays of objects.",
55
"main": "lib/deeks.js",
66
"scripts": {

test/tests.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,28 @@ describe('deeks Module', () => {
129129
.and.have.lengthOf(3);
130130
done();
131131
});
132+
133+
it('should include empty array key paths in the generated key list', (done) => {
134+
let testObj = {
135+
a: {
136+
b: [],
137+
c: {
138+
f: 4,
139+
e: []
140+
}
141+
},
142+
b: []
143+
},
144+
keys = deeks.deepKeys(testObj);
145+
146+
keys.should.be.an.instanceOf(Array)
147+
.and.containEql('a.b')
148+
.and.containEql('a.c.f')
149+
.and.containEql('a.c.e')
150+
.and.containEql('b')
151+
.and.have.lengthOf(4);
152+
done();
153+
});
132154
});
133155

134156
describe('Custom Options', () => {
@@ -375,6 +397,28 @@ describe('deeks Module', () => {
375397
.and.have.lengthOf(6);
376398
done();
377399
});
400+
401+
it('[ignoreEmptyArrays] should ignore empty arrays when generating key list and when specified', (done) => {
402+
let testObj = {
403+
a: {
404+
b: [],
405+
c: {
406+
f: 4,
407+
e: []
408+
}
409+
},
410+
b: []
411+
},
412+
options = {
413+
ignoreEmptyArrays: true
414+
},
415+
keys = deeks.deepKeys(testObj, options);
416+
417+
keys.should.be.an.instanceOf(Array)
418+
.and.containEql('a.c.f')
419+
.and.have.lengthOf(1);
420+
done();
421+
});
378422
});
379423
});
380424

@@ -581,6 +625,27 @@ describe('deeks Module', () => {
581625
.and.have.lengthOf(1);
582626
done();
583627
});
628+
629+
it('should include empty array key paths in the generated key list', (done) => {
630+
let testList = [
631+
{
632+
a: {
633+
b: [],
634+
c: {
635+
f: 4,
636+
e: []
637+
}
638+
},
639+
b: []
640+
}
641+
],
642+
keys = deeks.deepKeysFromList(testList);
643+
644+
keys.should.be.an.instanceOf(Array)
645+
.and.containEql(['a.b', 'a.c.f', 'a.c.e', 'b'])
646+
.and.have.lengthOf(1);
647+
done();
648+
});
584649
});
585650

586651
describe('Custom Options', () => {
@@ -791,6 +856,30 @@ describe('deeks Module', () => {
791856
.and.have.lengthOf(1);
792857
done();
793858
});
859+
860+
it('[ignoreEmptyArrays] should not include empty array key paths in the generated key list when specified', (done) => {
861+
let testList = [
862+
{
863+
a: {
864+
b: [],
865+
c: {
866+
f: 4,
867+
e: []
868+
}
869+
},
870+
b: []
871+
}
872+
],
873+
options = {
874+
ignoreEmptyArrays: true
875+
},
876+
keys = deeks.deepKeysFromList(testList, options);
877+
878+
keys.should.be.an.instanceOf(Array)
879+
.and.containEql(['a.c.f'])
880+
.and.have.lengthOf(1);
881+
done();
882+
});
794883
});
795884
});
796885
});

0 commit comments

Comments
 (0)