Cafe-Utils
使用
NPM
npm i cafe-utils
在项目中引用:
// 单独引用
import { uuid } from 'cafe-utils';
uuid();
// 整体引用
import * as cafeUtils from 'cafe-utils';
cafeUtils.uuid();
CDN
<script src="https://unpkg.com/cafe-utils/lib/utils.umd.min.js"></script>
<!-- 或者 -->
<script src="https://cdn.jsdelivr.net/npm/cafe-utils/lib/utils.umd.min.js"></script>
在项目中使用:
cafeUtils.uuid()
Jest功能覆盖
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---|---|---|---|---|---|
All files | 94.88 | 83.33 | 88.17 | 94.12 | |
depend | 100 | 100 | 100 | 100 | |
numToString.ts | 100 | 100 | 100 | 100 | |
modules | 94.86 | 83.2 | 88.11 | 94.08 | |
array.ts | 100 | 100 | 100 | 100 | |
cookies.ts | 100 | 80 | 100 | 100 | 46-57 |
date.ts | 100 | 100 | 100 | 100 | |
dom.ts | 40 | 0 | 0 | 28.57 | 15-17,27,37-39,50-53 |
event.ts | 30.77 | 7.69 | 22.22 | 28 | 16-27,42-49,64-71 |
floatCalculate.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 86.25 | 100 | |
money.ts | 98.51 | 94.59 | 100 | 98.36 | 115 |
number.ts | 100 | 100 | 100 | 100 | |
storage.ts | 100 | 100 | 100 | 100 | |
string.ts | 100 | 94.29 | 100 | 100 | 71-90 |
tools.ts | 100 | 97.5 | 100 | 100 | 90 |
type.ts | 100 | 100 | 100 | 100 | |
validator.ts | 100 | 100 | 100 | 100 |
现有方法
类型判断
-
isObject
:判断对象类型 -
isArray
:判断数组类型 -
isDate
:判断日期类型 -
isNumber
:判断数字类型 -
isString
:判断字符串类型 -
isBoolean
:判断布尔类型 -
isFunction
:判断函数类型 -
isNull
:判断空类型
表单验证
-
isPhone
:判断手机号 -
isTel
:判断固定电话 -
isZIPCode
:判断邮政编码 -
isEmail
:判断邮箱 -
isIDCard
:判断身份证 -
isURL
:判断URL -
isImg
:判断图片格式 -
isCar
:判断车牌号码 -
isIPV4/isIPV6
:判断IP地址
字符串类
-
formatStr
:格式化字符串 -
toCamelCase
:字符串转换为驼峰 -
fromCamelCase
:驼峰转字符串 -
trim
:去除空格 -
escapeHTML
:转义HTML字符串 -
unescapeHTML
:反转义HTML -
stripHTMLTags
:过滤HTML代码 -
subGbString
:截取字符,区分中英文 -
getGbLen
:判断字符长度,区分中英文 -
changeCase
:字符串写法转换
数组类
-
arrayMax
:返回数组中的最大值 -
arrayMin
:返回数组中的最小值 -
arrayChunk
:切割数组 -
flatten
:指定扁平化数组 -
deepFlatten
:深度扁平化数组 -
union
:数组并集 -
intersection
:数组交集 -
difference
:数组差集 -
initArrayFillValues
:初始化并填充具有指定值的数组 -
sample
:返回数组中的随机元素
数字类
-
toFixed
:浮点数保留格式 -
randomIntegerInRange
:返回指定范围内的随机整数 -
randomNumberInRange
:返回指定范围内的随机数
金额类
-
formatMoney
:金额格式化 -
convertMoney
:金额转大写
日期类
-
formatDate
:日期格式化 -
isWeekend
:判断是否为周末 -
isWeekday
:判断是否为工作日 -
dayName
:日期转名称 -
getTimestamp
:日期转时间戳 -
fromTimestamp
:时间戳转日期 -
roundDate
:日期转换为零点
浮点计算类
-
addExact
:两个数相加 -
subExact
:两个数相减 -
mulExact
:两个数相乘 -
divExact
:两个数相除
工具类
-
clone
:浅拷贝 -
deepClone
:深拷贝 -
debounce
:防抖 -
throttle
:节流 -
randomId
:生成随机id -
uuid
:生成uuid -
hexToRGB
:16进制转RGB -
rgbToHex
:RGB转16进制
存储类
cookie
-
getCookie
:获取cookie -
setCookie
:设置cookie -
delCookie
:删除cookie -
clearCookie
:清空cookie
localStorage
-
setLStorage
:存储localStorage -
getLStorage
:取出localStorage -
delLStorage
:删除localStorage -
clearLStorage
:清空localStorage
sessionStorage
-
setSStorage
:存储sessionStorage -
getSStorage
:取出sessionStorage -
delSStorage
:删除sessionStorage -
clearSStorage
:清空sessionStorage
DOM类操作
-
hasClass
:判断元素是否包含类名 -
addClass
:添加class类名 -
removeClass
:移除class类名 -
toggleClass
:切换class类名
事件处理相关函数
-
listener
:事件监听器 -
on
:绑定事件 -
off
:解绑事件