Skip to content
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
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- [unique](#unique)
- [where](#where)
- [xor](#xor)
- [and](#and)
- [String](#string)
- [endsWith](#endswith)
- [repeat](#repeat)
Expand Down Expand Up @@ -888,6 +889,44 @@ $scope.users2 = [
<!--result:
2, foo bag
```
###and
And between two collections<br/>
**Usage:** ```collection1 | and: collection2: expression[optional]```<br/>

Example1:
```html
<p ng-repeat="elm in [1,2,3,4] | and: [2,3,5]">
{{ elm }}
</p>
<!--result:
2 3
```
Example2:
```js
$scope.users1 = [
{ id: 0, details: { first_name: 'foo', last_name: 'bar' } },
{ id: 1, details: { first_name: 'foo', last_name: 'baz' } },
{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }
];
$scope.users2 = [
{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }
{ id: 3, details: { first_name: 'foo', last_name: 'baz' } }
];
```
```html
<th ng-repeat="user in users1 | and: users2">
{{ user.id }}
</th>
<!--result:
2
-->
<th ng-repeat="user in users1 | and: users2: 'details.last_name'">
{{ user.id }}, {{ user.details.first_name }} {{ user.details.last_name }}
</th>
<!--result:
1, foo baz
2, foo bag
```
###toArray
Convert objects into stable arrays. <br/>
**Usage:** ```object | toArray: addKey[optional]```<br/>
Expand Down
35 changes: 35 additions & 0 deletions src/_filter/collection/and.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @ngdoc filter
* @name and
* @kind function
*
* @description
* And filter by expression
*/

angular.module('a8m.and', [])

.filter('and', ['$parse', function($parse) {
return function (col1, col2, expression) {

expression = expression || false;

col1 = isObject(col1) ? toArray(col1) : col1;
col2 = isObject(col2) ? toArray(col2) : col2;

if(!isArray(col1) || !isArray(col2)) return col1;

return col1.filter(function(elm) {
return some(elm, col1) && some(elm, col2);
});

function some(el, col) {
var getter = $parse(expression);
return col.some(function(dElm) {
return expression
? equals(getter(dElm), getter(el))
: equals(dElm, el);
});
}
}
}]);
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ angular.module('angular.filter', [
'a8m.every',
'a8m.filter-by',
'a8m.xor',
'a8m.and',
'a8m.map',
'a8m.first',
'a8m.last',
Expand Down
64 changes: 64 additions & 0 deletions test/spec/filter/collection/and.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

describe('andFilter', function() {
var filter;

beforeEach(module('a8m.and'));

beforeEach(inject(function($filter) {
filter = $filter('and');
}));

it('should get 2 array collection and return exclusive or between them', function() {
expect(filter([1,2], [1])).toEqual([1]);
expect(filter([1, 2, 3], [5, 2, 1, 4])).toEqual([1, 2]);

expect(filter([1, 2, 3], [4, 5])).toEqual([]);
expect(filter([1, 2, 3], [2, 3, 4])).toEqual([2, 3]);
});

it('should get objects as collection', function() {
expect(filter({ 0: 1, 1: 2 }, { 0: 3 })).toEqual([]);
expect(filter({ 0: 1, 1: 2 }, { 0: 1 })).toEqual([1]);
});

it('should get an objects collection and filters by value', function() {

var array = [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
{ id: 3, name: 'baz' }
];

expect(filter(array, array)).toEqual(array);
expect(filter(array, [ { id: 1, name:'foo' } ])).toEqual([array[0]]);

expect(filter(array, [ { id: 1 } ])).toEqual([]);
});

it('should filter by specific property', function() {
var users = [
{ id: 0, details: { first_name: 'foo', last_name: 'bar' } },
{ id: 1, details: { first_name: 'foo', last_name: 'baz' } },
{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }
];

expect(filter(users, [{ details: { first_name: 'foo' } }], 'details.first_name'))
.toEqual(users);
expect(filter(users, [{ id: 0 }, { id: 1 }], 'id')).toEqual([users[0], users[1]]);

expect(filter(users, [{ id: 3, details: { first_name: 'foo', last_name: 'bag' }}], 'id'))
.toEqual([]);
});

it('should filter by expression', function() {
expect(filter([ { id: 2 }, { id: 3 }], [ { id: 4 } ], 'id % 2')).toEqual([{ id: 2 }]);
});

it('should get !collection and return it as-is', function() {
expect(filter(999)).toEqual(999);
expect(filter(!1)).toBeFalsy();
expect(null).toEqual(null);
});

});