iso8601-duration
Advanced tools
Comparing version 2.0.0 to 2.1.0
/** | ||
* @description A module for parsing ISO8601 durations | ||
*/ | ||
interface Duration { | ||
export interface Duration { | ||
years?: number; | ||
@@ -6,0 +6,0 @@ months?: number; |
@@ -8,15 +8,14 @@ "use strict"; | ||
/** | ||
* The pattern used for parsing ISO8601 duration (PnYnMnDTnHnMnS). | ||
* This does not cover the week format PnW. | ||
* The pattern used for parsing ISO8601 duration (PnYnMnWnDTnHnMnS). | ||
*/ | ||
// PnYnMnDTnHnMnS | ||
var numbers = "\\d+(?:[\\.,]\\d+)?"; | ||
var weekPattern = "(".concat(numbers, "W)"); | ||
var datePattern = "(".concat(numbers, "Y)?(").concat(numbers, "M)?(").concat(numbers, "D)?"); | ||
var timePattern = "T(".concat(numbers, "H)?(").concat(numbers, "M)?(").concat(numbers, "S)?"); | ||
var iso8601 = "P(?:".concat(weekPattern, "|").concat(datePattern, "(?:").concat(timePattern, ")?)"); | ||
// PnYnMnWnDTnHnMnS | ||
var numbers = "\\d+"; | ||
var fractionalNumbers = "".concat(numbers, "(?:[\\.,]").concat(numbers, ")?"); | ||
var datePattern = "(".concat(numbers, "Y)?(").concat(numbers, "M)?(").concat(numbers, "W)?(").concat(numbers, "D)?"); | ||
var timePattern = "T(".concat(fractionalNumbers, "H)?(").concat(fractionalNumbers, "M)?(").concat(fractionalNumbers, "S)?"); | ||
var iso8601 = "P(?:".concat(datePattern, "(?:").concat(timePattern, ")?)"); | ||
var objMap = [ | ||
"weeks", | ||
"years", | ||
"months", | ||
"weeks", | ||
"days", | ||
@@ -42,8 +41,17 @@ "hours", | ||
var parse = function (durationString) { | ||
// Slice away first entry in match-array | ||
return durationString | ||
.match(exports.pattern) | ||
.slice(1) | ||
.reduce(function (prev, next, idx) { | ||
prev[objMap[idx]] = parseFloat(next) || 0; | ||
var matches = durationString.replace(/,/g, ".").match(exports.pattern); | ||
if (!matches) { | ||
throw new RangeError("invalid duration: ".concat(durationString)); | ||
} | ||
// Slice away first entry in match-array (the input string) | ||
var slicedMatches = matches.slice(1); | ||
if (slicedMatches.filter(function (v) { return v != null; }).length === 0) { | ||
throw new RangeError("invalid duration: ".concat(durationString)); | ||
} | ||
// Check only one fraction is used | ||
if (slicedMatches.filter(function (v) { return /\./.test(v || ""); }).length > 1) { | ||
throw new RangeError("only the smallest unit can be fractional"); | ||
} | ||
return slicedMatches.reduce(function (prev, next, idx) { | ||
prev[objMap[idx]] = parseFloat(next || "0") || 0; | ||
return prev; | ||
@@ -63,6 +71,6 @@ }, {}); | ||
then.setDate(then.getDate() + duration.days); | ||
then.setHours(then.getHours() + duration.hours); | ||
then.setMinutes(then.getMinutes() + duration.minutes); | ||
// Then.setSeconds(then.getSeconds() + duration.seconds); | ||
then.setMilliseconds(then.getMilliseconds() + duration.seconds * 1000); | ||
// set time as milliseconds to get fractions working for minutes/hours | ||
var hoursInMs = duration.hours * 3600 * 1000; | ||
var minutesInMs = duration.minutes * 60 * 1000; | ||
then.setMilliseconds(then.getMilliseconds() + duration.seconds * 1000 + hoursInMs + minutesInMs); | ||
// Special case weeks | ||
@@ -69,0 +77,0 @@ then.setDate(then.getDate() + duration.weeks * 7); |
{ | ||
"name": "iso8601-duration", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Node/Js-module for parsing and making sense of ISO8601-durations", | ||
@@ -11,3 +11,3 @@ "main": "lib/index.js", | ||
"unittests": "uvu -r ts-node/register test", | ||
"watch": "onchange '**/*.js' -- npm run test", | ||
"tdd": "onchange '**/*.ts' -- npm run unittests", | ||
"compile": "tsc", | ||
@@ -40,2 +40,3 @@ "prepublishOnly": "npm run compile", | ||
"prettier": "^2.6.2", | ||
"@js-temporal/polyfill": "*", | ||
"ts-node": "^10.7.0", | ||
@@ -42,0 +43,0 @@ "typescript": "4.6.3", |
@@ -5,18 +5,18 @@ # ISO8601-duration | ||
[![Build Status: Travis](https://img.shields.io/travis/tolu/ISO8601-duration/master.svg)][travis] | ||
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Ftolu%2Fiso8601-duration%2Fbadge&style=popout)][gh-action] | ||
[![npm version](https://img.shields.io/npm/v/iso8601-duration.svg)][npm] | ||
![npm bundle size][bundlephobia] | ||
> A new standard is on it's way, see [Temporal.Duration](https://tc39.es/proposal-temporal/docs/duration.html) | ||
> A new standard is on it's way, see [Temporal.Duration](https://tc39.es/proposal-temporal/docs/duration.html) | ||
> Tests (most) in this module now validate against [@js-temporal/polyfill](https://www.npmjs.com/package/@js-temporal/polyfill) | ||
## The ISO8601 duration format | ||
Durations in ISO8601 comes in two formats: | ||
Durations in ISO8601 comes in this string format: | ||
- **`PnYnMnDTnHnMnS`** - `P<date>T<time>` | ||
- **`PnYnMnWnDTnHnMnS`** - `P<date>T<time>` | ||
The `n` is replaced by the value for each of the date and time elements that follow the `n`. | ||
Leading zeros are not required | ||
- **`PnW`** - the week format | ||
Check out the details on [Wikipedia](https://en.wikipedia.org/wiki/ISO_8601#Durations) | ||
Check out the details on [Wikipedia](https://en.wikipedia.org/wiki/ISO_8601#Durations) or in the coming [Temporal.Duration](https://tc39.es/proposal-temporal/docs/duration.html) spec. | ||
@@ -106,4 +106,4 @@ ## Install | ||
[travis]: https://travis-ci.org/tolu/ISO8601-duration "travis build status" | ||
[gh-action]: https://actions-badge.atrox.dev/tolu/iso8601-duration/goto | ||
[npm]: https://www.npmjs.com/package/iso8601-duration "npm package" | ||
[bundlephobia]: https://img.shields.io/bundlephobia/minzip/iso8601-duration |
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
8907
123
6