summaryrefslogtreecommitdiffstats
path: root/js/vendor/angular-ui/modules/filters/unique
diff options
context:
space:
mode:
Diffstat (limited to 'js/vendor/angular-ui/modules/filters/unique')
-rw-r--r--js/vendor/angular-ui/modules/filters/unique/test/uniqueSpec.js81
-rw-r--r--js/vendor/angular-ui/modules/filters/unique/unique.js45
2 files changed, 126 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/filters/unique/test/uniqueSpec.js b/js/vendor/angular-ui/modules/filters/unique/test/uniqueSpec.js
new file mode 100644
index 000000000..ae1251f4b
--- /dev/null
+++ b/js/vendor/angular-ui/modules/filters/unique/test/uniqueSpec.js
@@ -0,0 +1,81 @@
+describe('unique', function () {
+ var uniqueFilter;
+
+ beforeEach(module('ui.filters'));
+ beforeEach(inject(function ($filter) {
+ uniqueFilter = $filter('unique');
+ }));
+
+ it('should return unique entries based on object equality', function () {
+ var arrayToFilter = [
+ {key: 'value'},
+ {key: 'value2'},
+ {key: 'value'}
+ ];
+ expect(uniqueFilter(arrayToFilter)).toEqual([
+ {key: 'value'},
+ {key: 'value2'}
+ ]);
+ });
+
+ it('should return unique entries based on object equality for complex objects', function () {
+ var arrayToFilter = [
+ {key: 'value', other: 'other1'},
+ {key: 'value2', other: 'other2'},
+ {other: 'other1', key: 'value'}
+ ];
+ expect(uniqueFilter(arrayToFilter)).toEqual([
+ {key: 'value', other: 'other1'},
+ {key: 'value2', other: 'other2'}
+ ]);
+ });
+
+ it('should return unique entries based on the key provided', function () {
+ var arrayToFilter = [
+ {key: 'value'},
+ {key: 'value2'},
+ {key: 'value'}
+ ];
+ expect(uniqueFilter(arrayToFilter, 'key')).toEqual([
+ {key: 'value'},
+ {key: 'value2'}
+ ]);
+ });
+
+ it('should return unique entries based on the key provided for complex objects', function () {
+ var arrayToFilter = [
+ {key: 'value', other: 'other1'},
+ {key: 'value2', other: 'other2'},
+ {key: 'value', other: 'other3'}
+ ];
+ expect(uniqueFilter(arrayToFilter, 'key')).toEqual([
+ { key: 'value', other: 'other1' },
+ { key: 'value2', other: 'other2' }
+ ]);
+ });
+
+ it('should return unique primitives in arrays', function () {
+ expect(uniqueFilter([1, 2, 1, 3])).toEqual([1, 2, 3]);
+ });
+
+ it('should work correctly for arrays of mixed elements and object equality', function () {
+ expect(uniqueFilter([1, {key: 'value'}, 1, {key: 'value'}, 2, "string", 3])).toEqual([1, {key: 'value'}, 2, "string", 3]);
+ });
+
+ it('should work correctly for arrays of mixed elements and a key specified', function () {
+ expect(uniqueFilter([1, {key: 'value'}, 1, {key: 'value'}, 2, "string", 3], 'key')).toEqual([1, {key: 'value'}, 2, "string", 3]);
+ });
+
+ it('should return unmodified object if not array', function () {
+ expect(uniqueFilter('string', 'someKey')).toEqual('string');
+ });
+
+ it('should return unmodified array if provided key === false', function () {
+ var arrayToFilter = [
+ {key: 'value1'},
+ {key: 'value2'}
+ ];
+ expect(uniqueFilter(arrayToFilter, false)).toEqual(arrayToFilter);
+ });
+
+}); \ No newline at end of file
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;
+ };
+});