From 678028dd3d60509b99dfec47aed7f1088140d19d Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Sat, 8 Jun 2024 15:47:30 +0900 Subject: [PATCH] feat(noop): Add `noop` function --- docs/.vitepress/en.mts | 1 + docs/.vitepress/ko.mts | 1 + docs/ko/reference/function/noop.md | 28 ++++++++++++++++++++++++++++ docs/reference/function/noop.md | 28 ++++++++++++++++++++++++++++ package.json | 2 +- src/function/index.ts | 1 + src/function/noop.spec.ts | 12 ++++++++++++ src/function/noop.ts | 10 ++++++++++ 8 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 docs/ko/reference/function/noop.md create mode 100644 docs/reference/function/noop.md create mode 100644 src/function/noop.spec.ts create mode 100644 src/function/noop.ts diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index 8e052f738..d022b3846 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -83,6 +83,7 @@ function sidebar(): DefaultTheme.Sidebar { { text: 'debounce', link: '/reference/function/debounce' }, { text: 'throttle', link: '/reference/function/throttle' }, { 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 35b673efd..8d77a2956 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -82,6 +82,7 @@ function sidebar(): DefaultTheme.Sidebar { { text: 'debounce', link: '/ko/reference/function/debounce' }, { text: 'throttle', link: '/ko/reference/function/throttle' }, { text: 'once', link: '/ko/reference/function/once' }, + { text: 'noop', link: '/ko/reference/function/noop' }, ], }, { diff --git a/docs/ko/reference/function/noop.md b/docs/ko/reference/function/noop.md new file mode 100644 index 000000000..f1c4c5fe0 --- /dev/null +++ b/docs/ko/reference/function/noop.md @@ -0,0 +1,28 @@ +# noop + +아무것도 하지 않는 함수예요. 함수를 요구하는 곳에 빈 자리를 채우기 위해 사용하거나, 기본값으로 사용할 수 있어요. + +## 인터페이스 + +```typescript +function noop(): void; +``` + +### 반환 값 + +(`void`): 이 함수는 아무것도 반환하지 않아요. + +## 예시 + +```typescript +import { noop } from 'es-toolkit/function'; + +interface Props { + onChange?: () => void; +} + +function MyComponent({ onChange = noop }: Props) { + // 여기서 onChange는 undefined일 수 없어서, 자유롭게 부를 수 있어요. + onChange(); +} +``` diff --git a/docs/reference/function/noop.md b/docs/reference/function/noop.md new file mode 100644 index 000000000..bd58c6d54 --- /dev/null +++ b/docs/reference/function/noop.md @@ -0,0 +1,28 @@ +# noop + +A no-operation function that does nothing. This can be used as a placeholder or default function. + +## Signature + +```typescript +function noop(): void; +``` + +### Returns + +(`void`): This function does not return anything. + +## Examples + +```typescript +import { noop } from 'es-toolkit/function'; + +interface Props { + onChange?: () => void; +} + +function MyComponent({ onChange = noop }: Props) { + // Here onChange is guaranteed to be a function, so it's safe to call. + onChange(); +} +``` diff --git a/package.json b/package.json index a5f9128e2..62444e7fd 100644 --- a/package.json +++ b/package.json @@ -130,4 +130,4 @@ "lint": "eslint ./src --ext .ts", "format": "prettier --write ." } -} \ No newline at end of file +} diff --git a/src/function/index.ts b/src/function/index.ts index e4243831e..92413f090 100644 --- a/src/function/index.ts +++ b/src/function/index.ts @@ -1,3 +1,4 @@ export { debounce } from './debounce'; +export { noop } from './noop'; export { once } from './once'; export { throttle } from './throttle'; diff --git a/src/function/noop.spec.ts b/src/function/noop.spec.ts new file mode 100644 index 000000000..167e0bbb3 --- /dev/null +++ b/src/function/noop.spec.ts @@ -0,0 +1,12 @@ +import { describe, it, expect } from 'vitest'; +import { noop } from './path/to/your/noop'; + +describe('noop', () => { + it('should be a function', () => { + expect(typeof noop).toBe('function'); + }); + + it('should return undefined', () => { + expect(noop()).toBeUndefined(); + }); +}); diff --git a/src/function/noop.ts b/src/function/noop.ts new file mode 100644 index 000000000..2f039c058 --- /dev/null +++ b/src/function/noop.ts @@ -0,0 +1,10 @@ +/** + * A no-operation function that does nothing. + * This can be used as a placeholder or default function. + * + * @example + * noop(); // Does nothing + * + * @returns {void} This function does not return anything. + */ +export function noop(): void {}