Skip to content

Commit

Permalink
Allow convertObjectToArray to handle objects with no prototype
Browse files Browse the repository at this point in the history
Currently this method expects `hasOwnProperty` to exist on the passed object. By using the method on `Object.prototype` instead, objects created with `Object.create(null)` can also be properly handled.

Ran into this bug trying to pass graphQL input objects (heavily uses `Object.create(null)`) directly to hmset commands

Use call
  • Loading branch information
southpolesteve committed Aug 12, 2017
1 parent f4f8cba commit 7389f42
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,12 @@ exports.timeout = function (callback, timeout) {
* ```
*/
exports.convertObjectToArray = function (obj) {
var result = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
result.push(key, obj[key]);
return Object.keys(obj).reduce(function (array, key) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
array.push(key, obj[key]);
}
}
return result;
return array;
}, []);
};

/**
Expand Down
3 changes: 3 additions & 0 deletions test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ describe('utils', function () {

describe('.convertObjectToArray', function () {
it('should return correctly', function () {
var nullObject = Object.create(null);
nullObject.abc = 'def';
expect(utils.convertObjectToArray(nullObject)).to.eql(['abc', 'def']);
expect(utils.convertObjectToArray({ 1: 2 })).to.eql(['1', 2]);
expect(utils.convertObjectToArray({ 1: '2' })).to.eql(['1', '2']);
expect(utils.convertObjectToArray({ 1: '2', abc: 'def' })).to.eql(['1', '2', 'abc', 'def']);
Expand Down

0 comments on commit 7389f42

Please sign in to comment.