From 610f47959397f159ef8163c2c32adc31ba20c3b3 Mon Sep 17 00:00:00 2001 From: raon0211 Date: Mon, 14 Oct 2024 15:36:07 +0900 Subject: [PATCH] style: Fix style --- .scripts/docs/generate-docs.mts | 2 +- docs/ja/reference/compat/array/first.md | 34 ++++++++++++++ docs/ko/reference/compat/array/first.md | 49 ++++++++++++++++++++ docs/reference/compat/array/first.md | 34 ++++++++++++++ docs/zh_hans/reference/compat/array/first.md | 34 ++++++++++++++ src/compat/array/intersection.ts | 17 +++++++ src/compat/string/escape.ts | 13 ++++++ 7 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 docs/ja/reference/compat/array/first.md create mode 100644 docs/ko/reference/compat/array/first.md create mode 100644 docs/reference/compat/array/first.md create mode 100644 docs/zh_hans/reference/compat/array/first.md diff --git a/.scripts/docs/generate-docs.mts b/.scripts/docs/generate-docs.mts index eb3cda62..dbda636e 100644 --- a/.scripts/docs/generate-docs.mts +++ b/.scripts/docs/generate-docs.mts @@ -120,7 +120,7 @@ function toDocumentationItems(docs: DocNode[]): DocumentationItems { }) .map(([name, entries]) => { const sourcePath = fileURLToPath(entries[0].location.filename); - const docPath: string = path.relative(basePath, sourcePath).replace(/.ts$/g, '.md'); + const docPath: string = path.join(path.dirname(path.relative(basePath, sourcePath)), `${name}.md`); return { docPath: docPath, diff --git a/docs/ja/reference/compat/array/first.md b/docs/ja/reference/compat/array/first.md new file mode 100644 index 00000000..ac0639c7 --- /dev/null +++ b/docs/ja/reference/compat/array/first.md @@ -0,0 +1,34 @@ +# first + +::: info +この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。 + +`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。 +::: + +配列の最初の要素を返します。配列が空の場合は`undefined`を返します。 + +この関数は配列を受け取り、配列の最初の要素を返します。 +配列が空の場合、関数は`undefined`を返します。 + +## インターフェース + +```typescript +function first(arr: ArrayLike | undefined | null): T | undefined; +``` + +### パラメータ + +- `arr` (`ArrayLike | undefined | null`): 最初の要素を取得するための配列です。 + +### 戻り値 + +(`T | undefined`): 配列の最初の要素、または配列が空の場合は`undefined`です。 + +## 例 + +```typescript +const emptyArr: number[] = []; +const noElement = head(emptyArr); +// noElement will be undefined +``` diff --git a/docs/ko/reference/compat/array/first.md b/docs/ko/reference/compat/array/first.md new file mode 100644 index 00000000..5682dd97 --- /dev/null +++ b/docs/ko/reference/compat/array/first.md @@ -0,0 +1,49 @@ +# first + +::: info +이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요. + +`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요. +::: + +배열의 첫 번째 요소를 반환해요. + +이 함수는 배열을 입력받아 배열의 첫 번째 요소를 반환해요. 배열이 비어 있는 경우, 함수는 `undefined`를 반환해요. + +## 인터페이스 + +```typescript +function first(arr: ArrayLike | undefined | null): T | undefined; +``` + +### 파라미터 + +- `arr` (`T[]`): 첫 번째 요소를 가져올 배열. + +### 반환 값 + +(`T | undefined`): 배열의 첫 번째 요소, 배열이 비어 있는 경우 `undefined`. + +## 예시 + +```typescript +const arr1 = [1, 2, 3]; +const firstElement1 = first(arr1); +// firstElement1은 1이에요. + +const arr2: string[] = []; +const firstElement2 = first(arr2); +// firstElement2는 undefined에요. + +const arr3 = ['a', 'b', 'c']; +const firstElement3 = first(arr3); +// firstElement3는 'a'이에요. + +const arr4 = [true, false, true]; +const firstElement4 = first(arr4); +// firstElement4는 true에요. + +const arr5: [number, string, boolean] = [1, 'a', true]; +const firstElement5 = first(arr5); +// firstElement5는 1이에요. +``` diff --git a/docs/reference/compat/array/first.md b/docs/reference/compat/array/first.md new file mode 100644 index 00000000..2e83afec --- /dev/null +++ b/docs/reference/compat/array/first.md @@ -0,0 +1,34 @@ +# first + +::: info +This function is only available in `es-toolkit/compat` for compatibility reasons. It either has alternative native JavaScript APIs or isn’t fully optimized yet. + +When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md). +::: + +Returns the first element of an array or `undefined` if the array is empty. + +This function takes an array and returns the first element of the array. +If the array is empty, the function returns `undefined`. + +## Signature + +```typescript +function first(arr: ArrayLike | undefined | null): T | undefined; +``` + +### Parameters + +- `arr` (`ArrayLike | undefined | null`): The array from which to get the first element. + +### Returns + +(`T | undefined`): The first element of the array, or `undefined` if the array is empty. + +## Examples + +```typescript +const emptyArr: number[] = []; +const noElement = head(emptyArr); +// noElement will be undefined +``` diff --git a/docs/zh_hans/reference/compat/array/first.md b/docs/zh_hans/reference/compat/array/first.md new file mode 100644 index 00000000..7093bff5 --- /dev/null +++ b/docs/zh_hans/reference/compat/array/first.md @@ -0,0 +1,34 @@ +# first + +::: info +出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。 + +从 `es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)。 +::: + +返回数组的第一个元素,如果数组为空,则返回`undefined`。 + +此函数接受一个数组并返回数组的第一个元素。 +如果数组为空,函数将返回`undefined`。 + +## 签名 + +```typescript +function first(arr: ArrayLike | undefined | null): T | undefined; +``` + +### 参数 + +- `arr` (`ArrayLike | undefined | null`): 获取第一个元素的数组。 + +### 返回值 + +(`T | undefined`): 数组的第一个元素,如果数组为空,则为`undefined`。 + +## 示例 + +```typescript +const emptyArr: number[] = []; +const noElement = head(emptyArr); +// noElement will be undefined +``` diff --git a/src/compat/array/intersection.ts b/src/compat/array/intersection.ts index 93f81e10..1fb9c310 100644 --- a/src/compat/array/intersection.ts +++ b/src/compat/array/intersection.ts @@ -2,6 +2,23 @@ import { intersection as intersectionToolkit } from '../../array/intersection.ts import { uniq } from '../../array/uniq.ts'; import { isArrayLikeObject } from '../predicate/isArrayLikeObject.ts'; +/** + * Returns the intersection of multiple arrays. + * + * This function takes multiple arrays and returns a new array containing the elements that are + * present in all provided arrays. It effectively filters out any elements that are not found + * in every array. + * + * @template T - The type of elements in the arrays. + * @param {...(ArrayLike | null | undefined)} arrays - The arrays to compare. + * @returns {T[]} A new array containing the elements that are present in all arrays. + * + * @example + * const array1 = [1, 2, 3, 4, 5]; + * const array2 = [3, 4, 5, 6, 7]; + * const result = intersection(array1, array2); + * // result will be [3, 4, 5] since these elements are in both arrays. + */ export function intersection(...arrays: Array | null | undefined>): T[] { if (arrays.length === 0) { return []; diff --git a/src/compat/string/escape.ts b/src/compat/string/escape.ts index 94355d5d..63967447 100644 --- a/src/compat/string/escape.ts +++ b/src/compat/string/escape.ts @@ -1,6 +1,19 @@ import { escape as escapeToolkit } from '../../string/escape.ts'; import { toString } from '../util/toString.ts'; +/** + * Converts the characters "&", "<", ">", '"', and "'" in `str` to their corresponding HTML entities. + * For example, "<" becomes "<". + * + * @param {string} str The string to escape. + * @returns {string} Returns the escaped string. + * + * @example + * escape('This is a
element.'); // returns 'This is a <div> element.' + * escape('This is a "quote"'); // returns 'This is a "quote"' + * escape("This is a 'quote'"); // returns 'This is a 'quote'' + * escape('This is a & symbol'); // returns 'This is a & symbol' + */ export function escape(string?: string): string { return escapeToolkit(toString(string)); }