From 3d7ba7d6918608114b2d2410674ab88da9e5381a Mon Sep 17 00:00:00 2001 From: Dongho Kim <70563791+mass2527@users.noreply.github.com> Date: Mon, 7 Oct 2024 22:14:13 +0900 Subject: [PATCH] docs(before): Update docs (#682) * docs(before): Correct JSDoc example * docs(before): Add lodash compatibility section --------- Co-authored-by: Sojin Park --- docs/ja/reference/function/before.md | 23 +++++++++++++++++++++++ docs/ko/reference/function/before.md | 22 ++++++++++++++++++++++ docs/reference/function/before.md | 23 +++++++++++++++++++++++ docs/zh_hans/reference/function/before.md | 23 +++++++++++++++++++++++ src/compat/function/before.ts | 3 +-- 5 files changed, 92 insertions(+), 2 deletions(-) diff --git a/docs/ja/reference/function/before.md b/docs/ja/reference/function/before.md index c5b86b77..d7ebeb97 100644 --- a/docs/ja/reference/function/before.md +++ b/docs/ja/reference/function/before.md @@ -47,3 +47,26 @@ afterFn(); // '実行されました' がログに出力されます afterFn(); ``` + +## Lodash 互換性 + +`es-toolkit/compat` から `before` をインポートすると、Lodash と互換になります。 + +- `n` が負の場合でもエラーをスローしません。 +- `func` が関数でない場合はエラーをスローします。 +- 呼び出し回数が `n` に達するか、それ以上になると、`func` の最後の結果を返します。 + +```typescript +import { before } from 'es-toolkit/compat'; + +let count = 0; + +const before3 = before(3, () => { + console.log('カウントを増やします...'); + return ++count; +}); + +console.log(before3()); // カウントを増やします... => 1 +console.log(before3()); // カウントを増やします... => 2 +console.log(before3()); // => 2 +``` \ No newline at end of file diff --git a/docs/ko/reference/function/before.md b/docs/ko/reference/function/before.md index 5336f553..ed472a98 100644 --- a/docs/ko/reference/function/before.md +++ b/docs/ko/reference/function/before.md @@ -47,3 +47,25 @@ afterFn(); // '실행됨'을 로깅해요. afterFn(); ``` +## Lodash와의 호환성 + +`es-toolkit/compat`에서 `before`를 가져오면 lodash와 호환돼요. + +- `n`이 음수여도 에러를 던지지 않아요. +- `func`가 함수가 아니면 에러를 던져요. +- 호출 횟수가 `n`에 도달하거나 초과하면 `func`의 마지막 결과를 반환해요. + +```typescript +import { before } from 'es-toolkit/compat'; + +let count = 0; + +const before3 = before(3, () => { + console.log('카운트를 증가시켜요...'); + return ++count; +}); + +console.log(before3()); // 카운트를 증가시켜요... => 1 +console.log(before3()); // 카운트를 증가시켜요... => 2 +console.log(before3()); // => 2 +``` \ No newline at end of file diff --git a/docs/reference/function/before.md b/docs/reference/function/before.md index 02be16b1..88026583 100644 --- a/docs/reference/function/before.md +++ b/docs/reference/function/before.md @@ -48,3 +48,26 @@ beforeFn(); // Will not log anything. beforeFn(); ``` + +## Lodash Compatibility + +Import `before` from `es-toolkit/compat` for full compatibility with lodash. + +- `before` does not throw an error when `n` is negative. +- `before` throws an error if `func` is not a function. +- `before` returns the last result of `func` when the number of calls reaches or exceeds `n`. + +```typescript +import { before } from 'es-toolkit/compat'; + +let count = 0; + +const before3 = before(3, () => { + console.log('Incrementing count...'); + return ++count; +}); + +console.log(before3()); // Incrementing count... => 1 +console.log(before3()); // Incrementing count... => 2 +console.log(before3()); // => 2 +``` \ No newline at end of file diff --git a/docs/zh_hans/reference/function/before.md b/docs/zh_hans/reference/function/before.md index 91c03247..607f269c 100644 --- a/docs/zh_hans/reference/function/before.md +++ b/docs/zh_hans/reference/function/before.md @@ -48,3 +48,26 @@ beforeFn(); // 不会打印 anything. beforeFn(); ``` + +## Lodash 兼容性 + +从 `es-toolkit/compat` 中导入 `before` 以实现与 lodash 的完全兼容。 + +- `n` 为负数时不会抛出错误。 +- 如果 `func` 不是一个函数,会抛出错误。 +- 当调用次数达到或超过 `n` 时,`before` 返回 `func` 的最后结果。 + +```typescript +import { before } from 'es-toolkit/compat'; + +let count = 0; + +const before3 = before(3, () => { + console.log('正在增加计数...'); + return ++count; +}); + +console.log(before3()); // 正在增加计数... => 1 +console.log(before3()); // 正在增加计数... => 2 +console.log(before3()); // => 2 +``` \ No newline at end of file diff --git a/src/compat/function/before.ts b/src/compat/function/before.ts index 66f444f1..5985ca34 100644 --- a/src/compat/function/before.ts +++ b/src/compat/function/before.ts @@ -21,8 +21,7 @@ import { toInteger } from '../util/toInteger.ts'; * * before3(); // => 1 * before3(); // => 2 - * before3(); // => 3 - * before3(); // => 3 + * before3(); // => 2 */ export function before any>( n: number,