Comparing version 1.0.0 to 1.1.0
@@ -8,4 +8,4 @@ var FORMAT_REGEXP = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/; | ||
exports.fromSeconds = function(seconds){ | ||
if(typeof seconds !== "number"){ | ||
throw new TypeError("Argument `seconds` must be a number"); | ||
if(typeof seconds !== 'number'){ | ||
throw new TypeError('Argument `seconds` must be a number'); | ||
} | ||
@@ -20,4 +20,4 @@ | ||
seconds: fullSeconds | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -29,5 +29,5 @@ exports.fromString = function(string){ | ||
var matches = string.match(FORMAT_REGEXP) | ||
var matches = string.match(FORMAT_REGEXP); | ||
if(matches === null || (matches[1] === undefined && matches[2] === undefined && matches[3] === undefined)){ | ||
throw new Error('Could not parse "' + string + '" as a duration.') | ||
throw new Error('Could not parse "' + string + '" as a duration.'); | ||
} | ||
@@ -39,4 +39,4 @@ | ||
seconds: matchToInteger(matches[3]) | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -48,12 +48,12 @@ exports.toString = function(duration) { | ||
var result = "PT"; | ||
var result = 'PT'; | ||
if(duration.hours > 0){ | ||
result += duration.hours + "H"; | ||
result += duration.hours + 'H'; | ||
} | ||
if(duration.minutes > 0){ | ||
result += duration.minutes + "M"; | ||
result += duration.minutes + 'M'; | ||
} | ||
if(duration.seconds > 0){ | ||
result += duration.seconds + "S"; | ||
result += duration.seconds + 'S'; | ||
} | ||
@@ -67,1 +67,11 @@ | ||
}; | ||
exports.toSeconds = function(stringOrDuration) { | ||
var duration = stringOrDuration; | ||
if(typeof stringOrDuration === 'string') { | ||
duration = exports.fromString(stringOrDuration); | ||
} | ||
return duration.hours * 3600 + duration.minutes * 60 + duration.seconds; | ||
}; |
{ | ||
"name": "durational", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Small library for dealing with Durations", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/miksago/durational", |
@@ -26,7 +26,7 @@ var Lab = require('lab'); | ||
expect(function(){ | ||
durational.fromSeconds(15650) | ||
durational.fromSeconds(15650); | ||
}).to.not.throw(); | ||
expect(function(){ | ||
durational.fromSeconds("15650") | ||
durational.fromSeconds('15650'); | ||
}).to.throw(); | ||
@@ -113,3 +113,3 @@ | ||
expect(function() { | ||
durational.fromString(string) | ||
durational.fromString(string); | ||
}).to.throw(); | ||
@@ -132,7 +132,25 @@ }); | ||
it('should convert from a number', function(done) { | ||
expect(durational.toString(4220)).to.equal('PT1H10M20S') | ||
expect(durational.toString(4220)).to.equal('PT1H10M20S'); | ||
done(); | ||
}); | ||
}) | ||
}) | ||
}); | ||
describe('toSeconds', function() { | ||
it('should convert from string', function(done) { | ||
expect(durational.toSeconds('PT1H10M20S')).to.equal(4220); | ||
done(); | ||
}); | ||
it('should convert from a duration object', function(done) { | ||
expect(durational.toSeconds({ | ||
hours: 1, | ||
minutes: 10, | ||
seconds: 21 | ||
})).to.equal(4221); | ||
done(); | ||
}); | ||
}); | ||
}); |
10391
9
179