From 85ee981be4b604a225666e1cae19fcadf465d8a8 Mon Sep 17 00:00:00 2001 From: Jeff Nusz Date: Fri, 2 Sep 2016 12:55:21 -0700 Subject: [PATCH] cleaner ForEach loops in common.js --- src/dat/utils/common.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/dat/utils/common.js b/src/dat/utils/common.js index c566019..1a952ae 100644 --- a/src/dat/utils/common.js +++ b/src/dat/utils/common.js @@ -25,14 +25,12 @@ const Common = { extend: function(target) { this.each(ARR_SLICE.call(arguments, 1), function(obj) { - if (!this.isUndefined(obj)) { - const keys = Object.keys(obj); - keys.forEach(function(key) { - if (!this.isUndefined(obj[key])) { - target[key] = obj[key]; - } - }.bind(this)); - } + const keys = this.isObject(obj) ? Object.keys(obj) : []; + keys.forEach(function(key) { + if (!this.isUndefined(obj[key])) { + target[key] = obj[key]; + } + }.bind(this)); }, this); return target; @@ -40,14 +38,12 @@ const Common = { defaults: function(target) { this.each(ARR_SLICE.call(arguments, 1), function(obj) { - if (!this.isUndefined(obj)) { - const keys = Object.keys(obj); - keys.forEach(function(key) { - if (this.isUndefined(target[key])) { - target[key] = obj[key]; - } - }.bind(this)); - } + const keys = this.isObject(obj) ? Object.keys(obj) : []; + keys.forEach(function(key) { + if (this.isUndefined(target[key])) { + target[key] = obj[key]; + } + }.bind(this)); }, this); return target; @@ -80,14 +76,11 @@ const Common = { } } } else { - if (this.isUndefined(obj)) return; - - const keys = Object.keys(obj); - keys.forEach(function(key) { + for (const key in obj) { if (itr.call(scope, obj[key], key) === this.BREAK) { return; } - }.bind(this)); + } } },