Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 4208d7d

Browse files
committed
use findIndex instead of indexOf when looking up activeIndex (includes findIndex polyfill)
1 parent fa47fba commit 4208d7d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/uiSelectController.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ uis.controller('uiSelectCtrl',
6666
ctrl.search = EMPTY_SEARCH;
6767
//reset activeIndex
6868
if (ctrl.selected && ctrl.items.length && !ctrl.multiple) {
69-
ctrl.activeIndex = ctrl.items.indexOf(ctrl.selected);
69+
ctrl.activeIndex = ctrl.items.findIndex(function(item){
70+
return angular.equals(this, item);
71+
}, ctrl.selected);
7072
}
7173
}
7274
}
@@ -259,7 +261,7 @@ uis.controller('uiSelectCtrl',
259261
return false;
260262
}
261263
var itemIndex = ctrl.items.indexOf(itemScope[ctrl.itemProperty]);
262-
var isActive = itemIndex === ctrl.activeIndex;
264+
var isActive = itemIndex == ctrl.activeIndex;
263265

264266
if ( !isActive || ( itemIndex < 0 && ctrl.taggingLabel !== false ) ||( itemIndex < 0 && ctrl.taggingLabel === false) ) {
265267
return false;
@@ -573,3 +575,27 @@ uis.controller('uiSelectCtrl',
573575
});
574576

575577
}]);
578+
579+
// Array findIndex polyfill (source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex#Polyfill)
580+
if (!Array.prototype.findIndex) {
581+
Array.prototype.findIndex = function(predicate) {
582+
if (this === null) {
583+
throw new TypeError('Array.prototype.findIndex called on null or undefined');
584+
}
585+
if (typeof predicate !== 'function') {
586+
throw new TypeError('predicate must be a function');
587+
}
588+
var list = Object(this);
589+
var length = list.length >>> 0;
590+
var thisArg = arguments[1];
591+
var value;
592+
593+
for (var i = 0; i < length; i++) {
594+
value = list[i];
595+
if (predicate.call(thisArg, value, i, list)) {
596+
return i;
597+
}
598+
}
599+
return -1;
600+
};
601+
}

0 commit comments

Comments
 (0)