Skip to content

test 类型检测 ​

17:43

说明 ​

$u.test 是一组纯谓词方法,覆盖类型、数字格式、字符串格式、证件号码、媒体地址、渐变与空值判断,所有方法都返回布尔值。

示例 ​

类型判断 ​

string / number / array / object / func 五个类型谓词各判断一个值,结果以标签展示。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in typeList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

const typeList: TestItem[] = [
    { desc: '$u.test.string("hello")', result: $u.test.string('hello'), last: false },
    { desc: '$u.test.number("123")', result: $u.test.number('123'), last: false },
    { desc: '$u.test.array([1,2,3])', result: $u.test.array([1, 2, 3]), last: false },
    { desc: '$u.test.object({})', result: $u.test.object({}), last: false },
    { desc: '$u.test.func(() => {})', result: $u.test.func((): void => {}), last: true }
];
</script>

数值验证 ​

对比 number(允许小数与负号)、digits(只允许纯数字)与 range(闭区间)对合法和非法输入的判定差异。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in numberList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

const numberList: TestItem[] = [
    { desc: '$u.test.number("123")', result: $u.test.number('123'), last: false },
    { desc: '$u.test.number("-12.34")', result: $u.test.number('-12.34'), last: false },
    { desc: '$u.test.number("12a")', result: $u.test.number('12a'), last: false },
    { desc: '$u.test.digits("123")', result: $u.test.digits('123'), last: false },
    { desc: '$u.test.digits("12.3")', result: $u.test.digits('12.3'), last: false },
    { desc: '$u.test.range(5, 1, 10)', result: $u.test.range(5, 1, 10), last: false },
    { desc: '$u.test.range(15, 1, 10)', result: $u.test.range(15, 1, 10), last: true }
];
</script>

字符串验证 ​

覆盖邮箱、手机号、URL、中文、字母、子串包含与长度区间,其中 letter('abc123') 返回 false。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in stringList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

const stringList: TestItem[] = [
    { desc: '$u.test.email("user@example.com")', result: $u.test.email('user@example.com'), last: false },
    { desc: '$u.test.mobile("13800138000")', result: $u.test.mobile('13800138000'), last: false },
    { desc: '$u.test.url("https://www.example.com")', result: $u.test.url('https://www.example.com'), last: false },
    { desc: '$u.test.chinese("中文")', result: $u.test.chinese('中文'), last: false },
    { desc: '$u.test.letter("abc")', result: $u.test.letter('abc'), last: false },
    { desc: '$u.test.letter("abc123")', result: $u.test.letter('abc123'), last: false },
    { desc: '$u.test.contains("hello world", "world")', result: $u.test.contains('hello world', 'world'), last: false },
    { desc: '$u.test.rangeLength("hello", 2, 10)', result: $u.test.rangeLength('hello', 2, 10), last: true }
];
</script>

证件与号码校验 ​

idCard 校验 18 位身份证号,landline 校验座机号,amount 校验金额格式,长度或格式不符的输入都返回 false。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in regexList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

const regexList: TestItem[] = [
    { desc: '$u.test.idCard("110101199003077777")', result: $u.test.idCard('110101199003077777'), last: false },
    { desc: '$u.test.idCard("12345")', result: $u.test.idCard('12345'), last: false },
    { desc: '$u.test.landline("010-12345678")', result: $u.test.landline('010-12345678'), last: false },
    { desc: '$u.test.landline("010-1234")', result: $u.test.landline('010-1234'), last: false },
    { desc: '$u.test.amount("1,234.56")', result: $u.test.amount('1,234.56'), last: false },
    { desc: '$u.test.amount("1234.567")', result: $u.test.amount('1234.567'), last: false },
    { desc: '$u.test.amount("0.5")', result: $u.test.amount('0.5'), last: true }
];
</script>

结构与类型判定 ​

null 不把空串算作空,code 的第二参数为位数(传 null 时按 6 位),enOrNum 允许空串但拒绝连字符。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in structList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

const structList: TestItem[] = [
    // null 只判断 null/undefined,空串不算
    { desc: '$u.test.null(null)', result: $u.test.null(null), last: false },
    { desc: '$u.test.null("")', result: $u.test.null(''), last: false },
    // code 第二位为位数,null 时按 6 位
    { desc: '$u.test.code("123456", null)', result: $u.test.code('123456', null), last: false },
    { desc: '$u.test.code("1234", 4)', result: $u.test.code('1234', 4), last: false },
    { desc: '$u.test.code("123", null)', result: $u.test.code('123', null), last: false },
    // enOrNum 允许空串
    { desc: '$u.test.enOrNum("abc123")', result: $u.test.enOrNum('abc123'), last: false },
    { desc: '$u.test.enOrNum("abc-123")', result: $u.test.enOrNum('abc-123'), last: false },
    { desc: '$u.test.enOrNum("")', result: $u.test.enOrNum(''), last: true }
];
</script>

媒体与渐变 ​

image 与 video 按扩展名判断,image 会忽略查询串;gradientColor 只认 linear- / radial- / conic-gradient( 开头的值。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in mediaList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

const mediaList: TestItem[] = [
    // image 按扩展名判断,查询串不参与
    { desc: '$u.test.image("https://a.com/x.png?v=1")', result: $u.test.image('https://a.com/x.png?v=1'), last: false },
    { desc: '$u.test.image("a.pdf")', result: $u.test.image('a.pdf'), last: false },
    { desc: '$u.test.video("a.mp4")', result: $u.test.video('a.mp4'), last: false },
    { desc: '$u.test.video("a.m3u8")', result: $u.test.video('a.m3u8'), last: false },
    { desc: '$u.test.video("a.txt")', result: $u.test.video('a.txt'), last: false },
    // gradientColor 需带 linear-/radial-/conic-gradient(
    { desc: '$u.test.gradientColor("linear-gradient(90deg, #fff, #000)")', result: $u.test.gradientColor('linear-gradient(90deg, #fff, #000)'), last: false },
    { desc: '$u.test.gradientColor("#fff")', result: $u.test.gradientColor('#fff'), last: true }
];
</script>

空值判断 ​

empty 把 null、空串、空数组、0 与 false 都判为空,非空字符串则返回 false。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item
            v-for="(item, index) in emptyList"
            :key="index"
            :title="item.desc"
            :arrow="false"
            :border-bottom="!item.last"
        >
            <ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
        </ux-cell-item>
    </ux-cell-group>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

type TestItem = {
    desc: string;
    result: boolean;
    last: boolean;
};

// null / 空串 / 空数组 / 0 / NaN / false / 空对象都算空
const emptyList: TestItem[] = [
    { desc: '$u.test.empty(null)', result: $u.test.empty(null), last: false },
    { desc: '$u.test.empty("")', result: $u.test.empty(''), last: false },
    { desc: '$u.test.empty([])', result: $u.test.empty([] as any[]), last: false },
    { desc: '$u.test.empty(0)', result: $u.test.empty(0), last: false },
    { desc: '$u.test.empty("hello")', result: $u.test.empty('hello'), last: true }
];
</script>

API ​

string ​

参数:

参数说明类型默认值版本
value待判断的值any--

返回值:

类型说明
boolean是否为字符串

number ​

参数:

参数说明类型默认值版本
value待判断的值string--

返回值:

类型说明
boolean是否为合法数字(支持小数与科学计数法)

array ​

参数:

参数说明类型默认值版本
value待判断的值any--

返回值:

类型说明
boolean是否为数组

object ​

参数:

参数说明类型默认值版本
value待判断的值any--

返回值:

类型说明
boolean是否为普通对象(非 null、非数组)

func ​

参数:

参数说明类型默认值版本
value待判断的值any--

返回值:

类型说明
boolean是否为函数

digits ​

参数:

参数说明类型默认值版本
value待判断的值string--

返回值:

类型说明
boolean是否全为 0-9 数字

range ​

参数:

参数说明类型默认值版本
value待判断的数值number--
min区间下限(含)number--
max区间上限(含)number--

返回值:

类型说明
boolean是否落在 [min, max] 闭区间内

email ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否匹配邮箱格式

mobile ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否为大陆 11 位手机号

url ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否为 http(s) URL

chinese ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否全部为中文汉字

letter ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否全部为英文字母,空串返回 true

contains ​

参数:

参数说明类型默认值版本
value待检索的字符串string--
param要查找的子串string--

返回值:

类型说明
boolean是否包含指定子串

rangeLength ​

参数:

参数说明类型默认值版本
value待判断的字符串string--
min最小长度(含)number--
max最大长度(含)number--

返回值:

类型说明
boolean字符串长度是否在 [min, max] 闭区间内

idCard ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否为 18 位身份证号

landline ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否为座机号,格式为 区号-号码,可带分机

amount ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否为合法金额(支持千分位、最多两位小数)

null ​

参数:

参数说明类型默认值版本
value待判断的值any | null--

返回值:

类型说明
boolean是否为 null / undefined;App 端只判断 null

code ​

参数:

参数说明类型默认值版本
value待判断的字符串string--
len数字位数,null 时按 6 位number | nullnull-

返回值:

类型说明
boolean是否为指定位数的纯数字

enOrNum ​

参数:

参数说明类型默认值版本
value待判断的字符串string--

返回值:

类型说明
boolean是否全部为英文或数字,空串返回 true

image ​

参数:

参数说明类型默认值版本
value待判断的地址string--

返回值:

类型说明
boolean是否为图片地址,按扩展名判断且忽略查询串

video ​

参数:

参数说明类型默认值版本
value待判断的地址string--

返回值:

类型说明
boolean是否为视频地址,按扩展名判断

gradientColor ​

参数:

参数说明类型默认值版本
value待判断的样式值string | null--

返回值:

类型说明
boolean是否为 linear / radial / conic 渐变

empty ​

参数:

参数说明类型默认值版本
value待判断的值any | null--

返回值:

类型说明
boolean是否为空值(null、空串、0、NaN、false、空数组、空对象)

注意事项 ​

  • null('') 返回 false,空串不算 null;App 端 null 只判断 null,其他端同时判断 undefined。
  • letter('') 与 enOrNum('') 返回 true,这两个谓词允许空串。
  • code 的第二参数为 null 时按 6 位处理,len 必须是非负整数。
  • image 判断前会去掉 ? 之后的查询串,video 不做该处理。