🚀 DAY 4 OF LAUNCH WEEK: Introducing GitHub Actions Scanning Support.Learn more →
Socket
Book a DemoInstallSign in
Socket

@blakek/range

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blakek/range

🔢 Generate a range of numbers

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

range

🔢 Generate a range of numbers

Utilities for creating and sizing a range of numbers.

Install

Using Yarn:

$ yarn add @blakek/range

…or using npm:

$ npm i --save @blakek/range

Usage

import { range, rangeG, rangeSize } from '@blakek/range';

// Create a list of numbers
range(1, 3); //» [ 1, 2, 3 ]
range(10, 20, 5); //» [ 10, 15, 20 ]

// Iterate over a range
for (const i of rangeG(1, 10, 4)) {
  console.log(i);
}
//» 1
//» 5
//» 9

// Get how many steps a range would contain
rangeSize(1, 20); //» 20
rangeSize(1, 20, 5); //» 4

API

range

function range(from: number, to: number, step?: number = 1): number[];

Creates an array of numbers from a number to another, stepping in increments of step.

range(1, 5); //» [ 1, 2, 3, 4, 5 ]

range(0, 20, 5); //» [ 0, 5, 10, 15, 20 ]

range(10, 0, -2); //» [ 10, 8, 6, 4, 2, 0 ]

range(5, 0, 1);
//» RangeError: would create infinte range due to stepping in the wrong direction

rangeG

function rangeG(
  from: number,
  to: number,
  step?: number = 1
): Generator<number, void, number>;

Generates numbers from a number to another, stepping in increments of step.

[...rangeG(1, 3)]; //» [ 1, 2, 3 ]

[...rangeG(5, 1, -1)]; //» [ 5, 4, 3, 2, 1 ]

[...rangeG(5, 1, 1)]; //» RangeError: would create infinte range due to stepping in the wrong direction

for (const i of rangeG(0, -Infinity, -2)) {
  if (i < -20) break;
  console.log(i);
}
//» 0, -2, -4, -6, -8, -10, -12, -14, -16, -18, -20

rangeSize

function rangeSize(from: number, to: number, step?: number = 1): number;

Returns the number of steps necessary to go from one number to another, stepping in increments of step.

rangeSize(1, 100); //» 100

rangeSize(2, 1024, 2); //» 512

rangeSize(100, 1, -5); //» 20

rangeSize(1, 10, -1); //» Infinity

Contributing

Node.js and Yarn are required to work with this project.

To install all dependencies, run:

yarn

Useful Commands

yarn buildBuilds the project to ./dist
yarn formatFormat the source following the Prettier styles
yarn testRun project tests
yarn test --watchRun project tests, watching for file changes

License

MIT

Keywords

range

FAQs

Package last updated on 15 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts