Skip to content
This repository has been archived by the owner on Aug 26, 2020. It is now read-only.

Commit

Permalink
Add: Increment helper (#40)
Browse files Browse the repository at this point in the history
* Add: indexPlusOne helper

* Add: indexPlusOne description + example to README

* Add: Closing backtick to slice example

* Reposition: indexPlusOne helper below slice (so same as README order)

* Amend: Helper to receive argument by which to increment index value

* Remove: parseInt() from incrementBy argument as already number type

* Add: Increment helper to express test
  • Loading branch information
andygout authored Jan 17, 2017
1 parent 6e6a05c commit 75791ab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ Outputs contents if a complex boolean logic expression is satisfied. Uses string

### slice
Loop through a subset of items
- `{{#slice items limit="2" offset="4"}} some content {{/slice}}
- `{{#slice items limit="2" offset="4"}} some content {{/slice}}`

### increment
Displays `@index` value added to given argument (e.g. an argument of `1` will display ordinal not cardinal numbers, i.e. values start from 1 not 0).
- `{{#each array}}{{#increment 1}}{{@index}}{{/increment}}{{/each}}`


## Presenter helpers

Expand Down
1 change: 1 addition & 0 deletions src/extend-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function (helpers) {
helpers.defineBlock = require('./helpers/defineBlock');
helpers.outputBlock = require('./helpers/outputBlock');
helpers.slice = require('./helpers/slice');
helpers.increment = require('./helpers/increment');
helpers.json = require('./helpers/json');
helpers.concat = require('./helpers/concat');
helpers.usePartial = require('./helpers/usePartial');
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/increment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function (incrementBy, options) {
return parseInt(options.fn(this)) + incrementBy;
};
6 changes: 6 additions & 0 deletions test/express.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ describe('express handlebars setup', function() {
.get('/templated')
.expect(200, /slice\:34\:end/, done);
});

it('should provide an index increment helper', function(done) {
request(app)
.get('/templated')
.expect(200, /increment\:12345\:end/, done);
});
});

describe('logic helpers', function () {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/app/views/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ <h2>Dates test:-</h2>
ifBool:{{#ifBool thing1 thing2 nothing "$0 && ($1 || $2)"}}first{{else}}not first{{/ifBool}} {{#ifBool @root/thing2 nothing thing1 "($0 && $1) && $2"}}second{{else}}not second{{/ifBool}}

slice:{{#slice items limit="2" offset="2"}}{{this}}{{/slice}}:end
increment:{{#each items}}{{#increment 1}}{{@index}}{{/increment}}{{/each}}:end

{{encode "http://domain.com?q=this / that" mode='uri'}} {{encode "http://domain.com?q=this / that"}}
{{#resize 200}}http://image.jpg{{/resize}}
Expand Down

0 comments on commit 75791ab

Please sign in to comment.