Skip to content

Commit cf82856

Browse files
committed
add null replacement to groupBy filter (to control sorting for null keys)
don't replace false keys
1 parent 2e7f289 commit cf82856

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

Diff for: 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 == null || val == undefined)){
21+
return nullReplacement;
22+
}
23+
return val;
24+
},
1825
prop;
1926

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

Diff for: test/spec/filter/collection/group-by.js

+62
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,66 @@ 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+
{name: 'Sven', team: false }
88+
];
89+
90+
expect(filter(players, 'team', 'a')).toEqual( {
91+
a: [players[5], players[6]],
92+
alpha: [players[0]],
93+
beta: [players[1], players[3]],
94+
gamma: [players[2], players[4]],
95+
'false': [players[7]]
96+
});
97+
98+
});
99+
100+
it('should replace null when asked, support nested properties to', function() {
101+
var orders = [
102+
{ id:10, customer: { name: 'foo', id: 1 } },
103+
{ id:11, customer: { name: 'bar', id: 2 } },
104+
{ id:12, customer: { name: 'foo', id: 1 } },
105+
{ id:13, customer: { name: 'bar', id: 2 } },
106+
{ id:14, customer: { name: 'bar', id: 3 } },
107+
{ id:15, customer: { name: null } },
108+
{ id:16, customer: {} },
109+
2, null, true
110+
];
111+
112+
expect(filter(orders, 'customer.name', 0)).toEqual( {
113+
foo: [orders[0], orders[2]],
114+
bar: [orders[1], orders[3], orders[4]],
115+
0: [orders[5], orders[6], 2, null, true]
116+
});
117+
118+
});
119+
120+
121+
it('should replace null when asked, get object as collection, property(nested to) as identifier and ' +
122+
'returns the composed aggregate object.', function() {
123+
var dataObject = {
124+
0: { id: 1, data: {} },
125+
1: { id: 1, data: {} },
126+
2: { id: 2, data: {} },
127+
3: { id: 2, data: {} },
128+
4: { id: null, data: {} }
129+
};
130+
131+
expect(filter(dataObject, 'id', 0)).toEqual({
132+
0: [dataObject[4]],
133+
1: [dataObject[0], dataObject[1]],
134+
2: [dataObject[2], dataObject[3]]
135+
});
136+
137+
});
76138
});

0 commit comments

Comments
 (0)