Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 864bee5

Browse files
refact(ngOptions): clean up iterating over option values
1 parent bf74c72 commit 864bee5

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/ng/directive/ngOptions.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ var NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s
230230

231231
var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
232232

233+
function iterateOverObjectProperties(obj, fn) {
234+
// extract keys, in enumeration order, unsorted
235+
var keys = Object.keys(obj);
236+
keys.forEach(function(key) {
237+
if (key.charAt(0) !== '$') {
238+
fn(obj[key], key);
239+
}
240+
});
241+
}
242+
243+
233244
function parseOptionsExpression(optionsExp, selectElement, scope) {
234245

235246
var match = optionsExp.match(NG_OPTIONS_REGEXP);
@@ -321,30 +332,11 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
321332

322333
var optionItems = [];
323334
var selectValueMap = {};
324-
325-
// The option values were already computed in the `getWatchables` fn,
326-
// which must have been called to trigger `getOptions`
327335
var optionValues = valuesFn(scope) || [];
328-
var optionValuesKeys;
336+
var forEachOptionValue = (!keyName && isArrayLike(optionValues)) ? forEach : iterateOverObjectProperties;
329337

330338

331-
if (!keyName && isArrayLike(optionValues)) {
332-
optionValuesKeys = optionValues;
333-
} else {
334-
// if object, extract keys, in enumeration order, unsorted
335-
optionValuesKeys = [];
336-
for (var itemKey in optionValues) {
337-
if (optionValues.hasOwnProperty(itemKey) && itemKey.charAt(0) !== '$') {
338-
optionValuesKeys.push(itemKey);
339-
}
340-
}
341-
}
342-
343-
var optionValuesLength = optionValuesKeys.length;
344-
345-
for (var index = 0; index < optionValuesLength; index++) {
346-
var key = (optionValues === optionValuesKeys) ? index : optionValuesKeys[index];
347-
var value = optionValues[key];
339+
forEachOptionValue(optionValues, function(value, key) {
348340
var locals = getLocals(value, key);
349341
var viewValue = viewValueFn(scope, locals);
350342
var selectValue = getTrackByValue(viewValue, locals);
@@ -355,7 +347,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
355347

356348
optionItems.push(optionItem);
357349
selectValueMap[selectValue] = optionItem;
358-
}
350+
});
359351

360352
return {
361353
items: optionItems,

0 commit comments

Comments
 (0)