Skip to content

Commit

Permalink
fix: make mergeOptions behave the same across browsers (#4090)
Browse files Browse the repository at this point in the history
* Make an object util function `keys` return an empty array for non objects
* Use that function in object utils instead of `Object.assign`
  • Loading branch information
forbesjo authored and brandonocasey committed Feb 16, 2017
1 parent c02c6c6 commit ce19ed5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/js/utils/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@
*/
const toString = Object.prototype.toString;

/**
* Get the keys of an Object
*
* @param {Object}
* The Object to get the keys from
*
* @return {string[]}
* An array of the keys from the object. Returns an empty array if the
* object passed in was invalid or had no keys.
*
* @private
*/
const keys = function(object) {
return isObject(object) ? Object.keys(object) : [];
};

/**
* Array-like iteration for objects.
*
Expand All @@ -40,7 +56,7 @@ const toString = Object.prototype.toString;
* The callback function which is called for each key in the object.
*/
export function each(object, fn) {
Object.keys(object).forEach(key => fn(object[key], key));
keys(object).forEach(key => fn(object[key], key));
}

/**
Expand All @@ -61,7 +77,7 @@ export function each(object, fn) {
* The final accumulated value.
*/
export function reduce(object, fn, initial = 0) {
return Object.keys(object).reduce(
return keys(object).reduce(
(accum, key) => fn(accum, object[key], key), initial);
}

Expand Down
7 changes: 7 additions & 0 deletions test/unit/utils/merge-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import mergeOptions from '../../../src/js/utils/merge-options.js';

QUnit.module('merge-options');

QUnit.test('should merge options objects', function(assert) {
const ob1 = {
a: true,
Expand All @@ -27,3 +28,9 @@ QUnit.test('should merge options objects', function(assert) {
d: true
}, 'options objects merged correctly');
});

QUnit.test('should ignore non-objects', function(assert) {
const obj = { a: 1 };

assert.deepEqual(mergeOptions(obj, true), obj, 'ignored non-object input');
});

0 comments on commit ce19ed5

Please sign in to comment.