Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 3ab89bb

Browse files
cfleschhutKent C. Dodds
authored and
Kent C. Dodds
committed
feat(range): Add range function (#126)
1 parent 9810685 commit 3ab89bb

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import max from './max'
4343
import slugify from './slugify'
4444
import convertToRoman from './convertToRoman'
4545
import gcd from './gcd'
46+
import range from './range'
4647

4748
export {
4849
isOdd,
@@ -90,4 +91,5 @@ export {
9091
slugify,
9192
convertToRoman,
9293
gcd,
94+
range,
9395
}

src/range.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default range
2+
3+
/**
4+
* @module range
5+
* @description This method creates an array of numbers (positive and/or
6+
* negative) progressing from start up to, but not including, end.
7+
*
8+
* @param {Number} start - The start of the range
9+
* @param {Number} end - The end of the range
10+
* @param {Number} step - The value to increment or decrement by
11+
* @return {Array} - Returns an array of integers containing an arithmetic
12+
* progression
13+
* */
14+
15+
function range(start = 0, end, step = 1) {
16+
if (end === undefined) {
17+
end = start
18+
start = 0
19+
}
20+
21+
const length = Math.max(Math.ceil((end - start) / step), 0)
22+
const rangeArr = Array(length)
23+
24+
for (let idx = 0; idx < length; idx++, start += step) {
25+
rangeArr[idx] = start
26+
}
27+
28+
return rangeArr
29+
}

test/flatten.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ test('deep flattens an array of arrays', t => {
2121
const original = [1, 2, [3, 4, [5, 6], 7], 8]
2222
const expected = [1, 2, 3, 4, 5, 6, 7, 8]
2323
const actual = flatten(original)
24-
t.same(actual, expected)
24+
t.deepEqual(actual, expected)
2525
})

test/range.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import test from 'ava'
2+
import { range } from '../src'
3+
4+
test('creates an array of numbers from start up to end', t => {
5+
const expected = [1, 2, 3, 4]
6+
const actual = range(1, 5);
7+
t.deepEqual(actual, expected)
8+
})
9+
10+
test('creates an array of numbers starting from zero given an implicit end', t => {
11+
const expected = [0, 1, 2, 3]
12+
const actual = range(4);
13+
t.deepEqual(actual, expected)
14+
})
15+
16+
test('creates an array of numbers in given steps', t => {
17+
const expected = [0, 5, 10, 15]
18+
const actual = range(0, 20, 5);
19+
t.deepEqual(actual, expected)
20+
})
21+
22+
test('creates an array of negative numbers given a negative step', t => {
23+
const expected = [0, -1, -2, -3]
24+
const actual = range(0, -4, -1);
25+
t.deepEqual(actual, expected)
26+
})
27+
28+
test('creates an empty array with no parameters given', t => {
29+
const expected = []
30+
const actual = range();
31+
t.deepEqual(actual, expected)
32+
})

0 commit comments

Comments
 (0)