mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-27 14:57:44 +03:00
feat(negate): Add negate
(#177)
* feat(negate): add negate base lodash implement * Update docs/ko/reference/function/negate.md * Update negate.md * Update negate.md * Update negate.spec.ts * Update src/function/negate.ts * Update src/function/index.ts --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
This commit is contained in:
parent
4b00379320
commit
bda3bef5c3
13
benchmarks/negate.bench.ts
Normal file
13
benchmarks/negate.bench.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { bench, describe } from 'vitest';
|
||||
import { negate as negateToolkit } from 'es-toolkit';
|
||||
import { negate as negateLodash } from 'lodash';
|
||||
|
||||
describe('compact', () => {
|
||||
bench('es-toolkit', () => {
|
||||
negateToolkit(() => true)() === false;
|
||||
});
|
||||
|
||||
bench('lodash', () => {
|
||||
negateLodash(() => true)() === false;
|
||||
});
|
||||
});
|
@ -104,6 +104,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
items: [
|
||||
{ text: 'debounce', link: '/reference/function/debounce' },
|
||||
{ text: 'throttle', link: '/reference/function/throttle' },
|
||||
{ text: 'negate', link: '/reference/function/negate' },
|
||||
{ text: 'once', link: '/reference/function/once' },
|
||||
{ text: 'noop', link: '/reference/function/noop' },
|
||||
],
|
||||
|
@ -103,6 +103,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
items: [
|
||||
{ text: 'debounce', link: '/ko/reference/function/debounce' },
|
||||
{ text: 'throttle', link: '/ko/reference/function/throttle' },
|
||||
{ text: 'negate', link: '/reference/function/negate' },
|
||||
{ text: 'once', link: '/ko/reference/function/once' },
|
||||
{ text: 'noop', link: '/ko/reference/function/noop' },
|
||||
],
|
||||
|
28
docs/ko/reference/function/negate.md
Normal file
28
docs/ko/reference/function/negate.md
Normal file
@ -0,0 +1,28 @@
|
||||
# negate
|
||||
|
||||
참과 거짓을 반환하는 함수 `func` 의 실행 결과를 반대로 바꿔요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function negate<F extends (...args: unknown[]) => boolean>(func: F): F;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `func` (`F extends (args: ...Parameters) => unknown`): 반환 값을 반대로 바꿀 함수.
|
||||
|
||||
### 반환 값
|
||||
|
||||
- (`F`): 반환 값이 반대로 바뀐 함수.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
import { negate } from 'es-toolkit/function';
|
||||
|
||||
negate(() => true)(); // returns 'false'
|
||||
negate(() => false)(); // returns 'false'
|
||||
negate(() => 1); // returns 'false'
|
||||
negate(() => 0); // returns 'true'
|
||||
```
|
28
docs/reference/function/negate.md
Normal file
28
docs/reference/function/negate.md
Normal file
@ -0,0 +1,28 @@
|
||||
# negate
|
||||
|
||||
Creates a function that negates the result of the predicate function.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function negate<F extends (...args: unknown[]) => boolean>(func: F): F;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
- `func` (`F extends (args: ...Parameters) => unknown`): The function to negate.
|
||||
|
||||
### Returns
|
||||
|
||||
- (`F`): Returns the new negated function.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
import { negate } from 'es-toolkit/function';
|
||||
|
||||
negate(() => true)(); // returns 'false'
|
||||
negate(() => false)(); // returns 'false'
|
||||
negate(() => 1); // returns 'false'
|
||||
negate(() => 0); // returns 'true'
|
||||
```
|
@ -2,3 +2,4 @@ export { debounce } from './debounce.ts';
|
||||
export { noop } from './noop.ts';
|
||||
export { once } from './once.ts';
|
||||
export { throttle } from './throttle.ts';
|
||||
export { negate } from './negate.ts';
|
||||
|
10
src/function/negate.spec.ts
Normal file
10
src/function/negate.spec.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { negate } from './negate';
|
||||
|
||||
describe('negate', () => {
|
||||
it('should negate the given predicate function', () => {
|
||||
expect(typeof negate(() => true)).toBe('function');
|
||||
expect(negate(() => true)()).toBe(false);
|
||||
expect(negate(() => false)()).toBe(true);
|
||||
});
|
||||
});
|
4
src/function/negate.ts
Normal file
4
src/function/negate.ts
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
export function negate<F extends (...args: unknown[]) => boolean>(func: F): F {
|
||||
return ((...args: any[]) => !func(...args)) as F;
|
||||
}
|
Loading…
Reference in New Issue
Block a user