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

Add support for weeks and multiple units - #46, #54 #55

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var w = d * 7;
var y = d * 365.25;
var separators = [' ', '.', ','];

/**
* Parse or format the given `val`.
Expand All @@ -23,24 +25,79 @@ var y = d * 365.25;

module.exports = function(val, options){
options = options || {};
if ('string' == typeof val) return parse(val);
if ('string' === typeof val) return parse(val);
return options['long']
? fmtLong(val)
: fmtShort(val);
};

/**
* Parse the given `str` and return milliseconds.
* Parse the given `str` and return milliseconds. Can contain multiple units.
*
* @param {String} str
* @return {Number}
* @api private
*/

function parse(str) {
var units = tokenize(str);
if (!units.length) { return; }

var ms = 0;
var parsed, i;
for (i = 0; i < units.length; i++) {
parsed = parseString(units[i]);
if ('undefined' === typeof parsed) { return; }
ms += parsed;
}
return ms;
}

/**
* Splits the given `str` into multiple unit/value tokens.
*
* @param {String} str
* @return {Array}
* @api private
*/

function tokenize(str) {
var units = [];
var buf = '';
var sawLetter = false;
var i, c;
for (i = 0; i < str.length; i++) {
c = str[i];
if (~separators.indexOf(c)) {
buf += c;
} else if (isNaN(c)) {
sawLetter = true;
buf += c;
} else {
if (sawLetter) {
units.push(buf.trim());
buf = '';
}
sawLetter = false;
buf += c;
}
}
if (buf.length) { units.push(buf.trim()); }
return units;
}

/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/

function parseString(str) {
str = '' + str;
if (str.length > 10000) return;
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str);
var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(str);
if (!match) return;
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
Expand All @@ -51,6 +108,10 @@ function parse(str) {
case 'yr':
case 'y':
return n * y;
case 'weeks':
case 'week':
case 'w':
return n * w;
case 'days':
case 'day':
case 'd':
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"scripts": {
"ms/index.js": "index.js"
}
},
"scripts": {
"test": "./node_modules/mocha/bin/_mocha ./test/* -R spec"
}
}
24 changes: 22 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ describe('ms(string)', function(){
expect(ms('1m')).to.be(60000);
});

it('should convert from h to ms', function () {
expect(ms('1h')).to.be(3600000);
it('should convert w to ms', function () {
expect(ms('2w')).to.be(1209600000);
});

it('should convert d to ms', function () {
expect(ms('2d')).to.be(172800000);
});

it('should convert from h to ms', function () {
expect(ms('1h')).to.be(3600000);
});

it('should convert s to ms', function () {
expect(ms('1s')).to.be(1000);
});
Expand All @@ -43,6 +47,14 @@ describe('ms(string)', function(){
expect(ms('1 s')).to.be(1000);
});

it('should work with multiple units', function () {
expect(ms('1h1m32s')).to.be(3692000);
});

it('should work with multiple units with spaces', function () {
expect(ms('1 h 1 m 32 s')).to.be(3692000);
});

it('should return NaN if invalid', function () {
expect(isNaN(ms('☃'))).to.be(true);
});
Expand Down Expand Up @@ -83,9 +95,17 @@ describe('ms(long string)', function(){
expect(ms('2 days')).to.be(172800000);
});

it('should convert weeks to ms', function () {
expect(ms('2 weeks')).to.be(1209600000);
});

it('should work with decimals', function () {
expect(ms('1.5 hours')).to.be(5400000);
});

it('should work with multiple units', function () {
expect(ms('1 hour 1 minute 32 seconds')).to.be(3692000);
});
})

// numbers
Expand Down