-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.js
111 lines (91 loc) · 3.24 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* globals describe, it */
const { expect } = require('chai')
const timestring = require('./index')
describe('timestring', () => {
it('can parse a timestring', () => {
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(2629800)
expect(timestring('1y')).to.equal(31557600)
})
it('can parse different unit identifiers', () => {
const unitMap = {
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'],
mth: ['mon', 'mth', 'mths', 'month', 'months'],
y: ['y', 'yr', 'yrs', 'year', 'years']
}
unitMap.ms.forEach(unit => {
expect(timestring(`500 ${unit}`)).to.equal(0.5)
})
unitMap.s.forEach(unit => {
expect(timestring(`3 ${unit}`)).to.equal(3)
})
unitMap.m.forEach(unit => {
expect(timestring(`2 ${unit}`)).to.equal(120)
})
unitMap.h.forEach(unit => {
expect(timestring(`7 ${unit}`)).to.equal(25200)
})
unitMap.d.forEach(unit => {
expect(timestring(`4 ${unit}`)).to.equal(345600)
})
unitMap.w.forEach(unit => {
expect(timestring(`2 ${unit}`)).to.equal(1209600)
})
unitMap.mth.forEach(unit => {
expect(timestring(`9 ${unit}`)).to.equal(23668200)
})
unitMap.y.forEach(unit => {
expect(timestring(`1 ${unit}`)).to.equal(31557600)
})
})
it('can return a value in a specified unit', () => {
expect(timestring('1m', 'ms')).to.equal(60000)
expect(timestring('5m', 's')).to.equal(300)
expect(timestring('5m', 'm')).to.equal(5)
})
it('uses the passed options instead of the defaults', () => {
const opts = {
hoursPerDay: 1,
daysPerWeek: 2,
weeksPerMonth: 3,
monthsPerYear: 4,
daysPerYear: 30
}
expect(timestring('1d', 'h', opts)).to.equal(1)
expect(timestring('1w', 'd', opts)).to.equal(2)
expect(timestring('1mth', 'w', opts)).to.equal(3.75)
expect(timestring('1y', 'mth', opts)).to.equal(4)
})
it('throws an error when an invalid unit is used in the timestring', () => {
expect(() => timestring('1g')).to.throw(Error)
})
it('throws an error when no numbers are in the timestring', () => {
expect(() => timestring('asdf')).to.throw(Error)
})
it('throws an error when numbers tail the timestring', () => {
expect(() => timestring('asdf123')).to.throw(Error)
})
it('can parse a messy time string', () => {
expect(timestring('5 D a YS 4 h 2 0 mI nS')).to.equal(447600)
})
it('can parse a time string containing a decimal value', () => {
expect(timestring('1.5 hours')).to.equal(5400)
expect(timestring('2.75 mins')).to.equal(165)
expect(timestring('1.5 w')).to.equal(907200)
})
it('can parse a number', () => {
expect(timestring(300000)).to.equal(300)
expect(timestring(5000.006)).to.equal(5)
expect(timestring('600000', 'm')).to.equal(10)
})
})