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

python-range

v1.0.4

Published

A JavaScript implementation of the Python's range() function.

Downloads

592

Readme

range

NPM Version Build Status dependency Status devDependency Status Coverage Status

A JavaScript implementation of the Python's range() function.

Installation

npm i python-range

Examples

Try it out in your browser.

Basic usage

import range from 'python-range';

const r = range(10);
console.log(r); // range(0, 10, 1)
console.log(Array.from(r)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(r[3]); // 3
console.log(r.length); // 10

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

Iteration

const r = range(3, 6);
for (const n of r) {
  console.log(n); // logs 3, 4 and 5
}
r.forEach(n => console.log(n)); // logs 3, 4, and 5

Lazy evaluation

Unlike other range modules, python-range has lazy evaluation.

See Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

console.log(range(1000000000000001).includes(1000000000000000)); // true
console.log(range(0, 100000000000001, 10).includes(100000000000000)); // true
console.log(range(0, -1000000000, -3)[12345678]); // -37037034

Documentation

Exported values

python-range exports two values: a PythonRange class, and a range function (a default export), which returns a PythonRange object.

import range, {PythonRange} from 'python-range';
console.log(range(10) instanceof PythonRange); // true

new PythonRange(<int> start, <int> stop[, <int> step])

The PythonRange constructor creates a range starting with start (inclusive) and ending with stop (exclusive). The step parameter defaults to 1 and specifies the distance between two elements. The step parameter must be different than 0.

new PythonRange(<int> stop)

When called with only one argument, the start parameter defaults to 0 and the passed argument becomes stop.

const r = range(3);
console.log(r.start); // 0
console.log(r.stop); // 3

Numeric properties

You can access range elements using array indices. Note that this requires native Proxy support.

const r = range(2, 5);
console.log(r[0]); // 2
console.log(r[2]); // 4

start, stop and step properties

The PythonRange constructor creates these properties based on the arguments passed to the constructor, or the default values. These properties are writable, and changing them automatically updates the range.

const r = range(5);
console.log(Array.from(r)); // [0, 1, 2, 3, 4]
r.step = 2;
console.log(Array.from(r)); // [0, 2, 4]

length property

The length property specifies the number of elements in the range. It's updated automatically when the start, stop and step properties are changed.

const r = range(5);
console.log(r.length); // 5
r.stop = 3;
console.log(r.length); // 3

PythonRange.prototype.get(<int> index)

Works the same as accessing numeric properties (e.g. r[2]). Use this method if you want to execute your code in environments without native Proxy support.

console.log(range(2, 5).get(1)); // 3

PythonRange.prototype.forEach(<callable> callback[, <object> thisArg])

Executes the callback once for each element with element, current index and the range as arguments. Works the same as Array.prototype.forEach().

PythonRange.prototype.includes(<int> value)

Returns true if the range contains the specified value; otherwise returns false.

console.log(range(3, 5).includes(3)); // true
console.log(range(10).includes(10)); // false

PythonRange.prototype.min()

Returns the smallest value in the range.

console.log(range(10).min()); // 0
console.log(range(0, -15, -1).min()); // -14

PythonRange.prototype.max()

Returns the largest value in the range.

console.log(range(10).max()); // 9
console.log(range(12, 0, -2).max()); // 12

PythonRange.prototype.reverse()

Reverses the range in-place and returns it.

console.log(range(2, 5).reverse()); // range(4, 1, -1)
console.log(range(10, 0, -1).reverse()); // range(1, 11, 1)

PythonRange.prototype.toString()

Returns a string range(<start>, <stop>, <step>), where <start>, <stop> and <step> are the start, stop and step properties of the range.

console.log(String(range(3, 6, 2))); // range(3, 6, 2)

PythonRange.prototype.valueOf()

Returns the result of the toString() method.

PythonRange.prototype[@@iterator]()

Returns the result of calling Array.prototype.values() on the range.

PythonRange.areEqual(<PythonRange> a, <PythonRange> b)

Returns true if the passed arguments are equal; otherwise returns false. Two ranges are considered equals if they contain the same set of values. For example, range(3, 3) and range(0) are equal, because they're both empty ranges. range(4, 5, 2) and range(4, 6, 3) are equal too, because they both contain only one element: 4.

const r = range(3);
console.log(PythonRange.areEqual(r, r)); // true
console.log(PythonRange.areEqual(range(10), range(10))); // true
console.log(PythonRange.areEqual(range(3, 3), range(0))); // true, because both ranges are empty
console.log(PythonRange.areEqual(range(4, 5, 2), range(4, 6, 3))); // true, because both ranges contain only one element: 4
console.log(PythonRange.areEqual(range(2), range(3))); // false

range(...args)

Returns an instance of PythonRange with the specified arguments.

Supported environments

In order to be able to access numeric properties (e.g. r[2]), native Proxy support is required (see compatibility table). If you want to execute your code in environments without Proxy support, you can use the get() method instead.