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..3f1fc1b --- /dev/null +++ b/src/_filter/array/in-array.js @@ -0,0 +1,21 @@ +/** + * @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.in-array', []) + .filter('inArray', ['$filter', function($filter) { + return function(list, arrayFilter, element){ + if (arrayFilter) { + return $filter("filter")(list, function(listItem) { + return arrayFilter.indexOf(listItem[element]) != -1; + }); + } else { + return list; + } + } + }]); + 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..2abf69a --- /dev/null +++ b/test/spec/filter/array/in-array.js @@ -0,0 +1,22 @@ +'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(srcarray, [5, 6], filterproperty).length).toEqual(0); + expect(filter(srcarray, undefined, undefined)).toEqual(srcarray); + }); + +});