diff --git a/benchmarks/negate.bench.ts b/benchmarks/negate.bench.ts new file mode 100644 index 00000000..569830a7 --- /dev/null +++ b/benchmarks/negate.bench.ts @@ -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; + }); +}); diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index 78561fe9..9b252ba2 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -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' }, ], diff --git a/docs/.vitepress/ko.mts b/docs/.vitepress/ko.mts index eb51b261..903c49d3 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -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' }, ], diff --git a/docs/ko/reference/function/negate.md b/docs/ko/reference/function/negate.md new file mode 100644 index 00000000..495cc85b --- /dev/null +++ b/docs/ko/reference/function/negate.md @@ -0,0 +1,28 @@ +# negate + +참과 거짓을 반환하는 함수 `func` 의 실행 결과를 반대로 바꿔요. + +## 인터페이스 + +```typescript +function negate 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' +``` diff --git a/docs/reference/function/negate.md b/docs/reference/function/negate.md new file mode 100644 index 00000000..0bf93bd6 --- /dev/null +++ b/docs/reference/function/negate.md @@ -0,0 +1,28 @@ +# negate + +Creates a function that negates the result of the predicate function. + +## Signature + +```typescript +function negate 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' +``` diff --git a/src/function/index.ts b/src/function/index.ts index 80506a09..bb8f822e 100644 --- a/src/function/index.ts +++ b/src/function/index.ts @@ -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'; diff --git a/src/function/negate.spec.ts b/src/function/negate.spec.ts new file mode 100644 index 00000000..ee0face6 --- /dev/null +++ b/src/function/negate.spec.ts @@ -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); + }); +}); diff --git a/src/function/negate.ts b/src/function/negate.ts new file mode 100644 index 00000000..f6e0b3c6 --- /dev/null +++ b/src/function/negate.ts @@ -0,0 +1,4 @@ + +export function negate boolean>(func: F): F { + return ((...args: any[]) => !func(...args)) as F; +}