# range-operator

> One missing feature in ES.Next

Latest version **1.0.0** (published 2018-02-24) · AGPL-3.0+ license · 0 weekly downloads

## Install

```sh
npm install range-operator
pnpm add range-operator
yarn add range-operator
bun add range-operator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-02-24 |
| First published | 2018-02-24 |
| Weekly downloads | 0 |
| License | AGPL-3.0+ |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 36.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jumonji Kiwa |
| Maintainers | jumonji |

## Links

- npm: https://www.npmjs.com/package/range-operator
- Repository: https://github.com/jumonji-k/range-operator
- Homepage: https://github.com/jumonji-k/range-operator#readme
- Issues: https://github.com/jumonji-k/range-operator/issues
- npm.io page: https://npm.io/package/range-operator

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.5

## Recent versions

- 1.0.0 (latest) — 2018-02-24

## README

# range-operator

    import 'range-operator'

    for (let i of 1..to(10)) {
      console.log(i) // prints 1,2,3,4,5,6,7,8,9,10
    }

## Installation

    npm install range-operator

## Motivation

The one key missing feature in JavaScript is a Python-like [`range()`](https://docs.python.org/library/functions.html#func-range) operator that produces an array of numbers in certain range.

As of ES6, there are no obvious way to perform that task. The one way is to use [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from):

    Array.from({length: end - start + 1}, (_, i) => i + start)

But of course this is far more unreadable. In case of `start === 0`, this can be simpler, though:

    [...Array(end + 1).keys()]

[lodash.range](https://lodash.com/docs#range) solves this problem particularly:

    import _ from 'lodash'
    _.range(start, end + 1)

`range-operator` introduces a more native-ish syntax.

### Why the range operator is needed at all

Some people think the old C-style for loop `for (i = 0; i < 10; i++)` is good enough. But consider this example:

    if (start <= end) {
      for (let i = start; i <= end; i++) {
        console.log(i)
      }
    } else {
      for (let i = start; i >= end; i--) {
        console.log(i)
      }
    }

This is a common pattern for looping over a range `[start, end]`, depending which endpoint is larger. With range operator, this is elegantly simplified:

    for (let i of start.to(end)) {
      console.log(i)
    }

Neither Python, Perl, Ruby or Swift's range operator is behaved that way. This is one crucial feature of JavaScript.

---
_Source: https://npm.io/package/range-operator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
