add more tests + fix year conversion bug
This commit is contained in:
parent
9099f90693
commit
f4b5099f6a
@ -3,6 +3,46 @@ var expect = chai.expect;
|
|||||||
var timestring = require('../timestring');
|
var timestring = require('../timestring');
|
||||||
|
|
||||||
describe('timestring', function() {
|
describe('timestring', function() {
|
||||||
|
it('can parse a timestring', function(done) {
|
||||||
|
var ts = new timestring();
|
||||||
|
|
||||||
|
expect(ts.parse('1s')).to.equal(1);
|
||||||
|
expect(ts.parse('1m')).to.equal(60);
|
||||||
|
expect(ts.parse('1h')).to.equal(3600);
|
||||||
|
expect(ts.parse('1d')).to.equal(86400);
|
||||||
|
expect(ts.parse('1w')).to.equal(604800);
|
||||||
|
expect(ts.parse('1mth')).to.equal(2419200);
|
||||||
|
expect(ts.parse('1y')).to.equal(29030400);
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can return a value in a specified unit', function(done) {
|
||||||
|
expect((new timestring()).parse('5m', 's')).to.equal(300);
|
||||||
|
expect((new timestring()).parse('5m', 'm')).to.equal(5);
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the passed settings instead of the defaults', function(done) {
|
||||||
|
var settings = {
|
||||||
|
hoursPerDay: 1,
|
||||||
|
daysPerWeek: 2,
|
||||||
|
weeksPerMonth: 3,
|
||||||
|
monthsPerYear: 4
|
||||||
|
};
|
||||||
|
|
||||||
|
var ts = new timestring(settings);
|
||||||
|
|
||||||
|
expect(ts.parse('1d', 'h')).to.equal(1);
|
||||||
|
expect(ts.parse('1w', 'd')).to.equal(2);
|
||||||
|
expect(ts.parse('1mth', 'w')).to.equal(3);
|
||||||
|
expect(ts.parse('1y', 'mth')).to.equal(4);
|
||||||
|
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
it('throws an error when an invalid unit is used in the timestring', function(done) {
|
it('throws an error when an invalid unit is used in the timestring', function(done) {
|
||||||
var ts = new timestring();
|
var ts = new timestring();
|
||||||
|
|
||||||
@ -11,6 +51,12 @@ describe('timestring', function() {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can parse a messy time string', function(done) {
|
||||||
|
expect((new timestring()).parse('5 D a YS 4 h 2 0 mI nS')).to.equal(447600);
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
it('should expose a method on String.prototype that will parse the string as a timestring', function(done){
|
it('should expose a method on String.prototype that will parse the string as a timestring', function(done){
|
||||||
var str = '1min';
|
var str = '1min';
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h;
|
this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h;
|
||||||
this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d;
|
this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d;
|
||||||
this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w;
|
this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w;
|
||||||
this.unitValues.y = this.settings.monthsPerYear * this.unitValues.w;
|
this.unitValues.y = this.settings.monthsPerYear * this.unitValues.mth;
|
||||||
};
|
};
|
||||||
|
|
||||||
Timestring.prototype.parse = function(string, returnUnit) {
|
Timestring.prototype.parse = function(string, returnUnit) {
|
||||||
|
Loading…
Reference in New Issue
Block a user