2017-04-22 15:07:52 +02:00
|
|
|
'use strict'
|
|
|
|
|
2016-12-16 20:52:21 +01:00
|
|
|
/* globals describe, it */
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
const chai = require('chai')
|
|
|
|
const timestring = require('./index')
|
2016-12-16 20:52:21 +01:00
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
const expect = chai.expect
|
2016-12-16 20:52:21 +01:00
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
describe('timestring', () => {
|
|
|
|
it('can parse a timestring', () => {
|
2016-12-16 20:52:21 +01:00
|
|
|
expect(timestring('500ms')).to.equal(0.5)
|
|
|
|
expect(timestring('1s')).to.equal(1)
|
|
|
|
expect(timestring('1m')).to.equal(60)
|
|
|
|
expect(timestring('1h')).to.equal(3600)
|
|
|
|
expect(timestring('1d')).to.equal(86400)
|
|
|
|
expect(timestring('1w')).to.equal(604800)
|
|
|
|
expect(timestring('1mth')).to.equal(2419200)
|
|
|
|
expect(timestring('1y')).to.equal(29030400)
|
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
it('can parse different unit identifiers', () => {
|
|
|
|
let unitMap = {
|
2016-11-15 18:37:20 +01:00
|
|
|
ms: ['ms', 'milli', 'millisecond', 'milliseconds'],
|
|
|
|
s: ['s', 'sec', 'secs', 'second', 'seconds'],
|
|
|
|
m: ['m', 'min', 'mins', 'minute', 'minutes'],
|
|
|
|
h: ['h', 'hr', 'hrs', 'hour', 'hours'],
|
|
|
|
d: ['d', 'day', 'days'],
|
|
|
|
w: ['w', 'week', 'weeks'],
|
2016-12-16 20:52:21 +01:00
|
|
|
mth: ['mon', 'mth', 'mths', 'month', 'months'],
|
|
|
|
y: ['y', 'yr', 'yrs', 'year', 'years']
|
|
|
|
}
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.ms.forEach(unit => {
|
|
|
|
expect(timestring(`500 ${unit}`)).to.equal(0.5)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.s.forEach(unit => {
|
|
|
|
expect(timestring(`3 ${unit}`)).to.equal(3)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.m.forEach(unit => {
|
|
|
|
expect(timestring(`2 ${unit}`)).to.equal(120)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.h.forEach(unit => {
|
|
|
|
expect(timestring(`7 ${unit}`)).to.equal(25200)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.d.forEach(unit => {
|
|
|
|
expect(timestring(`4 ${unit}`)).to.equal(345600)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.w.forEach(unit => {
|
|
|
|
expect(timestring(`2 ${unit}`)).to.equal(1209600)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.mth.forEach(unit => {
|
|
|
|
expect(timestring(`9 ${unit}`)).to.equal(21772800)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
unitMap.y.forEach(unit => {
|
|
|
|
expect(timestring(`1 ${unit}`)).to.equal(29030400)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
it('can return a value in a specified unit', () => {
|
2016-12-16 20:52:21 +01:00
|
|
|
expect(timestring('1m', 'ms')).to.equal(60000)
|
|
|
|
expect(timestring('5m', 's')).to.equal(300)
|
|
|
|
expect(timestring('5m', 'm')).to.equal(5)
|
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
it('uses the passed options instead of the defaults', () => {
|
|
|
|
let opts = {
|
2014-07-18 21:27:58 +02:00
|
|
|
hoursPerDay: 1,
|
|
|
|
daysPerWeek: 2,
|
|
|
|
weeksPerMonth: 3,
|
|
|
|
monthsPerYear: 4
|
2016-12-16 20:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(timestring('1d', 'h', opts)).to.equal(1)
|
|
|
|
expect(timestring('1w', 'd', opts)).to.equal(2)
|
|
|
|
expect(timestring('1mth', 'w', opts)).to.equal(3)
|
|
|
|
expect(timestring('1y', 'mth', opts)).to.equal(4)
|
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
it('throws an error when an invalid unit is used in the timestring', () => {
|
|
|
|
expect(() => timestring('1g')).to.throw(Error)
|
2016-12-16 20:52:21 +01:00
|
|
|
})
|
|
|
|
|
2017-04-22 15:07:52 +02:00
|
|
|
it('can parse a messy time string', () => {
|
2016-12-16 20:52:21 +01:00
|
|
|
expect(timestring('5 D a YS 4 h 2 0 mI nS')).to.equal(447600)
|
|
|
|
})
|
|
|
|
})
|