From 3e2b0bb763f43b272d847f61c9029fbe675b4b03 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 3 Apr 2017 10:35:26 -0700 Subject: [PATCH] Use more for-of --- .internal/baseDifference.js | 7 ++----- .internal/baseSum.js | 6 ++---- .internal/copyObject.js | 7 +------ cond.js | 4 +--- fromPairs.js | 9 ++++----- maxBy.js | 9 ++++----- minBy.js | 9 ++++----- 7 files changed, 18 insertions(+), 33 deletions(-) diff --git a/.internal/baseDifference.js b/.internal/baseDifference.js index 348f8e003..b29a5c26e 100644 --- a/.internal/baseDifference.js +++ b/.internal/baseDifference.js @@ -19,14 +19,12 @@ const LARGE_ARRAY_SIZE = 200 * @returns {Array} Returns the new array of filtered values. */ function baseDifference(array, values, iteratee, comparator) { - let index = -1 let includes = arrayIncludes let isCommon = true - const { length } = array const result = [] const valuesLength = values.length - if (!length) { + if (!array.length) { return result } if (iteratee) { @@ -42,8 +40,7 @@ function baseDifference(array, values, iteratee, comparator) { values = new SetCache(values) } outer: - while (++index < length) { - let value = array[index] + for (let value of array) { const computed = iteratee == null ? value : iteratee(value) value = (comparator || value !== 0) ? value : 0 diff --git a/.internal/baseSum.js b/.internal/baseSum.js index 46d710503..8f8cefb87 100644 --- a/.internal/baseSum.js +++ b/.internal/baseSum.js @@ -8,11 +8,9 @@ */ function baseSum(array, iteratee) { let result - let index = -1 - const { length } = array - while (++index < length) { - const current = iteratee(array[index]) + for (value of array) { + const current = iteratee(value) if (current !== undefined) { result = result === undefined ? current : (result + current) } diff --git a/.internal/copyObject.js b/.internal/copyObject.js index 86c01a817..bcabc9bc1 100644 --- a/.internal/copyObject.js +++ b/.internal/copyObject.js @@ -15,12 +15,7 @@ function copyObject(source, props, object, customizer) { const isNew = !object object || (object = {}) - let index = -1 - const length = props.length - - while (++index < length) { - const key = props[index] - + for (const key of props) { let newValue = customizer ? customizer(object[key], source[key], key, object, source) : undefined diff --git a/cond.js b/cond.js index b9ecc4ef0..8a41fa718 100644 --- a/cond.js +++ b/cond.js @@ -38,9 +38,7 @@ function cond(pairs) { }) return (...args) => { - let index = -1 - while (++index < length) { - const pair = pairs[index] + for (const pair of pairs) { if (pair[0].apply(this, args)) { return pair[1].apply(this, args) } diff --git a/fromPairs.js b/fromPairs.js index 630d15c2a..ea10a363b 100644 --- a/fromPairs.js +++ b/fromPairs.js @@ -12,12 +12,11 @@ * // => { 'a': 1, 'b': 2 } */ function fromPairs(pairs) { - let index = -1 - const length = pairs == null ? 0 : pairs.length const result = {} - - while (++index < length) { - const pair = pairs[index] + if (pairs == null) { + return result + } + for (const pair of pairs) { result[pair[0]] = pair[1] } return result diff --git a/maxBy.js b/maxBy.js index d28118165..9d346ebfa 100644 --- a/maxBy.js +++ b/maxBy.js @@ -19,12 +19,11 @@ import isSymbol from './isSymbol.js' */ function maxBy(array, iteratee) { let result - let index = -1 - const length = array == null ? 0 : array.length - - while (++index < length) { + if (array == null) { + return result + } + for (const value of array) { let computed - const value = array[index] const current = iteratee(value) if (current != null && (computed === undefined diff --git a/minBy.js b/minBy.js index 045d38974..8f5879aeb 100644 --- a/minBy.js +++ b/minBy.js @@ -19,12 +19,11 @@ import isSymbol from './isSymbol.js' */ function minBy(array, iteratee) { let result - let index = -1 - const length = array == null ? 0 : array.length - - while (++index < length) { + if (array == null) { + return result + } + for (const value of array) { let computed - const value = array[index] const current = iteratee(value) if (current != null && (computed === undefined