diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..297131b --- /dev/null +++ b/.github/dependabot.yml @@ -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 diff --git a/README.md b/README.md index f0e249e..1e4e88d 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/demo/index.html b/demo/index.html index e0543ec..1617772 100644 --- a/demo/index.html +++ b/demo/index.html @@ -2,7 +2,8 @@ \ No newline at end of file diff --git a/package.json b/package.json index b637807..ec8bdca 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/helpers.js b/src/helpers.js index 02c1cef..7bee725 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -10,7 +10,7 @@ var isArray = function(value) { return Object.prototype.toString.call(value) === '[object Array]'; - } + }; var ExpressionRegistry = function() { this.expressions = []; @@ -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; }); @@ -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]; @@ -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' + '
' + '$2'); @@ -106,4 +142,4 @@ return eR; -})); \ No newline at end of file +})); diff --git a/test/helpersTest.js b/test/helpersTest.js index 958fe83..69a8a2b 100644 --- a/test/helpersTest.js +++ b/test/helpersTest.js @@ -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 "/"'); }); @@ -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
tags', function () { c('{{nl2br this}}', 'Hey\r\nThere!').should.equal('Hey
\r\nThere!');