From e80896f5c9496a11ef603f7273891b23edc4ed36 Mon Sep 17 00:00:00 2001 From: Rosina Bignall Date: Tue, 9 Aug 2016 12:57:39 -0700 Subject: [PATCH 1/4] Add inArray filter --- README.md | 30 +++++++++++++++++++++++++++++- src/_filter/array/in-array.js | 19 +++++++++++++++++++ src/filters.js | 4 +++- test/spec/filter/array/in-array.js | 21 +++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/_filter/array/in-array.js create mode 100644 test/spec/filter/array/in-array.js diff --git a/README.md b/README.md index 5c1bfda..4b6a75d 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ Bunch of useful filters for AngularJS *(with no external dependencies!)* - [isNotEqualTo](#isnotequalto) `!=` - [isIdenticalTo](#isidenticalto) `===` - [isNotIdenticalTo](#isnotidenticalto) `!==` +- [Array](#array) + - [inArray](#inarray) ## Get Started **(1)** You can install angular-filter using 4 different methods: @@ -1356,7 +1358,33 @@ Converts kilobytes into formatted display
``` -## Changelog + +#Array +Filters for arrays + +###inArray + +Filters the objects in an array by the values in another array +```js +function MainController ($scope) { + $scope.array = [ {a: 1}, {a: 2}, {a:1}, {a:3} ]; + $scope.filterarray = [1, 3]; + $scope.filterproperty = 'a'; +} +``` + +```html +
  • + {{ elm.a }} +
  • + + +``` + +#Changelog ###0.5.7 * fix issue #119 diff --git a/src/_filter/array/in-array.js b/src/_filter/array/in-array.js new file mode 100644 index 0000000..c5b395f --- /dev/null +++ b/src/_filter/array/in-array.js @@ -0,0 +1,19 @@ +/** + * @ngdoc filter + * @name inArray + * @kind function + * + * @description + * filter an array by a property in another array based on example http://jsbin.com/owIXEPE/2/edit?html,js,output + */ +angular.module('a8m.filter-by', []) + .filter('inArray', function($filter){ + return function(list, arrayFilter, element){ + if(arrayFilter){ + return $filter("filter")(list, function(listItem){ + return arrayFilter.indexOf(listItem[element]) != -1; + }); + } + }; + }); + diff --git a/src/filters.js b/src/filters.js index 19f5ddb..2b5c079 100644 --- a/src/filters.js +++ b/src/filters.js @@ -73,5 +73,7 @@ angular.module('angular.filter', [ 'a8m.conditions', 'a8m.is-null', - 'a8m.filter-watcher' + 'a8m.filter-watcher', + + 'a8m.in-array' ]); diff --git a/test/spec/filter/array/in-array.js b/test/spec/filter/array/in-array.js new file mode 100644 index 0000000..738fb3c --- /dev/null +++ b/test/spec/filter/array/in-array.js @@ -0,0 +1,21 @@ +'use strict'; + +describe('inArrayFilter', function() { + var filter; + + beforeEach(module('a8m.in-array')); + beforeEach(inject(function($filter) { + filter = $filter('inArray'); + })); + + it('should filter by specific properties and avoid the rest', function() { + var srcarray = [ {a: 1}, {a: 2}, {a:1}, {a:3} ]; + var filterarray = [1, 3]; + var filterproperty = 'a'; + var result = [ {a: 1}, {a: 1}, {a: 3} ]; + + expect(filter(srcarray, filterarray, filterproperty)).toEqual(result); + expect(filter(users, [5, 6], filterproperty)).length == 0); + }); + +}); From 938bcf4b8e24f1dcc5908e09592ca3d8466a0a30 Mon Sep 17 00:00:00 2001 From: Rosina Bignall Date: Sat, 20 Aug 2016 20:14:40 -0700 Subject: [PATCH 2/4] Add option for when arrayFilter is not defined --- src/_filter/array/in-array.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/_filter/array/in-array.js b/src/_filter/array/in-array.js index c5b395f..2bc0535 100644 --- a/src/_filter/array/in-array.js +++ b/src/_filter/array/in-array.js @@ -6,14 +6,16 @@ * @description * filter an array by a property in another array based on example http://jsbin.com/owIXEPE/2/edit?html,js,output */ -angular.module('a8m.filter-by', []) - .filter('inArray', function($filter){ +angular.module('a8m.in-array', []) + .filter('inArray', function($filter) { return function(list, arrayFilter, element){ - if(arrayFilter){ - return $filter("filter")(list, function(listItem){ + if (arrayFilter) { + return $filter("filter")(list, function(listItem) { return arrayFilter.indexOf(listItem[element]) != -1; }); + } else { + return list; } - }; + } }); From ec1ec6c35244ea7daf579e5912c4d42ab7b2fda4 Mon Sep 17 00:00:00 2001 From: Rosina Bignall Date: Wed, 24 Aug 2016 13:38:42 -0700 Subject: [PATCH 3/4] Fix tests --- test/spec/filter/array/in-array.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/spec/filter/array/in-array.js b/test/spec/filter/array/in-array.js index 738fb3c..2abf69a 100644 --- a/test/spec/filter/array/in-array.js +++ b/test/spec/filter/array/in-array.js @@ -15,7 +15,8 @@ describe('inArrayFilter', function() { var result = [ {a: 1}, {a: 1}, {a: 3} ]; expect(filter(srcarray, filterarray, filterproperty)).toEqual(result); - expect(filter(users, [5, 6], filterproperty)).length == 0); + expect(filter(srcarray, [5, 6], filterproperty).length).toEqual(0); + expect(filter(srcarray, undefined, undefined)).toEqual(srcarray); }); }); From 4e4c6c14fc2e83602c49f2b42e58afe3dceb347b Mon Sep 17 00:00:00 2001 From: Rosina Bignall Date: Wed, 7 Sep 2016 11:36:45 -0700 Subject: [PATCH 4/4] Fix reference to --- src/_filter/array/in-array.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_filter/array/in-array.js b/src/_filter/array/in-array.js index 2bc0535..3f1fc1b 100644 --- a/src/_filter/array/in-array.js +++ b/src/_filter/array/in-array.js @@ -7,7 +7,7 @@ * filter an array by a property in another array based on example http://jsbin.com/owIXEPE/2/edit?html,js,output */ angular.module('a8m.in-array', []) - .filter('inArray', function($filter) { + .filter('inArray', ['$filter', function($filter) { return function(list, arrayFilter, element){ if (arrayFilter) { return $filter("filter")(list, function(listItem) { @@ -17,5 +17,5 @@ angular.module('a8m.in-array', []) return list; } } - }); + }]);