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

rangeit

v1.0.2

Published

Models a range of numbers between a start and stop with a variable step interval.

Downloads

5

Readme

rangeit

This module is an ES6 class for creating number ranges with step intervals. Basic usage is inspired by Python's range function, but with extra methods for validating values, accessing steps out of order and by index.

Installation

Rangeit can be installed using npm:

npm install rangeit

Testing

Unit tests are provided using Jest. They can be run using npm:

npm test

Usage

Example code is provided in the file examples/examples.js. They can be run from the project directory using npm:

npm run example

JSDOC

Constructor

new Range(start, stop, step)

Creates a new number range object. If one number is passed, the range will be 0 to the given number. If two numbers are passed, the range will run from start to stop (inclusive). If three numbers are passed, the range will run from start to stop with interval step.

Parameters: | Param | Type | Description | | --- | --- | --- | | start | Number | (optional) First value in range | | stop | Number | Last value in range | | step | Number | (optional) Step interval |

Example

import { Range } from "rangeit";

const rng1 = new Range(10);      // (0,1,2,3,4,5,6,7,8,9,10)
const rng2 = new Range(1,10);    // (1,2,3,4,5,6,7,8,9,10)
const rng3 = new Range(1,10,2);  // (1,3,5,7,9)
const rng4 = new Range(10,1);    // (10,9,8,7,6,5,4,3,2,1)
const rng5 = new Range(10,1,-2); // (10,8,6,4,2)

Range objects can also be created with the range function:

import { range } from "rangeit";

const rng1 = range(10);      // (0,1,2,3,4,5,6,7,8,9,10)
const rng2 = range(1,10);    // (1,2,3,4,5,6,7,8,9,10)
const rng3 = range(1,10,2);  // (1,3,5,7,9)
const rng4 = range(10,1);    // (10,9,8,7,6,5,4,3,2,1)
const rng5 = range(10,1,-2); // (10,8,6,4,2)

Iterator

Range values can be iterated over in for of loops or using the spread operator, for instance:

import { range } from "rangeit";

for (const value of range(5)) {
    console.log(value);
}

console.log([...range(5)]);

Size

The size property gets the number of valid steps in the range.

import { range } from "rangeit";

const rng = range(5); // (0,1,2,3,4,5)
console.log(rng.size); // 6

Step

step(index)

The step method returns an arbitary step value from the range based on it's index.

Parameters: | Param | Type | Description | | --- | --- | --- | | index | Number | The index of the step |

import { range } from "rangeit";

const rng = range(1,10,2); // (1,3,5,7,9)
console.log(rng.step(2));  // 5

indexOf

indexOf(value)

Returns the index of a given range step value, or -1 if the value is not in the range.

Parameters: | Param | Type | Description | | --- | --- | --- | | value | Number | Step value to find |

import { range } from "rangeit";

const rng = range(1,10,2);   // (1,3,5,7,9)
console.log(rng.indexOf(5)); // 2 

inRange

inRange(value)

Returns whether the given value is a valid step within the current range

Parameters: | Param | Type | Description | | --- | --- | --- | | value | Number | Value to test |

import { range } from "rangeit";

const rng = range(1,10,2); // (1,3,5,7,9)
console.log(rng.inRange(-1)); // false
console.log(rng.inRange(10)); // false
console.log(rng.inRange(6));  // false
console.log(rng.inRange(5));  // true

wrap

wrap(value)

Wraps the given value within the range. If the value is above the maximum, it's wrapped back from the minimum and vice versa. If the value is within the range, but is not a valid step, the closest valid step is returned.

Parameters: | Param | Type | Description | | --- | --- | --- | | value | Number | Value to wrap |

import { range } from "rangeit";

const rng = range(1,10); // (1,2,3,4,5,6,7,8,9,10)
console.log(rng.wrap(-2));  // 7
console.log(rng.wrap(12));  // 3
console.log(rng.wrap(1.5)); // 2

clamp

clamp(value)

Clamps the given value within the range. If the value is above the maximum, maximum is returned. If it is below the minimum, minimum is returned. If the value is within the range, but is not a valid step, the closest valid step is returned.

Parameters: | Param | Type | Description | | --- | --- | --- | | value | Number | Value to clamp |

import { range } from "rangeit";

const rng = range(1,10); // (1,2,3,4,5,6,7,8,9,10)
console.log(rng.clamp(-2));  // 1
console.log(rng.clamp(12));  // 10
console.log(rng.clamp(1.5)); // 2