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 select filter #1278

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

* Add [`select` filter](https://mozilla.github.io/nunjucks/templating.html#select).

3.2.1 (Mar 17 2020)
-------------------
* Replace yargs with commander to reduce number of dependencies. Merge of
Expand Down
27 changes: 27 additions & 0 deletions docs/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,33 @@ escaping enabled this variable will not be escaped.
foo <a href="http://www.example.com/">http://www.example.com/</a> bar
```

### select

Filters a sequence of objects by applying a test to each object, and only
selecting the objects with the test succeeding.

If no test is specified, each object will be evaluated as a boolean.

**Input**

```jinja
{% set numbers=[0, 1, 2, 3, 4, 5] %}

{{ numbers | select("odd") | join }}
{{ numbers | select("even") | join }}
{{ numbers | select("divisibleby", 3) | join }}
{{ numbers | select() | join }}
```

**Output**

```jinja
135
024
03
12345
```

### selectattr (only the single-argument form)

Filter a sequence of objects by applying a test to the specified attribute
Expand Down
11 changes: 11 additions & 0 deletions nunjucks/src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,17 @@ function rejectattr(arr, attr) {

exports.rejectattr = rejectattr;

function select(arr, testName = 'truthy', secondArg) {
const context = this;
const test = context.env.getTest(testName);

return arr.filter(function applyToTest(item) {
return test.call(context, item, secondArg);
});
}

exports.select = select;

function selectattr(arr, attr) {
return arr.filter((item) => !!item[attr]);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,22 @@
finish(done);
});

it('select', function(done) {
var context = {
numbers: [0, 1, 2, 3, 4, 5]
};

equal('{{ numbers | select("odd") | join }}', context, '135');

equal('{{ numbers | select("even") | join }}', context, '024');

equal('{{ numbers | select("divisibleby", 3) | join }}', context, '03');

equal('{{ numbers | select() | join }}', context, '12345');

finish(done);
});

it('selectattr', function(done) {
var foods = [{
tasty: true
Expand Down