-
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.
feat(utils): invert, identity 유틸 함수 추가
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@modern-kit/utils': minor | ||
--- | ||
|
||
feat(utils): invert, identity 유틸 함수 추가 - @ssi02014 |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { identity } from '.'; | ||
|
||
describe('identity', () => { | ||
it('should return the same string value', () => { | ||
const value = 'test'; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<string>(); | ||
}); | ||
|
||
it('should return the same number value', () => { | ||
const value = 42; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<number>(); | ||
}); | ||
|
||
it('should return the same boolean value', () => { | ||
const value = true; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<boolean>(); | ||
}); | ||
|
||
it('should return the same object reference', () => { | ||
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', () => { | ||
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', () => { | ||
const value = () => 'function'; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<() => string>(); | ||
}); | ||
|
||
it('should return the same value for null', () => { | ||
const value = null; | ||
const result = identity(value); | ||
|
||
expect(result).toBe(value); | ||
expectTypeOf(result).toEqualTypeOf<null>(); | ||
}); | ||
|
||
it('should return the same value for 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const identity = <T>(value: T) => value; |
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { identity } from '../../common'; | ||
|
||
export const invert = ( | ||
obj: Record<PropertyKey, any>, | ||
keyTransformer: (value: any) => PropertyKey = identity | ||
) => { | ||
const invertedObj: Record<PropertyKey, any> = {}; | ||
|
||
Object.keys(obj).forEach((key) => { | ||
const value = obj[key]; | ||
invertedObj[keyTransformer(value)] = key; | ||
}); | ||
|
||
return invertedObj; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { invert } from '.'; | ||
|
||
describe('invert', () => { | ||
it('should invert the keys and values of an object', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const result = invert(obj); | ||
|
||
expect(result).toEqual({ 1: 'a', 2: 'b', 3: 'c' }); | ||
}); | ||
|
||
it('should use the keyTransformer if provided', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const keyTransformer = (value: number) => `key_${value}`; | ||
const result = invert(obj, keyTransformer); | ||
|
||
expect(result).toEqual({ key_1: 'a', key_2: 'b', key_3: 'c' }); | ||
}); | ||
|
||
it('should handle an empty object', () => { | ||
const obj = {}; | ||
const result = invert(obj); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should handle objects with duplicate values', () => { | ||
const obj = { a: 1, b: 1, c: 2 }; | ||
const result = invert(obj); | ||
|
||
expect(result).toEqual({ 1: 'b', 2: 'c' }); | ||
}); | ||
|
||
it('should skip properties not directly on the object', () => { | ||
const obj = Object.create({ inherited: 'inherited' }); | ||
obj.a = 1; | ||
obj.b = 2; | ||
const result = invert(obj); | ||
|
||
expect(result).toEqual({ 1: 'a', 2: 'b' }); | ||
}); | ||
}); |