|
276 | 276 | ' while (<%= arrayBranch.loopExp %>) {\n' +
|
277 | 277 | ' <%= arrayBranch.inLoop %>\n' +
|
278 | 278 | ' }' +
|
279 |
| - ' <% if (objectBranch) { %>\n}\n<% }' + |
280 |
| - '}' + |
| 279 | + ' <% if (objectBranch) { %>\n}<% } %>' + |
| 280 | + '<% } %>' + |
281 | 281 |
|
282 | 282 | // the following branch is for iterating an object's own/inherited properties
|
283 |
| - 'if (objectBranch) {' + |
284 |
| - ' if (arrayBranch) { %>else {\n<% }' + |
285 |
| - ' if (!hasDontEnumBug) { %>' + |
| 283 | + '<% if (objectBranch) { %>' + |
| 284 | + ' <% if (arrayBranch) { %>\nelse {<% } %>' + |
| 285 | + ' <% if (!hasDontEnumBug) { %>\n' + |
286 | 286 | ' var skipProto = typeof <%= iteratedObject %> == \'function\' && \n' +
|
287 |
| - ' propertyIsEnumerable.call(<%= iteratedObject %>, \'prototype\');\n<%' + |
288 |
| - ' } %>' + |
289 |
| - ' <%= objectBranch.beforeLoop %>;\n<%' + |
| 287 | + ' propertyIsEnumerable.call(<%= iteratedObject %>, \'prototype\');\n' + |
| 288 | + ' <% } %>' + |
290 | 289 |
|
291 | 290 | // iterate own properties using `Object.keys` if it's fast
|
292 |
| - ' if (isKeysFast && useHas) { %>' + |
| 291 | + ' <% if (isKeysFast && useHas) { %>\n' + |
293 | 292 | ' var props = nativeKeys(<%= iteratedObject %>),\n' +
|
294 | 293 | ' propIndex = -1,\n' +
|
295 |
| - ' length = props.length;\n' + |
| 294 | + ' length = props.length;\n\n' + |
| 295 | + ' <%= objectBranch.beforeLoop %>;\n' + |
296 | 296 | ' while (++propIndex < length) {\n' +
|
297 | 297 | ' index = props[propIndex];\n' +
|
298 | 298 | ' if (!(skipProto && index == \'prototype\')) {\n' +
|
299 | 299 | ' <%= objectBranch.inLoop %>\n' +
|
300 | 300 | ' }\n' +
|
301 |
| - ' }\n' + |
| 301 | + ' }' + |
302 | 302 |
|
303 | 303 | // else using a for-in loop
|
304 |
| - ' <% } else { %>' + |
| 304 | + ' <% } else { %>\n' + |
| 305 | + ' <%= objectBranch.beforeLoop %>;\n' + |
305 | 306 | ' for (<%= objectBranch.loopExp %>) {' +
|
306 |
| - ' \n<%' + |
307 |
| - ' if (hasDontEnumBug) {' + |
308 |
| - ' if (useHas) { %> if (<%= hasExp %>) {\n <% } %>' + |
309 |
| - ' <%= objectBranch.inLoop %>;<%' + |
310 |
| - ' if (useHas) { %>\n }<% }' + |
311 |
| - ' }' + |
312 |
| - ' else { %>' + |
| 307 | + ' <% if (hasDontEnumBug) { %>\n' + |
| 308 | + ' <% if (useHas) { %>if (<%= hasExp %>) {\n <% } %>' + |
| 309 | + ' <%= objectBranch.inLoop %>;\n' + |
| 310 | + ' <% if (useHas) { %>}<% } %>' + |
| 311 | + ' <% } else { %>\n' + |
313 | 312 |
|
314 | 313 | // Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
|
315 | 314 | // (if the prototype or a property on the prototype has been set)
|
|
328 | 327 | // existing property and the `constructor` property of a prototype
|
329 | 328 | // defaults to non-enumerable, Lo-Dash skips the `constructor`
|
330 | 329 | // property when it infers it's iterating over a `prototype` object.
|
331 |
| - ' <% if (hasDontEnumBug) { %>\n' + |
| 330 | + ' <% if (hasDontEnumBug) { %>\n\n' + |
332 | 331 | ' var ctor = <%= iteratedObject %>.constructor;\n' +
|
333 | 332 | ' <% for (var k = 0; k < 7; k++) { %>\n' +
|
334 | 333 | ' index = \'<%= shadowed[k] %>\';\n' +
|
|
337 | 336 | ' %>!(ctor && ctor.prototype === <%= iteratedObject %>) && <%' +
|
338 | 337 | ' } %><%= hasExp %>) {\n' +
|
339 | 338 | ' <%= objectBranch.inLoop %>\n' +
|
340 |
| - ' }<%' + |
341 |
| - ' }' + |
342 |
| - ' }' + |
343 |
| - ' if (arrayBranch) { %>\n}<% }' + |
344 |
| - '} %>\n' + |
| 339 | + ' }' + |
| 340 | + ' <% } %>' + |
| 341 | + ' <% } %>' + |
| 342 | + ' <% if (arrayBranch) { %>\n}<% } %>' + |
| 343 | + '<% } %>\n' + |
345 | 344 |
|
346 | 345 | // add code to the bottom of the iteration function
|
347 | 346 | '<%= bottom %>;\n' +
|
|
411 | 410 | 'exit': 'if (!collection) return []',
|
412 | 411 | 'beforeLoop': {
|
413 | 412 | 'array': 'result = Array(length)',
|
414 |
| - 'object': 'result = []' |
| 413 | + 'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]') |
415 | 414 | },
|
416 | 415 | 'inLoop': {
|
417 | 416 | 'array': 'result[index] = callback(collection[index], index, collection)',
|
418 |
| - 'object': 'result.push(callback(collection[index], index, collection))' |
| 417 | + 'object': 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '(callback(collection[index], index, collection))' |
419 | 418 | }
|
420 | 419 | };
|
421 | 420 |
|
|
867 | 866 | ' isFunc = typeof methodName == \'function\'',
|
868 | 867 | 'inLoop': {
|
869 | 868 | 'array': 'result[index] = (isFunc ? methodName : collection[index][methodName]).apply(collection[index], args)',
|
870 |
| - 'object': 'result.push((isFunc ? methodName : collection[index][methodName]).apply(collection[index], args))' |
| 869 | + 'object': 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '((isFunc ? methodName : collection[index][methodName]).apply(collection[index], args))' |
871 | 870 | }
|
872 | 871 | });
|
873 | 872 |
|
|
920 | 919 | 'args': 'collection, property',
|
921 | 920 | 'inLoop': {
|
922 | 921 | 'array': 'result[index] = collection[index][property]',
|
923 |
| - 'object': 'result.push(collection[index][property])' |
| 922 | + 'object': 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '(collection[index][property])' |
924 | 923 | }
|
925 | 924 | });
|
926 | 925 |
|
|
1106 | 1105 | ' value: collection[index]\n' +
|
1107 | 1106 | '}',
|
1108 | 1107 | 'object':
|
1109 |
| - 'result.push({\n' + |
| 1108 | + 'result' + (isKeysFast ? '[propIndex] = ' : '.push') + '({\n' + |
1110 | 1109 | ' criteria: callback(collection[index], index, collection),\n' +
|
1111 | 1110 | ' value: collection[index]\n' +
|
1112 | 1111 | '})'
|
|
0 commit comments