Skip to content

Commit

Permalink
Merge pull request #575 from xnimorz/range-function
Browse files Browse the repository at this point in the history
Add negative steps support for range function
  • Loading branch information
carljm committed Nov 5, 2015
2 parents f5ab716 + c6f0ab2 commit 50e4963
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
master (unreleased)
-------------------

* Add negative steps support for range function
* Remove deprecation warning when using the `default` filter without specifying a third argument. Merge of
[#567](https://github.com/mozilla/nunjucks/pull/567).
* Add support for chaining of addGlobal, addFilter, etc. Thanks Rob Graeber. Merge of
Expand Down
11 changes: 9 additions & 2 deletions src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ var globals = {
}

var arr = [];
for(var i=start; i<stop; i+=step) {
arr.push(i);
var i;
if (step > 0) {
for (i=start; i<stop; i+=step) {
arr.push(i);
}
} else {
for (i=start; i>stop; i+=step) {
arr.push(i);
}
}
return arr;
},
Expand Down
4 changes: 2 additions & 2 deletions tests/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
equal('{% for i in range(5, 10, 2.5) %}{{ i }}{% endfor %}', '57.5');
equal('{% for i in range(5, 10, 2.5) %}{{ i }}{% endfor %}', '57.5');

//equal('{% for i in range(5, 10, -1) %}{{ i }}{% endfor %}', '56789');
//equal('{% for i in range(5, 10, -1 | abs) %}{{ i }}{% endfor %}','56789');
equal('{% for i in range(10, 5, -1) %}{{ i }}{% endfor %}', '109876');
equal('{% for i in range(10, 5, -2.5) %}{{ i }}{% endfor %}', '107.5');

finish(done);
});
Expand Down

0 comments on commit 50e4963

Please sign in to comment.