-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
24 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,60 @@ | ||
import { describe, it, expect, expectTypeOf } from 'vitest'; | ||
import { describe, it, expect } from 'vitest'; | ||
import { identity } from '.'; | ||
|
||
describe('identity', () => { | ||
it('should return the same string value', () => { | ||
it('동일한 문자열 값을 반환해야 합니다.', () => { | ||
const value = 'test'; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<string>(); | ||
}); | ||
|
||
it('should return the same number value', () => { | ||
it('동일한 숫자 값을 반환해야 합니다.', () => { | ||
const value = 42; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<number>(); | ||
}); | ||
|
||
it('should return the same boolean value', () => { | ||
it('동일한 부울 값을 반환해야 합니다.', () => { | ||
const value = true; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<boolean>(); | ||
}); | ||
|
||
it('should return the same object reference', () => { | ||
it('동일한 객체 참조를 반환해야 합니다.', () => { | ||
const value = { key: 'value' }; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expect(result).toEqual(value); // 참조가 동일한지 확인 | ||
expectTypeOf(result).toEqualTypeOf<{ | ||
key: string; | ||
}>(); | ||
}); | ||
|
||
it('should return the same array reference', () => { | ||
it('동일한 배열 참조를 반환해야 합니다.', () => { | ||
const value = [1, 2, 3]; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expect(result).toEqual(value); // 참조가 동일한지 확인 | ||
expectTypeOf(result).toEqualTypeOf<number[]>(); | ||
}); | ||
|
||
it('should return the same function reference', () => { | ||
it('동일한 함수 참조를 반환해야 합니다.', () => { | ||
const value = () => 'function'; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<() => string>(); | ||
}); | ||
|
||
it('should return the same value for null', () => { | ||
it('동일한 null 값을 반환해야 합니다.', () => { | ||
const value = null; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<null>(); | ||
}); | ||
|
||
it('should return the same value for undefined', () => { | ||
it('동일한 undefined 값을 반환해야 합니다.', () => { | ||
const value = undefined; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<undefined>(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
export function identity<T>(value: T) { | ||
/** | ||
* @description 주어진 값을 변형하지 않고 그대로 반환하는 단순한 유틸리티 함수입니다. | ||
* | ||
* 원본 입력을 그대로 반환하는 함수가 필요한 상황이나 고차 함수의 기본 매개변수로 유용하게 사용할 수 있습니다. | ||
* | ||
* @typeParam T - 입력 값의 타입입니다. | ||
* @param {T} value - 반환할 값입니다. | ||
* @returns {T} 입력된 `value`를 변형 없이 반환합니다. | ||
* | ||
* @example | ||
* const number = identity(42); // 42 | ||
* const text = identity("안녕하세요"); // "안녕하세요" | ||
* const obj = identity({ a: 1 }); // { a: 1 } | ||
*/ | ||
export function identity<T>(value: T): T { | ||
return value; | ||
} |