Skip to content

hiNISAL/jrange

Repository files navigation

license Coveralls NPM downloads

The range function of Python implemented using JavaScript.

range(start, end, step)

Install

npm i jrange -S

Usage

  • in browser
<script src="/dist/jrange.umd.js"></script>
<script>
  const list = jrange(10);
</script>
  • esm
import jrange from 'jrange';

const list = jrange(10, 1, -1);
  • CommonJS
const range = require('jrange'); // rename to range

const list = range(1, 100, 2);

arguments

/ default required
start 0 no
end / yes
step 1 no

range(10) -> range(0, 10, 1)

range(1, 10) -> range(1, 10, 1)

different with python

  • In Python, the range function returns a Range Class, while jrange returns an Array.

python

rng = range(0, 10)

print(rng, type(rng))) # range(0, 10) <class 'range'>

# Convert to list
list = list(rng)

javascript

const list = jrange(0, 10)

console.log(Array.isArray(list)) // true
  • In Python, if the step is zero, the range function will throw an exception, while jrange will return an empty array.

python

range(1, 10, 0)

# ValueError: range() arg 3 must not be zero

javascript

jrange(1, 10, 0); // return []

About

Python's range function for javascript

Resources

Stars

Watchers

Forks

Packages

No packages published