Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function support to helpers #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ function registerWith(Handlebars) {
}

function formatDate(date, format, options) {
if (typeof date === 'function') {
date = date.call(this);
}
date = new Date(date);
assertIsDate(date, 'A date or timestamp must be provided to {{formatDate}}');

Expand All @@ -121,6 +124,9 @@ function registerWith(Handlebars) {
}

function formatTime(date, format, options) {
if (typeof date === 'function') {
date = date.call(this);
}
date = new Date(date);
assertIsDate(date, 'A date or timestamp must be provided to {{formatTime}}');

Expand All @@ -136,6 +142,9 @@ function registerWith(Handlebars) {
}

function formatRelative(date, format, options) {
if (typeof date === 'function') {
date = date.call(this);
}
date = new Date(date);
assertIsDate(date, 'A date or timestamp must be provided to {{formatRelative}}');

Expand All @@ -158,6 +167,9 @@ function registerWith(Handlebars) {
}

function formatNumber(num, format, options) {
if (typeof num === 'function') {
num = num.call(this);
}
assertIsNumber(num, 'A number must be provided to {{formatNumber}}');

if (!options) {
Expand All @@ -176,6 +188,9 @@ function registerWith(Handlebars) {
options = message;
message = null;
}
if (typeof message === 'function') {
message = message.call(this);
}

var hash = options.hash;

Expand Down
39 changes: 39 additions & 0 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ describe('Helper `formatNumber`', function () {
expect(tmpl({ NUM: 40000.004 })).to.equal('40,000.004');
});

it('should support number as a function', function() {
var tmpl = intlBlock('{{formatNumber numFn}}', {locales: 'en-US'});
expect(tmpl({
numFn: function() { return 4; }
})).to.equal('4');
});

describe('in another locale', function () {
it('should return a string', function () {
var tmpl = intlBlock('{{formatNumber 4}}', {locales: 'de-DE'});
Expand Down Expand Up @@ -185,6 +192,13 @@ describe('Helper `formatDate`', function () {
var tmpl = intlBlock('{{formatDate 0}}', {locales: 'en-US'});
expect(tmpl()).to.equal(new Intl.DateTimeFormat('en').format(0));
});

it('should support date as a function', function() {
var tmpl = intlBlock('{{formatDate dateFn}}', {locales: 'en-US'});
expect(tmpl({
dateFn: function() { return timeStamp; }
})).to.equal('1/23/2014');
});
});

describe('Helper `formatTime`', function () {
Expand Down Expand Up @@ -219,6 +233,13 @@ describe('Helper `formatTime`', function () {
var tmpl = intlBlock('{{formatTime ' + timeStamp + ' hour="numeric" minute="numeric" timeZone="UTC"}}', {locales: 'en-US'});
expect(tmpl()).to.equal('11:00 PM');
});

it('should support date as a function', function() {
var tmpl = intlBlock('{{formatTime dateFn}}', {locales: 'en-US'});
expect(tmpl({
dateFn: function() { return timeStamp; }
})).to.equal('1/23/2014');
});
});

describe('Helper `formatRelative`', function () {
Expand Down Expand Up @@ -257,6 +278,13 @@ describe('Helper `formatRelative`', function () {
var tmpl = intlBlock('{{formatRelative 0 now=1000}}', {locales: 'en-US'});
expect(tmpl()).to.equal('1 second ago');
});

it('should support date as a function', function() {
var tmpl = intlBlock('{{formatRelative dateFn}}', {locales: 'en-US'});
expect(tmpl({
dateFn: function() { return tomorrow; }
})).to.equal('tomorrow');
});
});

describe('Helper `formatMessage`', function () {
Expand Down Expand Up @@ -333,6 +361,17 @@ describe('Helper `formatMessage`', function () {

expect(out).to.equal('This is my 3rd birthday.');
});

it('should support message as a function', function () {
var tmpl = intlBlock('{{formatMessage messageFn firstName=firstName lastName=lastName}}', {locales: 'en-US'}),
out = tmpl({
messageFn: function() { return 'Hi, my name is {firstName} {lastName}.'; },
firstName: 'Anthony',
lastName : 'Pipkin'
});

expect(out).to.equal('Hi, my name is Anthony Pipkin.');
});
});

describe('Helper `intl`', function () {
Expand Down