Skip to content

Allow string date timestamps in intlDate, resolves #25 #26

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

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 6 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@
throw new ReferenceError('@intlDate needs a `val` parameter');
}
val = _tap(params.val, chunk, context);

// Check if `val` is a string that's a positive integer
if (typeof val === 'string' && /^\+?(0|[1-9]\d*)$/.test(val)) {
val = parseFloat(val);
}

delete params.val; // since params might be interpretted as format options
val = new Date(val).getTime();

Expand Down
9 changes: 9 additions & 0 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ describe('Helper `intlDate`', function () {
});
});

it('should return a formatted string when passed string input', function () {
var tmpl = '{@intlDate val="' + timeStamp + '" /}',
ctx = {},
expected = "1/23/2014";
Dust.renderSource(tmpl, ctx, function(err, out) {
expect(out).to.equal(expected);
});
});

/** SINGLE VALUES ARE MUTED FOR NOW :: https://github.com/andyearnshaw/Intl.js/issues/56
it('should return a formatted string of option requested', function () {
var tmpl = '{@intlDate val=DATE year="numeric" /}',
Expand Down