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

unit-compare

v1.0.1

Published

Compare byte and datetime units

Downloads

272,827

Readme

unit-compare

Build Status Coverage Status

Compare date/time and byte units

Installation

npm install --save unit-compare

Features

  • Compares bytes sizes
  • Compares ISO datetimes

Usage

The example below asserts if a number if less then 10 megabytes:

const compare = require('unit-compare');

const n = 1024;
const isLessThen10Mb = compare.isNumber(n).assert('<10mb')
if (isLessThen10Mb) {
  console.log(`${n} is less than 10mb`);
}

Byte unit comparisons

Assert if a given number against a string byte size expression:

const greaterThan10kb = compare.isNumber(n).assert('>10kb')

Using comparison methods directly:

const greaterThan10kb = compare.isNumber(n).lessThan(10, 'k');

Compare dates with a string expression

Assert if a given number against a string datetime expression:

const date = moment();
const equalTo10Mins = compare.isDate(date).assert('>10 minutes');

Using comparison methods directly:

const date = moment().subtract(10, 'minutes');
const greaterThan10kb = compare.isDate(date).lessThan(10, 'days');

API

compare.isDate() -> DateAssertion

Parameters
  • ISO string date
Returns

Returns an DateAssertion instance.

compare.isNumber() -> ByteAssertion

Parameters
  • Accepts an integer
Returns

Returns a ByteAssertion instance.

ByteAssertion

.assert(sizeExpression) -> boolean

Parameters
  • sizeExpression - accepts a positive integer representing the file size. File size units are:

    • bytes, specified using b.
    • kilobytes, specified using k or kb,
    • megabytes, specified using m or mb
    • terabytes, specified using t or tb
    • gigabytes, specified using g or gb

    If no unit is specified, bytes is used by default.

    Optionally, expressions can be prefixed with a comparison operator, including:

    • less than using <
    • greater than using >
    • equality using == or =
    • less than or equal to using <=
    • greater than or equal to >=

    Examples:

    • equal to 10 bytes: 10
    • equal to 10 bytes: ==10b
    • less than 10 bytes: <10
    • greater than 50 megabytes: >10mg

.lessThan(int, unit) -> boolean

Parameters
  • integer
  • unit - k, m, g, t

.greaterThan(int, unit) -> boolean

Parameters
  • integer
  • unit - k, m, g, t

.equalTo(int, unit) -> boolean

Parameters
  • integer
  • unit - k, m, g, t

.greaterThanOrEqual(int, unit) -> boolean

Parameters
  • integer
  • unit - k, m, g, t

.lessThanOrEqual(int, unit) -> boolean

Parameters
  • integer
  • unit - k, m, g, t

DateAssertion

Parameters
  • dateExpression - accepts a time unit. Time units are:

    • minutes, specified using minutes, m, mins, min.
    • hours, specified using hours, h, hour.
    • days, specified using days, d, day.

    If no unit is specified, days is used by default.

    Optionally, expressions can be prefixed with a comparison operator, including:

    • less than using <
    • greater than using >
    • equality using == or =

If no comparison operator is specified, equality is used by default.

Examples:

  • equal to 10 days: 10
  • equal to 10 minutes: == minutes
  • less than 10 hours: < 10 hours
  • greater than 50 minutes: >50minutes
  • less than 50 minutes: <50m
Returns
  • Returns boolean

.lessThan(int, timeUnit) -> boolean

Parameters
  • integer
  • timeUnit - minutes, hours, days

.greaterThan(int, timeUnit) -> boolean

Parameters
  • integer
  • timeUnit - minutes, hours, days

.equalTo(int, timeUnit) -> boolean

Parameters
  • integer
  • timeUnit - minutes, hours, days

.greaterThanOrEqual(int, timeUnit) -> boolean

Parameters
  • integer
  • timeUnit - minutes, hours, days

.lessThanOrEqual(int, timeUnit) -> boolean

Parameters
  • integer
  • timeUnit - minutes, hours, days

Test

npm test

To generate a test coverage report:

npm run coverage

Contributing

  • If you're unsure if a feature would make a good addition, you can always create an issue first.
  • We aim for 100% test coverage. Please write tests for any new functionality or changes.
  • Any API changes should be fully documented.
  • Make sure your code meets our linting standards. Run npm run lint to check your code.
  • Maintain the existing coding style. There are some settings in .jsbeautifyrc to help.
  • Be mindful of others when making suggestions and/or code reviewing.