parse-time-to-ms
Advanced tools
Comparing version 0.0.1 to 0.1.0
10
index.js
module.exports = parseTime | ||
module.exports.s = seq | ||
var multiplier = [60, 60, 1000, 1] // hh, mm, ss, ms | ||
var multiplier = [60, 60, 1000] // hh, mm, ss | ||
@@ -15,6 +15,6 @@ function seq (...times) { | ||
var values = time.match(/\d+/g) | ||
return multiplier.reduce(function (total, coef, i) { | ||
return (total + parseInt(values[i] || 0)) * coef | ||
}, 0) | ||
var values = time.match(/\d{2}(\.\d+)?/g) | ||
return parseInt(multiplier.reduce(function (total, coef, i) { | ||
return (total + parseFloat(values[i] || 0)) * coef | ||
}, 0)) | ||
} |
{ | ||
"name": "parse-time-to-ms", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Ultralight module to parse a time string into milliseconds.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# parse-time-to-ms | ||
Ultralight module to parse a time string into milliseconds. | ||
Ultralight module to parse ISO 8601 time string into milliseconds. | ||
Provided with a valid time string , the parser will convert it to milliseconds. | ||
Valid strings are those of ISO 8601 with separators: | ||
Valid strings are those defined in ISO 8601: | ||
``` | ||
hh:mm:ss.sss | ||
hh:mm:ss | ||
hh:mm | ||
hh:mm:ss.sss or hhmmss.sss | ||
hh:mm:ss or hhmmss | ||
hh:mm or hhmm | ||
hh | ||
``` | ||
Please note that the library do not validate format. It means `hhmmss` ISO will not be parsed properly, and time string have to start with hours (eg. '35:10' would be interpreted as 35 hours 10 minutes). | ||
Please note that this module do not validate format. For example time string have to start with hours (eg. '35:10' would be interpreted as 35 hours 10 minutes). | ||
Some examples of valid formats: `'02:35'`, `'02:35:55'` or `'2:35:55.010'`. | ||
Some examples of valid formats: `'02:35'`, `'02:35:55'` or `'023555.010'`. | ||
@@ -42,8 +42,5 @@ | ||
var day = new Date(2017, 0, 1) | ||
console.log(day.toString()) | ||
// => Sun Jan 01 2017 00:00:00 GMT+1100 (DST) | ||
var time = parseTime('15:00') | ||
var datetime = new Date(+day + time) | ||
console.log(datetime.toString()) | ||
@@ -65,4 +62,4 @@ | ||
* `timeString` {String|Number} - a time string starting with hours up to milliseconds: `'02:35'` or `'2:35:55.010'`. | ||
An invalid date will not throw, but could result in unexpected result. Types other than string will be parsed to integer. | ||
* `timeString` {String|Number} - a time string starting with hours: `'02:35'` or `'2:35:55.010'`. | ||
An invalid time will not throw, but could result in unexpected result. Types other than string will be parsed to integer. | ||
@@ -73,3 +70,3 @@ ## `parseTime.s(timeString1, [timeString2, ...])` -> Array<Integer> | ||
* `timeString1`, `timeString2`, `...` {String|Number} - valid date strings. | ||
* `timeString1`, `timeString2`, `...` {String|Number} - valid time strings. | ||
@@ -76,0 +73,0 @@ |
@@ -7,7 +7,10 @@ var parseTime = require('../') | ||
test('complete time', function (t) { | ||
var time = '23:30:10.001' | ||
var parsedTime = parseTime(time) | ||
var timeA = '23:30:10.001' | ||
var timeB = '233010.001' | ||
var parsedTimeA = parseTime(timeA) | ||
var parsedTimeB = parseTime(timeB) | ||
var expectedMs = new Date(2017, 0, 1, 23, 30, 10, 1) - baseTime | ||
t.equal(parsedTime, expectedMs) | ||
t.equal(parsedTimeA, expectedMs) | ||
t.equal(parsedTimeB, expectedMs) | ||
t.end() | ||
@@ -26,3 +29,3 @@ }) | ||
test('h:mm format', function (t) { | ||
var time = '5:30' | ||
var time = '05:30' | ||
var parsedTime = parseTime(time) | ||
@@ -45,3 +48,4 @@ var expectedMs = new Date(2017, 0, 1, 5, 30, 0, 0) - baseTime | ||
test('sequence', function (t) { | ||
var sequence = parseTime.s('00:30', '02:25:30', '12:34:10.349') | ||
var sequenceA = parseTime.s('00:30', '02:25:30', '12:34:10.349') | ||
var sequenceB = parseTime.s('00:30', '02:25:30', '12:34:10.349') | ||
var expectedMs = [ | ||
@@ -53,7 +57,8 @@ new Date(2017, 0, 1, 0, 30, 0, 0) - baseTime, | ||
t.deepEqual(sequence, expectedMs) | ||
t.deepEqual(sequenceA, expectedMs) | ||
t.deepEqual(sequenceB, expectedMs) | ||
t.end() | ||
}) | ||
test('passing an int will be returned as it', function (t) { | ||
test('passing a number will be returned as it', function (t) { | ||
var time = 30000 | ||
@@ -60,0 +65,0 @@ var parsedTime = parseTime(time) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5154
85
92