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

x-duration

v1.0.0

Published

Syntactic sugar functions to handle durations and dates

Downloads

2

Readme

X-Duration

Syntactic sugar functions to handle duration and dates.

const duration = d(2, "days").plus(d(1, "hour"))

duration.inMinutes
// 2940

duration.ago
// 2036-12-24T10:11:12.013Z

Features

  • Syntactic sugar — Inspired from rails Duration utilities: manipulate durations and apply them on Dates.

  • Reliable — Based on luxon (JS native Date manipulation can give some unexpected results).

  • Typed — With Typescript.

  • MIT license — Use it like you want.

Getting started

npm install x-duration
import d from "x-duration"

const duration = d(2, "minutes")

// setTimeout(..., duration.inMs)

Documentation

import d, { XDurationFactory, XDuration } from "x-duration"

XDurationFactory

declare const d: {
  // Factory to create durations
  (value: number, unit: XDurationUnitInput): XDuration;

  // Re-wrap a duration (in milliseconds) after summing them
  sum(value: number): XDuration;
};

type XDurationUnitInput =
  | "milliseconds" | "millisecond" | "ms"
  | "seconds" | "second" | "s"
  | "minutes" | "minute" | "m"
  | "hours" | "hour" | "h"
  | "days" | "day" | "D"
  | "weeks" | "week" | "W"
  | "months" | "month" | "M"
  | "quarters" | "quarter" | "Q"
  | "years" | "year" | "Y";

Examples

d(10, "milliseconds")
d(1, "millisecond")

d(10, "seconds")
d(1, "second")

d(10, "minutes")
d(1, "minute")

d(10, "hours")
d(1, "hour")

d(10, "days")
d(1, "day")

d(10, "weeks")
d(1, "week")

d(10, "months")
d(1, "month")

d(10, "quarters")
d(1, "quarter")

d(10, "years")
d(1, "year")

d.sum(d(1, "hour") + d(10, "minutes"))
d.sum(d(1, "hour") - d(10, "minutes"))

XDuration

declare class XDuration {
  constructor(parts: XDurationInput);

  /**
   * Unit converters
   */
  get inMilliseconds(): number;
  get inMs(): number;
  get inSeconds(): number;
  get inMinutes(): number;
  get inHours(): number;
  get inDays(): number;
  get inWeeks(): number;
  get inMonths(): number;
  get inQuarters(): number;
  get inYears(): number;

  /**
   * Transformations (mutates the current object)
   */
  // Adds the given duration to current duration
  plus(duration: XDuration): XDuration;

  // Removes the given duration to current duration
  minus(duration: XDuration): XDuration;

  // Rescales units to their largest representation
  rescale(): XDuration;

  /**
   * Date converters
   */
  // Returns a new Date with the time of the given date plus the current duration
  after(date: Date): Date;

  // Alias of `after`
  since(date: Date): Date;

  // Alias of `after(new Date)`
  get fromNow(): Date;

  // Returns a new Date with the time of the given date minus the current duration
  before(date: Date): Date;

  // Alias of `before`
  until(date: Date): Date;

  // Alias of `before(new Date)`
  get ago(): Date;

  /**
   * Outputs
   */
  // Returns duration in milliseconds
  valueOf(): number;

  // Returns duration in milliseconds
  toString(): number;

  // Returns ISO 8601 format
  toISO(): string | null;

  // Alias of `toISO`
  toJSON(): string | null;

  // Returns the formatted duration
  toFormat(
    formatString: string,
    options?: {
      floor?: boolean;
    }
  ): string;

  // Returns the internal representation of the current duration
  toObject(): XDurationLikeObject;
}

Examples

/**
 * Unit converters
 */
d(86400000, "millisecond").inDays
// 1

d(30, "seconds").inMilliseconds
// 30000

d(1, "minute").inMilliseconds
// 60000

d(10, "minutes").inSeconds
// 600

d(1.5, "hours").inMinutes
// 90

d(1.5, "days").inHours
// 36

d(0.5, "weeks").inHours
// 84 (3 * 24 + 12)

d(0.5, "months").inDays
// 15

d(1.5, "quarters").inMonths
// 4.5

d(1.5, "years").inMonths
// 18
/**
 * Transformations
 */
d(2, "hours").plus(d(90, "minutes")).toObject()
// { hours: 2, minutes: 90 }

d(2, "hours").minus(d(90, "minutes")).toObject()
// { hours: 2, minutes: -90 }

d(2, "hours").plus(d(90, "minutes")).rescale().toObject()
// { hours: 3, minutes: 30 }
/**
 * Date converters
 */
// Let's say the current date is "2036-01-01 12:00:00 GMT"

d(3, "minutes").ago
// 2036-01-01T11:57:00.000Z

d(2, "hours").fromNow
// 2036-01-01T14:00:00.000Z

d(2, "hours").before(new Date("2036-01-01 12:00:00 GMT"))
// 2036-01-01T10:00:00.000Z

d(3, "minutes").after(new Date("2036-01-01 12:00:00 GMT"))
// 2036-01-01T12:03:00.000Z
/**
 * Outputs
 */
const duration = d(2, "months").plus(d(10, "days")).plus(d(1, "minute"))

duration.valueOf()
// 6048060000

duration.toISO()
// "P2M10DT1M"

duration.toFormat("M S")
// "2 864060000"

duration.toObject()
// { minutes: 1, days: 10, months: 2 }

Extra - Type unboxing

Operator overloading is not really possible in Javascript, but you can sum durations to other durations (though it has some limitations, and typescript is not really happy about it.)

d(2, "hours") + d(1, "minute")
// 7260000 (duration in milliseconds), so we can re-wrap it as a duration:

d.sum(d(2, "hours") + d(1, "minute")).toObject()
// { hours: 2, minutes: 1 }
setTimeout(..., d(1, "minute"))
// will work but it's better calling explicitly "setTimeout(..., d(1, "minute").inMs)"