Skip to content

Commit

Permalink
feat: add range function (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbutler27 committed Jul 19, 2024
1 parent bd26fc7 commit 02e9cc8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export * from './utils/get';
export * from './utils/escape-html';
export * from './utils/make-template';
export * from './utils/uniq';
export * from './utils/range';
26 changes: 26 additions & 0 deletions src/utils/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Creates an array of numbers (positive and/or negative) progressing from

Check failure on line 2 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
* start up to, but not including, end. A step of -1 is used if a negative start

Check failure on line 3 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
* is specified without an end or step. If end is not specified, it's set to

Check failure on line 4 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
* start with start then set to 0.
*/
export const range = (start: number, end?: number, step?: number): number[] => {
if (end === undefined) {
end = start

Check failure on line 9 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
start = 0

Check failure on line 10 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
}
step = step ?? (start < end ? 1 : -1)

Check failure on line 12 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

if (step === 0) {
const length = Math.max(end - start, 0)

Check failure on line 15 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
return Array(length).fill(start)

Check failure on line 16 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Expected blank line before this statement

Check failure on line 16 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
}

const length = Math.max(Math.ceil((end - start) / step), 0),
result = Array(length)

Check failure on line 20 in src/utils/range.ts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

for (let i = 0; i < length; i++) {
result[i] = start + (i * step)
}
return result
}
41 changes: 41 additions & 0 deletions tests/utils/range.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import {range} from '../../src';


describe('range', () => {
it('works when only start is provided', () => {
expect(range(4)).to.eql([0, 1, 2, 3])
})

it('works when only start and end are provided', () => {
expect(range(1, 5)).to.eql([1, 2, 3, 4])
})

it('works when start, end, and step is provided', () => {
expect(range(0, 20, 5)).to.eql([0, 5, 10, 15])
})

it('works when start is set to a negative number', () => [
expect(range(-4)).to.eql([0, -1, -2, -3])
])

it('fills array with start value if step is zero', () => {
expect(range(1, 4, 0)).to.eql([1, 1, 1])
})

it('works when step is a negative number ', () => {
expect(range(2, -4, -2)).to.eql([2, 0, -2])
})

it('return an empty array when end and start are the same', () => {
expect(range(0, 0)).to.eql([])
})

it('returns an empty array if only start is provided and equals zero', () => {
expect(range(0)).to.eql([])
})

it('returns an empty array if arguments do not work', () => {
expect(range(5, 1, 2)).to.eql([])
})
})

0 comments on commit 02e9cc8

Please sign in to comment.