py-range-js
v1.0.0
Published
Python-like range for JavaScript/TypeScript
Maintainers
Readme
py-range-js
Python-style range utilities for JavaScript and TypeScript.
This small library provides a Range class and a range() factory function that behave similarly to Python's built-in range() — lazy, iterable, and typed for TypeScript projects.
Package: py-range-js
Version: 1.0.0
Highlights
- Small, dependency-free TypeScript implementation
- Iterables compatible with
for..of,Array.from, and spread ([...]) - Methods:
length,at(),includes(),toString() - Supports positive and negative
stepvalues
Contents
- Installation
- Quick start (JavaScript & TypeScript)
- API reference
- Examples
- Build from source
- Contributing & license
Install
Install from npm:
npm install py-range-js
# or
yarn add py-range-jsIf you are working with the repository locally, run:
npm install
npm run buildQuick Start (TypeScript)
import { range, Range } from 'py-range-js';
for (const n of range(5)) {
console.log(n); // 0,1,2,3,4
}
const r = range(1, 10, 2); // Range from 1 up to (but not including) 10, step 2
console.log(r.length); // number of items
console.log(r.at(0)); // 1
console.log(r.includes(5)); // true
// Negative step
for (const n of range(5, -1, -1)) {
console.log(n); // 5,4,3,2,1,0
}Quick Start (JavaScript)
const { range } = require('py-range-js');
console.log([...range(3)]); // [0,1,2]API Reference
class Range implements Iterable<number>new Range(start: number, stop?: number, step: number = 1)- If
stopis omitted, the constructor treats the first argument asstopandstartdefaults to0(like Python). - Throws an
Errorifstep === 0.
- If
readonly start: number— inclusive start value.readonly stop: number— exclusive stop value.readonly step: number— step increment (may be negative).get length(): number— number of elements in the range.at(index: number): number— return value atindex. Negativeindexcounts from the end. ThrowsRangeErrorif out-of-bounds.includes(value: number): boolean— whethervalueis in the range (fast check using arithmetic).[Symbol.iterator]()— yields values fromstarttostopbystep.toString()— returns a short string likeRange(0, 5, 1).
function range(start: number, stop?: number, step?: number): Range— factory helper returning aRangeinstance.
Behavior notes:
stopis exclusive: values yielded satisfystart <= value < stopfor positive step, orstart >= value > stopfor negative step.lengthreturns the number of yielded values and will be0for empty ranges.
Examples
- Convert to array:
Array.from(range(0, 10, 2)); // [0,2,4,6,8]- Slicing-like access with
at(negative index supported):
const r = range(0, 5);
r.at(-1); // 4- Membership:
range(2, 10, 2).includes(6); // true
range(2, 10, 2).includes(7); // falseBuild & Distribution
This project uses TypeScript and Rollup to produce CommonJS and ES module builds plus type declarations in dist/.
- Build:
npm run build- Output files (from
package.json/rollup.config.js):dist/index.esm.js(ESM)dist/index.cjs.js(CJS)dist/index.d.ts(TypeScript types)
Development notes
- TypeScript settings are in
tsconfig.json(target ES2020,declaration: true). - Rollup configuration is in
rollup.config.jsusing@rollup/plugin-typescript.
Contributing
Contributions are welcome! Please open issues for bug reports or feature requests and submit pull requests for fixes and enhancements.
Guidelines:
- Keep changes focused and well-tested.
- Update or add examples if you introduce new behavior.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author / Maintainer: michael9992155 (see package.json)
