docs(before): Update docs (#682)

* docs(before): Correct JSDoc example

* docs(before): Add lodash compatibility section

---------

Co-authored-by: Sojin Park <raon0211@toss.im>
This commit is contained in:
Dongho Kim 2024-10-07 22:14:13 +09:00 committed by GitHub
parent 8593c6742a
commit 3d7ba7d691
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 92 additions and 2 deletions

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -21,8 +21,7 @@ import { toInteger } from '../util/toInteger.ts';
*
* before3(); // => 1
* before3(); // => 2
* before3(); // => 3
* before3(); // => 3
* before3(); // => 2
*/
export function before<F extends (...args: any[]) => any>(
n: number,