Skip to content

lbwa/tslang-utils

Repository files navigation

tslang utils

Some functional/semantic codes for TypeScript(JavaScript) development.

Pipeline npm (tag) npm bundle size

installation

$ yarn add tslang-utils

type checker

isDef

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

isFunc

Whether value is Function type

isFunc(() => {}) // true
isFunc(true) // false

isString

isString('') // true
isString(true) // false

isNumber

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

isStrictNumber

Equivalent to isNumber(/* value */, true).

hasOwnProp

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

Env

isServer

Whether current JS runtime is in the server(node.js) environment.

isSever() // boolean

isBrowser

Whether current JS runtime is in the browser environment.

isBrowser() // boolean

isWin

Whether we are in Windows

import { isWin } from 'tslang-utils'

isWin() // boolean

isLinux

Whether we are in linux

import { isLinux } from 'tslang-utils'

isLinux() // boolean

isMac

Whether we are in mac os

import { isMac } from 'tslang-utils'

isMac() // boolean

Types

First

get first element type in array

import { First } from 'tslang-utils/dist/type'

type FirstElement = First<[1, 2, 3, 4, 5]> // 1

Last

get last element type in array

import { Last } from 'tslang-utils/dist/type'

type LastElement = Last<1, 2, 3, 4, 5> // 5

PromiseValue

infer fulfilled promise instance value

import { PromiseValue } from 'tslang-utils/dist/type'

type promiseValue = PromiseValue<Promise<boolean>> // boolean

License

MIT © Liu Bowen

About

An opinionated functional/semantic codes for TypeScript(JavaScript) development.

Topics

Resources

License

Stars

Watchers

Forks