Skip to main content
Home

latest

Python's range function for javascript

This package works with Node.js, Deno, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 years ago (1.2.6)

jrange

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 []

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@aram/jrange

Import symbol

import * as jrange from "@aram/jrange";
or

Import directly with a jsr specifier

import * as jrange from "jsr:@aram/jrange";

Add Package

pnpm i jsr:@aram/jrange
or (using pnpm 10.8 or older)
pnpm dlx jsr add @aram/jrange

Import symbol

import * as jrange from "@aram/jrange";

Add Package

yarn add jsr:@aram/jrange
or (using Yarn 4.8 or older)
yarn dlx jsr add @aram/jrange

Import symbol

import * as jrange from "@aram/jrange";

Add Package

vlt install jsr:@aram/jrange

Import symbol

import * as jrange from "@aram/jrange";

Add Package

npx jsr add @aram/jrange

Import symbol

import * as jrange from "@aram/jrange";

Add Package

bunx jsr add @aram/jrange

Import symbol

import * as jrange from "@aram/jrange";