Skip to content

Commit ee3749c

Browse files
authored
Fix Type Error when null is passed to deepKeys (#26)
* fix: resolve "TypeError: Cannot convert undefined or null to object" Initially reported in mrodrig/json-2-csv#222 This issue appears when a `null` value is passed for key enumeration because technically `null` is an object, but doesn't have keys. This commit fixes the issue by adding a null check to prevent the type error. * chore(deps): npm audit fix * chore(rel): 2.6.0
1 parent 1ff233e commit ee3749c

File tree

4 files changed

+54
-56
lines changed

4 files changed

+54
-56
lines changed

lib/deeks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
*/
1616
function deepKeys(object, options) {
1717
options = mergeOptions(options);
18-
if (utils.isObject(object)) {
18+
if (utils.isObject(object) && object !== null) {
1919
return generateDeepKeysList('', object, options);
2020
}
2121
return [];

package-lock.json

Lines changed: 43 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deeks",
3-
"version": "2.5.4",
3+
"version": "2.6.0",
44
"description": "Retrieve all keys and nested keys from objects and arrays of objects.",
55
"main": "lib/deeks.js",
66
"scripts": {
@@ -33,7 +33,7 @@
3333
"@babel/eslint-parser": "7.16.5",
3434
"coveralls": "3.1.1",
3535
"eslint": "8.7.0",
36-
"mocha": "9.2.0",
36+
"mocha": "10.1.0",
3737
"nyc": "15.1.0",
3838
"should": "13.2.3"
3939
},

test/tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const deeks = require('../lib/deeks.js'),
77
describe('deeks Module', () => {
88
describe('deepKeys() - Objects', () => {
99
describe('Default Options', () => {
10+
it('should retrieve no keys for null', (done) => {
11+
let keys = deeks.deepKeys(null);
12+
13+
keys.should.be.an.instanceOf(Array)
14+
.and.have.lengthOf(0);
15+
done();
16+
});
17+
1018
it('should retrieve no keys for an empty object', (done) => {
1119
let testObj = {},
1220
keys = deeks.deepKeys(testObj);

0 commit comments

Comments
 (0)