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

@zadvorsky/ranger

v1.0.4

Published

A small, standalone JavaScript library for working with ranges of numbers.

Downloads

2

Readme

Ranger

Ranger.js is a small, standalone library for working with ranges of numbers in Javascript.

Ranger provides all the utility methods you'd expect, including clamp, wrap, map, and random. These methods are available statically, as well as through a Range object that can be instantiated for repeated use.

Ranger's main feature is the ability to provide a 'curve' function to certain methods, which will affect how numbers are distributed.

import ranger from '@zadvorsky/ranger';

// this will return a float between 20 and 40
// when called repeatedly, the distribution of numbers will be approximately linear. 
ranger.random(20, 40);

// just an arbitrary easing function    
function expoEaseOut(x) {
  return 1.0 - Math.pow(2.0, -10.0 * x);
}

// this will also return a float between 20 and 40
// but the distribution will be heavily skewed towards the higher end of the range
ranger.random(20, 40, expoEaseOut);

The curve can be any function that takes a float between 0.0 and 1.0, and returns a transformed float. Easing functions are a good example of this, but there are many others.

Curves can be applied to random, randomInt, map, divide, among others.

Being able to apply easing any value in your application is a powerful way to give yourself more control over the visual output.

Ranger does not ship with any curves itself, but there are plenty of sources for them.

If you are using GSAP, you can access its easing functions directly like this:

ranger.random(20, 40, Expo.easeOut.getRatio);

Usage

For the browser version, include dist/ranger.umd.js in a script tag. A global ranger namespace will then be available.

var value1 = ranger.random(20, 40);

var range = new ranger.Range(20, 40);
var value2 = range.random();

For usage with NPM, run npm install --save @zadvorsky/ranger.

// with ES6 imports
import ranger, { Range } from '@zadvorsky/ranger';

// or CommonJS
const ranger = require('@zadvorsky/ranger');
const { Range } = require('@zadvorsky/ranger');

// creates a range {min: 0, max: 100}    
const range1 = new Range(0, 100);

range1.random(); // returns a float between range.min and range.max
range1.contains(101) // returns false

// Range has chainable transformation methods
// this line creates a range of {min: -10, max: 10} 
const range2 = range.clone().set(-1, 1).scale(10);

// maps a value from range 2 to range 1    
range2.map(-5, range1); // returns 25     

// static method
ranger.map(-5, range2, range1) // same as above

// crates a range with expoEaseOut as the default curve.  
const range3 = new Range(0, 100, expoEaseOut);

Documentation

Documentation can be found here.

Examples can be found here.

Build

To build locally, run

npm install
npm run dev

This project uses Rollup to bundle the library.

Build examples

To build the es6 example application, run

cd examples/es6
npm install
npm run start

The example application uses WebPack 4. It has a bunch of boilerplate code, and uses GSAP for animations.

The actual example code can be found in examples/es6/src/js/examples.

License

MIT.