Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added colon separated time support #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.js
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
);
Expand Down
22 changes: 22 additions & 0 deletions tests.js
Expand Up @@ -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() {
Expand Down