-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add initial function * add initial test case * add initial bench mark * add initial docs
- Loading branch information
1 parent
6606d5b
commit 15a1a3c
Showing
10 changed files
with
239 additions
and
2 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,20 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { initial as lodashInitial } from 'lodash'; | ||
import { initial as esToolkitInitial } from 'es-toolkit'; // hypothetical library | ||
|
||
// Helper function to generate a large array | ||
function generateLargeArray(size) { | ||
return Array.from({ length: size }, (_, index) => index); | ||
} | ||
|
||
describe('initial function performance', () => { | ||
const largeArray = generateLargeArray(1000000); // 1 million elements | ||
|
||
bench('es-toolkit/initial', () => { | ||
esToolkitInitial(largeArray); | ||
}); | ||
|
||
bench('lodash/initial', () => { | ||
lodashInitial(largeArray); | ||
}); | ||
}); |
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
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,49 @@ | ||
# initial | ||
|
||
배열의 마지막 요소를 제외한 나머지 요소들로 구성된 새로운 배열을 반환해요. | ||
|
||
빈 배열이나 길이가 1인 배열의 경우, 빈 배열(`[]`)을 반환해요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function initial<T>(arr: T[]): T[]; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `arr`(`T[]`): 마지막 요소를 제외한 나머지 요소들로 구성된 새로운 배열을 반환할 배열. | ||
|
||
### 반환 값 | ||
|
||
(`T[]`): 배열의 마지막 요소를 제외한 나머지 배열. 빈 배열이나 길이가 1인 배열의 경우, 빈 배열(`[]`)을 반환해요. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
const arr1 = [1, 2, 3]; | ||
const result = initial(arr1); | ||
// result는 [1, 2]에요. | ||
|
||
const arr2: number[] = []; | ||
const result = initial(arr2); | ||
// result는 []에요. | ||
|
||
const arr3: number[] = [1]; | ||
const result = initial(arr3); | ||
// result는 []에요. | ||
|
||
const largeArray = Array(1000) | ||
.fill(0) | ||
.map((_, i) => i); | ||
const result = initial(largeArray); | ||
// result는 [0, 1, 2 ..., 998]에요. | ||
|
||
const nestedArray = [ | ||
[3, 1], | ||
[3, 2], | ||
[3, 3], | ||
]; | ||
const result = initial(nestedArray); | ||
// result는 [[3, 1], [3, 2]]에요. | ||
``` |
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,49 @@ | ||
# initial | ||
|
||
Returns a new array containing all elements except the last one from the input array. | ||
|
||
For an empty array or an array with a length of 1, it returns an empty array (`[]`). | ||
|
||
## Signature | ||
|
||
```typescript | ||
function initial<T>(arr: T[]): T[]; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `arr` (`T[]`): The array from which to return all elements except the last one. | ||
|
||
### Returns | ||
|
||
(T[]): A new array containing all elements except the last one from the input array. For an empty array or an array with a length of 1, it returns an empty array ([]). | ||
|
||
## Examples | ||
|
||
```typescript | ||
const arr1 = [1, 2, 3]; | ||
const result = initial(arr1); | ||
// result is [1, 2]. | ||
|
||
const arr2: number[] = []; | ||
const result = initial(arr2); | ||
// result is []. | ||
|
||
const arr3: number[] = [1]; | ||
const result = initial(arr3); | ||
// result is []. | ||
|
||
const largeArray = Array(1000) | ||
.fill(0) | ||
.map((_, i) => i); | ||
const result = initial(largeArray); | ||
// result is [0, 1, 2 ..., 998]. | ||
|
||
const nestedArray = [ | ||
[3, 1], | ||
[3, 2], | ||
[3, 3], | ||
]; | ||
const result = initial(nestedArray); | ||
// result is [[3, 1], [3, 2]]. | ||
``` |
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,49 @@ | ||
# initial | ||
|
||
返回一个新数组,包含输入数组中除最后一个元素外的所有元素。 | ||
|
||
对于空数组或长度为1的数组,它返回一个空数组(`[]`)。 | ||
|
||
## 签名 | ||
|
||
```typescript | ||
function initial<T>(arr: T[]): T[]; | ||
``` | ||
|
||
### 参数 | ||
|
||
- `arr` (`T[]`): 要从中返回除最后一个元素外所有元素的数组。 | ||
|
||
### 返回值 | ||
|
||
(`T[]`): 一个新数组,包含输入数组中除最后一个元素外的所有元素。对于空数组或长度为1的数组,它返回一个空数组(`[]`)。 | ||
|
||
## 示例 | ||
|
||
```typescript | ||
const arr1 = [1, 2, 3]; | ||
const result = initial(arr1); | ||
// result 是 [1, 2] | ||
|
||
const arr2: number[] = []; | ||
const result = initial(arr2); | ||
// result 是 [] | ||
|
||
const arr3: number[] = [1]; | ||
const result = initial(arr3); | ||
// result 是 [] | ||
|
||
const largeArray = Array(1000) | ||
.fill(0) | ||
.map((\_, i) => i); | ||
const result = initial(largeArray); | ||
// result 是 [0, 1, 2 ..., 998] | ||
|
||
const nestedArray = [ | ||
[3, 1], | ||
[3, 2], | ||
[3, 3], | ||
]; | ||
const result = initial(nestedArray); | ||
// result 是 [[3, 1], [3, 2]] | ||
``` |
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,38 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { initial } from './initial'; | ||
|
||
describe('initial', () => { | ||
it('returns a new array containing all elements except the last one from the input array', () => { | ||
expect(initial([1, 2, 3])).toEqual([1, 2]); | ||
expect(initial(['a', 'b', 'c'])).toEqual(['a', 'b']); | ||
expect(initial([1, true, 'string'])).toEqual([1, true]); | ||
expect(initial([])).toEqual([]); | ||
}); | ||
|
||
// Edge cases | ||
it('returns an empty array for a single-element array', () => { | ||
expect(initial(['one'])).toEqual([]); | ||
}); | ||
|
||
it('returns all elements except the last one for a large array', () => { | ||
const largeArray = Array(1000) | ||
.fill(0) | ||
.map((_, i) => i); | ||
const expectedArray = Array(999) | ||
.fill(0) | ||
.map((_, i) => i); | ||
expect(initial(largeArray)).toEqual(expectedArray); | ||
}); | ||
|
||
it('returns all elements except the last one for a nested array', () => { | ||
const nestedArray = [ | ||
[3, 1], | ||
[3, 2], | ||
[3, 3], | ||
]; | ||
expect(initial(nestedArray)).toEqual([ | ||
[3, 1], | ||
[3, 2], | ||
]); | ||
}); | ||
}); |
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,29 @@ | ||
/** | ||
* Returns a new array containing all elements except the last one from the input array. | ||
* If the input array is empty or has only one element, the function returns an empty array. | ||
* | ||
* @template T The type of elements in the array. | ||
* @param {T[]} arr - The input array to query. | ||
* @returns {T[]} A new array containing all but the last element of the input array. | ||
* | ||
* @example | ||
* const arr = [1, 2, 3, 4]; | ||
* const newArr = initial(arr); | ||
* // newArr will be [1, 2, 3] | ||
* | ||
* @example | ||
* const emptyArr: number[] = []; | ||
* const newEmptyArr = initial(emptyArr); | ||
* // newEmptyArr will be [] | ||
* | ||
* @example | ||
* const singleElementArr = ['only one']; | ||
* const newSingleElementArr = initial(singleElementArr); | ||
* // newSingleElementArr will be [] | ||
*/ | ||
export function initial<T>(arr: T[]): T[] { | ||
if (arr.length <= 1) { | ||
return []; | ||
} | ||
return arr.slice(0, -1); | ||
} |