-
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.
fix(range): Add docs for range, and match its behavior with python's …
…range
- Loading branch information
Showing
8 changed files
with
130 additions
and
37 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
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,39 @@ | ||
# range | ||
|
||
`start`에서 시작해서 `end` 전에 끝나는 숫자의 배열을 반환해요. 연속한 숫자는 `step` 만큼 차이가 나요. | ||
|
||
`step`이 기본값은 1이고, 0일 수 없어요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function range(end: number): number[]; | ||
function range(start: number, end: number): number[]; | ||
function range(start: number, end: number, step: number): number[]; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `start` (`number`): 시작할 숫자. 배열은 이 숫자를 포함해요. | ||
- `end` (`number`): 끝날 숫자. 배열은 이 숫자를 포함하지 않아요. | ||
- `step` (`number`): 배열에서 연속한 숫자의 차이. 기본값은 `1`이에요. | ||
|
||
### 반환 값 | ||
|
||
- (`number[]`): `start`에서 시작해서 `end` 전에 끝나는, 연속한 숫자가 `step` 만큼 차이나는 배열. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
// Returns [0, 1, 2, 3] | ||
range(4); | ||
|
||
// Returns [0, 5, 10, 15] | ||
range(0, 20, 5); | ||
|
||
// Returns [] | ||
range(0, -4, -1); | ||
|
||
// Throws an error: The step value must be a non-zero integer. | ||
range(1, 4, 0); | ||
``` |
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,42 @@ | ||
# range | ||
|
||
Returns an array of numbers from `start` to `end`, incrementing by `step`. | ||
|
||
If `step` is not provided, it defaults to `1`. Note that `step` must be a non-zero integer. | ||
|
||
## Signature | ||
|
||
```typescript | ||
function range(end: number): number[]; | ||
function range(start: number, end: number): number[]; | ||
function range(start: number, end: number, step: number): number[]; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `start` (`number`): The starting number of the range (inclusive). | ||
- `end` (`number`): The end number of the range (exclusive). | ||
- `step` (`number`): The step value for the range. (default: `1`) | ||
|
||
### Returns | ||
|
||
- (`number[]`): An array of numbers from `start` to `end` with the specified `step`. | ||
|
||
## Examples | ||
|
||
```typescript | ||
// Returns [0, 1, 2, 3] | ||
range(4); | ||
|
||
// Returns [0, 5, 10, 15] | ||
range(0, 20, 5); | ||
|
||
// Returns [0, 5, 10, 15, 20] | ||
range(0, 21, 5); | ||
|
||
// Returns [] | ||
range(0, -4, -1); | ||
|
||
// Throws an error: The step value must be a non-zero integer. | ||
range(1, 4, 0); | ||
``` |
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { range } from './range'; | ||
|
||
describe('range', () => { | ||
it('returns 0, 1, 2, 3 for range 0 to 4', () => { | ||
expect(range(4)).toEqual([0, 1, 2, 3]); | ||
expect(range(0, 4)).toEqual([0, 1, 2, 3]); | ||
}); | ||
|
||
it('returns an empty array for range 0 to -4', () => { | ||
expect(range(-4)).toEqual([]); | ||
expect(range(0, -4)).toEqual([]); | ||
}); | ||
|
||
it('can have positive step', () => { | ||
expect(range(0, 20, 5)).toEqual([0, 5, 10, 15]); | ||
}); | ||
|
||
it('returns an empty array when the step is negative', () => { | ||
expect(range(0, 4, -1)).toEqual([]); | ||
}); | ||
|
||
it('throws an error when step is zero', () => { | ||
expect(() => range(1, 4, 0)).toThrowErrorMatchingInlineSnapshot( | ||
`[Error: The step value must be a non-zero integer.]` | ||
); | ||
}); | ||
|
||
it('returns an empty array when for range 0 to 0', () => { | ||
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