Skip to content

is 函数

is 函数 判断是否满足条件

is 检查给定的值是否为指定的类型

javascript
/**
 * @description is 检查给定的值是否为指定的类型
 * @param {unknown} val - 要检查的值
 * @param {string} type - 期望的类型名称(例如 'Object', 'String')
 * @returns {boolean} 如果值的类型与指定的类型匹配,则返回 true,否则返回 false
 * @example
 * is('aaa', 'string') // true
 * is(111, 'String') // false
 */

isDef 检查给定的值是否已定义

typescript
/**
 * @description isDef 检查给定的值是否已定义
 * @param {<T = unknown>} val - 要检查的值
 * @returns {boolean} 如果值已定义,则返回 true,否则返回 false
 * @example
 * const a = undefined
 * isDef(a) // false
 * const b = 'aaa'
 * isDef(b) // true
 */

isUnDef 检查给定的值是否未定义

typescript
/**
 * @description isUnDef 检查给定的值是否未定义
 * @param {<T = unknown>} val - 要检查的值
 * @returns {boolean} 如果值未定义,则返回 true,否则返回 false
 * @example
 * const a = undefined
 * isDef(a) // true
 * const b = 'aaa'
 * isDef(b) // false
 */

isObject 检查给定的值是否为对象

typescript
/**
 * @description isObject 检查给定的值是否为对象
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为对象,则返回 true,否则返回 false
 * @example
 * const a = {}
 * isObject(a) // true
 * const b = 111
 * isObject(b) // false
 */

isEmpty 检查给定的值是否为空

typescript
/**
 * @description isEmpty 检查给定的值是否为空
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为空,则返回 true,否则返回 false
 * @example
 * isEmpty([]) // true
 * isEmpty({}) // true
 * isEmpty('') // true
 * isEmpty(undedfined) // true
 * isEmpty(null) // true
 * isEmpty(false) // false
 * isEmpty(1) // false
 * isEmpty([1]) // false
 * isEmpty({ a: 1 }) // false
 */

isDate 检查给定的值是否为日期对象

typescript
/**
 * @description isDate 检查给定的值是否为日期对象
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为日期对象,则返回 true,否则返回 false
 * @example
 * isDate(new Date()) // true
 * isDate(new Date('2023-01-01')) // true
 */

isNull 检查给定的值是否为 null

typescript
/**
 * @description isNull 检查给定的值是否为 null
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为 null,则返回 true,否则返回 false
 * @example
 * isNull(null) // true
 * isNull(1) // false
 * isNull(undefined) // false
 * isNull('') // false
 */

isNullOrUnDef 检查给定的值是否为 null 或未定义

typescript
/**
 * @description isNullOrUnDef 检查给定的值是否为 null 或未定义
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为 null 或未定义,则返回 true,否则返回 false
 * @example
 * let data = {
 *  a: '1',
 *  b: null,
 *  c: undefined,
 *  d: '',
 *  e: {},
 *  f: []
 * }
 * isNullOrUnDef(data.a) // false
 * isNullOrUnDef(data.b) // true
 * isNullOrUnDef(data.c) // true
 * isNullOrUnDef(data.d) // false
 * isNullOrUnDef(data.e) // false
 * isNullOrUnDef(data.f) // false
 */

isNumber 检查给定的值是否为数字

typescript
/**
 * @description isNumber 检查给定的值是否为数字
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为数字,则返回 true,否则返回 false
 * @example
 * isNumber(1) // true
 * isNumber('1') // false
 */

isPromise 检查给定的值是否为 Promise 对象

typescript
/**
 * @description isPromise 检查给定的值是否为 Promise 对象
 * @param {<T = unknown>} val - 要检查的值
 * @returns {boolean} 如果值为 Promise 对象,则返回 true,否则返回 false
 * @example
 * const p1 = new Promise((resolve) => resolve(1))
 * const p2 = {
 *  then: () => {},
 *  catch: () => {},
 *  [Symbol.toStringTag]: 'Promise' // 设置类型标识
 * }
 * async function testAsync() {
 *  return 42
 * }
 * const p3 = testAsync()
 * const p4 = () => {
 *  const obj = { then: () => {}, catch: () => {} }
 *  // 关键步骤:用 defineProperty 设置类型标识
 *  Object.defineProperty(obj, Symbol.toStringTag, {
 *    value: 'Promise',
 *    configurable: true
 *  })
 *  return obj
 * }
 * isPromise(p1) // true
 * isPromise(p2) // true
 * isPromise(p3) // true
 * isPromise(p4()) // true
 */

isString 检查给定的值是否为字符串

typescript
/**
 * @description isString 检查给定的值是否为字符串
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为字符串,则返回 true,否则返回 false
 * @example
 * isString('aaa') // true
 * isString(111) // false
 */

isFunction 检查给定的值是否为函数

typescript
/**
 * @description isFunction 检查给定的值是否为函数
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为函数,则返回 true,否则返回 false
 * @example
 * isFunction(() => {}) // true
 * isFunction(function () {}) // true
 * isFunction('aaa') // false
 */

isBoolean 检查给定的值是否为布尔值

typescript
/**
 * @description isBoolean 检查给定的值是否为布尔值
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为布尔值,则返回 true,否则返回 false
 * @example
 * isBoolean(true) // true
 * isBoolean(false) // true
 * isBoolean('true') // false
 * isBoolean(1) // false
 * isBoolean(0) // false
 * isBoolean(null) // false
 * isBoolean(undefined) // false
 */

isRegExp 检查给定的值是否为正则表达式对象

typescript
/**
 * @description isRegExp 检查给定的值是否为正则表达式对象
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为正则表达式对象,则返回 true,否则返回 false
 * @example
 * isRegExp(/abc/) // true
 * isRegExp(new RegExp('abc')) // true
 * isRegExp('abc') // false
 * isRegExp(123) // false
 */

isArray 检查给定的值是否为数组

typescript
/**
 * @description isArray 检查给定的值是否为数组
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为数组,则返回 true,否则返回 false
 * @example
 * isArray([1, 2, 3]) // true
 * isArray({}) // false
 * isArray(123) // false
 */

isWindow 检查给定的值是否为 Window 对象

typescript
/**
 * @description isWindow 检查给定的值是否为 Window 对象
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为 Window 对象,则返回 true,否则返回 false
 * @example
 * isWindow(window) // true
 * isWindow(document) // false
 */

isElement 检查给定的值是否为 真实 DOM 元素

typescript
/**
 * @description isElement 检查给定的值是否为 真实DOM 元素
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为 真实DOM 元素,则返回 true,否则返回 false
 * @example
 * isElement(document.createElement('div')) // true
 * isElement(document.body) // true
 * isElement(document) // false
 * isElement(document.createTextNode('text')) // false
 * isElement(document.createComment('comment')) // false
 * isElement({ tagName: 'div' }) // false
 * isElement(null) // false
 */

isVNode 检查给定的值是否为 vue 虚拟 Dom VNode 对象 vue3 环境

typescript
/**
 * @description isVNode 检查给定的值是否为 vue虚拟Dom VNode 对象 vue3环境
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为 vue虚拟Dom VNode 对象,则返回 true,否则返回 false
 * isVNode(h('div')) // true
 */

isMap 检查给定的值是否为 Map 对象

typescript
/**
 * @description isMap 检查给定的值是否为 Map 对象
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为 Map 对象,则返回 true,否则返回 false
 * @example
 * isMap(new Map()) // true
 * isMap(new Set()) // false
 * isMap(new Object()) // false
 * isMap(new WeakMap()) // false
 */

isServer 检查当前环境是否为服务器端

typescript
/**
 * @description isServer 检查当前环境是否为服务器端
 * @returns {boolean} 如果当前环境为服务器端,则返回 true,否则返回 false
 * @example
 * console.log(isServer) // true||false
 */

isClient 检查当前环境是否为客户端

typescript
/**
 * @description isClient 检查当前环境是否为客户端
 * @returns {boolean} 如果当前环境为客户端,则返回 true,否则返回 false
 * @example
 * console.log(isClient) // true || false
 */

isDark 检查当前系统是否为暗色模式

typescript
/**
 * @description isDark 检查当前系统是否为暗色模式
 * @returns {boolean} 如果当前系统为暗色模式,则返回 true,否则返回 false
 * @example
 * console.log(isDark()) // true || false
 */

isImgPath 检查给定的路径是否为图片链接

typescript
/**
 * @description isImgPath 检查给定的路径是否为图片链接
 * @param {string} path - 要检查的路径
 * @returns {boolean} 如果路径为图片链接,则返回 true,否则返回 false
 * @example
 * isImgPath('https://example.com/image.jpg') // true
 * isImgPath('https://example.com/image.png') // true
 * isImgPath('https://example.com/image.gif') // true
 * isImgPath('https://example.com/image.bmp') // false
 * isImgPath('https://example.com/image.txt') // false
 * isImgPath('https://example.com/image.html') // false
 * ......
 */

isEmptyVal 检查给定的值是否为空值(空字符串、null 或 undefined)

typescript
/**
 * @description isEmptyVal 检查给定的值是否为空值(空字符串、null 或 undefined)
 * @param {unknown} val - 要检查的值
 * @returns {boolean} 如果值为空值,则返回 true,否则返回 false
 * @example
 * isEmptyVal('') // true
 * isEmptyVal(null) // true
 * isEmptyVal(undefined) // true
 * isEmptyVal(0) // false
 * isEmptyVal(' ') // false
 */

hasKey 检查对象中是否存在指定的键

typescript
/**
 * @description hasKey 检查对象中是否存在指定的键
 * @param {T extends object} obj - 要检查的对象
 * @param {K extends number | string | symbol} key - 要检查的键
 * @returns {boolean} 如果对象中存在指定的键,则返回 true,否则返回 false
 * @example
 * hasKey({ a: 1, b: 2 }, 'a') // true
 * hasKey({ a: 1, b: 2 }, 'c') // false
 */

isLessThanIntMax 检查给定的值是否小于 int 类型的最大值( 2147483647 )结合后端的限制

typescript
/**
 * @description isLessThanIntMax 检查给定的值是否小于 int 类型的最大值( 2147483647 )结合后端的限制
 * @param {number|string} val - 要检查的值
 * @returns {boolean} 如果值小于 int 类型的最大值,则返回 true,否则返回 false
 * @example
 * isLessThanIntMax(2147483647) // true
 * isLessThanIntMax(2147483648) // false
 */

isHexColor 判断是否为十六进制颜色值

  • 输入形式可为 #fff000 #f00
typescript
/**
 * @description isHexColor 判断是否为十六进制颜色值.
 * 输入形式可为 #fff000 #f00
 *
 * @param  {string}  color   十六进制颜色值
 * @return {boolean}  Boolean
 * @example
 * isHexColor('#fff000') // true
 * isHexColor('#f00') // true
 * isHexColor('fff000') // false
 */

isRgbColor 检查是否为 RGB 色值

typescript
/**
 * @description isRgbColor 检查是否为RGB色值
 * @param {string} color - 色值
 * @returns {boolean} Boolean
 * @example
 * isRgbColor('rgb(255, 0, 0)') // true
 * isRgbColor('rgb(255 0 0)') // true
 * isRgbColor('Rgb(255,0,0)') // true
 * isRgbColor('Rgb(100% 50% 30% / 0.5)') // true
 * isRgbColor('rgb(255 0 0 / 0.5)') // true
 * isRgbColor('rgb(255 0 0 / 50%)') // true
 * isRgbColor('rgba(255, 0, 0)') // false
 */

isRgbaColor 检查是否为 RGBA 色值

typescript
/**
 * @description isRgbaColor 检查是否为RGBA色值
 * @param {string} color - 色值
 * @returns {boolean} Boolean
 * @example
 * isRgbaColor('rgba(255, 0, 0, 0.5)') // true
 * isRgbaColor('rgba(100%, 50%, 30%, 0.5)') //true
 * isRgbaColor('rgba(255 255 0 / 100%)') //true
 * isRgbaColor('rgba(100% 50% 0% / 0.5)') //true
 */

isHslColor 检查是否为 HSL 色值

typescript
/**
 * @description isHslColor 检查是否为HSL色值
 * @param {string} color - 色值
 * @returns {boolean} boolean
 * @example
 * isHslColor('hsl(0, 100%, 50%)') // true
 * isHslColor('hsl(360 100% 50% / 0.5)') // true
 * isHslColor('hsl(360 100% 50% / 50%)') // true
 * isHslColor('hsl(-180 100% 50% / 0.5)') // true
 * isHslColor('hsl(-180deg 100% 50% / 0.5)') // true
 * isHslColor('hsl(-180grad 100% 50% / 0.5)') // true
 * ......
 */

isHslaColor 检查是否为 HSLA 色值

typescript
/**
 * @description isHslaColor 检查是否为HSLA色值
 * @param {string} color - 色值
 * @returns {boolean}  boolean
 * @example
 * isHslaColor('hsla(0, 100%, 50%, 0.5)') // true
 * isHslaColor('hsla(0 100% 50% / 0.5)') // true
 * isHslaColor('hsla(-180 100% 50% / 50%)') // true
 * isHslaColor('hsla(-180deg 100% 50% / 50%)') // true
 * isHslaColor('hsla(-180grad 100% 50% / 50%)') // true
 * isHslaColor('hsla(-180rad 100% 50% / 50%)') // true
 * isHslaColor('hsla(-180turn 100% 50% / 50%)') // true
 * ......
 */

isCssNameColor 检查是否为 css Name 色值

typescript
/**
 * @description isCssNameColor 检查是否为css Name色值
 * @param {string} color - 色值
 * @returns {boolean} Boolean
 * @example
 * isCssNameColor('red') // true
 * isCssNameColor('blue') // true
 */

isColor 检查是否为色值

typescript
/**
 * @description isColor 检查是否为色值
 * @param {string} color - 色值
 * @returns {boolean} Boolean
 * @example
 * isColor('red') // true
 * isColor('#fff') // true
 * isColor('rgb(255, 0, 0)') // true
 * isColor('rgba(255, 0, 0, 0.5)') // true
 * isColor('hsl(360,100%,50%)') // true
 * isColor('hsla(360,100%,50%,0.5)') // true
 * ......
 */

isBase64 检查字符串是否为 Base64 编码的图片

此函数主要用于验证传入的字符串是否符合 Base64 编码的图片格式它通过使用正则表达式来检测字符串的格式

typescript
/**
 * @description isBase64 检查字符串是否为Base64编码的图片
 *
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是Base64编码的图片,则返回true;否则返回false
 * @example
 * isBase64('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJ) // true
 */

isEmail 检查字符串是否为 email 格式

typescript
/**
 * @description isEmail 检查字符串是否为email格式
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是email格式,则返回true;否则返回false
 * @example
 * isEmail('test@example.com') // true
 */

isPhoneNumber 检查字符串是否为手机号码格式 境内

typescript
/**
 * @description isPhoneNumber 检查字符串是否为手机号码格式 境内
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是手机号码格式,则返回true;否则返回false
 * @example
 * isPhoneNumber('13812345678') // true
 * isPhoneNumber('+8613812345678') // true
 */

isChinese 检查字符串是否为汉字

typescript
/**
 * @description isChinese 检查字符串是否为汉字
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是汉字,则返回true;否则返回false
 * @example
 * isChinese('中国') // true
 * isChinese('chinese') // false
 */

isIdCard 检查字符串是否为身份证号 包括 15 位和 18 位 中国

typescript
/**
 * @description isIdCard 检查字符串是否为身份证号 包括15位和18位 中国
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是身份证号,则返回true;否则返回false
 * @example
 * isIdCard('32****1990****1***') // true 用自己的身份证测试
 */

isQQ 检查字符串是否为 QQ 号码

typescript
/**
 * @description isQQ 检查字符串是否为QQ号码
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是QQ,则返回true;否则返回false
 * @example
 * isQQ('1422000000') // true
 */

isPostalCode 检查字符串是否为中国邮政编码

typescript
/**
 * @description isPostalCode 检查字符串是否为中国邮政编码
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是邮政编码,则返回true;否则返回false
 * @example
 * isPostalCode('100000') // true
 */

isIPv4 字符串是否为 IP-v4 地址

typescript
/**
 * @description isIPv4 字符串是否为IP-v4地址
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是IP-v4地址,则返回true;否则返回false
 * @example
 * isIPv4('192.168.0.1') // true
 */

isIPv6 字符串是否为 IP-v6 地址

typescript
/**
 * @description isIPv6 字符串是否为IP-v6地址
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是IP-v6地址,则返回true;否则返回false
 * @example
 * isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334') // true
 */

isHexNumber 检查字符串是否为 16 进制的数字

typescript
/**
 * @description isHexNumber 检查字符串是否为16进制的数字
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果字符串是16进制的数字,则返回true;否则返回false
 * @example
 * isHexNumber('0xFF') // true
 */

isJson 检查数据源是否为 json 数据源 字符串

typescript
/**
 * @description isJson 检查数据源是否为json数据源 字符串
 * @param {string} str 待验证的字符串
 * @returns {boolean} 如果数据源是json数据,则返回true;否则返回false
 * @example
 * isJson('{"name":"John","age":30,"city":"New York"}') // true
 */

isCron 检查字符串是否为 cron 表达式

typescript
/**
 * @description isCron 检查字符串是否为cron表达式
 * @param str 待验证的cron表达式
 * @returns 如果字符串是cron表达式,则返回true;否则返回false
 * @example
    ┌──────────── [可选] 秒 (0 - 59)
    | ┌────────── 分钟 (0 - 59)
    | | ┌──────── 小时 (0 - 23)
    | | | ┌────── 天数 (1 - 31)
    | | | | ┌──── 月份 (1 - 12) OR jan,feb,mar,apr ...
    | | | | | ┌── 星期几 (0 - 6, 星期天 = 0) OR sun,mon ...
    | | | | | |
    * * * * * * 命令
 * 例如: isCron('0 0 * * *') // true
 */
typescript
/**
 * @description isVue3 检查是否为Vue3环境 不完善 继续完善中
 * @returns {boolean} 如果是Vue3环境,则返回true;否则返回false
 * @example
 * isVue3() // true
 */
typescript
/**
 * @description isVue2 检查是否为Vue2环境  不完善 继续完善中
 * @returns {boolean} 如果是Vue2环境,则返回true;否则返回false
 * @example
 * isVue2() // true
 */
typescript
/**
 * @description isVue 检查是否为Vue环境
 * @returns {boolean} 如果是Vue环境,则返回true;否则返回false
 * @example
 * isVue() // true
 */
typescript
/**
 * @description isWindows 检查给定的值是否为 windows 系统
 * @returns {boolean} 如果是 windows 系统,则返回 true,否则返回 false
 * @example
 * isWindows() // true
 * isWindows() // false
 */
typescript
/**
 * @description isMac 检查给定的值是否为 mac 系统
 * @returns {boolean} 如果是 mac 系统,则返回 true,否则返回 false
 * @example
 * isMac() // true
 * isMac() // false
 */
typescript
/**
 * @description isIOS 检查给定的值是否为 ios 系统
 * @returns {boolean} 如果是 ios 系统,则返回 true,否则返回 false
 * @example
 * isIOS() // true
 * isIOS() // false
 */
typescript
/**
 * @description isAndroid 检查给定的值是否为 android 系统
 * @returns {boolean} 如果是 android 系统,则返回 true,否则返回 false
 * @example
 * isAndroid() // true
 * isAndroid() // false
 */
typescript
/**
 * @description isLinux 检查给定的值是否为 linux 系统
 * @returns {boolean} 如果是 linux 系统,则返回 true,否则返回 false
 * @example
 * isLinux() // true
 * isLinux() // false
 */
typescript
/**
 * @description isTablet 检查给定的值是否为 平板 设备
 * @returns {boolean} 如果是 平板 设备,则返回 true,否则返回 false
 * @example
 * isTablet() // true
 * isTablet() // false
 */
typescript
/**
 * @description isMobile 检查给定的值是否为移动设备(手机)
 * @returns {boolean} 如果是移动设备(非平板),则返回 true,否则返回 false
 */
typescript
/**
 * @description isPC 检查给定的值是否为 PC 设备
 * @returns {boolean} 如果是 PC 设备,则返回 true,否则返回 false
 * @example
 * isPC() // true
 * isPC() // false
 */

在MIT许可下发布    备案号: 晋ICP备2024051569号-1