🐛Fixed {{get}} helper's date comparison (#9454)

no issue

- Date comparisons are possible via API, but there's no way to inject a valid date into the get helper
- JavaScript's Date.toString() function outputs dates in a useless format
- Swap to using Date.toISOString() and now the format can be understood anywhere!
- {{#get "posts" filter="published_at:<='{{published_at}}'"}}{{/get}} works now as expected
This commit is contained in:
Hannah Wolfe 2018-02-14 17:33:07 +00:00 committed by Katharina Irrgang
parent 9ede5905f6
commit fe0197b226
2 changed files with 29 additions and 6 deletions

View File

@ -61,10 +61,17 @@ function resolvePaths(data, value) {
// Handle Handlebars .[] style arrays
path = path.replace(/\.\[/g, '[');
// Do the query, and convert from array to string
result = jsonpath.query(data, path).join(',');
// Do the query, which always returns an array of matches
result = jsonpath.query(data, path);
return result;
// Handle the case where the single data property we return is a Date
// Data.toString() is not DB compatible, so use `toISOString()` instead
if (_.isDate(result[0])) {
result[0] = result[0].toISOString();
}
// Concatenate the results with a comma, handles common case of multiple tag slugs
return result.join(',');
});
return value;

View File

@ -300,9 +300,11 @@ describe('{{#get}} helper', function () {
});
describe('path resolution', function () {
var browseStub, readStub, data = {
post: {id: 3, title: 'Test 3', author: {slug: 'cameron'}, tags: [{slug: 'test'}, {slug: 'magic'}]}
};
var browseStub, readStub,
pubDate = new Date(),
data = {
post: {id: 3, title: 'Test 3', author: {slug: 'cameron'}, tags: [{slug: 'test'}, {slug: 'magic'}], published_at: pubDate}
};
beforeEach(function () {
browseStub = sandbox.stub(api.posts, 'browse').returns(new Promise.resolve());
@ -365,6 +367,20 @@ describe('{{#get}} helper', function () {
}).catch(done);
});
it('should handle dates', function (done) {
helpers.get.call(
data,
'posts',
{hash: {filter: "published_at:<='{{post.published_at}}'"}, fn: fn, inverse: inverse}
).then(function () {
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
browseStub.firstCall.args[0].filter.should.eql(`published_at:<='${pubDate.toISOString()}'`);
done();
}).catch(done);
});
it('should output nothing if path does not resolve', function (done) {
helpers.get.call(
data,