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

@libchrono/core

v1.0.15

Published

Small library to handle durations parsing and displaying.

Downloads

45

Readme

@libchrono/core

npm version GitHub license

@libchrono/core is a library for parsing and validating strings representing durations of time. It provides a convenient way to parse strings like "1w 2h" into Duration objects, and provides convenient functions for performing basic math operations on those objects. So whether you need to parse duration strings, or perform calculations on them, @libchrono/core is the perfect library for your project.

Installation

To use @libchrono/core in your project, install it via npm:

npm install @libchrono/core

or yarn:

yarn add @libchrono/core

Usage

Parse duration literal to Duration object:

const { DurationUtils } = require('@libchrono/core');

const duration = DurationUtils.parse('1w 2h');

console.log(duration);
/**
 * Output:
 * {
 *    timestamp: 612000000,                 // count of millis in duration
 *    getWeeks: () => number,               // returns current count of weeks
 *    getDays: () => number,                // returns current count of days
 *    getHours: () => number,               // returns current count of hours
 *    getMinutes: () => number,             // returns current count of minutes
 *    getSeconds: () => number,             // returns current count of seconds
 *    getMillis: () => number,              // returns current count of millis
 *    toPlainDuration: () => PlainDuration, // returns object containing all units
 *    toStringLiteral: () => string,        // returns string like 1d 14h
 *    toWeeks: () => number,                // returns count of weeks
 *    toDays: () => number,                 // returns count of days with weeks 
 *                                          // converted to days
 *    toHours: () => number,                // returns count of hours with days 
 *                                          // and weeks converted to hours
 *    toMinutes: () => number,              // returns count of minutes with 
 *                                          // hours, days and weeks converted
 *                                          // to minutes
 *    toSeconds: () => number,              // returns count of seconds with
 *                                          // minutes, hours, days and weeks
 *                                          // converted to seconds
 *    toMillis: () => number,               // returns count of millis with
 *                                          // seconds, minutes, hours and
 *                                          // weeks converted to millis
 * }
 */

Parse duration literal with operations to Duration object:

const { DurationUtils } = require('@libchrono/core');

const duration = DurationUtils.parse('(1w 2h + 5h + 30m) * 2');

console.log(duration.toStringLiteral()); // Output: 2w 15h

Parse duration literal to Duration object and perform operations:

const { DurationUtils } = require('@libchrono/core');

const duration = DurationUtils.parse('1w');

console.log(duration.add(DurationUtils.parse('2d')).toStringLiteral());
// Output: 1w 2d

console.log(duration.sub(DurationUtils.parse('2d')).toStringLiteral());
// Output: 5d

console.log(duration.mul(2).toStringLiteral());
// Output: 2w

console.log(duration.div(2).toStringLiteral());
// Output: 3d 12h

Validate duration literal

const { DurationUtils } = require('@libchrono/core');

const duration1 = DurationUtils.validate('1w 2h');

console.log(duration1.isValid); // Output: true

const duration2 = DurationUtils.validate('1w 1q');

console.log(duration2.isValid); // Output: false

const duration3 = DurationUtils.validate('1d 1w');

console.log(duration3.isValid); // Output: false

Todo

  • ~~support for arithmetic operations in Duration object~~
  • ~~support for arithmetic operations in duration literal~~
  • ~~simple format function to display duration literal from timestamp~~
  • ~~add option to convert Duration to count of specific unit~~
  • ~~added JSDoc for methods and classes~~
  • add option to set schema for duration (assign count of hours in day and days in week)