Skip to content

Commit 8e0af23

Browse files
committed
feat(and): create and filter
1 parent ab4db01 commit 8e0af23

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- [unique](#unique)
4545
- [where](#where)
4646
- [xor](#xor)
47+
- [and](#and)
4748
- [String](#string)
4849
- [endsWith](#endswith)
4950
- [repeat](#repeat)
@@ -888,6 +889,44 @@ $scope.users2 = [
888889
<!--result:
889890
2, foo bag
890891
```
892+
###and
893+
And between two collections<br/>
894+
**Usage:** ```collection1 | and: collection2: expression[optional]```<br/>
895+
896+
Example1:
897+
```html
898+
<p ng-repeat="elm in [1,2,3,4] | and: [2,3,5]">
899+
{{ elm }}
900+
</p>
901+
<!--result:
902+
2 3
903+
```
904+
Example2:
905+
```js
906+
$scope.users1 = [
907+
{ id: 0, details: { first_name: 'foo', last_name: 'bar' } },
908+
{ id: 1, details: { first_name: 'foo', last_name: 'baz' } },
909+
{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }
910+
];
911+
$scope.users2 = [
912+
{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }
913+
{ id: 3, details: { first_name: 'foo', last_name: 'baz' } }
914+
];
915+
```
916+
```html
917+
<th ng-repeat="user in users1 | and: users2">
918+
{{ user.id }}
919+
</th>
920+
<!--result:
921+
2
922+
-->
923+
<th ng-repeat="user in users1 | and: users2: 'details.last_name'">
924+
{{ user.id }}, {{ user.details.first_name }} {{ user.details.last_name }}
925+
</th>
926+
<!--result:
927+
1, foo baz
928+
2, foo bag
929+
```
891930
###toArray
892931
Convert objects into stable arrays. <br/>
893932
**Usage:** ```object | toArray: addKey[optional]```<br/>

src/_filter/collection/and.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @ngdoc filter
3+
* @name and
4+
* @kind function
5+
*
6+
* @description
7+
* And filter by expression
8+
*/
9+
10+
angular.module('a8m.and', [])
11+
12+
.filter('and', ['$parse', function($parse) {
13+
return function (col1, col2, expression) {
14+
15+
expression = expression || false;
16+
17+
col1 = isObject(col1) ? toArray(col1) : col1;
18+
col2 = isObject(col2) ? toArray(col2) : col2;
19+
20+
if(!isArray(col1) || !isArray(col2)) return col1;
21+
22+
return col1.filter(function(elm) {
23+
return some(elm, col1) && some(elm, col2);
24+
});
25+
26+
function some(el, col) {
27+
var getter = $parse(expression);
28+
return col.some(function(dElm) {
29+
return expression
30+
? equals(getter(dElm), getter(el))
31+
: equals(dElm, el);
32+
});
33+
}
34+
}
35+
}]);

src/filters.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ angular.module('angular.filter', [
5050
'a8m.every',
5151
'a8m.filter-by',
5252
'a8m.xor',
53+
'a8m.and',
5354
'a8m.map',
5455
'a8m.first',
5556
'a8m.last',

test/spec/filter/collection/and.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
describe('andFilter', function() {
4+
var filter;
5+
6+
beforeEach(module('a8m.and'));
7+
8+
beforeEach(inject(function($filter) {
9+
filter = $filter('and');
10+
}));
11+
12+
it('should get 2 array collection and return exclusive or between them', function() {
13+
expect(filter([1,2], [1])).toEqual([1]);
14+
expect(filter([1, 2, 3], [5, 2, 1, 4])).toEqual([1, 2]);
15+
16+
expect(filter([1, 2, 3], [4, 5])).toEqual([]);
17+
expect(filter([1, 2, 3], [2, 3, 4])).toEqual([2, 3]);
18+
});
19+
20+
it('should get objects as collection', function() {
21+
expect(filter({ 0: 1, 1: 2 }, { 0: 3 })).toEqual([]);
22+
expect(filter({ 0: 1, 1: 2 }, { 0: 1 })).toEqual([1]);
23+
});
24+
25+
it('should get an objects collection and filters by value', function() {
26+
27+
var array = [
28+
{ id: 1, name: 'foo' },
29+
{ id: 2, name: 'bar' },
30+
{ id: 3, name: 'baz' }
31+
];
32+
33+
expect(filter(array, array)).toEqual(array);
34+
expect(filter(array, [ { id: 1, name:'foo' } ])).toEqual([array[0]]);
35+
36+
expect(filter(array, [ { id: 1 } ])).toEqual([]);
37+
});
38+
39+
it('should filter by specific property', function() {
40+
var users = [
41+
{ id: 0, details: { first_name: 'foo', last_name: 'bar' } },
42+
{ id: 1, details: { first_name: 'foo', last_name: 'baz' } },
43+
{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }
44+
];
45+
46+
expect(filter(users, [{ details: { first_name: 'foo' } }], 'details.first_name'))
47+
.toEqual(users);
48+
expect(filter(users, [{ id: 0 }, { id: 1 }], 'id')).toEqual([users[0], users[1]]);
49+
50+
expect(filter(users, [{ id: 3, details: { first_name: 'foo', last_name: 'bag' }}], 'id'))
51+
.toEqual([]);
52+
});
53+
54+
it('should filter by expression', function() {
55+
expect(filter([ { id: 2 }, { id: 3 }], [ { id: 4 } ], 'id % 2')).toEqual([{ id: 2 }]);
56+
});
57+
58+
it('should get !collection and return it as-is', function() {
59+
expect(filter(999)).toEqual(999);
60+
expect(filter(!1)).toBeFalsy();
61+
expect(null).toEqual(null);
62+
});
63+
64+
});

0 commit comments

Comments
 (0)