From 20fe995361868420eff0030e829a0b43d143b3d6 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 31 Oct 2017 19:59:27 +0100 Subject: [PATCH] Added colon separated time support --- index.js | 9 +++++++++ tests.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/index.js b/index.js index 6a522b1..a629c12 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var m = s * 60; var h = m * 60; var d = h * 24; var y = d * 365.25; +var colonOrder = ['h', 'm', 's']; /** * Parse or format the given `val`. @@ -49,6 +50,14 @@ function parse(str) { if (str.length > 100) { return; } + var segments = str.split(':'); + if (segments.length > 1 && segments.length < 4) { + return segments + .map(function (t, i) { + return parseInt(t) + colonOrder[i] + }) + .join(''); + } var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec( str ); diff --git a/tests.js b/tests.js index 9b437f4..4df664a 100644 --- a/tests.js +++ b/tests.js @@ -100,6 +100,28 @@ describe('ms(long string)', function() { }); }); +// colon separated strings + +describe('ms(colon separated string)', function() { + it('should not throw an error', function() { + expect(function() { + ms('07:55'); + }).to.not.throwError(); + }); + + it('should support hours and minutes', function() { + expect(ms('07:55')).to.be('7h55m'); + }); + + it('should support hours, minutes and seconds', function() { + expect(ms('07:55:10')).to.be('7h55m10s'); + }); + + it('should remove leading zeros', function() { + expect(ms('07:05:05')).to.be('7h5m5s'); + }); +}); + // numbers describe('ms(number, { long: true })', function() {