From 8e1792064376a043dc317971365ffe9c8e99e179 Mon Sep 17 00:00:00 2001 From: Steve Faulkner Date: Sat, 12 Aug 2017 11:42:00 -0400 Subject: [PATCH] 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 --- lib/utils/index.js | 8 ++++---- test/unit/utils.js | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index 573de2af..d2342b8d 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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; }; diff --git a/test/unit/utils.js b/test/unit/utils.js index a2664ff3..0eccc3ee 100644 --- a/test/unit/utils.js +++ b/test/unit/utils.js @@ -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']);