npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@txjs/bool

v1.0.5

Published

数据类型校验函数库

Readme

@txjs/bool

用于验证和判断数据类型的实用函数

使用 npm

npm i @txjs/bool

使用 pnpm

pnpm add @txjs/bool

使用 yarn

yarn add @txjs/bool

导入方式

import { is, isNil } from '@txjs/bool'
// 或
import { isNil } from '@txjs/bool/dist/isNil'

函数列表

toString

将值转为字符串。

function toString(value: unknown): string

示例:

toString(123) // "123"
toString(true) // "true"
toString([1, 2, 3]) // "1,2,3"

is

验证 value 值类型

function is(value: unknown, type: string): value is T

示例:

is(123, 'number') // true
is({}, 'object') // true
is([], 'array') // true

isArray

检查 value 是否是 Array 类型

function isArray(value: T): value is T extends Array ? T : never

示例:

isArray([1, 2, 3]) // true
isArray('abc') // false

isBoolean

检查 value 是否是 boolean 类型

function isBoolean(value: unknown): value is boolean

示例:

isBoolean(true) // true
isBoolean('true') // false

isFunction

检查 value 是否是 function 类型

function isFunction(value: unknown): value is (...args: any[]) => any

示例:

isFunction(() => {}) // true
isFunction('string') // false

isAsyncFunction

检查 value 是否是 async function 类型

function isAsyncFunction(value: unknown): value is (...args: any[]) => Promise

示例:

isAsyncFunction(async () => {}) // true
isAsyncFunction(() => {}) // false

isInteger

检查 value 是否是整数,包含 0

function isInteger(value: unknown): value is number

示例:

isInteger(10) // true
isInteger(3.14) // false

isNil

检查 value 是否是 undefinednull 类型

function isNil(value: unknown): value is null | undefined

示例:

isNil(null) // true
isNil(undefined) // true

isNull

检查 value 是否是 null 类型

function isNull(value: unknown): value is null

示例:

isNull(null) // true
isNull(undefined) // false

isNumber

检查 value 是否是 number 类型,值不能为 NaN

function isNumber(value: unknown): value is number

示例:

isNumber(3) // true
isNumber('3') // false

isNumeric

检查 value 是否是 number 类型,支持字符串校验

function isNumeric(value: unknown): value is number | string

示例:

isNumeric(3) // true
isNumeric('3') // true
isNumeric('abc') // false

isPhone

检查 value 是否是手机号码(包含虚拟号段)

function isPhone(value: unknown): value is string

示例:

isPhone('13566667777') // true
isPhone('17012345678') // false

isNonVirtualPhone

检查 value 是否为手机号码(不包含虚拟号段)

function isNonVirtualPhone(value: unknown): value is string

示例:

isNonVirtualPhone('13987654321') // true
isNonVirtualPhone('17712345678') // false

isPlainObject

检查 value 是否是 object 类型,不包含 null 类型

function isPlainObject(value: T): value is T extends Record ? T : never

示例:

isPlainObject({}) // true
isPlainObject(null) // false

isPromise

检查 value 是否是 promise 类型

function isPromise(value: T): value is T extends Promise ? T : never

示例:

isPromise(Promise.resolve()) // true
isPromise({}) // false

isString

检查 value 是否是 string 类型

function isString(value: unknown): value is string

示例:

isString('hello') // true
isString(123) // false

isSymbol

检查 value 是否是 symbol 类型

function isSymbol(value: unknown): value is symbol

示例:

isSymbol(Symbol('symbol')) // true
isSymbol('symbol') // false

isUndefined

检查 value 是否是 undefined 类型

function isUndefined(value: unknown): value is undefined

示例:

isUndefined(undefined) // true
isUndefined(null) // false

isHttpUrl

检查 value 是否是以 httphttps 开头的url

function isHttpUrl(value: unknown): value is string

示例:

isHttpUrl('http://example.com') // true
isHttpUrl('ftp://example.com') // false

isAbsoluteUrl

检查 value 是否是绝对的 URL

function isAbsoluteUrl(value: unknown): boolean

示例:

isAbsoluteUrl('http://example.com') // true
isAbsoluteUrl('/example') // false

isEqual

检查两个值是否相等

function isEqual(value: any, other: any, seen?: WeakMap): boolean

示例:

isEqual({ a: 1 }, { a: 1 }) // true
isEqual([1, 2], [1, 2]) // true
isEqual(1, '1') // false

isEmail

检查 value 是否是 email 格式

export declare function isEmail(value: unknown): boolean

示例:

isEmail('[email protected]') // true
isEmail('example.com') // false

isLandline

检查 value 是否是座机号码

export declare function isLandline(value: unknown): value is string

示例:

isLandline('13215666') // false
isLandline('0592-5966633') // true
isLandline('0592-5966633-123') // true

containsHTML

检查 value 是否是包含 HTML

export declare function containsHTML(value: unknown): value is string

示例:

containsHTML('<div>Test</div>'); // true
containsHTML('<img src='image.jpg' />'); // true
containsHTML('</>'); // false
containsHTML('1 < 2 && 3 > 4'); // false

isNonEmptyObject

检查 value 是否不为空对象

export declare function isNonEmptyObject<T>(value: T): value is T extends Record<any, any> ? T : never;****

示例:

isNonEmptyObject({}) // false
isNonEmptyObject({ a: 'text1' }) // true