angular-filter
angular-filter copied to clipboard
allow replacing null keys with specified value in groupBy filter
I gave up on trying to figure out why, but the combination of groupBy, toArray and angular's built-in orderBy filter had some disastrous side effects for me. So added an option to replace null/undefined keys as a means to control where missing keys fall in the natural ordering.
It seems to build OK and the tests pass, I just didn't check the combined files in because a "reverseArray" function that I didn't touch is showing as removed in angular-filter.js.
@AlexCuse Thx for the PR.
Could you provide a Plunker example (plnkr.co) ?
Why not use the defaults
filter for undefined
values ?
e.g:
$scope.players = [
{ name: 'Gene', team: 'alpha' },
{ name: 'George', team: 'beta' },
{ name: 'Steve', team: 'gamma' },
{ name: 'Paula', team: 'beta' },
{ name: 'Scruath',team: 'gamma' },
{ name: 'Dan' },
{ name: 'Alex' }
];
$scope.fallback = { team: 'others' };
<ul ng-repeat="(key, value) in players | defaults:fallback | groupBy: 'team'" >
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }}
</li>
</ul>
let me know what do you think...
I didn't see the defaults filter, does seem like that would work.
Here is a plunker using defaults: http://plnkr.co/edit/rEDbqLkQ4ZaZ9z2Dlmro?p=info
And another using the null replacement syntax I added: http://plnkr.co/edit/RYc3RriigIJYgHF9vGlJ
As long as the defaults can be inlined like that (so I can tell why I am doing something special with the default value later on) I think defaults would be fine, if a bit less concise.