Skip to content

Commit

Permalink
✨ Add is utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 15, 2021
1 parent 4e7edee commit b82fada
Show file tree
Hide file tree
Showing 26 changed files with 862 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const STRING = 'string'
export const NUMBER = 'number'
export const UNDEFINED = 'undefined'
export const SYMBOL = 'symbol'
export const BOOLEAN = 'boolean'
export const OBJECT = 'object'
export const FUNCTION = 'function'
export const NULL = null
export const BIGINT = 'bigint'
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
export { add } from '@/add'
export { divide } from '@/divide'
export type { IsBigint } from '@/isBigint'
export { isBigint } from '@/isBigint'
export type { IsNill } from '@/isNill'
export { isNill } from '@/isNill'
export type { IsNull } from '@/isNull'
export { isNull } from '@/isNull'
export type { IsNumber } from '@/isNumber'
export { isNumber } from '@/isNumber'
export type { IsString } from '@/isString'
export { isString } from '@/isString'
export type { IsSymbol } from '@/isSymbol'
export { isSymbol } from '@/isSymbol'
export { multiply } from '@/multiply'
export { subtract } from '@/subtract'
export type { AnyFn } from '@/types'
24 changes: 24 additions & 0 deletions src/isBigint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BIGINT } from '@/constants'

/**
* Bigint or not
*
* @typeParam T - any value
*/
type IsBigint<T extends unknown> = T extends bigint ? true : false

/**
* Whatever argument is type of bigint or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'bigint'`
*
* @example
* isBigint(1n) // true
* isBigint(1000) // false
*/
const isBigint = <T extends unknown>(val: T): IsBigint<T> =>
(typeof val === BIGINT) as IsBigint<T>

export { isBigint }
export type { IsBigint }
24 changes: 24 additions & 0 deletions src/isBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BOOLEAN } from '@/constants'

/**
* Boolean or not
*
* @typeParam T - any value
*/
type IsBoolean<T extends unknown> = T extends boolean ? true : false

/**
* Whatever argument is type of boolean or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'boolean'`
*
* @example
* isBoolean(true) // true
* isBoolean('hello') // false
*/
const isBoolean = <T extends unknown>(val: T): IsBoolean<T> =>
(typeof val === BOOLEAN) as IsBoolean<T>

export { isBoolean }
export type { IsBoolean }
25 changes: 25 additions & 0 deletions src/isFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FUNCTION } from '@/constants'
import type { AnyFn } from '@/types'

/**
* Function or not
*
* @typeParam T - any value
*/
type IsFunction<T extends unknown> = T extends AnyFn ? true : false

/**
* Whatever argument is type of function or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'function'`
*
* @example
* isFunction(function) // true
* isFunction('hello') // false
*/
const isFunction = <T extends unknown>(val: T): IsFunction<T> =>
(typeof val === FUNCTION) as IsFunction<T>

export { isFunction }
export type { IsFunction }
29 changes: 29 additions & 0 deletions src/isNill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { IsNull } from '@/isNull'
import { isNull } from '@/isNull'
import { IsUndefined, isUndefined } from '@/isUndefined'
/**
* Undefiled or null, or not
*
* @typeParam T - any value
*/
type IsNill<T extends unknown> = IsUndefined<T> extends true
? true
: IsNull<T> extends true
? true
: false

/**
* Whatever argument is type of undefined or null.
*
* @param val - input any value
* @returns The result of type of `val` is undefined or null
*
* @example
* isNumber(0) // true
* isNumber('hello') // false
*/
const isNill = <T extends unknown>(val: T): IsNill<T> =>
((isUndefined(val) || isNull(val)) as unknown) as IsNill<T>

export { isNill }
export type { IsNill }
24 changes: 24 additions & 0 deletions src/isNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NULL } from '@/constants'

/**
* Null or not
*
* @typeParam T - any value
*/
type IsNull<T extends unknown> = T extends null ? true : false

/**
* Whatever argument is type of null or not.
*
* @param val - input any value
* @returns The result of `val === null`
*
* @example
* isNull(null) // true
* isNull(undefined) // false
*/
const isNull = <T extends unknown>(val: T): IsNull<T> =>
(val === NULL) as IsNull<T>

export { isNull }
export type { IsNull }
24 changes: 24 additions & 0 deletions src/isNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NUMBER } from '@/constants'

/**
* Number or not
*
* @typeParam T - any value
*/
type IsNumber<T extends unknown> = T extends number ? true : false

/**
* Whatever argument is type of number or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'number'`
*
* @example
* isNumber(0) // true
* isNumber('hello') // false
*/
const isNumber = <T extends unknown>(val: T): IsNumber<T> =>
(typeof val === NUMBER) as IsNumber<T>

export { isNumber }
export type { IsNumber }
40 changes: 40 additions & 0 deletions src/isObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { IsPrimitive, isPrimitive } from '@/isPrimitive'

/**
* Object or not
*
* @typeParam T - any value
*/
type IsObject<T extends unknown> = IsPrimitive<T> extends true ? false : true

/**
* Whatever argument is type of object or not.
*
* @param val - input any value
* @returns The result of object or not
*
* @remarks
* Definition of Object
*
* Not Primitive
*
* @remarks
* Definition of Primitive
* - string
* - number
* - bigint
* - boolean
* - symbol
* - undefined
* - null
*
*
* @example
* isObject([]) // true
* isObject('hello') // false
*/
const isObject = <T extends unknown>(val: unknown): IsObject<T> =>
!isPrimitive(val) as IsObject<T>

export { isObject }
export type { IsObject }
53 changes: 53 additions & 0 deletions src/isPrimitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { IsBigint, isBigint } from '@/isBigint'
import { IsBoolean, isBoolean } from '@/isBoolean'
import { IsNill, isNill } from '@/isNill'
import { IsNumber, isNumber } from '@/isNumber'
import { IsString, isString } from '@/isString'
import { IsSymbol, isSymbol } from '@/isSymbol'

/**
* Primitive or not
*
* @typeParam T - any value
*/
type IsPrimitive<T extends unknown> = IsBigint<T> extends true
? true
: IsBoolean<T> extends true
? true
: IsNill<T> extends true
? true
: IsNumber<T> extends true
? true
: IsString<T> extends true
? true
: IsSymbol<T> extends true
? true
: false

/**
* Whatever argument is primitive or not.
*
* @param val - input any value
* @returns The result of primitive or not
*
* @remarks
* Definition of Primitive
* - string
* - number
* - bigint
* - boolean
* - symbol
* - undefined
* - null
*
* @example
* isPrimitive(true) // true
* isPrimitive([]) // false
*/
const isPrimitive = <T extends unknown>(val: T): IsPrimitive<T> =>
[isNill, isBoolean, isNumber, isString, isBigint, isSymbol].some((is) =>
is(val)
) as IsPrimitive<T>

export { isPrimitive }
export type { IsPrimitive }
24 changes: 24 additions & 0 deletions src/isString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { STRING } from '@/constants'

/**
* String or not
*
* @typeParam T - any value
*/
type IsString<T extends unknown> = T extends string ? true : false

/**
* Whatever argument is type of string or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'string'`
*
* @example
* isString('hello world') // true
* isString(1000) // false
*/
const isString = <T extends unknown>(val: T): IsString<T> =>
(typeof val === STRING) as IsString<T>

export { isString }
export type { IsString }
24 changes: 24 additions & 0 deletions src/isSymbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SYMBOL } from '@/constants'

/**
* Symbol or not
*
* @typeParam T - any value
*/
type IsSymbol<T extends unknown> = T extends symbol ? true : false

/**
* Whatever argument is type of symbol or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'symbol'`
*
* @example
* isSymbol(Symbol('hello')) // true
* isSymbol('hello') // false
*/
const isSymbol = <T extends unknown>(val: T): IsSymbol<T> =>
(typeof val === SYMBOL) as IsSymbol<T>

export { isSymbol }
export type { IsSymbol }
24 changes: 24 additions & 0 deletions src/isUndefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { UNDEFINED } from '@/constants'

/**
* Undefined or not
*
* @typeParam T - any value
*/
type IsUndefined<T extends unknown> = T extends undefined ? true : false

/**
* Whatever argument is type of undefined or not.
*
* @param val - input any value
* @returns The result of `typeof val === 'undefined'`
*
* @example
* isUndefined(undefined) // true
* isUndefined('hello') // false
*/
const isUndefined = <T extends unknown>(val: T): IsUndefined<T> =>
(typeof val === UNDEFINED) as IsUndefined<T>

export { isUndefined }
export type { IsUndefined }
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Type of any function
*
* @typeParam T - argument types
*/
export type AnyFn<T = any> = (...args: T[]) => unknown
1 change: 1 addition & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@test/constants'
Loading

0 comments on commit b82fada

Please sign in to comment.