Skip to content

Commit

Permalink
Merge pull request #705 from ricordisamoa/mapset
Browse files Browse the repository at this point in the history
Support ECMAScript 2015 Map and Set in length filter
  • Loading branch information
carljm committed Apr 5, 2016
1 parent f521bad commit eac7619
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,17 @@ var filters = {
length: function(val) {
var value = normalize(val, '');

return value !== undefined ? value.length : 0;
if(value !== undefined) {
if(
(typeof Map === 'function' && value instanceof Map) ||
(typeof Set === 'function' && value instanceof Set)
) {
// ECMAScript 2015 Maps and Sets
return value.size;
}
return value.length;
}
return 0;
},

list: function(val) {
Expand Down
10 changes: 10 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@
equal('{{ undefined | length }}', '0');
equal('{{ null | length }}', '0');
equal('{{ nothing | length }}', '0');
if(typeof Map === 'function') {
var map = new Map([['key1', 'value1'], ['key2', 'value2']]);
map.set('key3', 'value3');
equal('{{ map | length }}', {map: map}, '3');
}
if(typeof Set === 'function') {
var set = new Set(['value1']);
set.add('value2');
equal('{{ set | length }}', {set: set}, '2');
}
finish(done);
});

Expand Down

0 comments on commit eac7619

Please sign in to comment.