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

force-bounds

v0.0.3

Published

Force a numerical value to be within bounds, or a safe default.

Downloads

3

Readme

force-bounds

A simple package for limiting numerical values to within a defined range and defaulting to a known value when undefined.

Useage

Force a value to be within set bounds, or a default if undefined

const bounds = require('force-bounds');

const safe = bounds.force(value, min, max, default)

// returns value if it within [min, max]. If value is outside of that range, the closest bound is returned (like saturation).
// If value was undefined, or any other 'falsy' value (including NaN, but excluding 0), then the default value is returned.
// If value is defined, but value | typeof value === 'number' is false, then an error is thrown to prevent overwriting non-numerical defined values.
// If no value is provided for default, max is assumed to be the default, if no value is provided for max, max === min is assumed.
// If min > max, they are internally swapped so that min < max.
// If min, max, or default are defined and are non numerical values or x | x === Number.NaN is true for any of them, then the function will throw an error.
// If default is outside of the range [max, min], an error is thrown.
// If none of min, max, default are provided, an error is thrown, since that use case is meaningless and likely a mistake.

Check if a value is within bounds

const bounds = require('force-bounds');

const safe = bounds.check(value, min, max)

// performs the same checks as bounds.force, but returns true if value would be returned, and throws an error or returns false otherwise.

Examples

Forcing values

const bounds = require('force-bounds');

const speed = 10; // untrusted input

let safeSpeed = bounds.force(speed, 0, 60, 0);
// safeSpeed === 10

safeSpeed = bounds.force(120, 0, 60, 55);
// safeSpeed === 60

safeSpeed = bounds.force(undefined, 0, 60, 55);
// safeSpeed === 55

safeSpeed = bounds.force(false, 0, 60, 55);
// safeSpeed === 55

safeSpeed = bounds.force(Number.NaN, 0, 60, 55);
// safeSpeed === 55

safeSpeed = bounds.force('', 0, 60, 55);
// safeSpeed === 55

safeSpeed = bounds.force('120', 0, 60, 55);
// Error thrown, does not clobber existing data

Checking values

const bounds = require('force-bounds');

const speed = 120; // untrusted input

const isSafeSpeed = bounds.check(speed, 0, 60, 0);
// isSafeSpeed === false