Skip to content

Commit

Permalink
Fix Type Error when null is passed to deepKeys (#26)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mrodrig authored Nov 22, 2022
1 parent 1ff233e commit ee3749c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lib/deeks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
*/
function deepKeys(object, options) {
options = mergeOptions(options);
if (utils.isObject(object)) {
if (utils.isObject(object) && object !== null) {
return generateDeepKeysList('', object, options);
}
return [];
Expand Down
96 changes: 43 additions & 53 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deeks",
"version": "2.5.4",
"version": "2.6.0",
"description": "Retrieve all keys and nested keys from objects and arrays of objects.",
"main": "lib/deeks.js",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"@babel/eslint-parser": "7.16.5",
"coveralls": "3.1.1",
"eslint": "8.7.0",
"mocha": "9.2.0",
"mocha": "10.1.0",
"nyc": "15.1.0",
"should": "13.2.3"
},
Expand Down
8 changes: 8 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const deeks = require('../lib/deeks.js'),
describe('deeks Module', () => {
describe('deepKeys() - Objects', () => {
describe('Default Options', () => {
it('should retrieve no keys for null', (done) => {
let keys = deeks.deepKeys(null);

keys.should.be.an.instanceOf(Array)
.and.have.lengthOf(0);
done();
});

it('should retrieve no keys for an empty object', (done) => {
let testObj = {},
keys = deeks.deepKeys(testObj);
Expand Down

0 comments on commit ee3749c

Please sign in to comment.