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

@musement/iso-duration

v1.0.0

Published

JS's library created to provide easy API for working with time duration. Based on ISO 8601. Inspired by [HumanizeDuration](https://github.com/EvanHahn/HumanizeDuration.js) and [ISO8601-duration](https://github.com/tolu/ISO8601-duration).

Downloads

3,886

Readme

@musement/iso-duration

JS's library created to provide easy API for working with time duration.
Based on ISO 8601.
Inspired by HumanizeDuration and ISO8601-duration.

Installation:

npm i @musement/iso-duration --save

Quick start:

// Import isoDuration and locales
import { isoDuration, en, pl, it } from '@musement/iso-duration'

// Setup locales
//   key - string you want to use in `humanize` function
//   value - IsoDuration i18n object.
isoDuration.setLocales(
  {
    en,
    pl,
    it,
  },
  {
    fallbackLocale: 'en',
  }
)

//Create duration object
const duration = isoDuration("P8DT30M")
// OR
const duration = isoDuration({
  days: 8,
  minutes: 30
})

//Get JS duration object
console.log(duration.parse())
// {
//   weeks: 0,
//   years: 0,
//   months: 0,
//   days: 8,
//   hours: 0,
//   minutes: 30,
//   seconds: 0
// }

//Get duration ISO string
console.log(duration.toString())
// P8DT30M

//Humanize duration 
console.log(duration.humanize('en'))
// 8 days 30 minutes

API:

Duration object supports:

  • ISO_8601 duration string (PnYnMnDTnHnMnS, PnW)
  • JS objects:
{
  "weeks": Number,
  "years": Number,
  "months": Number,
  "days": Number,
  "hours": Number,
  "minutes": Number,
  "seconds": Number,
}

Example:

import { isoDuration } from '@musement/iso-duration@'

const durationFromString = isoDuration("P8DT30M")
const durationFromObj = isoDuration({
  days: 8,
  minutes: 30,
})

i18n

All languages which are expected to be available in .humanize method needs to be loaded using isoDuration.setLocales function. Currently, library provides support for languages listed under /src/locales.

import { isoDuration, en, pl, it } from '@musement/iso-duration'

isoDuration.setLocales({
  en,
  pl,
  it,
  'en-GB': en,
  'en-US': en,  
})

Configuration

isoDuration.setLocales can take an option object as second argument (optional):

| Key | Type | Description | Default | ---------------------- | -------- |-------------------------------------------------------------- | ----------- | fallbackLocale | String | fallback locale used when an invalid locale is passed to IsoDuration.humanize | undefined

IsoDuration object:

  • parse() return DurationObj with all time periods represented as JS object
console.log(isoDuration("P8T30M").parse())
// {
//   weeks: 0,
//   years: 0,
//   months: 0,
//   days: 8,
//   hours: 0,
//   minutes: 30,
//   seconds: 0
// }
  • toString() returns ISO_8601 string:
console.log(isoDuration("P8T30M").toString())
// P8T30M
  • humanize(lang: string, config?: HumanizeConfig) returns duration in human readable way:
    ⚠ Warning ️⚠ - used lang needs to be previously added to the library using isoDuration.setLocales
console.log(isoDuration("P8T30M").humanize('en'))
// 8 hours 30 minutes

HumanizeConfig Object:

{
  largest: number
}

largest - Reduce number of humanized to N the largest units:

console.log(
  isoDuration({
    years: 2,
    months: 0,
    days: 8,
    hours: 20,
    minutes: 30,
    seconds: 15
  }).humanize('en', { largest: 2 })
)
// 2 years 8 days
  • normalize(date?: Date) returns normalized IsoDuration object:
console.log(isoDuration("PT90M").normalize().parse())
// PT1H30M

This method takes an optional argument date which defines start of duration. It's used to correctly normalize number of days basing on corresponding month's length.
If it's not present normalize function will use current date (new Date()) instead.

Month with 31 days:

console.log("Duration:", isoDuration("P31D").normalize(new Date(2020, 0, 1)).humanize('en'))
//Duration: 31 days

Month with 29 days:

console.log("Duration:", isoDuration("P31D").normalize(new Date(2020, 1, 1)).humanize('en'))
//Duration: 1 month 2 days

Credits

EvanHahn - author of HumanizeDuration
tolu author of ISO8601-duration