npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iso8601-duration

v2.1.2

Published

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

Downloads

1,522,530

Readme

ISO8601-duration

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

Build Status npm version npm bundle size

A new standard is on it's way, see Temporal.Duration
Tests (most) in this module now validate against @js-temporal/polyfill

The ISO 8601 duration format

TL;DR
PnYnMnWnDTnHnMnS - P<date>T<time>.
(P) Years, Months, Weeks, Days (T) Hours, Minutes, Seconds.
Example: P1Y1M1DT1H1M1.1S = One year, one month, one day, one hour, one minute, one second, and 100 milliseconds

Durations in ISO 8601 comes in 2 variants:

ISO 8601-1
Weeks are not allowed to appear together with any other units and durations can only be positive (used until v2.0.0 in this module).
Valid patterns with weeks: P2W.
Invalid patterns with weeks: P2W2D.

ISO 8601-2
An extension to the standard, allows combining weeks with other units (supported since v2.1.0 in this module).
Valid patterns with weeks: P2W & P2W2DT5H, etc.

ISO 8601-2 also allows for a sign character at the start of the string (-P1D, +P1M), this is not yet supported by this module.

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.
  • Fractions are allowed on the smallest unit in the string, e.g. P0.5D or PT1.0001S but not PT0.5M0.1S.

Check out the details on Wikipedia or in the coming Temporal.Duration spec.

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;   // ISO 8601 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/