Adds Unit Tests and Updates ReadMe

This commit is contained in:
Ian Mitchell 2016-11-15 09:37:20 -08:00
parent 1ef5c298b8
commit 5c7c064177
2 changed files with 46 additions and 1 deletions

View File

@ -59,7 +59,7 @@ Timestring will parse the following keywords into time values:
4. `h, hr, hrs, hour, hours` - will parse to hours
5. `d, day, days` - will parse to days
6. `w, week, weeks` - will parse to weeks
7. `mth, mths, month, months` - will parse to months
7. `mon, mth, mths, month, months` - will parse to months
8. `y, yr, yrs, year, years` - will parse to years
Keywords can be used interchangeably:

45
test.js
View File

@ -15,6 +15,51 @@ describe('timestring', function() {
expect(timestring('1y')).to.equal(29030400);
});
it('can parse different unit identifiers', function() {
var 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(function(msUnit) {
expect(timestring('500 ' + msUnit)).to.equal(0.5);
});
unitMap.s.forEach(function(sUnit) {
expect(timestring('3 ' + sUnit)).to.equal(3);
});
unitMap.m.forEach(function(mUnit) {
expect(timestring('2 ' + mUnit)).to.equal(120);
});
unitMap.h.forEach(function(hUnit) {
expect(timestring('7 ' + hUnit)).to.equal(25200);
});
unitMap.d.forEach(function(dUnit) {
expect(timestring('4 ' + dUnit)).to.equal(345600);
});
unitMap.w.forEach(function(wUnit) {
expect(timestring('2 ' + wUnit)).to.equal(1209600);
});
unitMap.mth.forEach(function(mthUnit) {
expect(timestring('9 ' + mthUnit)).to.equal(21772800);
});
unitMap.y.forEach(function(yUnit) {
expect(timestring('1 ' + yUnit)).to.equal(29030400);
});
});
it('can return a value in a specified unit', function() {
expect(timestring('1m', 'ms')).to.equal(60000);
expect(timestring('5m', 's')).to.equal(300);