From a1f401abf9572e0df821b3486571fd31718c394c Mon Sep 17 00:00:00 2001 From: elephantengine Date: Thu, 16 Feb 2017 13:41:28 -0800 Subject: [PATCH 1/3] Adding two helpers to make it easier to compare dates --- helpers/getTime.js | 30 +++++++++++ helpers/phpDateToTimestamp.js | 84 ++++++++++++++++++++++++++++++ test/helpers/phpDateToTimestamp.js | 50 ++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 helpers/getTime.js create mode 100644 helpers/phpDateToTimestamp.js create mode 100644 test/helpers/phpDateToTimestamp.js diff --git a/helpers/getTime.js b/helpers/getTime.js new file mode 100644 index 00000000..fc306c84 --- /dev/null +++ b/helpers/getTime.js @@ -0,0 +1,30 @@ +/* +** 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}} +*/ + +'use strict'; + +function helper(paper) { + paper.handlebars.registerHelper('getTime', function () { + var d = new Date(); + return d.getTime(); + }); +} + +module.exports = helper; + diff --git a/helpers/phpDateToTimestamp.js b/helpers/phpDateToTimestamp.js new file mode 100644 index 00000000..91d32cb8 --- /dev/null +++ b/helpers/phpDateToTimestamp.js @@ -0,0 +1,84 @@ +/* +** 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.') +*/ + +'use strict'; + +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; + diff --git a/test/helpers/phpDateToTimestamp.js b/test/helpers/phpDateToTimestamp.js new file mode 100644 index 00000000..c226c538 --- /dev/null +++ b/test/helpers/phpDateToTimestamp.js @@ -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', function(done) { + + expect(c('{{phpDateToTimestamp st}}', context)) + .to.be.equal('1488355200000'); + done(); + }); + + it('should return the unix timestamp for Mar 2nd 2017', function(done) { + + expect(c('{{phpDateToTimestamp nd}}', context)) + .to.be.equal('1488441600000'); + done(); + }); + + it('should return the unix timestamp for Mar 3rd 2017', function(done) { + + expect(c('{{phpDateToTimestamp rd}}', context)) + .to.be.equal('1488528000000'); + done(); + }); + + it('should return the unix timestamp for Mar 4th 2017', function(done) { + + expect(c('{{phpDateToTimestamp th}}', context)) + .to.be.equal('1488614400000'); + done(); + }); + +}); From 857a2fc4a55e5896094e86bbc615f20c1ff106c0 Mon Sep 17 00:00:00 2001 From: elephantengine Date: Thu, 16 Feb 2017 14:13:02 -0800 Subject: [PATCH 2/3] Fixed travis linting errors --- helpers/getTime.js | 46 +++++++-------- helpers/phpDateToTimestamp.js | 107 +++++++++++++++++----------------- 2 files changed, 77 insertions(+), 76 deletions(-) diff --git a/helpers/getTime.js b/helpers/getTime.js index fc306c84..8f6b761a 100644 --- a/helpers/getTime.js +++ b/helpers/getTime.js @@ -1,29 +1,29 @@ -/* -** 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}} -*/ - '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(); - }); + paper.handlebars.registerHelper('getTime', function () { + var d = new Date(); + return d.getTime(); + }); } module.exports = helper; diff --git a/helpers/phpDateToTimestamp.js b/helpers/phpDateToTimestamp.js index 91d32cb8..e2b96eca 100644 --- a/helpers/phpDateToTimestamp.js +++ b/helpers/phpDateToTimestamp.js @@ -1,3 +1,5 @@ +'use strict'; + /* ** FUNCTION ** phpDateToTimestamp(dateString) @@ -19,65 +21,64 @@ ** ** {{#if (phpDateToTimestamp 'category.products[0].date_added.') > ** ((getTime) - theme_settings.new_product_lag)) }} -** /* product is newer than the given time interval */ +** // product is newer than the given time interval ** {{/if}} ** ** and similarly with brands: 'brands.products[0].date_added.') */ -'use strict'; - 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(); - }); + 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; From 5d344e1cc8272e7634bd01dcd5fc97f383eedfab Mon Sep 17 00:00:00 2001 From: elephantengine Date: Thu, 16 Feb 2017 14:26:28 -0800 Subject: [PATCH 3/3] Fixed test to reflect GMT timezone --- test/helpers/phpDateToTimestamp.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/helpers/phpDateToTimestamp.js b/test/helpers/phpDateToTimestamp.js index c226c538..18095699 100644 --- a/test/helpers/phpDateToTimestamp.js +++ b/test/helpers/phpDateToTimestamp.js @@ -19,31 +19,31 @@ describe('phpDateToTimestamp helper', function() { th: 'Mar 4th 2017' }; - it('should return the unix timestamp for Mar 1st 2017', function(done) { + it('should return the unix timestamp for Mar 1st 2017 GMT', function(done) { expect(c('{{phpDateToTimestamp st}}', context)) - .to.be.equal('1488355200000'); + .to.be.equal('1488326400000'); done(); }); - it('should return the unix timestamp for Mar 2nd 2017', function(done) { + it('should return the unix timestamp for Mar 2nd 2017 GMT', function(done) { expect(c('{{phpDateToTimestamp nd}}', context)) - .to.be.equal('1488441600000'); + .to.be.equal('1488412800000'); done(); }); - it('should return the unix timestamp for Mar 3rd 2017', function(done) { + it('should return the unix timestamp for Mar 3rd 2017 GMT', function(done) { expect(c('{{phpDateToTimestamp rd}}', context)) - .to.be.equal('1488528000000'); + .to.be.equal('1488499200000'); done(); }); - it('should return the unix timestamp for Mar 4th 2017', function(done) { + it('should return the unix timestamp for Mar 4th 2017 GMT', function(done) { expect(c('{{phpDateToTimestamp th}}', context)) - .to.be.equal('1488614400000'); + .to.be.equal('1488585600000'); done(); });