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

Now we can compare more than two variables #19

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
ignore:
- dependency-name: sinon
versions:
- 10.0.0
- 9.2.4
- dependency-name: mocha
versions:
- 8.2.1
- 8.3.0
- 8.3.1
- dependency-name: chai
versions:
- 4.2.0
- 4.3.0
- 4.3.1
- 4.3.3
- dependency-name: handlebars
versions:
- 4.7.6
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ var template = '{{#is x "same" y}} Are the same {{else}} Not the same {{/is}}';
Handlebars.compile(template)({ x: 5, y: '5' }); // => " Not the same "
```

### More complex comparisons

You can also compare more than two variables by using `are`:
```
{{#are x ">" y "and" x "not" z}} ... {{else}} ... {{/are}}
{{#are x "===" y "or" x ">=" z}} ... {{else}} ... {{/are}}
```

`are` add two more comparators:
* `and`
* `or`

### Logging

Log one or multiple values to the console:
Expand Down
3 changes: 2 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<script src="../src/helpers.js"></script>

<script>
document.writeln(Handlebars.compile('{{#is foo bar}}Defaults work.{{else}}:({{/is}}')({foo: false, bar: false}));
document.writeln(Handlebars.compile('{{#is foo bar}}Is helpers work.{{else}}:({{/is}}')({foo: false, bar: false}));
document.writeln(Handlebars.compile('{{#are foo "<" bar "or" foo ">" foo2 "and" foo "<" bar}}Are helpers work.{{else}}:({{/are}}')({foo: 5, bar: 10, foo2: 15}));
HandlebarsHelpersRegistry.add('foobar', function(l,r) { return false; });
document.writeln(Handlebars.compile('{{#is foo "foobar" bar}}:({{else}}And custom helpers work.{{/is}}')({foo: false, bar: false}));
</script>
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"author": "Dan Harper",
"license": "WTFPL",
"dependencies": {
"handlebars": "1.0.10"
"handlebars": "4.7.7"
},
"devDependencies": {
"mocha": "1.8.2",
"chai": "1.5.0",
"sinon": "1.6.0"
"mocha": "9.0.0",
"chai": "4.3.4",
"sinon": "11.1.1"
}
}
54 changes: 45 additions & 9 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var isArray = function(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
};

var ExpressionRegistry = function() {
this.expressions = [];
Expand All @@ -28,7 +28,7 @@
return this.expressions[operator](left, right);
};

var eR = new ExpressionRegistry;
var eR = new ExpressionRegistry();
eR.add('not', function(left, right) {
return left != right;
});
Expand Down Expand Up @@ -56,14 +56,19 @@
}
return right.indexOf(left) !== -1;
});
eR.add('and', function(left, right) {
return left && right;
});
eR.add('or', function(left, right) {
return left || right;
});

var isHelper = function() {
var args = arguments
, left = args[0]
, operator = args[1]
, right = args[2]
, options = args[3]
;
var args = arguments,
left = args[0],
operator = args[1],
right = args[2],
options = args[3];

if (args.length == 2) {
options = args[1];
Expand All @@ -84,7 +89,38 @@
return options.inverse(this);
};

var areHelper = function() {
var args = arguments,
nbArgs = args.length - 1,
options = args[args.length - 1],
operators = [],
expResult = [];

if (nbArgs % 2 === 0 || parseInt(nbArgs % 3) + 1 !== parseInt(nbArgs / 3)) {
throw new Error('Invalid number of arguments');
}

for (var i = 0; i < nbArgs; i += 3) {
expResult.push(eR.call(args[i + 1], args[i], args[i + 2]));
if (i + 3 < nbArgs) {
operators.push(args[i + 3]);
++i;
}
}

for (i = 0; i < operators.length; ++i) {
var j = i - 1 < 0 ? 0 : i;
expResult[j + 1] = eR.call(operators[i], expResult[j], expResult[j + 1]);
}

if (expResult[expResult.length - 1]) {
return options.fn(this);
}
return options.inverse(this);
};

Handlebars.registerHelper('is', isHelper);
Handlebars.registerHelper('are', areHelper);

Handlebars.registerHelper('nl2br', function(text) {
var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
Expand All @@ -106,4 +142,4 @@

return eR;

}));
}));
31 changes: 30 additions & 1 deletion test/helpersTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('#is', function () {

describe('with three arguments', function () {
it('throws an error when an operator does not exist', function () {
(function () { c('{{#is foo "/" bar}}Y{{/is}}', {foo:'x', bar:'y'}) })
(function () { c('{{#is foo "/" bar}}Y{{/is}}', {foo:'x', bar:'y'}); })
.should.throw('Unknown operator "/"');
});

Expand Down Expand Up @@ -150,6 +150,35 @@ describe('#is', function () {
});
});

describe('#are', function() {
describe('With seven arguments', function() {
it('throws an error when an operator does not exist', function () {
(function () { c('{{#are foo ">=" bar "foobar" bar "!==" 10}}Y{{/are}}', {foo:5, bar:10}); })
.should.throw('Unknown operator "foobar"');
});

describe('the and operator', function () {
it('passes when left and right are true', function(){
c('{{#are foo "not" bar "and" foo "===" "f"}}Y{{/are}}', {foo:'f', bar:'b'}).should.equal('Y');
});

it('fails when left is true and right is false', function(){
c('{{#are foo "not" bar "and" foo "!==" 5}}Y{{/are}}', {foo:5, bar:'b'}).should.equal('');
});
});

describe('the or operator', function () {
it('passes when left is true and right is false', function(){
c('{{#are foo "not" bar "or" foo "!==" "a"}}Y{{/are}}', {foo:'f', bar:'b'}).should.equal('Y');
});

it('fails when left is false and right is false', function(){
c('{{#are foo "===" bar "or" foo "===" 10}}Y{{/are}}', {foo:5, bar:'b'}).should.equal('');
});
});
});
});

describe('#nl2br', function () {
it('Converts new lines to <br> tags', function () {
c('{{nl2br this}}', 'Hey\r\nThere!').should.equal('Hey<br>\r\nThere!');
Expand Down