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 +``` 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..ea646b6 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, symbols) { + if (text !=null && text.length >= symbols){ + return text.substring(0,symbols) + "..." + }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..e4085d3 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 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('') + }); +}); + +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}') + }); +}); +