Skip to content

Commit 453a539

Browse files
committed
fix(Angular.js): Fix isArrayLike implementation
Convert if-return boolean statements to single boolean return. Assign boolean conditionals to break up the long boolean return and avoid repeated calls. Declare variables at the beginning of function to avoid hoisting errors. Break long lines into indented and aligned expressions. angular#4751
1 parent 1bfafad commit 453a539

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/Angular.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,44 @@ if (isNaN(msie)) {
167167
* String ...)
168168
*/
169169
function isArrayLike(obj) {
170-
var length, objExists, isNodeList, isArguments, isSomeOtherObj;
170+
// snake case is to avoid shadowing camel-cased globals
171+
var length, objExists, isNodeList, isArguments, isSomeOtherObj, is_array, is_string, is_object;
171172
objExists = isDefined(obj) && obj !== null;
172173
length = objExists ? obj.length : undefined;
174+
is_array = isArray(obj);
175+
is_string = isString(obj);
176+
is_object = isObject(obj);
173177
isNodeList = objExists && obj.nodeType === 1 && obj.hasChildNodes();
174178
isArguments = objExists && obj.hasOwnProperty('length') && obj.hasOwnProperty('callee');
175-
isSomeOtherObj = objExists && !isArguments && !isNodeList && !isArray(obj) && !isString(obj) && (!isObject(obj) && length === 0 || (typeof length === 'number' && length >= 0 && (length - 1) in obj));
179+
// this only works if it doesn't return 'object' from typeof and isn't another arrayLike
180+
isSomeOtherObj = objExists &&
181+
!isArguments &&
182+
!isNodeList &&
183+
!is_array &&
184+
!is_string &&
185+
(
186+
!is_object &&
187+
length === 0 ||
188+
(
189+
isNumber(length) &&
190+
length >= 0 &&
191+
(length - 1) in obj
192+
)
193+
);
176194

177-
return objExists && !isWindow(obj) && (((obj.nodeType === 1 && obj.hasChildNodes()) || (obj.hasOwnProperty('length') && obj.hasOwnProperty('callee')) || isString(obj) || isArray(obj)) || (!isObject(obj) && length === 0 || (typeof length === 'number' && length >= 0 && (length - 1) in obj)) );
178-
195+
return (
196+
objExists &&
197+
!isWindow(obj) &&
198+
(
199+
(
200+
isNodeList ||
201+
isArguments ||
202+
is_string ||
203+
is_array
204+
) ||
205+
isSomeOtherObj
206+
)
207+
);
179208
}
180209

181210
/**

0 commit comments

Comments
 (0)