GithubHelp home page GithubHelp logo

toshb / iso8601-duration Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tolu/iso8601-duration

0.0 2.0 0.0 26 KB

Node/Js-module for parsing and making sense of ISO8601-durations

JavaScript 100.00%

iso8601-duration's Introduction

ISO8601-duration

Node/Js-module for parsing and making sense of ISO8601-durations

XO code style Build Status: Travis npm version

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

Install

$ npm install iso8601-duration

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

export const toSeconds; // fn = (obj, date?) => number
export const pattern;   // ISO8601 RegExp
export const parse;     // fn = string => obj
export default {
	toSeconds,
	pattern,
	parse
}

Example

Simple usage

import {parse, end, 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

console.log ( end( parse('P1D') ) );
// outputs => DateObj 2017-10-04T10:14:50.190Z

A more complete usecase / example

import {parse, toSeconds, pattern} 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 @ https://tolu.mit-license.org/

iso8601-duration's People

Contributors

tolu avatar zurfyx avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.