Skip to content

Commit 9dd4217

Browse files
committed
First commit, yo!
0 parents  commit 9dd4217

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bower_components
2+
node_modules
3+
*.log
4+
.DS_Store
5+
bundle.js

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bower_components
2+
node_modules
3+
*.log
4+
.DS_Store
5+
bundle.js
6+
test
7+
test.js
8+
demo/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2014 Matt DesLauriers
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
20+
OR OTHER DEALINGS IN THE SOFTWARE.
21+

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# array-range
2+
3+
[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)
4+
5+
Tiny module to create a new dense array with the specified range.
6+
7+
```js
8+
var range = require('array-range')
9+
range(3) // -> [ 0, 1, 2 ]
10+
range(1, 4) // -> [ 1, 2, 3 ]
11+
```
12+
13+
Mainly useful for functional programming. ES6 examples:
14+
15+
```js
16+
var array = require('array-range')
17+
18+
array(5).map( x => x*x )
19+
// -> [ 0, 1, 4, 9, 16 ]
20+
21+
array(2, 10).filter( x => x%2===0 )
22+
// -> [ 2, 4, 6, 8 ]
23+
```
24+
25+
It can also be useful for creating a fixed size dense array. Cleaner than `apply` and does not create an intermediate array:
26+
27+
```js
28+
Array.apply(null, new Array(5))
29+
30+
vs.
31+
32+
array(5)
33+
```
34+
35+
## Usage
36+
37+
[![NPM](https://nodei.co/npm/array-range.png)](https://nodei.co/npm/array-range/)
38+
39+
#### `array(start, end)`
40+
41+
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)`
42+
43+
#### `array(len)`
44+
45+
Creates a new dense array with `len` number of elements, from zero to `len-1`.
46+
47+
## License
48+
49+
MIT, see [LICENSE.md](http://github.com/mattdesl/array-range/blob/master/LICENSE.md) for details.

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
module.exports = function newArray(start, end) {
3+
var n0 = typeof start === 'number',
4+
n1 = typeof end === 'number'
5+
6+
if (n0 && !n1) {
7+
end = start
8+
start = 0
9+
} else if (!n0 && !n1) {
10+
start = 0
11+
end = 0
12+
}
13+
14+
start = start|0
15+
end = end|0
16+
var len = end-start
17+
if (len<0)
18+
throw new Error('array length must be positive')
19+
20+
var a = new Array(len)
21+
for (var i=0, c=start; i<len; i++, c++)
22+
a[i] = c
23+
return a
24+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "array-range",
3+
"version": "1.0.0",
4+
"description": "creates a new array with given range",
5+
"main": "index.js",
6+
"license": "MIT",
7+
"author": {
8+
"name": "Matt DesLauriers",
9+
"email": "[email protected]",
10+
"url": "https://github.com/mattdesl"
11+
},
12+
"dependencies": {},
13+
"devDependencies": {
14+
"tape": "^3.0.2"
15+
},
16+
"scripts": {
17+
"test": "node test.js"
18+
},
19+
"keywords": [
20+
"filled",
21+
"array",
22+
"fill",
23+
"list",
24+
"dense",
25+
"range",
26+
"functional"
27+
],
28+
"repository": {
29+
"type": "git",
30+
"url": "git://github.com/mattdesl/array-range.git"
31+
},
32+
"homepage": "https://github.com/mattdesl/array-range",
33+
"bugs": {
34+
"url": "https://github.com/mattdesl/array-range/issues"
35+
}
36+
}

test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var range = require('./')
2+
var test = require('tape').test
3+
4+
test("creates a new dense array for functional use", function(t) {
5+
var expected = [0,1,2]
6+
t.deepEqual(range(3), expected, 'creates range from zero')
7+
8+
expected = [1,2,3]
9+
t.deepEqual(range(1, 4), expected, 'creates range from zero')
10+
11+
t.deepEqual(range(), [], 'empty array')
12+
t.deepEqual(range('foo'), [], 'zero array')
13+
14+
function index(x, i, self) {
15+
return i+1
16+
}
17+
18+
expected = [1,2,3,4]
19+
t.deepEqual(range(4).map(index), expected, 'works with map function')
20+
t.deepEqual(range(5).map( function(x) { return x*x }), [ 0, 1, 4, 9, 16 ])
21+
22+
t.throws(range.bind(null, 5, 2), 'throws error')
23+
t.deepEqual(range(-2,1), [-2,-1,0], 'accepts negatives')
24+
t.end()
25+
})
26+
27+

0 commit comments

Comments
 (0)