Skip to content
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(reverse): Add reverse function #807

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions benchmarks/performance/reverse.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bench, describe } from 'vitest';
import { reverse as reverseToolkit_ } from 'es-toolkit';
import { reverse as reverseCompat_ } from 'es-toolkit/compat';
import { reverse as reverseLodash_ } from 'lodash';

const testArray = Array.from({ length: 1000 }, (_, i) => i);

describe('Reverse function performance tests', () => {
bench('es-toolkit reverse', () => {
const arrayCopy = [...testArray];
reverseToolkit_(arrayCopy);
});

bench('es-toolkit compat reverse', () => {
const arrayCopy = [...testArray];
reverseCompat_(arrayCopy);
});

bench('lodash reverse', () => {
const arrayCopy = [...testArray];
reverseLodash_(arrayCopy);
});
});
34 changes: 34 additions & 0 deletions docs/ja/reference/array/reverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# reverse

配列の要素をその場で反転します。

この関数は配列の要素をその場で反転し、元の配列を直接変更します。入力がnullまたはundefinedの場合、その値をそのまま返します。

## インターフェース

```typescript
function reverse<T>(array: T[] | null | undefined): T[] | null | undefined;
```

### パラメータ

- - `array` (`T[] | null | undefined`): 反転する配列

### 戻り値

(`T[] | null | undefined`): 反転された配列、または入力がnullやundefinedの場合はその値を返します。

## ## 例

```typescript
const array = [1, 2, 3, 4, 5];
const reversedArray = reverse(array);
console.log(reversedArray); // [5, 4, 3, 2, 1]
console.log(array); // [5, 4, 3, 2, 1] (元の配列が変更されます)

const emptyArray = reverse([]);
console.log(emptyArray); // []

const nullArray = reverse(null);
console.log(nullArray); // null
```
34 changes: 34 additions & 0 deletions docs/ko/reference/array/reverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# reverse

배열의 요소들을 제자리에서 반전시켜요

이 함수는 배열을 반전시키면서 원본 배열을 직접 수정해요. 입력이 null 또는 undefined일 경우, 입력 값을 그대로 반환해요.

## 인터페이스

```typescript
function reverse<T>(array: T[] | null | undefined): T[] | null | undefined;
```

### 파라미터

- `array` (`T[] | null | undefined`): 반전할 배열

### 반환 값

(`T[] | null | undefined`): 제거된 요소들의 배열.

## 예시

```typescript
const array = [1, 2, 3, 4, 5];
const reversedArray = reverse(array);
console.log(reversedArray); // [5, 4, 3, 2, 1]
console.log(array); // [5, 4, 3, 2, 1] (원본 배열이 수정되요)

const emptyArray = reverse([]);
console.log(emptyArray); // []

const nullArray = reverse(null);
console.log(nullArray); // null
```
34 changes: 34 additions & 0 deletions docs/reference/array/reverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# reverse

Reverses the elements of an array in place.

This function reverses the array elements in place, directly modifying the original array. If the input is null or undefined, it returns the input as is.

## Signature

```typescript
function reverse<T>(array: T[] | null | undefined): T[] | null | undefined;
```

### Parameters

- - `array` (`T[] | null | undefined`): The array to reverse.

### Returns

(`T[] | null | undefined`): The reversed array, or null/undefined if the input was null/undefined.

## Examples

```typescript
const array = [1, 2, 3, 4, 5];
const reversedArray = reverse(array);
console.log(reversedArray); // [5, 4, 3, 2, 1]
console.log(array); // [5, 4, 3, 2, 1] (The original array is modified)

const emptyArray = reverse([]);
console.log(emptyArray); // []

const nullArray = reverse(null);
console.log(nullArray); // null
```
34 changes: 34 additions & 0 deletions docs/zh_hans/reference/array/reverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# pullAt

在原位置反转数组的元素。

此函数在原位置反转数组的元素,直接修改原数组。如果输入为 null 或 undefined,则返回该输入值。

## 签名

```typescript
function reverse<T>(array: T[] | null | undefined): T[] | null | undefined;
```

### 参数

- - `array` (`T[] | null | undefined`): 要反转的数组

### 返回值

(`T[] | null | undefined`): 反转后的数组;如果输入为 null 或 undefined,则返回该输入值。

## 示例

```typescript
const array = [1, 2, 3, 4, 5];
const reversedArray = reverse(array);
console.log(reversedArray); // [5, 4, 3, 2, 1]
console.log(array); // [5, 4, 3, 2, 1] (原数组已被修改)

const emptyArray = reverse([]);
console.log(emptyArray); // []

const nullArray = reverse(null);
console.log(nullArray); // null
```
42 changes: 42 additions & 0 deletions src/array/reverse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest';
import { reverse } from './reverse';

describe('reverse', () => {
it('should return null if input is null', () => {
expect(reverse(null)).toBeNull();
});

it('should return undefined if input is undefined', () => {
expect(reverse(undefined)).toBeUndefined();
});

it('should return an empty array if input is an empty array', () => {
expect(reverse([])).toEqual([]);
});

it('should reverse an array with one element', () => {
expect(reverse([1])).toEqual([1]);
});

it('should reverse an array with multiple elements', () => {
expect(reverse([1, 2, 3, 4, 5])).toEqual([5, 4, 3, 2, 1]);
});

it('should reverse an array with even number of elements', () => {
expect(reverse([1, 2, 3, 4])).toEqual([4, 3, 2, 1]);
});

it('should reverse an array with odd number of elements', () => {
expect(reverse([1, 2, 3])).toEqual([3, 2, 1]);
});

it('should handle an array with duplicate elements', () => {
expect(reverse([1, 2, 2, 3])).toEqual([3, 2, 2, 1]);
});

it('should modify the original array', () => {
const array = [1, 2, 3];
reverse(array);
expect(array).toEqual([3, 2, 1]);
});
});
24 changes: 24 additions & 0 deletions src/array/reverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Reverses the elements of an array in place.
*
* This function takes an array and reverses its elements in place, modifying the original array.
* If the input is `null` or `undefined`, it returns the input as is.
*
* @template T - The type of elements in the array.
* @param {T[] | null | undefined} array - The array to reverse. If `null` or `undefined`, it returns the input as is.
* @returns {T[] | null | undefined} The reversed array, or `null`/`undefined` if the input was `null`/`undefined`.
*
* @example
* const array = [1, 2, 3, 4, 5];
* const reversedArray = reverse(array);
* // reversedArray is [5, 4, 3, 2, 1], and array is also modified to [5, 4, 3, 2, 1].
*
* const emptyArray = reverse([]);
* // emptyArray is [].
*
* const nullArray = reverse(null);
* // nullArray is null.
*/
export function reverse<T>(array: T[] | null | undefined): T[] | null | undefined {
return array == null ? array : array.reverse();
}
62 changes: 62 additions & 0 deletions src/compat/array/reverse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from 'vitest';
import { reverse } from './reverse';

// reverse 함수 경로에 맞게 수정하세요

describe('reverse', () => {
it('should return null if input is null', () => {
expect(reverse(null)).toBeNull();
});

it('should return undefined if input is undefined', () => {
expect(reverse(undefined)).toBeUndefined();
});

it('should return an empty array if input is an empty array', () => {
const array: number[] = [];
const result = reverse(array);
expect(result).toEqual([]);
expect(result).toBe(array);
});

it('should reverse an array with multiple elements', () => {
const array = [1, 2, 3, 4, 5];
const result = reverse(array);
expect(result).toEqual([5, 4, 3, 2, 1]);
expect(result).toBe(array);
});

it('should reverse an array with one element', () => {
const array = [42];
const result = reverse(array);
expect(result).toEqual([42]);
expect(result).toBe(array);
});

it('should handle an array with duplicate elements', () => {
const array = [1, 2, 2, 3];
const result = reverse(array);
expect(result).toEqual([3, 2, 2, 1]);
expect(result).toBe(array);
});

it('should modify the original array', () => {
const array = [1, 2, 3];
reverse(array);
expect(array).toEqual([3, 2, 1]);
});

it('should work with arrays of strings', () => {
const array = ['a', 'b', 'c'];
const result = reverse(array);
expect(result).toEqual(['c', 'b', 'a']);
expect(result).toBe(array);
});

it('should work with mixed types', () => {
const array = [1, 'two', 3, 'four'];
const result = reverse(array);
expect(result).toEqual(['four', 3, 'two', 1]);
expect(result).toBe(array);
});
});
40 changes: 40 additions & 0 deletions src/compat/array/reverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Reverses the elements of an array in place.
*
* This function takes an array and reverses its elements in place, modifying the original array.
* If the input is `null` or `undefined`, it returns the input as is.
*
* @template T - The type of elements in the array.
* @param {T[] | null | undefined} array - The array to reverse. If `null` or `undefined`, the input is returned as is.
* @returns {T[] | null | undefined} The reversed array, or `null`/`undefined` if the input was `null`/`undefined`.
*
* @example
* const array = [1, 2, 3, 4, 5];
* const reversedArray = reverse(array);
* // reversedArray is [5, 4, 3, 2, 1], and array is also modified to [5, 4, 3, 2, 1].
*
* const emptyArray = reverse([]);
* // emptyArray is [].
*
* const nullArray = reverse(null);
* // nullArray is null.
*/
export function reverse<T>(array: T[] | null | undefined): T[] | null | undefined {
if (array == null) {
return array;
}

let left = 0;
let right = array.length - 1;

while (left < right) {
const temp = array[left];
array[left] = array[right];
array[right] = temp;

left++;
right--;
}

return array;
}