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

@antoniovdlc/range

v1.4.2

Published

Implement ranges in JavaScript

Downloads

26

Readme

range

version issues downloads license

Implement ranges in JavaScript.

Installation

This package is distributed via npm:

npm install @antoniovdlc/range

Motivation

Ranges are natively supported by a few (popular) programming languages. They allow for iteration over a defined space, while not having a linear increase in their memory footprint (all ranges always store a similar amount of data).

Usage

You can use this library either as an ES module or a CommonJS package:

import range from "@antoniovdlc/range";

- or -

const range = require("@antoniovdlc/range");

To create a range:

const start = 0;
const stop = 10;
const step = 2; // Defaults to `1` if not passed
const inclusive = true; // Defaults to `false` if not passed

const r = range(start, stop, step, inclusive);

You can also pass an options object for convenience:

const start = 0;
const stop = 10;
const opts = {
  step: 2, // Defaults to `1` if not passed
  inclusive: true, // Defaults to `false` if not passed
};

const r = range(start, stop, opts);

Getters

You can use the following getters on ranges:

.start

Returns the start value of a range:

range(0, 10).start; // 0

.stop

Returns the stop value of a range:

range(0, 10).stop; // 10

.step

Returns the step value of a range:

range(0, 10).step; // 1 (by default)

.isInclusive

Returns true if the range is inclusive of its last value:

range(0, 10).isInclusive; // false (by default)

.first

Returns the first value of a range:

range(0, 10).first; // 0

.last

Returns the last value of a range:

range(0, 10).last; // 9
range(0, 10, 1, true).last; // 10

Methods

You can use the following methods on ranges:

.toString()

Returns a string representation of a range:

range(0, 10).toString(); // 0..10
range(0, 10, 2).toString(); // 0..10{2}
range(0, 10, 2, true).toString(); // 0..=10{2}

.toArray()

Returns an array representation of a range:

range(0, 10).toArray(); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(0, 10, 2).toArray(); // [0, 2, 4, 6, 8]
range(0, 10, 1, true).toArray(); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

You can also use the follow syntax as ranges are iterable: [...range(0, 10)]

.equal(range)

Checks if 2 ranges are equal (same start, stop, and step):

range(0, 10).equal(range(0, 10)); // true
range(0, 10, 2).equal(range(0, 10)); // false
range(0, 10, 2, true).equal(range(0, 10, 2, false)); // false

.includes(range)

Checks if one range includes another (irrespective of step values):

range(0, 10).includes(range(1, 5)); // true
range(0, 10).includes(range(1, 11)); // false

.add(range)

Adds ranges together (only if step values are equal):

range(0, 5).add(range(1, 10)); // range(0, 10)

.forEach(fn)

Iterate over a range and apply fn function to every element of the range:

range(0, 10).forEach((i) => console.log(i));
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

range(0, 10, 1, true).forEach((i) => console.log(i));
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

range(0, 10, 2).forEach((i) => console.log(i));
// 0, 2, 4, 6, 8

.forEachAsync(fn)

Iterate over a range and apply fn function to every element of the range asynchronously:

await range(0, 10).forEachAsync((i) => console.log(i));
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

await range(0, 10, 1, true).forEachAsync((i) => console.log(i));
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

await range(0, 10, 2).forEachAsync((i) => console.log(i));
// 0, 2, 4, 6, 8

.map(fn)

Map over a range and apply fn function to every element of the range:

const result = range(0, 5).map((i) => i ** 2);
// [0, 1, 4, 9, 16]

const result = range(0, 5, 1, true).map((i) => i ** 2);
// [0, 1, 4, 9, 16, 25]

const result = range(0, 10, 2).map((i) => i ** 2);
// [0, 4, 16, 36, 64]

.mapAsync(fn)

Map over a range and apply fn function to every element of the range asynchronously:

const result = await range(0, 5).mapAsync(
  (i) => new Promise((resolve) => resolve(i ** 2))
);
// [0, 1, 4, 9, 16]

const result = await range(0, 5, 1, true).mapAsync(
  (i) => new Promise((resolve) => resolve(i ** 2))
);
// [0, 1, 4, 9, 16, 25]

const result = await range(0, 10, 2).mapAsync(
  (i) => new Promise((resolve) => resolve(i ** 2))
);
//  [0, 4, 16, 36, 64]

Iteration protocols

Ranges implement both the iterable and the iterator protocols. This allows iteration over ranges as follow:

for (let i of range(0, 10)) {
  console.log(i);
}
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

for (let i of range(0, 10, 1, true)) {
  console.log(i);
}
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

for (let i of range(0, 10, 2)) {
  console.log(i);
}
// 0, 2, 4, 6, 8
const r = range(0, 10);
console.log(r.next()); // { value: 0, done: false }
console.log(r.next()); // { value: 1, done: false }
console.log(r.next()); // { value: 2, done: false }
...
console.log(r.next()); // { value: 8, done: false }
console.log(r.next()); // { value: 9, done: false }
console.log(r.next()); // { value: undefined, done: true }

Note that ranges are not consumed after a single iteration. They can be reused in consecutive iterations!

License

MIT