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

betterduration

v1.0.0

Published

ISO-8601 duration parsing and serialization

Downloads

42

Readme

BetterDuration

Note : This is a fork of betterduration with some additional features in active development.

A small javascript package to parse and serialize ISO-8601 durations. This package does only 3 things:

  1. It parses a duration string to an object
    • (e.g. P1DT12H to { days: 1, hours: 12 })
  2. The reverse, i.e. serialize an object to a string.
  3. Converts the parsed string to milliseconds.

This lib has 0 dependencies.

Installation

  • NPM: npm install --save betterduration
  • Yarn: yarn add betterduration

Usage

import { parse, serialize, parseToMilliseconds } from 'betterduration'

// Basic parsing
const durationObj = parse('P1Y2M3DT4H5M6S')
assert(durationObj, {
    years: 1,
    months: 2,
    days: 3,
    hours: 4,
    minutes: 5,
    seconds: 6,
})

// Serialization
assert(serialize(durationObj), 'P1Y2M3DT4H5M6S')

Development

This library is written in TypeScript. During publication of the package, the code is transpiled to javascript and put into the dist folder.

The tests can be found the src folder under *.test.ts, testing is done using Jest

Additional commands you'll need for development:

  • yarn test to run all tests
  • yarn lint to run the linter
  • yarn prettify to auto-fix the indenting issues
  • yarn ci to run coverage and linting

API

Type: Duration

| Property | Type | Description | | -------- | ------------------------ | --------------------------------- | | negative | boolean or undefined | Duration is positive if undefined | | years | number or undefined | | | months | number or undefined | | | weeks | number or undefined | | | days | number or undefined | | | hours | number or undefined | | | minutes | number or undefined | | | seconds | number or undefined | |

Type: ParseConfig

| Property | Type | Description | | ---------------------- | ------------------------ | ------------------- | | allowMultipleFractions | boolean or undefined | Defaults to true. |

Function: parse(durationStr: string, config: ParseConfig): Duration

parse accepts a string and returns a Duration object.

No attempt is made to change lower units into higher ones, e.g. to change 120 minutes into 2 hours.

Throws InvalidDurationError if an invalid duration string is supplied.

Throws MultipleFractionsError if an the duration string contains multiple fractions while disabled in the config. According to the spec multiple fractions are not allowed. Currently this is not enforced and the allowMultipleFractions config parameter defaults to true.

import { parse } from 'betterduration'

const duration = parse('P1W')
assert(duration, { weeks: 1 })

try {
    parse('invalid-duration')
} catch (e) {
    assert(e.message === 'Invalid duration')
}

Function: parseToMilliseconds(durationStr: string, config: ParseConfig): Duration

parse accepts a string and returns duration in milliseconds

import { parseToMilliseconds } from 'betterduration'

const ms = parseToMilliseconds('PT1M')
assert(ms, 60000)

Function: serialize(Duration): string

serialize accepts a Duration object and returns a serialized duration according to ISO-8601.

If the duration is empty (i.e. all values are 0), PT0S is returned.

import * as Duration from 'betterduration'

const durationStr = Duration.serialize({ weeks: 1 })
assert(durationStr, 'P1W')

const durationStr = Duration.serialize({})
assert(durationStr, 'PT0S')

License

MIT