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:
CreeJee 2024-07-14 18:10:01 +09:00 committed by GitHub
parent 4b00379320
commit bda3bef5c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 86 additions and 0 deletions

View 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;
});
});

View File

@ -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' },
],

View File

@ -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' },
],

View 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'
```

View 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'
```

View File

@ -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';

View 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
View File

@ -0,0 +1,4 @@
export function negate<F extends (...args: unknown[]) => boolean>(func: F): F {
return ((...args: any[]) => !func(...args)) as F;
}