Skip to content

Commit bb829aa

Browse files
feat: multi concat (#236)
1 parent 9917efe commit bb829aa

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const helpersList = [
3636
'limit',
3737
'moment',
3838
'money',
39+
'multiConcat',
3940
'nl2br',
4041
'occurrences',
4142
'option',

helpers/multiConcat.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
/**
4+
* Concats multi values, primarily used as a subhelper
5+
* @example
6+
* {{multiConcat "string1" "string2" "string3"}}
7+
*/
8+
9+
10+
const factory = globals => {
11+
return function(...args) {
12+
// Take the last arg which is a Handlebars options object out of args array
13+
args.pop();
14+
15+
return args.join('');
16+
};
17+
18+
};
19+
20+
21+
module.exports = [{
22+
name: 'multiConcat',
23+
factory: factory,
24+
}];

spec/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('helper registration', () => {
5454
'limit',
5555
'moment',
5656
'money',
57+
'multiConcat',
5758
'nl2br',
5859
'occurrences',
5960
'option',

spec/helpers/multiConcat.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const Lab = require('lab'),
2+
lab = exports.lab = Lab.script(),
3+
describe = lab.experiment,
4+
it = lab.it,
5+
testRunner = require('../spec-helpers').testRunner;
6+
7+
8+
describe('multiConcat helper', function() {
9+
const context = {
10+
string1: "First",
11+
string2: "Second",
12+
string3: "Third",
13+
string4: "Fourth"
14+
};
15+
16+
const runTestCases = testRunner({context});
17+
18+
it('should concatenate all strings by default', function(done) {
19+
runTestCases([
20+
{
21+
input: '{{multiConcat string1 string2 string3}}',
22+
output: 'FirstSecondThird',
23+
},
24+
{
25+
input: '{{multiConcat string1 string2 string3 string4}}',
26+
output: 'FirstSecondThirdFourth',
27+
},
28+
{
29+
input: '{{multiConcat string1 string2}}',
30+
output: 'FirstSecond',
31+
}
32+
], done);
33+
});
34+
35+
it('should accept string, number, boolean, empty', function(done) {
36+
runTestCases([
37+
{
38+
input: '{{multiConcat "First" 2}}',
39+
output: 'First2',
40+
},
41+
{
42+
input: '{{multiConcat string1 3 "" "4" true}}',
43+
output: 'First34true',
44+
},
45+
{
46+
input: '{{multiConcat string1 3 false "" "4" true}}',
47+
output: 'First3false4true',
48+
},
49+
{
50+
input: '{{multiConcat string1 ""}}',
51+
output: 'First',
52+
}
53+
], done);
54+
});
55+
});

0 commit comments

Comments
 (0)