Skip to content

Commit

Permalink
fix: allow convertObjectToArray to handle objects with no prototype (#…
Browse files Browse the repository at this point in the history
…507)

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 authored and luin committed Aug 12, 2017
1 parent f4f8cba commit 8e17920
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ 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]);
}
var keys = Object.keys(obj);

for (var i = 0, l = keys.length; i < l; i++) {
result.push(keys[i], obj[keys[i]]);
}
return result;
};
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 8e17920

Please sign in to comment.