Skip to content

Commit 8e17920

Browse files
southpolesteveluin
authored andcommitted
fix: allow convertObjectToArray to handle objects with no prototype (#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
1 parent f4f8cba commit 8e17920

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

lib/utils/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ exports.timeout = function (callback, timeout) {
170170
*/
171171
exports.convertObjectToArray = function (obj) {
172172
var result = [];
173-
for (var key in obj) {
174-
if (obj.hasOwnProperty(key)) {
175-
result.push(key, obj[key]);
176-
}
173+
var keys = Object.keys(obj);
174+
175+
for (var i = 0, l = keys.length; i < l; i++) {
176+
result.push(keys[i], obj[keys[i]]);
177177
}
178178
return result;
179179
};

test/unit/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ describe('utils', function () {
8282

8383
describe('.convertObjectToArray', function () {
8484
it('should return correctly', function () {
85+
var nullObject = Object.create(null);
86+
nullObject.abc = 'def';
87+
expect(utils.convertObjectToArray(nullObject)).to.eql(['abc', 'def']);
8588
expect(utils.convertObjectToArray({ 1: 2 })).to.eql(['1', 2]);
8689
expect(utils.convertObjectToArray({ 1: '2' })).to.eql(['1', '2']);
8790
expect(utils.convertObjectToArray({ 1: '2', abc: 'def' })).to.eql(['1', '2', 'abc', 'def']);

0 commit comments

Comments
 (0)