Some functional/semantic codes for TypeScript(JavaScript) development.
$ yarn add tslang-utils
Whether value isn't undefined
or null
.
const a = { name: 'github user', bio: null }
isDef(a.name) // true
isDef(a.age) // false
isDef(a.bio) // false
Whether value is Function
type
isFunc(() => {}) // true
isFunc(true) // false
isString('') // true
isString(true) // false
Whether value is Number
type. It will omit NaN
when we set second parameter to true
.
isNumber(0) // true
isNumber(NaN) // true
isNumber(NaN, true) // false
Equivalent to isNumber(/* value */, true)
.
Whether an object has a property with the specified name.
const a = { name: 'github user', bio: null }
hasOwnProp(a, 'name') // true
hasOwnProp(a, 'toString') // false
Whether current JS runtime is in the server(node.js) environment.
isSever() // boolean
Whether current JS runtime is in the browser environment.
isBrowser() // boolean
Whether we are in Windows
import { isWin } from 'tslang-utils'
isWin() // boolean
Whether we are in linux
import { isLinux } from 'tslang-utils'
isLinux() // boolean
Whether we are in mac os
import { isMac } from 'tslang-utils'
isMac() // boolean
get first element type in array
import { First } from 'tslang-utils/dist/type'
type FirstElement = First<[1, 2, 3, 4, 5]> // 1
get last element type in array
import { Last } from 'tslang-utils/dist/type'
type LastElement = Last<1, 2, 3, 4, 5> // 5
infer fulfilled promise instance value
import { PromiseValue } from 'tslang-utils/dist/type'
type promiseValue = PromiseValue<Promise<boolean>> // boolean