-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(range): Add range function (#77)
* wip range * feat(range): range function * chore(range): update bench name --------- Co-authored-by: Sojin Park <[email protected]>
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { range as rangeToolkit } from 'es-toolkit'; | ||
import { range as rangeLodash } from 'lodash'; | ||
|
||
describe('range', () => { | ||
bench('es-toolkit/range', () => { | ||
rangeToolkit(0, 100, 1); | ||
}); | ||
|
||
bench('lodash/range', () => { | ||
rangeLodash(0, 100, 1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { range } from './range'; | ||
|
||
describe('range', () => { | ||
it('handles positive range with default step', () => { | ||
expect(range(4)).toEqual([0, 1, 2, 3]); | ||
}); | ||
|
||
it('handles negative range with default step', () => { | ||
expect(range(-4)).toEqual([0, -1, -2, -3]); | ||
}); | ||
|
||
it('handles custom positive step', () => { | ||
expect(range(0, 20, 5)).toEqual([0, 5, 10, 15]); | ||
}); | ||
|
||
it('handles custom negative step', () => { | ||
expect(range(0, -4, -1)).toEqual([0, -1, -2, -3]); | ||
}); | ||
|
||
it('handles zero step', () => { | ||
expect(range(1, 4, 0)).toEqual([1, 1, 1]); | ||
}); | ||
|
||
it('handles zero range', () => { | ||
expect(range(0)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Returns an array of numbers from `start` to `end` with the specified `step` value. | ||
* | ||
* This function generates a range of numbers, starting from `start`, ending before `end`, | ||
* and incrementing by `step`. If `step` is not provided, it defaults to `1` for an | ||
* ascending range and `-1` for a descending range. | ||
* | ||
* @param {number} start - The starting number of the range. | ||
* @param {number} [end] - The end number of the range. | ||
* @param {number} [step] - The step value for the range. | ||
* @returns {number[]} An array of numbers from `start` to `end` with the specified `step`. | ||
* | ||
* @example | ||
* // Returns [0, 1, 2, 3] | ||
* range(4); | ||
* | ||
* @example | ||
* // Returns [0, 5, 10, 15] | ||
* range(0, 20, 5); | ||
* | ||
* @example | ||
* // Returns [0, -1, -2, -3] | ||
* range(0, -4, -1); | ||
* | ||
* @example | ||
* // Returns [1, 1, 1] | ||
* range(1, 4, 0); | ||
*/ | ||
|
||
export function range(start: number, end?: number, step?: number): number[] { | ||
if (end === undefined) { | ||
end = start; | ||
start = 0; | ||
} | ||
|
||
if (step === undefined) { | ||
step = start <= end ? 1 : -1; | ||
} | ||
|
||
const length = Math.max(Math.ceil((end - start) / (step || 1)), 0); | ||
const result = new Array(length); | ||
|
||
for (let i = 0; i < length; i++) { | ||
result[i] = start + i * step; | ||
} | ||
|
||
return result; | ||
} |