Package Exports
- iso8601-duration
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (iso8601-duration) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ISO8601-duration
Node/Js-module for parsing and making sense of ISO8601-durations
Install
$ npm install iso8601-duration
Usage
Interface
export const toSeconds; // fn = obj => number
export const pattern; // ISO8601 RegExp
export const parse; // fn = string => obj
export default {
toSeconds,
pattern,
parse
}
Example
import {parse, toSeconds, pattern} as iso8601 from 'iso8601-duration';
// convert iso8601 duration-strings to total seconds from some api
const getWithSensibleDUrations = someApiEndpoint => {
// return promise, like fetch does
return new Promise(resolve => {
// fetch text
fetch(someApiEndpoint)
.then(res => res.text())
.then(jsonString => {
// create new pattern that matches on surrounding double-quotes
// so we can replace the string with an actual number
const replacePattern = new RegExp(`\\"${pattern.source}\\"`, 'g');
jsonString = jsonString.replace(replacePattern, m => {
return toSeconds(parse(m));
});
// resolve original request with sensible durations in object
resolve( JSON.parse(jsonString) );
});
});
}
License
MIT © Tobias Lundin