@opdime/range

A JavaScript implementation of Python's range function.

Usage no npm install needed!

<script type="module">
  import opdimeRange from 'https://cdn.skypack.dev/@opdime/range';
</script>

README

Range-JS


Range-JS offers a similar implementation to Python's range function. It offers the ability to generate numeric arrays in a variety of ways. The numeric arrays returned by this function will always only contain integers and it never returns floating point numbers. So let's have a look at some examples:

Importing

Range-JS has a names export range. That results in the following ways of importing the function:

const range = require('@opdime/range').range;
// or
const { range } = require('@opdime/range');
// or
import { range } from '@opdime/range';

The function accepts one to three arguments and therefore it may be called in to following three ways:

range(limit)

By creating a range in that manner, the range will go from 0 to the given limit. In that case, the limit is an exclusive value, so it nicer to use it with arrays.

const r = range(5);
// evaluates to:
// [0, 1, 2, 3, 4]

range(start, limit)

By creating a range in that manner, the range will go from the start to the given limit. In that case, the start is an inclusive value, al well as the limit!

const r = range(3, 5);
// evaluates to:
// [3, 4, 5]

range(start, limit, step)

By creating a range in that manner, the range will go from the start to the given limit. In that case, the start is an inclusive value, al well as the limit. Also, the resulting array will only contain multiples of the step, offset by the value of the start.

const r = range(0, 6, 2);
// evaluates to:
// [0, 2, 4, 6]