Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ECMAScript 2015 Map and Set in length filter #705

Merged
merged 1 commit into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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