From 75791abea4640e4f1e2a03e4b49010db77dc7489 Mon Sep 17 00:00:00 2001 From: Andy Gout Date: Tue, 17 Jan 2017 15:03:37 +0000 Subject: [PATCH] Add: Increment helper (#40) * 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 --- README.md | 7 ++++++- src/extend-helpers.js | 1 + src/helpers/increment.js | 5 +++++ test/express.test.js | 6 ++++++ test/fixtures/app/views/main.html | 1 + 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/helpers/increment.js diff --git a/README.md b/README.md index e4b700c..4bf829a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/extend-helpers.js b/src/extend-helpers.js index ca7d247..52b7c24 100644 --- a/src/extend-helpers.js +++ b/src/extend-helpers.js @@ -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'); diff --git a/src/helpers/increment.js b/src/helpers/increment.js new file mode 100644 index 0000000..5eefef3 --- /dev/null +++ b/src/helpers/increment.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = function (incrementBy, options) { + return parseInt(options.fn(this)) + incrementBy; +}; diff --git a/test/express.test.js b/test/express.test.js index 15a12b1..c3dbe3a 100755 --- a/test/express.test.js +++ b/test/express.test.js @@ -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 () { diff --git a/test/fixtures/app/views/main.html b/test/fixtures/app/views/main.html index 8709251..65daa37 100644 --- a/test/fixtures/app/views/main.html +++ b/test/fixtures/app/views/main.html @@ -43,6 +43,7 @@

Dates test:-

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}}