Skip to content

Commit

Permalink
feat(range): Add range function (#77)
Browse files Browse the repository at this point in the history
* wip range

* feat(range):  range function

* chore(range): update bench name

---------

Co-authored-by: Sojin Park <[email protected]>
  • Loading branch information
WooWan and raon0211 authored Jun 28, 2024
1 parent cc6b4ac commit 41a3008
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
13 changes: 13 additions & 0 deletions benchmarks/range.bench.ts
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);
});
});
1 change: 1 addition & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { intersection } from './intersection';
export { intersectionBy } from './intersectionBy';
export { intersectionWith } from './intersectionWith';
export { partition } from './partition';
export { range } from './range';
export { sample } from './sample';
export { shuffle } from './shuffle';
export { take } from './take';
Expand Down
28 changes: 28 additions & 0 deletions src/array/range.spec.ts
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([]);
});
});
48 changes: 48 additions & 0 deletions src/array/range.ts
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;
}

0 comments on commit 41a3008

Please sign in to comment.