Skip to content

Commit 9cac7f0

Browse files
committed
add null replacement to groupBy filter (to control sorting for null keys)
1 parent 2e7f289 commit 9cac7f0

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/_filter/collection/group-by.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55
*
66
* @description
77
* Create an object composed of keys generated from the result of running each element of a collection,
8-
* each key is an array of the elements.
8+
* each key is an array of the elements. Specify null replacement to control sort behavior for null/undefined keys.
99
*/
1010

1111
angular.module('a8m.group-by', [ 'a8m.filter-watcher' ])
1212

1313
.filter('groupBy', [ '$parse', 'filterWatcher', function ( $parse, filterWatcher ) {
14-
return function (collection, property) {
14+
return function (collection, property, nullReplacement) {
1515

1616
var result,
17-
get = $parse(property),
17+
getParse = $parse(property),
18+
get = function(el) {
19+
var val = getParse(el);
20+
if(nullReplacement !== undefined && !val){
21+
return nullReplacement;
22+
}
23+
return val;
24+
},
1825
prop;
1926

2027
if(!isObject(collection) || isUndefined(property)) {

test/spec/filter/collection/group-by.js

+60
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,64 @@ describe('groupByFilter', function() {
7373

7474
});
7575

76+
it('should replace null when asked, get array as collection, property(nested to) as identifier and ' +
77+
'returns the composed aggregate object.', function() {
78+
79+
var players = [
80+
{name: 'Gene', team: 'alpha'},
81+
{name: 'George', team: 'beta'},
82+
{name: 'Steve', team: 'gamma'},
83+
{name: 'Paula', team: 'beta'},
84+
{name: 'Scruath', team: 'gamma'},
85+
{name: 'Alex', team: null },
86+
{name: 'Rob' }
87+
];
88+
89+
expect(filter(players, 'team', 'a')).toEqual( {
90+
a: [players[5], players[6]],
91+
alpha: [players[0]],
92+
beta: [players[1], players[3]],
93+
gamma: [players[2], players[4]]
94+
});
95+
96+
});
97+
98+
it('should replace null when asked, support nested properties to', function() {
99+
var orders = [
100+
{ id:10, customer: { name: 'foo', id: 1 } },
101+
{ id:11, customer: { name: 'bar', id: 2 } },
102+
{ id:12, customer: { name: 'foo', id: 1 } },
103+
{ id:13, customer: { name: 'bar', id: 2 } },
104+
{ id:14, customer: { name: 'bar', id: 3 } },
105+
{ id:15, customer: { name: null } },
106+
{ id:16, customer: {} },
107+
2, null, true
108+
];
109+
110+
expect(filter(orders, 'customer.name', 0)).toEqual( {
111+
foo: [orders[0], orders[2]],
112+
bar: [orders[1], orders[3], orders[4]],
113+
0: [orders[5], orders[6], 2, null, true]
114+
});
115+
116+
});
117+
118+
119+
it('should replace null when asked, get object as collection, property(nested to) as identifier and ' +
120+
'returns the composed aggregate object.', function() {
121+
var dataObject = {
122+
0: { id: 1, data: {} },
123+
1: { id: 1, data: {} },
124+
2: { id: 2, data: {} },
125+
3: { id: 2, data: {} },
126+
4: { id: null, data: {} }
127+
};
128+
129+
expect(filter(dataObject, 'id', 0)).toEqual({
130+
0: [dataObject[4]],
131+
1: [dataObject[0], dataObject[1]],
132+
2: [dataObject[2], dataObject[3]]
133+
});
134+
135+
});
76136
});

0 commit comments

Comments
 (0)