From 3675a7f0206586972cd0c5328af0269755dfab88 Mon Sep 17 00:00:00 2001 From: Ludovic Lerus Date: Sun, 10 May 2015 23:37:46 +0200 Subject: [PATCH 1/9] 'and' and 'or' added (&& and ||) --- src/helpers.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/helpers.js b/src/helpers.js index 02c1cef..b4fc461 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -56,6 +56,12 @@ } 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 @@ -84,7 +90,40 @@ return options.inverse(this); }; + var areHelper = function() { + var args = arguments + , 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 (var 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'); From aebab94edc35519f21fa15b568ec612ca75e8b99 Mon Sep 17 00:00:00 2001 From: Ludovic Lerus Date: Sun, 10 May 2015 23:39:06 +0200 Subject: [PATCH 2/9] example for 'and' and 'or' added --- demo/index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From aa9c514f4fbd8e5f1e323301e0b840dda73ab02c Mon Sep 17 00:00:00 2001 From: Ludovic Lerus Date: Thu, 14 May 2015 14:33:00 +0200 Subject: [PATCH 3/9] Code cleaned --- src/helpers.js | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index b4fc461..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; }); @@ -64,12 +64,11 @@ }); 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]; @@ -91,15 +90,13 @@ }; var areHelper = function() { - var args = arguments - , args = arguments - , nbArgs = args.length - 1 - , options = args[args.length - 1] - , operators = [] - , expResult = [] - ; - - if (nbArgs % 2 == 0 || parseInt(nbArgs % 3) + 1 != parseInt(nbArgs / 3)) { + 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'); } @@ -111,7 +108,7 @@ } } - for (var i = 0; i < operators.length; ++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]); } @@ -120,7 +117,7 @@ return options.fn(this); } return options.inverse(this); - } + }; Handlebars.registerHelper('is', isHelper); Handlebars.registerHelper('are', areHelper); @@ -145,4 +142,4 @@ return eR; -})); \ No newline at end of file +})); From 1256c66202b35e212a526cbb243abe0a29f22137 Mon Sep 17 00:00:00 2001 From: Ludovic Lerus Date: Thu, 14 May 2015 15:47:22 +0200 Subject: [PATCH 4/9] README update and test added --- README.md | 12 ++++++++++++ test/helpersTest.js | 31 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) 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/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!'); From fe4d20ddbe447f6977981f7abf63420f9fcf01d6 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 29 Apr 2021 21:31:02 +0000 Subject: [PATCH 5/9] Upgrade to GitHub-native Dependabot --- .github/dependabot.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/dependabot.yml 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 From 85aae23f35f38ff03993cadf449a123326403b33 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 27 May 2021 04:58:14 +0000 Subject: [PATCH 6/9] Bump sinon from 1.6.0 to 11.1.1 Bumps [sinon](https://github.com/sinonjs/sinon) from 1.6.0 to 11.1.1. - [Release notes](https://github.com/sinonjs/sinon/releases) - [Changelog](https://github.com/sinonjs/sinon/blob/master/CHANGELOG.md) - [Commits](https://github.com/sinonjs/sinon/compare/v1.6.0...v11.1.1) Signed-off-by: dependabot-preview[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b637807..75c3ca3 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,6 @@ "devDependencies": { "mocha": "1.8.2", "chai": "1.5.0", - "sinon": "1.6.0" + "sinon": "11.1.1" } } From 64a875fa512dea7b8de15209bc2264e542f6d9d2 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 8 Jun 2021 05:06:09 +0000 Subject: [PATCH 7/9] Bump mocha from 1.8.2 to 9.0.0 Bumps [mocha](https://github.com/mochajs/mocha) from 1.8.2 to 9.0.0. - [Release notes](https://github.com/mochajs/mocha/releases) - [Changelog](https://github.com/mochajs/mocha/blob/master/CHANGELOG.md) - [Commits](https://github.com/mochajs/mocha/compare/1.8.2...v9.0.0) Signed-off-by: dependabot-preview[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b637807..9e8f6ff 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "handlebars": "1.0.10" }, "devDependencies": { - "mocha": "1.8.2", + "mocha": "9.0.0", "chai": "1.5.0", "sinon": "1.6.0" } From 99ef8153553bd44b9b8113994370d09e7cb4daba Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 07:08:34 +0000 Subject: [PATCH 8/9] Bump chai from 1.5.0 to 4.3.4 Bumps [chai](https://github.com/chaijs/chai) from 1.5.0 to 4.3.4. - [Release notes](https://github.com/chaijs/chai/releases) - [Changelog](https://github.com/chaijs/chai/blob/main/History.md) - [Commits](https://github.com/chaijs/chai/compare/1.5.0...v4.3.4) Signed-off-by: dependabot-preview[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 986dc39..da994cc 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "devDependencies": { "mocha": "9.0.0", - "chai": "1.5.0", + "chai": "4.3.4", "sinon": "11.1.1" } } From 28ed18fcb7124ae9a78fa0875bc19a9bf0f0c676 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 07:08:35 +0000 Subject: [PATCH 9/9] [Security] Bump handlebars from 1.0.10 to 4.7.7 Bumps [handlebars](https://github.com/wycats/handlebars.js) from 1.0.10 to 4.7.7. **This update includes security fixes.** - [Release notes](https://github.com/wycats/handlebars.js/releases) - [Changelog](https://github.com/handlebars-lang/handlebars.js/blob/master/release-notes.md) - [Commits](https://github.com/wycats/handlebars.js/compare/v1.0.10...v4.7.7) Signed-off-by: dependabot-preview[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 986dc39..8823ca8 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "author": "Dan Harper", "license": "WTFPL", "dependencies": { - "handlebars": "1.0.10" + "handlebars": "4.7.7" }, "devDependencies": { "mocha": "9.0.0",