diff options
Diffstat (limited to 'js/vendor/angular-ui/modules/filters/unique/unique.js')
-rw-r--r-- | js/vendor/angular-ui/modules/filters/unique/unique.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/filters/unique/unique.js b/js/vendor/angular-ui/modules/filters/unique/unique.js new file mode 100644 index 000000000..382712297 --- /dev/null +++ b/js/vendor/angular-ui/modules/filters/unique/unique.js @@ -0,0 +1,45 @@ +/** + * Filters out all duplicate items from an array by checking the specified key + * @param [key] {string} the name of the attribute of each object to compare for uniqueness + if the key is empty, the entire object will be compared + if the key === false then no filtering will be performed + * @return {array} + */ +angular.module('ui.filters').filter('unique', function () { + + return function (items, filterOn) { + + if (filterOn === false) { + return items; + } + + if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) { + var hashCheck = {}, newItems = []; + + var extractValueToCompare = function (item) { + if (angular.isObject(item) && angular.isString(filterOn)) { + return item[filterOn]; + } else { + return item; + } + }; + + angular.forEach(items, function (item) { + var valueToCheck, isDuplicate = false; + + for (var i = 0; i < newItems.length; i++) { + if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) { + isDuplicate = true; + break; + } + } + if (!isDuplicate) { + newItems.push(item); + } + + }); + items = newItems; + } + return items; + }; +}); |