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

hmh

v2.0.5

Published

How many hours? Calculate hours easily.

Downloads

114

Readme

hmh - How many hours?

Calculate hours using time spaces

:us: English  |  :brazil: Português do Brasil

Installation

npm install hmh

Note: To install the previous version, when all methods just return a string, use:

npm install [email protected]

API Documentation

hmh.sum(value[, output])

Calculate the hours, summing time spaces.

value

Type: String or Array

Value is a number, representing a time space, that must have a suffix h (hours) or m (minutes). The suffix needs to be stuck in the number.

By time space, I mean:

  • 1 hour and 10 minutes = 1h 10m or 1h10m
  • 2 hours = 2h or 120m
  • Understood? xD

You can use any time spaces you want, and don't worry about spaces, they will be ignored:

const hmh = require('hmh')
console.log(hmh.sum('10m 20m 30m 40m 50m').toString()) // '2h 30m'
console.log(hmh.sum('10h2m2h    5m').toString()) // '12h 7m'

You may use an array too:

console.log(hmh.sum(['10m', '   20m', ' 30m ', '40m', '50m']).toString()) // '2h 30m'

See? Spaces between time spaces are completely ignored =)

output

Type: String Default: hours Options: minutes

The output is always shown in the best format. If it don't have hours, just minutes will be shown. Otherwise, it will be shown in hours.

But you may want to force the output in minutes. That's easy! Just pass it as second parameter:

const sum = hmh.sum('10m 20m 30m 40m', 'minutes')
console.log(sum.toString()) // '100m'

Return

Type: Object

All methods return an object, with the properties h, m and isNegative; and the method toString():

console.log(hmh.sum('1h 10m 20m 30m')) // { toString: [Function], h: 2, m: null, isNegative: false }
.h

Type: Number or null

This property returns the number of hours in result:

console.log(hmh.sum('1h 10m 20m 30m').h) // 2
.m

Type: Number or null

This property returns the number of minutes in result:

console.log(hmh.sum('1h 10m 30m').m) // 40
console.log(hmh.sum('1h 10m 20m 30m').m) // null
console.log(hmh.sum('1h 10m 20m 30m', 'minutes').m) // 120
.isNegative

Type: Boolean

This property returns if the result is negative:

console.log(hmh.sub('1h 2h').isNegative) // true
console.log(hmh.sub('2h 1h').isNegative) // false
.toString()

Type: Function Return: String

This method returns a String representation for the result (it is the same as the first version returned):

console.log(hmh.sum('1h 2h').toString()) // '3h'
console.log(hmh.sum('1h 2h', 'minutes').toString()) // '180m'
console.log(hmh.sum('1h 2h') + '') // '3h'

Look the last console.log. By default, JavaScript uses toString() method when the + operator is used with strings =)

hmh.sub(value[, output])

Calculate the hours, subtracting time spaces.

value and output options are the same than hmh.sum() method. The difference is this method subtracts time spaces:

console.log(hmh.sub('1h 20m').toString()) // '40m'
console.log(hmh.sub('3h 10m 1h').toString()) // '1h 50m'

This method considers that the all time spaces should be subtracted:

console.log(hmh.sub('1h10m 10m').toString()) // 40m

The result 40m is becausea all time spaces are subtracted:

1h - 10m - 10m = 40m

If you want to subtract 10m by 1h10m, firstly you need to convert 1h10m in just one time space. In this case, convert 1h10m to minutes. You can do something like:

const minutes = hmh.sum('1h10m', 'minutes').toString()
console.log(hmh.sub([minutes, '10m']).toString()) // '1h'

Because now, 1h10m it's just 70m.

70m - 10m = 60m = 1h. Easy? =)

hmh.diff(firstValue, secondValue[, output])

Calculate the difference between two time spaces.

Something like: How many hours I have between 10h 15m am and 12h pm? The answer is pretty simple:

console.log(hmh.diff('10h 15m', '12h').toString()) // '1h 45m'

Tada! :tada: :grin:

The output is the same than above methods ;)

hmh.div(value, divisor[, output])

Divide a time space by a number, passed in divisor parameter.

Think: you have 7h available to finish a job, and 4 days to use all this hours. How many hours can you spent per day?

console.log(hmh.div('7h', 4).toString()) // '1h 45m'

:dancer: :dancer:

And about output? The same than above ones!

Related

hmh-cli

License

MIT © Fernando Daciuk