From b605ecdd135b1f4c6edc4b09792a7adb4dc9f2d9 Mon Sep 17 00:00:00 2001 From: Fizer Khan Date: Fri, 1 Feb 2013 16:01:11 +0530 Subject: [PATCH 1/5] Added support for Direct Web, RequireJS and Node JS Compatible. --- helpers.js | 242 +++++++++++++++++++++++++++++------------------------ 1 file changed, 133 insertions(+), 109 deletions(-) diff --git a/helpers.js b/helpers.js index 1f2569c..3aa97dc 100644 --- a/helpers.js +++ b/helpers.js @@ -7,114 +7,138 @@ * http://sam.zoy.org/wtfpl/COPYING for more details. */ /** - * If Equals - * if_eq this compare=that + * Following lines make Handlebars helper function to work with all + * three such as Direct web, RequireJS AMD and Node JS. + * This concepts derived from UMD. + * @courtesy - https://github.com/umdjs/umd/blob/master/returnExports.js */ -Handlebars.registerHelper('if_eq', function(context, options) { - if (context == options.hash.compare) - return options.fn(this); - return options.inverse(this); -}); -/** - * Unless Equals - * unless_eq this compare=that - */ -Handlebars.registerHelper('unless_eq', function(context, options) { - if (context == options.hash.compare) - return options.inverse(this); - return options.fn(this); -}); - - -/** - * If Greater Than - * if_gt this compare=that - */ -Handlebars.registerHelper('if_gt', function(context, options) { - if (context > options.hash.compare) - return options.fn(this); - return options.inverse(this); -}); - -/** - * Unless Greater Than - * unless_gt this compare=that - */ -Handlebars.registerHelper('unless_gt', function(context, options) { - if (context > options.hash.compare) - return options.inverse(this); - return options.fn(this); -}); - - -/** - * If Less Than - * if_lt this compare=that - */ -Handlebars.registerHelper('if_lt', function(context, options) { - if (context < options.hash.compare) - return options.fn(this); - return options.inverse(this); -}); - -/** - * Unless Less Than - * unless_lt this compare=that - */ -Handlebars.registerHelper('unless_lt', function(context, options) { - if (context < options.hash.compare) - return options.inverse(this); - return options.fn(this); -}); - - -/** - * If Greater Than or Equal To - * if_gteq this compare=that - */ -Handlebars.registerHelper('if_gteq', function(context, options) { - if (context >= options.hash.compare) - return options.fn(this); - return options.inverse(this); -}); - -/** - * Unless Greater Than or Equal To - * unless_gteq this compare=that - */ -Handlebars.registerHelper('unless_gteq', function(context, options) { - if (context >= options.hash.compare) - return options.inverse(this); - return options.fn(this); -}); - - -/** - * If Less Than or Equal To - * if_lteq this compare=that - */ -Handlebars.registerHelper('if_lteq', function(context, options) { - if (context <= options.hash.compare) - return options.fn(this); - return options.inverse(this); -}); - -/** - * Unless Less Than or Equal To - * unless_lteq this compare=that - */ -Handlebars.registerHelper('unless_lteq', function(context, options) { - if (context <= options.hash.compare) - return options.inverse(this); - return options.fn(this); -}); - -/** - * Convert new line (\n\r) to
- * from http://phpjs.org/functions/nl2br:480 - */ -Handlebars.registerHelper('nl2br', function(text) { - var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '
' + '$2'); - return new Handlebars.SafeString(nl2br); -}); \ No newline at end of file +(function (root, factory) { + if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like enviroments that support module.exports, + // like Node. + module.exports = factory(require('handlebars')); + } else if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(['handlebars'], factory); + } else { + // Browser globals (root is window) + root.returnExports = factory(root.Handlebars); + } +}(this, function (Handlebars) { + + /** + * If Equals + * if_eq this compare=that + */ + Handlebars.registerHelper('if_eq', function(context, options) { + if (context == options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Equals + * unless_eq this compare=that + */ + Handlebars.registerHelper('unless_eq', function(context, options) { + if (context == options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Greater Than + * if_gt this compare=that + */ + Handlebars.registerHelper('if_gt', function(context, options) { + if (context > options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Greater Than + * unless_gt this compare=that + */ + Handlebars.registerHelper('unless_gt', function(context, options) { + if (context > options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Less Than + * if_lt this compare=that + */ + Handlebars.registerHelper('if_lt', function(context, options) { + if (context < options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Less Than + * unless_lt this compare=that + */ + Handlebars.registerHelper('unless_lt', function(context, options) { + if (context < options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Greater Than or Equal To + * if_gteq this compare=that + */ + Handlebars.registerHelper('if_gteq', function(context, options) { + if (context >= options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Greater Than or Equal To + * unless_gteq this compare=that + */ + Handlebars.registerHelper('unless_gteq', function(context, options) { + if (context >= options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + + /** + * If Less Than or Equal To + * if_lteq this compare=that + */ + Handlebars.registerHelper('if_lteq', function(context, options) { + if (context <= options.hash.compare) + return options.fn(this); + return options.inverse(this); + }); + + /** + * Unless Less Than or Equal To + * unless_lteq this compare=that + */ + Handlebars.registerHelper('unless_lteq', function(context, options) { + if (context <= options.hash.compare) + return options.inverse(this); + return options.fn(this); + }); + + /** + * Convert new line (\n\r) to
+ * from http://phpjs.org/functions/nl2br:480 + */ + Handlebars.registerHelper('nl2br', function(text) { + var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '
' + '$2'); + return new Handlebars.SafeString(nl2br); + }); + +})); \ No newline at end of file From 99fc986c07b85c10bb81eee2419688ef06647a35 Mon Sep 17 00:00:00 2001 From: Carsten Scheuer Date: Mon, 15 Apr 2013 14:11:47 +0200 Subject: [PATCH 2/5] Add escaping of source text in nl2br helper. --- helpers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/helpers.js b/helpers.js index 3aa97dc..56df9b6 100644 --- a/helpers.js +++ b/helpers.js @@ -137,6 +137,7 @@ * from http://phpjs.org/functions/nl2br:480 */ Handlebars.registerHelper('nl2br', function(text) { + text = Handlebars.Utils.escapeExpression(text); var nl2br = (text + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '
' + '$2'); return new Handlebars.SafeString(nl2br); }); From 43711db2f452bfd47c3663381735971b1b458326 Mon Sep 17 00:00:00 2001 From: Dan Harper Date: Mon, 4 Nov 2013 11:49:00 +0000 Subject: [PATCH 3/5] For classic style, use v1.1.0 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c712d4..f0e249e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A small collection of useful helpers for [Handlebars.js](https://github.com/wyca [![Build Status](https://travis-ci.org/danharper/Handlebars-Helpers.png?branch=develop)](https://travis-ci.org/danharper/Handlebars-Helpers) -This version includes a (very) large API change. Use [version 1.0.0](https://github.com/danharper/Handlebars-Helpers/tree/v1.0.0) if you'd prefer the "classic" style. +This version includes a (very) large API change. Use [version 1.1.0](https://github.com/danharper/Handlebars-Helpers/tree/v1.1.0) if you'd prefer the "classic" style. To use, just include `helpers.js` after you include Handlebars. Or, if you're using AMD/Node, just require the file. @@ -106,4 +106,4 @@ Convert new lines (`\r\n`, `\n\r`, `\r`, `\n`) to line breaks ``` {{nl2br description}} -``` \ No newline at end of file +``` From 8c70b3051a4ae93d9b6e4e9a40d2e212fb532fd0 Mon Sep 17 00:00:00 2001 From: Roman Sotnikov Date: Fri, 20 Jun 2014 10:31:04 +0700 Subject: [PATCH 4/5] More helpers --- package.json | 6 +++-- src/helpers.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ test/helpersTest.js | 35 +++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b637807..6005031 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,13 @@ "author": "Dan Harper", "license": "WTFPL", "dependencies": { - "handlebars": "1.0.10" + "handlebars": "1.0.10", + "moment" : "2.7.0" }, "devDependencies": { "mocha": "1.8.2", "chai": "1.5.0", - "sinon": "1.6.0" + "sinon": "1.6.0", + "moment" : "2.7.0" } } diff --git a/src/helpers.js b/src/helpers.js index 02c1cef..1272d88 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -28,6 +28,8 @@ return this.expressions[operator](left, right); }; + var moment = require('moment'); + var eR = new ExpressionRegistry; eR.add('not', function(left, right) { return left != right; @@ -104,6 +106,59 @@ )); }); + /** + * Hack for correct display JSON string + * {{json context}} + */ + Handlebars.registerHelper('json', function(context) { + if(typeof context == 'object'){ + return JSON.stringify(context, null, 2) + }else{ + var json_obj = JSON.parse(context); + return JSON.stringify(json_obj, null, 2) + }; + }); + + /** + * truncate string to 20 symbols + * {{trunc "text"}} + */ + Handlebars.registerHelper('trunc', function(text) { + if (text !=null && text.length >= 20){ + return text.substring(0,20) + "..." + }else{ + return false + }; + return text + }); + + /** + * Return only day and month: 26 Nov + * {{short_date text}} + */ + Handlebars.registerHelper('short_date', function(text) { + var m = new moment(text); + if(m.isValid()){ + return m.format("DD MMM"); + }else{ + return false + } + }); + + /** + * Return day month and year: 20 June 2014 + * {{long_date text}} + */ + Handlebars.registerHelper('long_date', function(text) { + var m = new moment(text); + if (m.isValid()){ + return m.format("DD MMM YYYY"); + }else{ + return false + } + + }); + return eR; })); \ No newline at end of file diff --git a/test/helpersTest.js b/test/helpersTest.js index 958fe83..3056180 100644 --- a/test/helpersTest.js +++ b/test/helpersTest.js @@ -2,6 +2,7 @@ var HelpersRegistry = require('../src/helpers'); var chai = require('chai') , sinon = require('sinon') +, moment = require('moment') , h = require('handlebars'); chai.should(); @@ -189,3 +190,37 @@ describe('Logging', function () { }); }); }); + +describe('#truncate', function() { + it('Truncates output to 20 chars', function() { + c('{{trunc this}}', "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum.").should.equal('Lorem ipsum dolor si...') + }); + it('Returns empty string', function() { + c('{{trunc }}').should.equal('') + }); +}); + +describe('#short_date', function() { + it('Returns short date', function() { + c('{{short_date this}}', '2014-06-20 00:00:00').should.equal('20 Jun') + }); + it('Returns empty string', function() { + c('{{short_date this}}', 'qazwsxedc').should.equal("") + }); +}); + +describe('#long_date', function() { + it('Returns long date', function() { + c('{{long_date this}}', '2014-06-20 00:00:00').should.equal('20 Jun 2014') + }); + it('Returns empty string', function() { + c('{{long_date this}}', 'qazwsxedc').should.equal("") + }); +}); + +describe('#json', function() { + it('Returns stringified JSON', function() { + c('{{json this}}',{'foo':'bar'}).should.equal('{\n "foo": "bar"\n}') + }); +}); + From e7acf1fca0d117d1bad6d98949f3f1584ecd4e13 Mon Sep 17 00:00:00 2001 From: Roman Sotnikov Date: Fri, 20 Jun 2014 10:44:51 +0700 Subject: [PATCH 5/5] Extend trunc helper --- src/helpers.js | 6 +++--- test/helpersTest.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 1272d88..ea646b6 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -123,9 +123,9 @@ * truncate string to 20 symbols * {{trunc "text"}} */ - Handlebars.registerHelper('trunc', function(text) { - if (text !=null && text.length >= 20){ - return text.substring(0,20) + "..." + Handlebars.registerHelper('trunc', function(text, symbols) { + if (text !=null && text.length >= symbols){ + return text.substring(0,symbols) + "..." }else{ return false }; diff --git a/test/helpersTest.js b/test/helpersTest.js index 3056180..e4085d3 100644 --- a/test/helpersTest.js +++ b/test/helpersTest.js @@ -193,7 +193,7 @@ describe('Logging', function () { describe('#truncate', function() { it('Truncates output to 20 chars', function() { - c('{{trunc this}}', "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum.").should.equal('Lorem ipsum dolor si...') + c('{{trunc text symbols}}', {text:"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur elementum.",symbols:20}).should.equal("Lorem ipsum dolor si...") }); it('Returns empty string', function() { c('{{trunc }}').should.equal('')