https://github.com/mattdesl/array-range
creates a new array with given range
https://github.com/mattdesl/array-range
Last synced: 8 months ago
JSON representation
creates a new array with given range
- Host: GitHub
- URL: https://github.com/mattdesl/array-range
- Owner: mattdesl
- License: mit
- Created: 2014-11-09T18:23:49.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2022-01-04T03:52:42.000Z (over 4 years ago)
- Last Synced: 2025-08-09T01:38:58.946Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 121 KB
- Stars: 25
- Watchers: 3
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-micro-npm-packages-zh - array-range - 创建具有给定范围的新数组. (模块 / 数组)
- fucking-awesome-micro-npm-packages - array-range - Creates a new array with given range. (Modules / Array)
- awesome-micro-npm-packages - array-range - Creates a new array with given range. (Modules / Array)
- awesome-micro-npm-packages - array-range - Creates a new array with given range. (Modules / Array)
README
# array-range
[](http://github.com/badges/stability-badges)
Tiny module to create a new dense array with the specified range.
```js
var range = require('array-range')
range(3) // -> [ 0, 1, 2 ]
range(1, 4) // -> [ 1, 2, 3 ]
```
Mainly useful for functional programming. ES6 examples:
```js
var array = require('array-range')
array(5).map( x => x*x )
// -> [ 0, 1, 4, 9, 16 ]
array(2, 10).filter( x => x%2===0 )
// -> [ 2, 4, 6, 8 ]
```
It can also be useful for creating a fixed size dense array. Cleaner than `apply` and does not create an intermediate array:
```js
array(5)
//vs.
Array.apply(null, new Array(5))
```
## Usage
[](https://nodei.co/npm/array-range/)
#### `array(start, end)`
Creates a new dense array with a length of `end-start` elements. `start` is inclusive, `end` is exclusive. Negative values also work, e.g. `range(-10, 10)`
#### `array(len)`
Creates a new dense array with `len` number of elements, from zero to `len-1`.
If `len` is unspecified, it defaults to zero (empty array).
## License
MIT, see [LICENSE.md](http://github.com/mattdesl/array-range/blob/master/LICENSE.md) for details.