Skip to content

Commit

Permalink
feat(utils): invert, identity 유틸 함수 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 committed Jun 9, 2024
1 parent ddd8202 commit d9e322d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-worms-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-kit/utils': minor
---

feat(utils): invert, identity 유틸 함수 추가 - @ssi02014
71 changes: 71 additions & 0 deletions packages/utils/src/common/identity/identity.spec.ts
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>();
});
});
1 change: 1 addition & 0 deletions packages/utils/src/common/identity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const identity = <T>(value: T) => value;
1 change: 1 addition & 0 deletions packages/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './getUniqId';
export * from './getUniqTime';
export * from './getViewportSize';
export * from './hexToRgba';
export * from './identity';
export * from './noop';
export * from './wrapInArray';
export * from './parseJson';
15 changes: 15 additions & 0 deletions packages/utils/src/object/invert/index.ts
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;
};
41 changes: 41 additions & 0 deletions packages/utils/src/object/invert/invert.spec.ts
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' });
});
});

0 comments on commit d9e322d

Please sign in to comment.