-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(initial): add initial
#188
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[]`): 마지막 요소를 제외한 나머지 요소들로 구성된 새로운 배열을 반환할 배열. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I guess it's better for us to simplify this description. |
||||||
|
||||||
### 반환 값 | ||||||
|
||||||
(`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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better for us to follow other benchmarks.