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

uhrwerk

v1.1.2

Published

time utility

Downloads

14

Readme

uhrwerk 🕰

package size downloads badge types badge version badge

Minimal time duration utility. Replacement for MomentJS Durations. If you are looking into the time component of MomentJS check out this awesome library dayjs.

📦 It's tiny: 2kB vs moment js 295kB

🌈 No dependencies, types included.

Quickstart 🚀

import { Duration } from 'uhrwerk'

const d = new Duration(10, 'days')
d.subtract(1, 'week')
d.add(5, 'minutes')

d.humanize() // '3 days'
d.minutes() // 5
d.asMinute() // 4325

d.subtract(3, 'days')
d.humanize() // 'a few minutes'

Reference 📒

new Duration(amount, interval)

  • amount: number
  • interval:
    • millisecond, milliseconds, ms
    • second, seconds, s
    • minute, minutes, m
    • hour, hours, h
    • day, days, d
    • week, weeks, w
    • year, years, y
Examples
const a = new Duration(1, 'day')
const b = new Duration(2, 'days')
const c = new Duration(0.5, 'year')
const d = new Duration(Date.now(), 'ms')

.add(amount, interval)

Adds a specified amount to an existing duration

Example
const a = new Duration(1, 'day')
a.add(12, 'hours')
a.asHour() // 36

.subtract(amount, interval)

Subtracts a specified amount to an existing duration

Example
const a = new Duration(1, 'day')
a.subtract(12, 'hours')
a.asHour() // 12

Getters

Gets the amount of time interval, not the total time

  • .milliseconds()
  • .seconds()
  • .minutes()
  • .hours()
  • .days()
  • .weeks()
  • .years()
Example
const a = new Duration(1, 'day')
a.days() // 1
a.add(5, 'minutes')
a.days() // 1
a.add(1, 'year')
a.days() // 1
a.add(24, 'hours')
a.days() // 2

As interval

Calculates the time duration as a time interval.

  • .asMilliseconds()
  • .asSeconds()
  • .asMinutes()
  • .asHours()
  • .asDays()
  • .asWeeks()
  • .asYears()
Example
const a = new Duration(1, 'day')
a.asHours() // 24

.humanize()

This functions takes a duration and tries to make a human readable version out of it.

Example
const a = new Duration(4, 'seconds')
a.humanize() // 'a moment'
a.add(5, 'minutes')
a.humanize() // 'a few minutes'
Own rules / i18n

If you want to pass a different humanize function you can. The order of the array is important. The first match will return, like in a standard server router. The first argument is a function that takes the duration and returns a boolean. The second takes also matched duration and returns a string for the user.

Example
const humanizer = [
  [(d) => d.days() > 1, (d) => `${d.days()} days`],
  [(d) => d.days() > 0, (d) => `1 day`],
  [() => true, () => 'catch all, below 1 day'],
]

const a = new Duration(2, 'days')
a.humanize(humanizer) // '2 days'
a.subtract(1, 'day')
a.humanize(humanizer) // '1 day'
a.subtract(12, 'hours')
a.humanize(humanizer) // 'catch all, below 1 day'