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(sortedIndexOf): Implement compat/sortedIndexOf #971

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(sortedIndexOf) - docs
  • Loading branch information
Kyujenius committed Mar 2, 2025
commit 3698eed9b264a4809bb53a3839eb32a3625b420b
53 changes: 53 additions & 0 deletions docs/ja/reference/compat/array/sortedIndexOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# sortedIndexOf

::: info
この関数は互換性のために `es-toolkit/compat` からのみインポートできます。これは、代替可能なネイティブ JavaScript API が存在するか、まだ十分に最適化されていないためです。

`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。
:::

この関数は、ソートされた配列内で2番目の引数値が最初に出現するインデックスを見つけます。つまり、探している値がソートされた配列の何番目に位置しているかを教えてくれる関数です。[sortedIndex](./sortedIndex.md)関数を使用して、繰り返し比較した後にインデックスを返します。

## インターフェース

```typescript
export function sortedIndexOf(array: ArrayLike | null | undefined, value: T): number;
```

### パラメータ

- `array` (`ArrayLike | null | undefined`): ソートされた配列です。配列が null または undefined の場合、-1 を返します。
- `value` (`T`): ソートされた配列内で比較を通じて探す値。

### 戻り値

(`number`): ソート順序を維持するために値を挿入すべきインデックス。

## 例

```typescript
import { sortedIndexOf } from 'es-toolkit/compat';

const numbers = [11, 22, 33, 44, 55];
const unSortedNumbers = [55, 33, 22, 11, 44];

// 通常のケース
sortedIndexOf(numbers, 11);
// 戻り値: 0
// 説明: numbers 配列において 11 と同じ値の位置は 0 です。

// 存在しない値
sortedIndexOf(numbers, 30);
// 戻り値: -1
// 説明: 30 は配列内に存在しないため、-1 を返します。

// 空の配列
sortedIndexOf([], 30);
// 戻り値: -1
// 説明: 空の配列内で値を探すと -1 を返します。

// ソートされていない配列
sortedIndexOf(unSortedNumbers, 11);
// 戻り値: -1
// 説明: ソートされていない配列を使用すると -1 を返します。
```
54 changes: 54 additions & 0 deletions docs/ko/reference/compat/array/sortedIndexOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# sortedIndexOf

::: info
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.

`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요.
:::

정렬된 배열에서 두 번째 인자값이 처음으로 등장하는 위치를 찾아주는 함수예요. 즉, 찾으려는 값이 정렬된 배열 속 몇 번째에 위치해있는지 알려주는 함수예요. [sortedIndex](./sortedIndex.md)함수를 이용해서 비교 후에 인덱스를 반환해요.

## 인터페이스

```typescript
export function sortedIndexOf<T>(array: ArrayLike<T> | null | undefined, value: T): number;
```

### 파라미터

- `array` (`ArrayLike<T> | null | undefined`): 정렬된 배열이예요. 배열이 null 또는 undefined일 경우 -1을 리턴해요.
- `value` (`T`): 정렬된 배열속 비교를 통해 찾으려는 값.

### 반환 값

(`number`): 정렬 순서를 유지하기 위해 값을 삽입할 인덱스입니다.

## 예시

```typescript
import { sortedIndexOf } from 'es-toolkit/compat';

const numbers = [11, 22, 33, 44, 55];
const unSortedNumbers = [55, 33, 22, 11, 44];

// 일반적인 경우
sortedIndexBy(numbers, 11);
// 반환값: 0
// 설명: numbers 배열 기준으로 11과 같은 값의 위치는 0이예요.

// 존재하지 않는 값
sortedIndexOf(numbers, 30);
// 반환값: -1
// 설명: 30은 배열 내에 존재하지 않으므로 -1을 반환해요.

// 빈 배열
sortedIndexOf([], 30);
// 반환값: -1
// 설명: 빈 배열 내에서 값을 찾으면 -1을 반환해요.

// 정렬되지 않은 배열
sortedIndexOf(unSortedNumbers, 11);
// 반환값: -1
// 설명: 정렬되지 않은 배열을 사용할 시에는 -1을 반환해요.

```
53 changes: 53 additions & 0 deletions docs/reference/compat/array/sortedIndexOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# sortedIndexOf

::: info
This function can only be imported from `es-toolkit/compat` for compatibility reasons. This is because there are native JavaScript APIs that can replace it, or it hasn't been sufficiently optimized yet.

When you import this function from `es-toolkit/compat`, it [behaves exactly the same as lodash](../../../compatibility.md).
:::

This function finds the index of the first occurrence of the second argument value in a sorted array. In other words, it tells you at which position the value you're looking for is located in the sorted array. It uses the [sortedIndex](./sortedIndex.md) function, repeatedly comparing and returning the index.

## Interface

```typescript
export function sortedIndexOf(array: ArrayLike | null | undefined, value: T): number;
```

### Parameters

- `array` (`ArrayLike | null | undefined`): A sorted array. If the array is null or undefined, it returns -1.
- `value` (`T`): The value to search for in the sorted array through comparison.

### Return Value

(`number`): The index at which the value should be inserted to maintain the sort order.

## Examples

```typescript
import { sortedIndexOf } from 'es-toolkit/compat';

const numbers = [11, 22, 33, 44, 55];
const unSortedNumbers = [55, 33, 22, 11, 44];

// Normal case
sortedIndexOf(numbers, 11);
// Return value: 0
// Explanation: The position of the value 11 in the numbers array is 0.

// Non-existent value
sortedIndexOf(numbers, 30);
// Return value: -1
// Explanation: 30 doesn't exist in the array, so it returns -1.

// Empty array
sortedIndexOf([], 30);
// Return value: -1
// Explanation: When searching for a value in an empty array, it returns -1.

// Unsorted array
sortedIndexOf(unSortedNumbers, 11);
// Return value: -1
// Explanation: When using an unsorted array, it returns -1.
```
53 changes: 53 additions & 0 deletions docs/zh_hans/reference/compat/array/sortedIndexOf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# sortedIndexOf

::: info
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。

从 `es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)。
:::

这个函数用于在已排序的数组中查找第二个参数值首次出现的索引。换句话说,它告诉你要查找的值在已排序数组中的位置。它使用 [sortedIndex](./sortedIndex.md) 函数,通过重复比较后返回索引。

## 签名

```typescript
export function sortedIndexOf(array: ArrayLike | null | undefined, value: T): number;
```

### 参数

- `array` (`ArrayLike | null | undefined`): 已排序的数组。如果数组为 null 或 undefined,则返回 -1。
- `value` (`T`): 要在已排序数组中通过比较查找的值。

### 返回值

(`number`): 为保持排序顺序应插入值的索引。

## 示例

```typescript
import { sortedIndexOf } from 'es-toolkit/compat';

const numbers = [11, 22, 33, 44, 55];
const unSortedNumbers = [55, 33, 22, 11, 44];

// 常规情况
sortedIndexOf(numbers, 11);
// 返回值: 0
// 解释: 在 numbers 数组中,值 11 的位置是 0。

// 不存在的值
sortedIndexOf(numbers, 30);
// 返回值: -1
// 解释: 30 不存在于数组中,因此返回 -1。

// 空数组
sortedIndexOf([], 30);
// 返回值: -1
// 解释: 在空数组中查找值时返回 -1。

// 未排序的数组
sortedIndexOf(unSortedNumbers, 11);
// 返回值: -1
// 解释: 使用未排序的数组时返回 -1。
```