56个JavaScript 实用工具函数
1. 数字操作
(1)生成指定范围随机数
1 | export const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; |
(2)数字千分位分隔
1 | export const format = (n) => { |
2. 数组操作
(1)数组乱序
1 | export const arrScrambling = (arr) => { |
(2)数组扁平化
1 | export const flatten = (arr) => { |
(3)数组中获取随机数
1 | export const sample = arr => arr[Math.floor(Math.random() * arr.length)]; |
3. 字符串操作
(1)生成随机字符串
1 | export const randomString = (len) => { |
(2)字符串首字母大写
1 | export const fistLetterUpper = (str) => { |
(3)手机号中间四位变成*
1 | export const telFormat = (tel) => { |
(4)驼峰命名转换成短横线命名
1 | export const getKebabCase = (str) => { |
(5)短横线命名转换成驼峰命名
1 | export const getCamelCase = (str) => { |
(6)全角转换为半角
1 | export const toCDB = (str) => { |
(7)半角转换为全角
1 | export const toDBC = (str) => { |
4. 格式转化
(1)数字转化为大写金额
1 | export const digitUppercase = (n) => { |
(2)数字转化为中文数字
1 | export const intToChinese = (value) => { |
5. 操作存储
(1)存储loalStorage
1 | export const loalStorageSet = (key, value) => { |
(2)获取localStorage
1 | export const loalStorageGet = (key) => { |
(3)删除localStorage
1 | export const loalStorageRemove = (key) => { |
(4)存储sessionStorage
1 | export const sessionStorageSet = (key, value) => { |
(5)获取sessionStorage
1 | export const sessionStorageGet = (key) => { |
(6)删除sessionStorage
1 | export const sessionStorageRemove = (key) => { |
6. 操作cookie
(1)设置cookie
1 | export const setCookie = (key, value, expire) => { |
(2)读取cookie
1 | export const getCookie = (key) => { |
(3)删除cookie
1 | export const delCookie = (key) => { |
7. 格式校验
(1)校验身份证号码
1 | export const checkCardNo = (value) => { |
(2)校验是否包含中文
1 | export const haveCNChars => (value) => { |
(3)校验是否为中国大陆的邮政编码
1 | export const isPostCode = (value) => { |
(4)校验是否为IPv6地址
1 | export const isIPv6 = (str) => { |
(5)校验是否为邮箱地址
1 | export const isEmail = (value) { |
(6)校验是否为中国大陆手机号
1 | export const isTel = (value) => { |
(7)校验是否包含emoji表情
1 | export const isEmojiCharacter = (value) => { |
8. 操作URL
(1)获取URL参数列表
1 | export const GetRequest = () => { |
(2)检测URL是否有效
1 | export const getUrlState = (URL) => { |
(3)键值对拼接成URL参数
1 | export const params2Url = (obj) => { |
(4)修改URL中的参数
1 | export const replaceParamVal => (paramName, replaceWith) { |
(5)删除URL中指定参数
1 | export const funcUrlDel = (name) => { |
9. 设备判断
(1)判断是移动还是PC设备
1 | export const isMobile = () => { |
(2)判断是否是苹果还是安卓移动设备
1 | export const isAppleMobileDevice = () => { |
(3)判断是否是安卓移动设备
1 | export const isAndroidMobileDevice = () => { |
(4)判断是Windows还是Mac系统
1 | export const osType = () => { |
(5)判断是否是微信/QQ内置浏览器
1 | export const broswer = () => { |
(6)浏览器型号和版本
1 | export const getExplorerInfo = () => { |
10. 浏览器操作
(1)滚动到页面顶部
1 | export const scrollToTop = () => { |
(2)滚动到页面底部
1 | export const scrollToBottom = () => { |
(3)滚动到指定元素区域
1 | export const smoothScroll = (element) => { |
(4)获取可视窗口高度
1 | export const getClientHeight = () => { |
(5)获取可视窗口宽度
1 | export const getPageViewWidth = () => { |
(6)打开浏览器全屏
1 | export const toFullScreen = () => { |
(7)退出浏览器全屏
1 | export const exitFullscreen = () => { |
11. 时间操作
(1)当前时间
1 | export const nowTime = () => { |
(2)格式化时间
1 | export const dateFormater = (formater, time) => { |
12. JavaScript操作
(1)阻止冒泡事件
1 | export const stopPropagation = (e) => { |
(2)防抖函数
1 | export const debounce = (fn, wait) => { |
(3)节流函数
1 | export const throttle = (fn, delay) => { |
(4)数据类型判断
1 | export const getType = (value) => { |
(5)对象深拷贝
1 | export const deepClone = (obj, hash = new WeakMap()) => { |
作者:CUGGZ
- Post title:56个JavaScript 实用工具函数
- Post author:Mark
- Create time:2021-11-20 14:09:56
- Post link:https://m.iqimeng.com/2021/11/20/study/56个JavaScript 实用工具函数/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.