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

@russgooday/range

v1.1.0

Published

Basic clone of Python's Range function

Downloads

208

Readme

Range

A Javascript implementation of Python's range function as an ESM import.

Installation

Console:

npm install @russgooday/range

ESM Importing

import { Range } from '@russgooday/range';

Usage

Range is a class that can be used to generate a range of numbers returning an iterable instance. It can be used in the following ways:

Examples

Creating a Range with a Single Argument

const range = new Range(10);
console.log(...range);
// Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Creating a Range with Two Arguments

const range = new Range(5, 15);
console.log(...range);
// Output: 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

Creating a Range with Three Arguments

const range = new Range(10, 20, 2);
console.log(...range);
// Output: 10, 12, 14, 16, 18

Using Negative Step

const range = new Range(20, 10, -2);
console.log(...range);
// Output: 20, 18, 16, 14, 12

Accessing a Specific Index

const range = new Range(0, 10); // 0,1,2,3,4,5,6,7,8,9

console.log(range.index(3)); // Output: 3
console.log(range.index(-1)); // Output: 9

Slicing a Range (Returns a new Range)

const range = new Range(0, 20, 2); // 0,2,4,6,8,10,12,14,16,18
const slicedRange = range.slice(2, 5);
console.log(slicedRange);
// Range { start: 4, stop: 10, step: 2 }
console.log('(0, 20, 2)[2:5]', ...slicedRange);
// Output: 4, 6, 8

Reverse with Slice (nulls used for placeholders)

const range = new Range(0, 10, 2); // 0,2,4,6,8
const reversedRange = range.slice(null,null,-1); // equivalent to range[::-1]
console.log(...reversedRange);
// Output: 8 6 4 2 0

Error Handling

try {
    // invalidRange - No arguments provided
    new Range();
} catch (error) {
    console.log(error.message); // Output: Range expected at least 1 argument, got 0
}

try {
    // invalidStepRange - Step value is zero
    new Range(0, 10, 0);
} catch (error) {
    console.log(error.message); // Output: step must not be zero
}

try {
    const rangeWithInvalidIndex = new Range(0, 5);
    console.log(rangeWithInvalidIndex.index(10));
} catch (error) {
    console.log(error.message); // Output: index 10 out of range
}

Running the tests

Console:

Needs to have vite and vitest installed

npm install vite
npm install -D vitest

Run the following command to run the tests:

npm run test