update interval module, add tests
This commit is contained in:
@@ -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);
|
|
||||||
}
|
|
||||||
38
src/helpers/index_timestamp.js
Normal file
38
src/helpers/index_timestamp.js
Normal file
@@ -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);
|
||||||
|
}
|
||||||
62
test/src/helpers/index_timestamp.js
Normal file
62
test/src/helpers/index_timestamp.js
Normal file
@@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user