Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In array filter #216

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1356,7 +1358,33 @@ Converts kilobytes into formatted display<br/>
<!--or: -->
<div ng-show="{{ array | map | sum | !==: num }}"></div>
```
## 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
<li ng-repeat="elm in array | inArray:filterarry:filterproperty">
{{ elm.a }}
</li>

<!--
result:
1 1 3
-->
```

#Changelog
###0.5.7
* fix issue #119

Expand Down
21 changes: 21 additions & 0 deletions src/_filter/array/in-array.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}]);

4 changes: 3 additions & 1 deletion src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ angular.module('angular.filter', [
'a8m.conditions',
'a8m.is-null',

'a8m.filter-watcher'
'a8m.filter-watcher',

'a8m.in-array'
]);
22 changes: 22 additions & 0 deletions test/spec/filter/array/in-array.js
Original file line number Diff line number Diff line change
@@ -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);
});

});