-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): isPrimitive 함수 추가 및 Primitive, Reference 유틸 타입 추가
- Loading branch information
Showing
10 changed files
with
193 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,6 @@ | ||
--- | ||
'@modern-kit/types': minor | ||
'@modern-kit/utils': minor | ||
--- | ||
|
||
feat(utils): isPrimitive 함수 추가 및 Primitive, Reference 유틸 타입 추가 - @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,39 @@ | ||
# isPrimitive | ||
|
||
주어진 인자가 `원시값`인지 검사하고, 맞다면 인자의 타입을 `Primitive`로 좁혀주는 함수입니다. | ||
|
||
<br /> | ||
|
||
## Code | ||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/utils/src/validator/isPrimitive/index.ts) | ||
|
||
## Interface | ||
```ts title="typescript" | ||
type Primitive = | ||
| string | ||
| number | ||
| boolean | ||
| symbol | ||
| bigint | ||
| null | ||
| undefined; | ||
|
||
const isPrimitive: (value: unknown) => value is Primitive | ||
``` | ||
## Usage | ||
```ts title="typescript" | ||
import { isPrimitive } from '@modern-kit/utils'; | ||
|
||
isPrimitive(123); // true | ||
isPrimitive('123'); // true | ||
isPrimitive(true); // true | ||
isPrimitive(Symbol()); // true | ||
isPrimitive(null); // true | ||
isPrimitive(undefined); // true | ||
|
||
isPrimitive({}); // false | ||
isPrimitive([]); // false | ||
isPrimitive(new Set()); // false | ||
isPrimitive(new Map()); // false | ||
``` |
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,35 @@ | ||
import { Primitive } from '.'; | ||
|
||
describe('Primitive', () => { | ||
it('should properly infer the type', () => { | ||
const value = 1 as Primitive; | ||
|
||
if (typeof value === 'number') { | ||
expectTypeOf(value).toEqualTypeOf<number>(); | ||
} | ||
|
||
if (typeof value === 'string') { | ||
expectTypeOf(value).toEqualTypeOf<string>(); | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
expectTypeOf(value).toEqualTypeOf<boolean>(); | ||
} | ||
|
||
if (typeof value === 'symbol') { | ||
expectTypeOf(value).toEqualTypeOf<symbol>(); | ||
} | ||
|
||
if (typeof value === 'bigint') { | ||
expectTypeOf(value).toEqualTypeOf<bigint>(); | ||
} | ||
|
||
if (value === null) { | ||
expectTypeOf(value).toEqualTypeOf<null>(); | ||
} | ||
|
||
if (value === undefined) { | ||
expectTypeOf(value).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,8 @@ | ||
export type Primitive = | ||
| string | ||
| number | ||
| boolean | ||
| symbol | ||
| bigint | ||
| null | ||
| 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,47 @@ | ||
import { Reference } from '.'; | ||
|
||
describe('Reference', () => { | ||
it('should properly infer the type', () => { | ||
const value = {} as Reference; | ||
|
||
if (typeof value === 'object') { | ||
expectTypeOf(value).toEqualTypeOf<Exclude<Reference, Function>>(); | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
expectTypeOf(value).toEqualTypeOf<any[]>(); | ||
} | ||
|
||
if (typeof value === 'function') { | ||
expectTypeOf(value).toEqualTypeOf<Function | ((...args: any[]) => any)>(); | ||
} | ||
|
||
if (value instanceof Set) { | ||
expectTypeOf(value).toEqualTypeOf<Set<any>>(); | ||
} | ||
|
||
if (value instanceof Map) { | ||
expectTypeOf(value).toEqualTypeOf<Map<any, any>>(); | ||
} | ||
|
||
if (value instanceof WeakMap) { | ||
expectTypeOf(value).toEqualTypeOf<WeakMap<object, any>>(); | ||
} | ||
|
||
if (value instanceof WeakSet) { | ||
expectTypeOf(value).toEqualTypeOf<WeakSet<object>>(); | ||
} | ||
|
||
if (value instanceof Date) { | ||
expectTypeOf(value).toEqualTypeOf<Date>(); | ||
} | ||
|
||
if (value instanceof RegExp) { | ||
expectTypeOf(value).toEqualTypeOf<RegExp>(); | ||
} | ||
|
||
if (value instanceof Error) { | ||
expectTypeOf(value).toEqualTypeOf<Error>(); | ||
} | ||
}); | ||
}); |
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,11 @@ | ||
export type Reference = | ||
| object | ||
| any[] | ||
| ((...args: any[]) => any) | ||
| Set<any> | ||
| Map<any, any> | ||
| WeakMap<object, any> | ||
| WeakSet<object> | ||
| Date | ||
| RegExp | ||
| Error; |
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
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 @@ | ||
import { Primitive } from '@modern-kit/types'; | ||
|
||
export const isPrimitive = (value: unknown): value is Primitive => { | ||
return Object(value) !== value; | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/utils/src/validator/isPrimitive/isPrimitive.spec.ts
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,39 @@ | ||
import { Primitive } from '@modern-kit/types'; | ||
import { isPrimitive } from '.'; | ||
import { isNumber } from '../isNumber'; | ||
import { isString } from '../isString'; | ||
|
||
describe('isPrimitive', () => { | ||
it('should return true if the value is a primitive type, and false if it is a reference type', () => { | ||
expect(isPrimitive(null)).toBeTruthy(); | ||
expect(isPrimitive(undefined)).toBeTruthy(); | ||
expect(isPrimitive(1)).toBeTruthy(); | ||
expect(isPrimitive(BigInt(1))).toBeTruthy(); | ||
expect(isPrimitive('')).toBeTruthy(); | ||
expect(isPrimitive(false)).toBeTruthy(); | ||
expect(isPrimitive(Symbol())).toBeTruthy(); | ||
|
||
expect(isPrimitive([])).toBeFalsy(); | ||
expect(isPrimitive({})).toBeFalsy(); | ||
expect(isPrimitive(new Set())).toBeFalsy(); | ||
expect(isPrimitive(new Map())).toBeFalsy(); | ||
}); | ||
|
||
it('should properly infer the type', () => { | ||
const value = 1 as unknown; | ||
|
||
if (isPrimitive(value)) { | ||
expectTypeOf(value).toEqualTypeOf<Primitive>(); | ||
|
||
if (isNumber(value)) { | ||
expectTypeOf(value).toEqualTypeOf<number>(); | ||
} | ||
|
||
if (isString(value)) { | ||
expectTypeOf(value).toEqualTypeOf<string>(); | ||
} | ||
} else { | ||
expectTypeOf(value).toEqualTypeOf<unknown>(); | ||
} | ||
}); | ||
}); |