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

Add support for finding object length (#283) #813

Merged
merged 2 commits into from
Aug 30, 2016
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@ Changelog
2.x (unreleased)
----------------

* Add support for finding an object's "length" in length filter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a link to the PR here, like in the other entries?

Merge of [#813](https://github.com/mozilla/nunjucks/pull/813).

* Ensure that precompiling on Windows still outputs POSIX-style path
separators. Merge of [#761](https://github.com/mozilla/nunjucks/pull/761).

6 changes: 5 additions & 1 deletion docs/templating.md
Original file line number Diff line number Diff line change
@@ -1209,18 +1209,22 @@ Get the last item in an array:

### length

Return the length of an array:
Return the length of an array or string, or the number of keys in an object:

**Input**

```jinja
{{ [1,2,3] | length }}
{{ "test" | length }}
{{ {key: value} | length }}
```

**Output**

```jinja
3
4
1
```


4 changes: 4 additions & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
@@ -183,6 +183,10 @@ var filters = {
// ECMAScript 2015 Maps and Sets
return value.size;
}
if(lib.isObject(value) && !(value instanceof r.SafeString)) {
// Objects (besides SafeStrings), non-primative Arrays
return Object.keys(value).length;
}
return value.length;
}
return 0;
12 changes: 12 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
@@ -261,9 +261,21 @@
equal('{{ [1,2,3] | length }}', '3');
equal('{{ blah|length }}', '0');
equal('{{ str | length }}', {str:r.markSafe('blah')}, '4');
equal('{{ str | length }}', {str:'blah'}, '4');
equal('{{ str | length }}', {str:new String('blah')}, '4'); //jshint ignore:line
equal('{{ undefined | length }}', '0');
equal('{{ null | length }}', '0');
equal('{{ nothing | length }}', '0');
equal('{{ obj | length }}', {obj: {}}, '0');
equal('{{ obj | length }}', {obj: {key: 'value'}}, '1');
equal('{{ obj | length }}', {obj: {key: 'value', length: 5}}, '2');
equal('{{ obj.length }}', {obj: {key: 'value', length: 5}}, '5');
equal('{{ arr | length }}', {arr: [0, 1]}, '2');
equal('{{ arr | length }}', {arr: [0, , 2]}, '3'); // jshint ignore:line
equal('{{ arr | length }}', {arr: new Array(0, 1)}, '2');
var arr = new Array(0, 1);
arr.key = 'value';
equal('{{ arr | length }}', {arr: arr}, '2');
if(typeof Map === 'function') {
var map = new Map([['key1', 'value1'], ['key2', 'value2']]);
map.set('key3', 'value3');