Skip to content

Commit f539ebf

Browse files
committed
Factor out createSizePropertyCheck (#2694)
1 parent dd9729f commit f539ebf

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

modules/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,26 @@ function deepGet(obj, path) {
156156
return length ? obj : void 0;
157157
}
158158

159+
// Common logic for isArrayLike and isBufferLike.
160+
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
161+
function createSizePropertyCheck(getSizeProperty) {
162+
return function(collection) {
163+
var sizeProperty = getSizeProperty(collection);
164+
return typeof sizeProperty == 'number' && sizeProperty >= 0 && sizeProperty <= MAX_ARRAY_INDEX;
165+
}
166+
}
167+
159168
// Helper for collection methods to determine whether a collection
160169
// should be iterated as an array or as an object.
161170
// Related: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
162171
// Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
163-
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
164172
var getLength = shallowProperty('length');
165-
function isArrayLike(collection) {
166-
var length = getLength(collection);
167-
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
168-
}
173+
var isArrayLike = createSizePropertyCheck(getLength);
169174

170175
// Likewise to determine whether we should spend extensive checks against
171176
// `ArrayBuffer` et al.
172177
var getByteLength = shallowProperty('byteLength');
173-
function isBufferLike(collection) {
174-
var byteLength = getByteLength(collection);
175-
return typeof byteLength == 'number' && byteLength >= 0 && byteLength <= MAX_ARRAY_INDEX;
176-
}
178+
var isBufferLike = createSizePropertyCheck(getByteLength);
177179

178180
// Collection Functions
179181
// --------------------

underscore.js

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)