iso8601-duration
Advanced tools
Comparing version 1.0.4 to 1.0.6
{ | ||
"name": "iso8601-duration", | ||
"version": "1.0.4", | ||
"version": "1.0.6", | ||
"description": "Node/Js-module for parsing and making sense of ISO8601-durations", | ||
@@ -9,5 +9,6 @@ "main": "lib/index.js", | ||
"test": "xo && ava", | ||
"watch": "onchange '**/*.js' -- npm run test", | ||
"compile": "babel -d lib/ src/", | ||
"prepublish": "npm run compile", | ||
"watch": "onchange '**/*.js' -- npm run test" | ||
"patch-release": "npm test && npm version patch && git push --follow-tags" | ||
}, | ||
@@ -52,4 +53,6 @@ "files": [ | ||
"verbose": true, | ||
"require": ["babel-core/register"] | ||
"require": [ | ||
"babel-core/register" | ||
] | ||
} | ||
} |
@@ -8,4 +8,13 @@ # ISO8601-duration | ||
## The ISO8601 duration format | ||
Durations in ISO8601 comes in two formats: | ||
* **`PnYnMnDTnHnMnS`** - `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) | ||
## Install | ||
@@ -18,7 +27,12 @@ | ||
## Usage | ||
Most noteworthy of the interface is the ability to provide a `date` for `toSeconds`-calculations. | ||
Why becomes evident when working with durations that span dates as all months are not equally long. | ||
E.g January of 2016 is **744 hours** compared to the **696 hours** of February 2016. | ||
If a date is not provided for `toSeconds` the timestamp `Date.now()` is used as baseline. | ||
### Interface | ||
```js | ||
export const toSeconds; // fn = obj => number | ||
export const toSeconds; // fn = (obj, date?) => number | ||
export const pattern; // ISO8601 RegExp | ||
@@ -34,7 +48,29 @@ export const parse; // fn = string => obj | ||
### Example | ||
Simple usage | ||
```js | ||
import {parse, toSeconds, pattern} as iso8601 from 'iso8601-duration'; | ||
import {parse, toSeconds, pattern} from 'iso8601-duration'; | ||
console.log(parse('P1Y2M4DT20H44M12.67S')); | ||
/* outputs => | ||
{ | ||
years: 1, | ||
months: 2, | ||
days: 4, | ||
hours: 20, | ||
minutes: 44, | ||
seconds: 12.67 | ||
} | ||
*/ | ||
console.log( toSeconds( parse('PT1H30M10.5S') ) ); | ||
// outputs => 5410.5 | ||
``` | ||
A more complete usecase / example | ||
```js | ||
import {parse, toSeconds, pattern} from 'iso8601-duration'; | ||
// convert iso8601 duration-strings to total seconds from some api | ||
const getWithSensibleDUrations = someApiEndpoint => { | ||
const getWithSensibleDurations = someApiEndpoint => { | ||
// return promise, like fetch does | ||
@@ -41,0 +77,0 @@ return new Promise(resolve => { |
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
9568
101