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

ranged-overflow

v1.1.3

Published

Provides ranged overflow for numbers, for example (minimum 1 and maximum 3 with value 5 would be 2)

Downloads

10

Readme

Ranged Overflow

A overflow function that overflows a value inside a range.

But wait, if you install now you'll get a complementary clamp and a within, I'll even throw in a outside too.

WHAT is there even more? Yes, the library comes with a valid and an assert functions for validation of parameters.

If you haven't installed this library yet, then you should check out the ASCII art and benchmarks below. Don't wait, install now.

Install

It's that easy.

npm install ranged-overflow

# For your project

npm install --save ranged-overflow

# For development purposes

npm install --save-dev ranged-overflow

Don't have NodeJS yet? No problem, and then n.

Use

Import it ...

import {
    overflow,
    clamp,
    within,
    outside,
    valid,
    assert,
} from 'ranged-overflow'

...

Or require it, the old fashion way ...

var ranged = require('ranged-overflow')

var overflow = ranged.overflow
var clamp = ranged.clamp
var within = ranged.within
var outside = ranged.outside
var valid = ranged.valid
var assert = ranged.assert

...

Functions

The library consist of functions that calculate a value using a range (min + max), check boundaries, and throw exceptions.

All function take three parameters, a minimum, a maximum and a value. Like so function(min, max, value).

The overflow

It takes value that can overflow the range (min + max).

Example:

overlow(1, 3, 5) === 2

This way any value you give overflow will count inside the range like so.

Range min 1, max 3:

value             range
        0  - |1  -  2  -  3|  -  4

 0           |            3|
 1           |1            |
 2           |      2      |
 3           |            3|
 4           |1            |
 5           |      2      |
... and on and on and on ...
 120961      |0            |

For a real world example you could name an array that wraps around the end, a carousel on web page or even a two dimensional world that wraps around the edges.

The clamp

It takes value and clamps it to the edges.

Example:

clamp(1, 3, 5) === 3

This way any value you give clamp will stick to the edges.

Range min 1, max 3:

value             range
        0  - |1  -  2  -  3|  -  4

 0           |1            |
 1           |1            |
 2           |      2      |
 3           |            3|
 4           |            3|
 5           |            3|
... and on and on and on ...
 120961      |            3|

The within

Returns true if a value is within the range, else false.

Example:

within(1, 3, 5) === false

The outside

Returns true if the value is outside the range, otherwise false

Example:

outside(1, 3, 5) === true

The valid

Checks all parameters, min, max and value if they are a finite number, if any of them is not then it throws a TypeError exception.

Also it checks if min is less or equal to max (min <= max), and if not it throws a RangeError exception.

This function always returns a true.

Example:

valid(1, 3, 5) === true

valid(1, '3', 5) // throws TypeError
valid(3, 1, 5) // throws RangeError

The assert

Assert checks if value is within the range, and if not it will throw an RangeError exception.

This function always returns a true.

Example:

assert(1, 3, 3) === true

assert(1, 3, 5) // throws RangeError

Unbiased benchmark results

Functions
  overflow x 223,834,804 ops/sec ±0.42% (95 runs sampled)
  clamp x 222,307,625 ops/sec ±0.32% (91 runs sampled)
  within x 223,844,303 ops/sec ±0.42% (93 runs sampled)
  outside x 222,657,700 ops/sec ±0.37% (95 runs sampled)
  assert x 221,045,434 ops/sec ±0.35% (95 runs sampled)
  valid x 220,605,742 ops/sec ±0.32% (93 runs sampled)
Fastest was overflow,within,outside,clamp

As you can clearly see from the results, that there is not much difference in speed of all these function compared to each other.

Validation + functions
  valid && overflow x 221,914,595 ops/sec ±0.42% (91 runs sampled)
  valid && clamp x 221,793,067 ops/sec ±0.33% (95 runs sampled)
  valid && assert x 200,725,337 ops/sec ±6.44% (86 runs sampled)
Fastest was valid && clamp,valid && assert

And there is no real drop in speed if you validate the input with valid or not.

Validating your input

There is no default way to validate input, choice to do so is yours to make.

Just remember to try .. catch or .catch() even.

// you could do this ..

valid(min, max, value) && overflow(min, max, value)

// or you could do that ..

if (valid(min, max, value)) {
    overflow(min, max, value)
}

// or you could do this ..

let spill = (min, max, value) =>
    valid(min, max, value) && overflow(min, max, value)

spill(min, max, value)

Or any other way is just fine.

Fastest validation method
  valid && overflow x 220,400,317 ops/sec ±0.23% (95 runs sampled)
  if valid then overflow x 220,930,642 ops/sec ±0.27% (94 runs sampled)
  spill x 202,382,744 ops/sec ±0.08% (99 runs sampled)
Fastest was if valid then overflow,valid && overflow

They are all very similar in speed, but spill seems to have a small penalty for some reason.

License

MIT