parse-duration
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -21,3 +21,3 @@ // ./index.d.ts | ||
type Parse = { | ||
(input: string, format?: parse.Units): number | undefined; | ||
(input: string, format?: parse.Units): number | null; | ||
[key: string]: number; | ||
@@ -24,0 +24,0 @@ } & { |
83
index.js
@@ -13,40 +13,40 @@ 'use strict' | ||
parse.nanosecond = | ||
parse.ns = 1 / 1e6 | ||
parse.year = | ||
parse.yr = | ||
parse.y = 60000 * 60 * 24 * 365.25 | ||
parse['µs'] = | ||
parse['μs'] = | ||
parse.us = | ||
parse.microsecond = 1 / 1e3 | ||
parse.month = | ||
parse.b = 60000 * 60 * 24 * (365.25 / 12) | ||
parse.millisecond = | ||
parse.ms = | ||
parse[''] = 1 | ||
parse.week = | ||
parse.wk = | ||
parse.w = 60000 * 60 * 24 * 7 | ||
parse.second = | ||
parse.sec = | ||
parse.s = parse.ms * 1000 | ||
parse.day = | ||
parse.d = 60000 * 60 * 24 | ||
parse.hour = | ||
parse.hr = | ||
parse.h = 60000 * 60 | ||
parse.minute = | ||
parse.min = | ||
parse.m = parse.s * 60 | ||
parse.min = | ||
parse.m = 60000 | ||
parse.hour = | ||
parse.hr = | ||
parse.h = parse.m * 60 | ||
parse.second = | ||
parse.sec = | ||
parse.s = 1000 | ||
parse.day = | ||
parse.d = parse.h * 24 | ||
parse.millisecond = | ||
parse.millisec = | ||
parse.ms = 1 | ||
parse.week = | ||
parse.wk = | ||
parse.w = parse.d * 7 | ||
parse['µs'] = | ||
parse['μs'] = | ||
parse.us = | ||
parse.microsecond = 1 / 1e3 | ||
parse.month = | ||
parse.b = | ||
parse.d * (365.25 / 12) | ||
parse.nanosecond = | ||
parse.ns = 1 / 1e6 | ||
parse.year = | ||
parse.yr = | ||
parse.y = parse.d * 365.25 | ||
@@ -61,17 +61,20 @@ /** | ||
function parse(str='', format='ms'){ | ||
var result = null | ||
function parse(str = '', format = 'ms') { | ||
var result = null, prevUnits | ||
// ignore commas/placeholders | ||
str = (str+'').replace(/(\d)[,_](\d)/g, '$1$2') | ||
var isNegative = str[0] === '-'; | ||
str.replace(durationRE, function(_, n, units){ | ||
units = unitRatio(units) | ||
if (units) result = (result || 0) + Math.abs(parseFloat(n, 10)) * units | ||
str = (str + '').replace(/(\d)[,_](\d)/g, '$1$2') | ||
str.replace(durationRE, function (_, n, units) { | ||
// if no units, find next smallest units or fall back to format value (ms) | ||
if (!units) { | ||
if (prevUnits) { | ||
for (var u in parse) if (parse[u] < prevUnits) { units = u; break } | ||
} | ||
else units = format | ||
} | ||
else units = units.toLowerCase() | ||
units = parse[units] || parse[units.replace(/s$/, '')] | ||
if (units) result = (result || 0) + Math.abs(parseFloat(n, 10)) * units, prevUnits = units | ||
}) | ||
return result && ((result / (unitRatio(format) || 1)) * (isNegative ? -1 : 1)) | ||
return result && ((result / (parse[format] || 1)) * (str[0] === '-' ? -1 : 1)) | ||
} | ||
function unitRatio(str) { | ||
return parse[str] || parse[str.toLowerCase().replace(/s$/, '')] | ||
} |
{ | ||
"name": "parse-duration", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "convert a human readable duration string to a duration format", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -68,3 +68,3 @@ | ||
```js | ||
parse('2hr -40mins') // => 1 * h + 20 * m | ||
parse('-1hr 40mins') // => 1 * h + 40 * m | ||
``` | ||
@@ -71,0 +71,0 @@ |
Sorry, the diff of this file is not supported yet
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
7293
144