From 5c7c064177c116376369720afd741f1a01bec016 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Tue, 15 Nov 2016 09:37:20 -0800 Subject: [PATCH] Adds Unit Tests and Updates ReadMe --- README.md | 2 +- test.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cf9679d..bfde5c2 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/test.js b/test.js index 03ff6f4..16e5773 100644 --- a/test.js +++ b/test.js @@ -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);