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

@kieranbarker/num-range

v1.0.1

Published

A JavaScript approximation of Python's range type.

Downloads

2

Readme

NumRange

A JavaScript approximation of Python's range type.

  1. Creating a new instance
  2. Instance properties
  3. Instance methods

Creating a new instance

To create a new NumRange instance, simply instantiate the class using the new operator. Pass in an object literal with start, stop, and step properties. The start value defaults to 0 and the step value defaults to 1. The stop value is required.

new NumRange({ stop: 6 }); // 0, 1, 2, 3, 4, 5
new NumRange({ start: 1, stop: 6 }); // 1, 2, 3, 4, 5
new NumRange({ start: 1, stop: 6, step: 2 }); // 1, 3, 5
new NumRange({ start: -1, stop: -6, step: -1 }); // -1, -2, -3, -4, -5

If the bounds of the range are invalid, then the NumRange instance will still be created, but it won't yield anything:

new NumRange({ start: 5, stop: -5, step: 1 }); // yields nothing
new NumRange({ start: -5, stop: 5, step: -1 }); // yields nothing

The newly created instance is iterable, meaning it can be consumed by most syntaxes expecting iterables. This includes spread syntax, the Array.from() method, and for...of loops.

const oneToFive = new NumRange({ start: 1, stop: 6 });

console.log(...oneToFive); // 1 2 3 4 5
console.log([...oneToFive]); // [1, 2, 3, 4, 5]
console.log(Array.from(oneToFive)); // [1, 2, 3, 4, 5]

// 1
// 2
// 3
// 4
// 5
for (const num of oneToFive) {
	console.log(num);
}

Instance properties

.start

The first value to be included in the range. Defaults to 0.

.stop

The first value not to be included in the range.

.step

The number of steps between each value in the range. Defaults to 1.

.length (getter)

The number of values in the range.

Instance methods

[@@iterator]()

Returns a Generator object that yields each value in the range. This method is called implicitly when the current instance is used where an iterable is expected, such as at the beginning of a for...of loop. You can also call the method directly.

Parameters: None.

Returns: {Generator} A Generator object that yields each value in the range.

const oneToFive = new NumRange({ start: 1, stop: 6 });

console.log(...oneToFive); // 1 2 3 4 5
console.log([...oneToFive]); // [1, 2, 3, 4, 5]
console.log(Array.from(oneToFive)); // [1, 2, 3, 4, 5]

// 1
// 2
// 3
// 4
// 5
for (const num of oneToFive) {
	console.log(num);
}

// You can also call the method directly:
const gen = oneToFive[Symbol.iterator]();

console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: 3, done: false }
console.log(gen.next()); // { value: 4, done: false }
console.log(gen.next()); // { value: 5, done: false }
console.log(gen.next()); // { value: undefined, done: true }

.at()

Returns the value at the given index or undefined if it doesn't exist. Accepts a negative index to count backwards from the end of the range.

Parameters: {number} The index whose value should be retrieved.

Returns: {number|undefined} The value at the given index or undefined if it doesn't exist.

const oneToFive = new NumRange({ start: 1, stop: 6 });

console.log(oneToFive.at(0)); // 1
console.log(oneToFive.at(-1)); // 5
console.log(oneToFive.at(5)); // undefined
console.log(oneToFive.at(-6)); // undefined

.indexOf()

Returns the index at which the given value can be found or -1 if it doesn't exist.

Parameters: {number} The value whose index should be retrieved.

Returns: {number} The index at which the given value can be found or -1 if it doesn't exist.

const oneToFive = new NumRange({ start: 1, stop: 6 });

console.log(oneToFive.indexOf(1)); // 0
console.log(oneToFive.indexOf(6)); // -1

.includes()

Returns true if the given value can be found in the range or false if not.

Parameters: {number} The value to search for.

Returns: {boolean} A boolean value which is true if the given value can be found in the range or false if not.

const oneToFive = new NumRange({ start: 1, stop: 6 });

console.log(oneToFive.includes(1)); // true
console.log(oneToFive.includes(7)); // false