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

Adding two helpers to make it easier to compare dates #107

Open
wants to merge 3 commits 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
30 changes: 30 additions & 0 deletions helpers/getTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

/*
* FUNCTION
* getTime()
*
* DESCRIPTION (WHAT)
* A wrapper for the Javascript getTime function which returns the Unix timestamp for the current date-time
*
* USE CASE (WHY)
* As a theme developer, I may want to compare the current time against a date returned by
* Stencil as part of the conditional logic used to display products on my storefront
*
* USAGE
*
* {{#if (phpDateToTimestamp 'category.products[0].date_added.') >
* ((getTime) - theme_settings.new_product_lag)) }}
* // product is newer than the given time interval
* {{/if}}
*/

function helper(paper) {
paper.handlebars.registerHelper('getTime', function () {
var d = new Date();
return d.getTime();
});
}

module.exports = helper;

85 changes: 85 additions & 0 deletions helpers/phpDateToTimestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

/*
** FUNCTION
** phpDateToTimestamp(dateString)
**
** DESCRIPTION (WHAT)
** The Stencil framework returns date strings using a PHP format of "M jS Y" in the
** page context. This function converts the string to milliseconds since Jan 1, 1970
** in order to make it easier to perform date comparisons and operations.
**
** USE CASE (WHY)
** Some examples of how this would be used include the following story: As a merchant,
** I want to display a carousel on my category pages that displays cards for products
** that were added to the catalog within the last month. In order to do that, I need
** to be able to convert the "date added" field that appears in the product cards under
** the category.products object. Likewise, the same behavior would apply to brand pages
** with products.
**
** USAGE
**
** {{#if (phpDateToTimestamp 'category.products[0].date_added.') >
** ((getTime) - theme_settings.new_product_lag)) }}
** // product is newer than the given time interval
** {{/if}}
**
** and similarly with brands: 'brands.products[0].date_added.')
*/

function helper(paper) {
paper.handlebars.registerHelper('phpDateToTimestamp', function (dateString) {
if (typeof dateString !== 'string') {
return 0;
}
var dateArray = dateString.split(" ");
var month = 12;

switch (dateArray[0]) {
case "Jan":
month = 0;
break;
case "Feb":
month = 1;
break;
case "Mar":
month = 2;
break;
case "Apr":
month = 3;
break;
case "May":
month = 4;
break;
case "Jun":
month = 5;
break;
case "Jul":
month = 6;
break;
case "Aug":
month = 7;
break;
case "Sep":
month = 8;
break;
case "Oct":
month = 9;
break;
case "Nov":
month = 10;
break;
case "Dec":
month = 11;
break;
}
// remove english ordinal suffix: nd, rd, st, th
var day = dateArray[1].replace(/[a-z]/g, "");
var year = dateArray[2];
var ts = new Date(year, month, day, 0, 0, 0);
return ts.getTime();
});
}

module.exports = helper;

50 changes: 50 additions & 0 deletions test/helpers/phpDateToTimestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var Code = require('code'),
Lab = require('lab'),
Paper = require('../../index'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
expect = Code.expect,
it = lab.it;

function c(template, context) {
return new Paper().loadTemplatesSync({template: template}).render('template', context);
}

describe('phpDateToTimestamp helper', function() {

var context = {
st: 'Mar 1st 2017',
nd: 'Mar 2nd 2017',
rd: 'Mar 3rd 2017',
th: 'Mar 4th 2017'
};

it('should return the unix timestamp for Mar 1st 2017 GMT', function(done) {

expect(c('{{phpDateToTimestamp st}}', context))
.to.be.equal('1488326400000');
done();
});

it('should return the unix timestamp for Mar 2nd 2017 GMT', function(done) {

expect(c('{{phpDateToTimestamp nd}}', context))
.to.be.equal('1488412800000');
done();
});

it('should return the unix timestamp for Mar 3rd 2017 GMT', function(done) {

expect(c('{{phpDateToTimestamp rd}}', context))
.to.be.equal('1488499200000');
done();
});

it('should return the unix timestamp for Mar 4th 2017 GMT', function(done) {

expect(c('{{phpDateToTimestamp th}}', context))
.to.be.equal('1488585600000');
done();
});

});