Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -106,4 +106,4 @@ Convert new lines (`\r\n`, `\n\r`, `\r`, `\n`) to line breaks

```
{{nl2br description}}
```
```
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
55 changes: 55 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

}));
35 changes: 35 additions & 0 deletions test/helpersTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var HelpersRegistry = require('../src/helpers');

var chai = require('chai')
, sinon = require('sinon')
, moment = require('moment')
, h = require('handlebars');

chai.should();
Expand Down Expand Up @@ -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}')
});
});