Skip to content

Commit

Permalink
Merge aba2dbe into defc56a
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodrig committed Jun 1, 2021
2 parents defc56a + aba2dbe commit 929d465
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ included in the returned key path list?
```
- ignoreEmptyArraysWhenExpanding = `false` results in: `['features.name', 'rebates']`
- ignoreEmptyArraysWhenExpanding = `true` results in: `['features.name']`
- escapeNestedDots - `Boolean` (Default: `false`) - Should `.` characters that appear in keys be escaped with a preceding `\` character.
- escapeNestedDots - `Boolean` (Default: `false`) - Should `.` characters that appear in keys be escaped with a preceding `\` character?
- Example:
```json
{
Expand All @@ -102,6 +102,22 @@ included in the returned key path list?
```
- escapeNestedDots = `false` results in: `['a.a', 'a.b.c', 'a.b.c.d']`
- escapeNestedDots = `true` results in: `['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d']`
- ignoreEmptyArrays - `Boolean` (Default: `false`) - Should key paths for empty arrays be ignored in the generated key list?
- Example:
```json
{
"a": {
"b": [],
"c": {
"f": 4,
"e": []
}
},
"b": []
}
```
- ignoreEmptyArrays = `false` results in `['a.b', 'a.c.f', 'a.c.e', 'b']`
- ignoreEmptyArrays = `true` results in `['a.c.f']`

Returns: `Array[String]`

Expand Down Expand Up @@ -159,6 +175,24 @@ included in the returned key path list?
```
- escapeNestedDots = `false` results in: `[ ['a.a', 'a.b.c', 'a.b.c.d'] ]`
- escapeNestedDots = `true` results in: `[ ['a\\.a', 'a\\.b.c', 'a\\.b.c\\.d'] ]`
- ignoreEmptyArrays - `Boolean` (Default: `false`) - Should key paths for empty arrays be ignored in the generated key list?
- Example:
```json
[
{
"a": {
"b": [],
"c": {
"f": 4,
"e": []
}
},
"b": []
}
]
```
- ignoreEmptyArrays = `false` results in `[ ['a.b', 'a.c.f', 'a.c.e', 'b'] ]`
- ignoreEmptyArrays = `true` results in `[ ['a.c.f'] ]`

Returns: `Array[Array[String]]`

Expand Down Expand Up @@ -211,8 +245,8 @@ $ npm run coverage

Current Coverage is:
```
Statements : 100% ( 45/45 )
Branches : 100% ( 32/32 )
Statements : 100% ( 47/47 )
Branches : 100% ( 37/37 )
Functions : 100% ( 18/18 )
Lines : 100% ( 44/44 )
Lines : 100% ( 46/46 )
```
3 changes: 3 additions & 0 deletions lib/deeks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function generateDeepKeysList(heading, data, options) {
} else if (options.expandArrayObjects && isArrayToRecurOn(data[currentKey])) {
// If we have a nested array that we need to recur on
return processArrayKeys(data[currentKey], keyName, options);
} else if (options.ignoreEmptyArrays && isArrayToRecurOn(data[currentKey]) && !data[currentKey].length) {
return [];
}
// Otherwise return this key name since we don't have a sub document
return keyName;
Expand Down Expand Up @@ -137,6 +139,7 @@ function mergeOptions(options) {
expandArrayObjects: false,
ignoreEmptyArraysWhenExpanding: false,
escapeNestedDots: false,
ignoreEmptyArrays: false,
...options || {}
};
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deeks",
"version": "2.4.1",
"version": "2.5.0",
"description": "Retrieve all keys and nested keys from objects and arrays of objects.",
"main": "lib/deeks.js",
"scripts": {
Expand Down
89 changes: 89 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ describe('deeks Module', () => {
.and.have.lengthOf(3);
done();
});

it('should include empty array key paths in the generated key list', (done) => {
let testObj = {
a: {
b: [],
c: {
f: 4,
e: []
}
},
b: []
},
keys = deeks.deepKeys(testObj);

keys.should.be.an.instanceOf(Array)
.and.containEql('a.b')
.and.containEql('a.c.f')
.and.containEql('a.c.e')
.and.containEql('b')
.and.have.lengthOf(4);
done();
});
});

describe('Custom Options', () => {
Expand Down Expand Up @@ -375,6 +397,28 @@ describe('deeks Module', () => {
.and.have.lengthOf(6);
done();
});

it('[ignoreEmptyArrays] should ignore empty arrays when generating key list and when specified', (done) => {
let testObj = {
a: {
b: [],
c: {
f: 4,
e: []
}
},
b: []
},
options = {
ignoreEmptyArrays: true
},
keys = deeks.deepKeys(testObj, options);

keys.should.be.an.instanceOf(Array)
.and.containEql('a.c.f')
.and.have.lengthOf(1);
done();
});
});
});

Expand Down Expand Up @@ -581,6 +625,27 @@ describe('deeks Module', () => {
.and.have.lengthOf(1);
done();
});

it('should include empty array key paths in the generated key list', (done) => {
let testList = [
{
a: {
b: [],
c: {
f: 4,
e: []
}
},
b: []
}
],
keys = deeks.deepKeysFromList(testList);

keys.should.be.an.instanceOf(Array)
.and.containEql(['a.b', 'a.c.f', 'a.c.e', 'b'])
.and.have.lengthOf(1);
done();
});
});

describe('Custom Options', () => {
Expand Down Expand Up @@ -791,6 +856,30 @@ describe('deeks Module', () => {
.and.have.lengthOf(1);
done();
});

it('[ignoreEmptyArrays] should not include empty array key paths in the generated key list when specified', (done) => {
let testList = [
{
a: {
b: [],
c: {
f: 4,
e: []
}
},
b: []
}
],
options = {
ignoreEmptyArrays: true
},
keys = deeks.deepKeysFromList(testList, options);

keys.should.be.an.instanceOf(Array)
.and.containEql(['a.c.f'])
.and.have.lengthOf(1);
done();
});
});
});
});

0 comments on commit 929d465

Please sign in to comment.