From 549095dfff52001140f4de638563b322cb58d050 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Fri, 22 Apr 2016 13:38:12 -0700 Subject: [PATCH] update interval module, add tests --- src/helpers/index_interval.js | 37 ----------------- src/helpers/index_timestamp.js | 38 ++++++++++++++++++ test/src/helpers/index_timestamp.js | 62 +++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 37 deletions(-) delete mode 100644 src/helpers/index_interval.js create mode 100644 src/helpers/index_timestamp.js create mode 100644 test/src/helpers/index_timestamp.js diff --git a/src/helpers/index_interval.js b/src/helpers/index_interval.js deleted file mode 100644 index 555f96d..0000000 --- a/src/helpers/index_interval.js +++ /dev/null @@ -1,37 +0,0 @@ -import moment from 'moment'; - -export const intervals = [ - 'y', - 'M', - 'w', - 'd', - 'h', - 'm' -]; - -export const intervalNames = [ - 'year', - 'month', - 'week', - 'day', - 'hour', - 'minute' -]; - -export default function getTimestamp(intervalStr) { - const index = intervals.indexOf(intervalStr); - if (index === -1) throw new Error('Invalid index interval: ', intervalStr); - - const startType = intervalNames[intervalStr]; - const m = moment(); - m.startOf(startType); - - let dateString = 'YYYY-MM-DD'; - if (startType === 'hour') { - dateString += '-HH'; - } - if (startType === 'minute') { - dateString += '-HH-mm'; - } - return m.format(dateString); -} \ No newline at end of file diff --git a/src/helpers/index_timestamp.js b/src/helpers/index_timestamp.js new file mode 100644 index 0000000..3a894b4 --- /dev/null +++ b/src/helpers/index_timestamp.js @@ -0,0 +1,38 @@ +import moment from 'moment'; + +export const intervals = [ + 'year', + 'month', + 'week', + 'day', + 'hour', + 'minute' +]; + +export default function indexTimestamp(intervalStr) { + const index = intervals.indexOf(intervalStr); + if (index === -1) throw new Error('Invalid index interval: ', intervalStr); + + const m = moment(); + m.startOf(intervalStr); + + let dateString; + switch (intervalStr) { + case 'year': + dateString = 'YYYY'; + break; + case 'month': + dateString = 'YYYY-MM'; + break; + case 'hour': + dateString = 'YYYY-MM-DD-HH'; + break; + case 'minute': + dateString = 'YYYY-MM-DD-HH-mm'; + break; + default: + dateString = 'YYYY-MM-DD'; + } + + return m.format(dateString); +} \ No newline at end of file diff --git a/test/src/helpers/index_timestamp.js b/test/src/helpers/index_timestamp.js new file mode 100644 index 0000000..f1274f6 --- /dev/null +++ b/test/src/helpers/index_timestamp.js @@ -0,0 +1,62 @@ +import expect from 'expect.js'; +import sinon from 'sinon'; +import moment from 'moment'; +import proxyquire from 'proxyquire'; + +const anchor = '2016-04-02T01:02:03.456'; // saturday + +const module = proxyquire.noPreserveCache()('../../../lib/helpers/index_timestamp', { + moment: () => moment(anchor) +}); +const indexTimestamp = module.default; + +describe('Index interval', function () { + describe('indexTimestamp construction', function () { + it('should throw given an invalid interval', function () { + const init = () => indexTimestamp('bananas'); + expect(init).to.throwException(/invalid.+interval/i); + }); + }); + + describe('indexTimestamp timestamps', function () { + let clock; + + beforeEach(function () { + clock = sinon.useFakeTimers(moment(anchor).valueOf()); + }); + + afterEach(function () { + clock.restore(); + }); + + it('should return the year', function () { + var timestamp = indexTimestamp('year'); + expect(timestamp).to.equal('2016'); + }); + + it('should return the year and month', function () { + var timestamp = indexTimestamp('month'); + expect(timestamp).to.equal('2016-04'); + }); + + it('should return the year, month, and first day of the week', function () { + var timestamp = indexTimestamp('week'); + expect(timestamp).to.equal('2016-03-27'); + }); + + it('should return the year, month, and day of the week', function () { + var timestamp = indexTimestamp('day'); + expect(timestamp).to.equal('2016-04-02'); + }); + + it('should return the year, month, day and hour', function () { + var timestamp = indexTimestamp('hour'); + expect(timestamp).to.equal('2016-04-02-01'); + }); + + it('should return the year, month, day, hour and minute', function () { + var timestamp = indexTimestamp('minute'); + expect(timestamp).to.equal('2016-04-02-01-02'); + }); + }); +});