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

formatmicro

v1.0.7

Published

Formats microseconds into a human consumable form.

Downloads

15

Readme

formatmicro Build Status Coverage Status Docs Status

Formats microseconds into a human consumable form.

Install

$ npm install formatmicro --save

Usage

import microseconds from 'microseconds'
import formatmicro from 'formatmicro'

const start = microseconds.now()

// do some stuf

const end = microseconds.now()
const period = end - start

console.log("Task completed in: " + formatmicro(period))
import formatmicro from 'formatmicro'

const oneµs = 1
const oneMs = 1000
const oneS  = 1000*1000
const oneM  = 60*1000*1000
const oneH  = 60*60*1000*1000
const oneD  = 24*60*60*1000*1000

const totalTime = 4*oneD + 12*oneH + 16*oneM + 59*oneS + 9*oneMs + 6*oneµs

console.log(formatmicro(totalTimeMult))
// 4 d 12 h 16 m 59 s 9 ms 6 µs

bignames

You can also import bignames, to output increments using their full names (e.g "days", "hours", etc.). For example:

import {bignames} from 'formatmicro'

const oneµs = 1
const oneMs = 1000
const oneS  = 1000*1000
const oneM  = 60*1000*1000
const oneH  = 60*60*1000*1000
const oneD  = 24*60*60*1000*1000

const totalTime = oneD + 12*oneH + 16*oneM + 59*oneS + oneMs + 6*oneµs

console.log("Task completed in: " + bignames(totalTime))
// Task completed in: 1 day 12 hours 16 minutes 59 seconds 1 millisecond 6 microseconds

The function signature is the same as formatmicro, so you could provide a custom reduce or custom increment names if you wanted.

onlytwo

You can also import onlytwo, to output only the first two, non-zero increments. For example:

import {onlytwo} from 'formatmicro'

const oneµs = 1
const oneMs = 1000
const oneS  = 1000*1000
const oneM  = 60*1000*1000
const oneH  = 60*60*1000*1000
const oneD  = 24*60*60*1000*1000

const totalTime = oneD + 12*oneH + 16*oneM + 59*oneS + oneMs + 6*oneµs

console.log("Task completed in: " + onlytwo(totalTime))
// Task completed in: 1 d 12 h

The function signature is the same as formatmicro, so you could provide a custom reduce or custom increment names if you wanted. For example:

import {onlytwo} from 'formatmicro'

const oneµs = 1
const oneMs = 1000
const oneS  = 1000*1000
const oneM  = 60*1000*1000
const oneH  = 60*60*1000*1000
const oneD  = 24*60*60*1000*1000

const totalTime = 12*oneH + oneS + 9*oneMs + oneµs

const incrementNames = {
    'd' : ['day', 'days'],
    'h' : ['hour', 'hours'],
    'm' : ['minute', 'minutes'],
    's' : ['second', 'seconds'],
    'ms' : ['millisecond', 'milliseconds'],
    'µs' : ['microsecond', 'microseconds'],
}

console.log("Task completed in: " + onlytwo(totalTime, incrementNames))
// Task completed in: 12 hours 1 second

onlytwoBignames

Just like onlytwo, but uses "bignames".

Options

You can optionally pass the names of the increments (e.g hours, minutes, days, etc.) as the second function parameter. The keys define the increment and the values are a two-item array, where the first item is the singular name of the increment (e.g day) and the second item is the plural name (e.g. days). If you don't provide some of the names, the defaults will be used.

For example:

import formatmicro from 'formatmicro'

const oneµs = 1
const oneMs = 1000
const oneS  = 1000*1000
const oneM  = 60*1000*1000
const oneH  = 60*60*1000*1000
const oneD  = 24*60*60*1000*1000

const totalTime = oneD + 12*oneH + 16*oneM + 59*oneS + oneMs + 6*oneµs

const incrementNames = {
    'd' : ['day', 'days'],
    'h' : ['hour', 'hours'],
    'm' : ['minute', 'minutes'],
    's' : ['second', 'seconds'],
    'ms' : ['millisecond', 'milliseconds'],
    'µs' : ['microsecond', 'microseconds'],
}

console.log("Task completed in: " + formatmicro(totalTime, incrementNames))
// Task completed in: 1 day 12 hours 16 minutes 59 seconds 1 millisecond 6 microseconds

Custom Reducer

Instead of passing just the names, you can pass a reducer function to format the final result. It accepts the following parameters:

  • carry (string) This is the return from the last call to the reducer function (starts as a empty string).
  • incrementKey (string) This will be: d, h, m, s, ms, or µs
  • value (int) The value for this increment (e.g. the number of seconds, or the number of minutes).

Here are some examples:

Default Behavior

import microseconds from 'microseconds'
import formatmicro from 'formatmicro'

const start = microseconds.now()

// do some stuf

const end = microseconds.now()
const period = end - start

const incrementNames = {
    'd' : ['d', 'd'],
    'h' : ['h', 'h'],
    'm' : ['m', 'm'],
    's' : ['s', 's'],
    'ms' : ['ms', 'ms'],
    'µs' : ['µs', 'µs'],
}

const formatReduce = (carry, incrementKey, value) => {
    if(value === 0) return carry
    else return carry +
        ((carry === "") ? "" : " ") +
        value.toString() + " " +
        ((value === 1) ? incrementNames[incrementKey][0] : incrementNames[incrementKey][1])
}

console.log("Task completed in: " + formatmicro(period, formatReduce))

Only Output the First Three Values Found

let foundNum = 0
const formatReduce = (carry, incrementKey, value) => {
    if(foundNum >= 3) return carry
    if(value === 0) return carry

    foundNum++

    return carry +
        ((carry !== "") ? " " : "") +
        value.toString() + " " +
        incrementKey
}

console.log("Task completed in: " + formatmicro(period, formatReduce))

License

MIT © Cully Larson