tyro-util
Version:
327 lines (274 loc) • 19.3 kB
Markdown
# 前端工具库
这是一个使用webpack打包的JavaScript类库。
> 目的:让前端开发更高效,更专注于业务代码的编写
业务开发过程中,会经常用到`数组的操作`、`日期格式化`、`url的操作`、`浏览器类型判断`等常用函数,为避免不同项目多次复制粘贴的麻烦,这里统一封装,以提高开发效率。
## 使用方式
### 浏览器:
直接下载项目,引入js文件
```
<script type="text/javascript" src="/dist/util.js"></script>
tUtil.uuid()
tUtil.trim(' Tyro CCC ')
```
### NODE:
1. 使用npm安装:
```
npm install tyro-util --save
```
2. ES6:
```
// 全部引入
import tUtil from 'tyro-util'
tUtil.uuid()
// 单个引入
import { uuid } from 'tyro-util'
uuid()
// 按需加载
import uuid from 'tyro-util/lib/uuid'
uuid()
```
3. Commonjs:
```
// 全部引入
const tUtil = require('tyro-util')
tUtil.uuid()
// 单个引入
const uuid = require('tyro-util').uuid
uuid()
// 按需加载
const uuid = require('tyro-util/lib/uuid')
uuid()
```
## 发布到 npm
```
npm run build // 先webpack打包类库,之后再发布
npm login // 登录
npm publish // 发布
```
## API文档
### array
####    [allEqual][allEqual]   检查数组各项是否相等
####    [arrayEqual][arrayEqual]   判断两个数组是否相等
####    [average][average]   取平均数
####    [countOccurrences][countOccurrences]   检测指定数值出现次数
####    [intersection][intersection]   两数组的交集
####    [sample][sample]   随机获取数组的某个值
####    [shuffle][shuffle]   数组“洗牌”
####    [sum][sum]   数组总和
####    [toTreeData][toTreeData]   递归数组转为树形结构
### clazz
####    [addClass][addClass]   为元素添加class
####    [hasClass][hasClass]   判断元素是否有某个class
####    [removeClass][removeClass]   为元素移除class
### cookie
####    [getCookie][getCookie]   根据name读取cookie
####    [removeCookie][removeCookie]   根据name删除cookie
####    [setCookie][setCookie]   设置Cookie
### device
####    [detectDeviceType][detectDeviceType]   判断是PC端还是移动端
####    [getExplore][getExplore]   获取浏览器类型和版本
####    [getOS][getOS]   获取操作系统类型
####    [getVersion][getVersion]   判断是否xx浏览器
####    [isAppleDevice][isAppleDevice]   判断是否为苹果设备
####    [isBrowserTabInView][isBrowserTabInView]   判断浏览器 Tab 窗口是否为活动窗口
####    [touchSupported][touchSupported]   判断浏览器是否支持触摸事件
### dom
####    [bottomVisible][bottomVisible]   检查页面底部是否可见
####    [elementContains][elementContains]   检查是否包含子元素
####    [elementIsInFocus][elementIsInFocus]   判断 DOM 元素是否已获得焦点
####    [getRect][getRect]   获取节点对象的上下左右边距及宽高
####    [getScrollTop][getScrollTop]   获取滚动条距顶部的距离
####    [getStyle][getStyle]   获取任一元素的任意属性
####    [hasChildren][hasChildren]   判断元素有没有子元素
####    [hasClass][hasClass]   校验指定元素是否有指定类
####    [offset][offset]   获取一个元素的距离文档(document)的位置,类似jQ中的offset()
####    [retSibling][retSibling]   返回元素的第n个兄弟节点
####    [scrollTo][scrollTo]   在${duration}时间内,滚动条平滑滚动到${to}指定位置
####    [setScrollTop][setScrollTop]   设置滚动条距顶部的距离
####    [windowResize][windowResize]   H5软键盘缩回、弹起回调
### func
####    [debounce][debounce]   函数防抖
####    [once][once]   只会调用一次的函数
####    [throttle][throttle]   函数节流
####    [timeTaken][timeTaken]   计算函数执行时间
### html
####    [htmlDecode][htmlDecode]   html解码
####    [htmlDecodeByRegExp][htmlDecodeByRegExp]   html正则解码
####    [htmlEncode][htmlEncode]   html转码
####    [htmlEncodeByRegExp][htmlEncodeByRegExp]   html正则转码
### money
####    [numToCN][numToCN]   数字现金转中文
### number
####    [isEven][isEven]   判读数字是否为偶数
####    [isFloat][isFloat]   判读数字是否为小数
####    [isInteger][isInteger]   判读数字是否为整数
####    [isNumber][isNumber]   判读是否为数字
####    [toFixed][toFixed]   保留小数点(非四舍五入)
### object
####    [equals][equals]   全等判断
####    [isEmptyObject][isEmptyObject]   判断obj是否为空
####    [isObject][isObject]   判断是否对象
####    [mergeObject][mergeObject]   合并对象
####    [isEven][isEven]   判读数字是否为偶数
### random
####    [randomBoolean][randomBoolean]   获取随机布尔值 (true/false)
####    [randomColor][randomColor]   随机生成颜色
####    [randomIntegerInRange][randomIntegerInRange]   生成指定范围的随机整数
####    [randomNumberInRange][randomNumberInRange]   生成指定范围的随机小数
####    [shuffle][shuffle]   随机化数组的顺序
####    [uuid][uuid]   UUID生成器
### regex
####    [addCommaToNum][addCommaToNum]   数字加逗号分隔
####    [capitalize][capitalize]   首字母大写
####    [encryptIdentity][encryptIdentity]   身份证号脱敏处理
####    [encryptPhone][encryptPhone]   手机号脱敏处理
####    [escapeHTML][escapeHTML]   转义HTML
####    [filterCENum][filterCENum]   只能输入英文、中文和数字,不符合字符的会替换为空
####    [filterENum][filterENum]   只能输入英文、和数字,不符合字符的会替换为空
####    [filterFloate][filterFloate]   只能输入数字和一个小数点,不符合字符的会替换为空
####    [filterNum][filterNum]   只能输入数字, 不符合字符的会替换为空
####    [isChinese][isChinese]   是否是中文
####    [isColor][isColor]   判断是否为16进制颜色,rgb 或 rgba
####    [isEmail][isEmail]   判断是否为邮箱地址
####    [isIdCard][isIdCard]   判断是否为身份证号
####    [isLandline][isLandline]   是否固定电话
####    [isLetter][isLetter]   是否是英文字母
####    [isPhoneNum][isPhoneNum]   判断是否为手机号
####    [isValidPwd][isValidPwd]   判断密码格式是否6-25字符必须包含大小写字母+数字
### str
####    [byteSize][byteSize]   返回字符串的字节长度
####    [capitalize][capitalize]   首字母大写
####    [capitalizeEveryWord][capitalizeEveryWord]   每个单词首字母大写
####    [decapitalize][decapitalize]   首字母小写
####    [fNumber][fNumber]   保留小数点后n位
####    [fNumberWithComma][fNumberWithComma]   数字每隔三位加一个逗号
####    [fromCamelCase][fromCamelCase]   驼峰转连字符
####    [isEmoji][isEmoji]   判断是否是emoji
####    [isNumber][isNumber]   判断是否数字
####    [isUrl][isUrl]   判断是否是url
####    [leftPad][leftPad]   补零
####    [reverseString][reverseString]   反转一个字符串
####    [rgbToHex][rgbToHex]   RGB转十六进制
####    [stripHTMLTags][stripHTMLTags]   从字符串中删除HTML/XML标签
####    [toCamelCase][toCamelCase]   连字符转驼峰
####    [trim][trim]   去空格
### time
####    [dayOfYear][dayOfYear]   当前日期天数
####    [formatPassTime][formatPassTime]   距现在的已过时间
####    [formatRemainTime][formatRemainTime]   现在距${endTime}的剩余时间
####    [getDaysDiffBetweenDates][getDaysDiffBetweenDates]   返回日期间的天数
####    [isLeapYear][isLeapYear]   是否为闰年
####    [isSameDay][isSameDay]   判断是否为同一天
####    [isWeekday][isWeekday]   检查日期是否为工作日
####    [timeLeft][timeLeft]   剩余时间
### url
####    [getQueryString][getQueryString]   获取url参数值
####    [httpsRedirect][httpsRedirect]   HTTP跳转HTTPS
####    [isIpv4][isIpv4]   判断是否为IPv4地址
####    [isUrl][isUrl]   判断是否为URL地址
####    [parseQueryToObj][parseQueryToObj]   url参数转对象
[allEqual]:https://github.com/tyrocjh/my-util/blob/master/src/array/allEqual.js
[arrayEqual]:https://github.com/tyrocjh/my-util/blob/master/src/array/arrayEqual.js
[average]:https://github.com/tyrocjh/my-util/blob/master/src/array/average.js
[countOccurrences]:https://github.com/tyrocjh/my-util/blob/master/src/array/countOccurrences.js
[intersection]:https://github.com/tyrocjh/my-util/blob/master/src/array/intersection.js
[sample]:https://github.com/tyrocjh/my-util/blob/master/src/array/sample.js
[shuffle]:https://github.com/tyrocjh/my-util/blob/master/src/array/shuffle.js
[sum]:https://github.com/tyrocjh/my-util/blob/master/src/array/sum.js
[toTreeData]:https://github.com/tyrocjh/my-util/blob/master/src/array/toTreeData.js
[addClass]:https://github.com/tyrocjh/my-util/blob/master/src/clazz/addClass.js
[hasClass]:https://github.com/tyrocjh/my-util/blob/master/src/clazz/hasClass.js
[removeClass]:https://github.com/tyrocjh/my-util/blob/master/src/clazz/removeClass.js
[getCookie]:https://github.com/tyrocjh/my-util/blob/master/src/cookie/getCookie.js
[removeCookie]:https://github.com/tyrocjh/my-util/blob/master/src/cookie/removeCookie.js
[setCookie]:https://github.com/tyrocjh/my-util/blob/master/src/cookie/setCookie.js
[detectDeviceType]:https://github.com/tyrocjh/my-util/blob/master/src/device/detectDeviceType.js
[getExplore]:https://github.com/tyrocjh/my-util/blob/master/src/device/getExplore.js
[getOS]:https://github.com/tyrocjh/my-util/blob/master/src/device/getOS.js
[getVersion]:https://github.com/tyrocjh/my-util/blob/master/src/device/getVersion.js
[isAppleDevice]:https://github.com/tyrocjh/my-util/blob/master/src/device/isAppleDevice.js
[isBrowserTabInView]:https://github.com/tyrocjh/my-util/blob/master/src/device/isBrowserTabInView.js
[touchSupported]:https://github.com/tyrocjh/my-util/blob/master/src/device/touchSupported.js
[bottomVisible]:https://github.com/tyrocjh/my-util/blob/master/src/dom/bottomVisible.js
[elementContains]:https://github.com/tyrocjh/my-util/blob/master/src/dom/elementContains.js
[elementIsInFocus]:https://github.com/tyrocjh/my-util/blob/master/src/dom/elementIsInFocus.js
[getRect]:https://github.com/tyrocjh/my-util/blob/master/src/dom/getRect.js
[getScrollTop]:https://github.com/tyrocjh/my-util/blob/master/src/dom/getScrollTop.js
[getStyle]:https://github.com/tyrocjh/my-util/blob/master/src/dom/getStyle.js
[hasChildren]:https://github.com/tyrocjh/my-util/blob/master/src/dom/hasChildren.js
[hasClass]:https://github.com/tyrocjh/my-util/blob/master/src/dom/hasClass.js
[offset]:https://github.com/tyrocjh/my-util/blob/master/src/dom/offset.js
[retSibling]:https://github.com/tyrocjh/my-util/blob/master/src/dom/retSibling.js
[scrollTo]:https://github.com/tyrocjh/my-util/blob/master/src/dom/scrollTo.js
[setScrollTop]:https://github.com/tyrocjh/my-util/blob/master/src/dom/setScrollTop.js
[windowResize]:https://github.com/tyrocjh/my-util/blob/master/src/dom/windowResize.js
[debounce]:https://github.com/tyrocjh/my-util/blob/master/src/func/debounce.js
[once]:https://github.com/tyrocjh/my-util/blob/master/src/func/once.js
[throttle]:https://github.com/tyrocjh/my-util/blob/master/src/func/throttle.js
[timeTaken]:https://github.com/tyrocjh/my-util/blob/master/src/func/timeTaken.js
[htmlDecode]:https://github.com/tyrocjh/my-util/blob/master/src/html/htmlDecode.js
[htmlDecodeByRegExp]:https://github.com/tyrocjh/my-util/blob/master/src/html/htmlDecodeByRegExp.js
[htmlEncode]:https://github.com/tyrocjh/my-util/blob/master/src/html/htmlEncode.js
[htmlEncodeByRegExp]:https://github.com/tyrocjh/my-util/blob/master/src/html/htmlEncodeByRegExp.js
[numToCN]:https://github.com/tyrocjh/my-util/blob/master/src/money/numToCN.js
[isEven]:https://github.com/tyrocjh/my-util/blob/master/src/number/isEven.js
[isFloat]:https://github.com/tyrocjh/my-util/blob/master/src/number/isFloat.js
[isInteger]:https://github.com/tyrocjh/my-util/blob/master/src/number/isInteger.js
[isNumber]:https://github.com/tyrocjh/my-util/blob/master/src/number/isNumber.js
[toFixed]:https://github.com/tyrocjh/my-util/blob/master/src/number/toFixed.js
[equals]:https://github.com/tyrocjh/my-util/blob/master/src/object/equals.js
[isEmptyObject]:https://github.com/tyrocjh/my-util/blob/master/src/object/isEmptyObject.js
[isObject]:https://github.com/tyrocjh/my-util/blob/master/src/object/isObject.js
[mergeObject]:https://github.com/tyrocjh/my-util/blob/master/src/object/mergeObject.js
[randomBoolean]:https://github.com/tyrocjh/my-util/blob/master/src/random/randomBoolean.js
[randomColor]:https://github.com/tyrocjh/my-util/blob/master/src/random/randomColor.js
[randomIntegerInRange]:https://github.com/tyrocjh/my-util/blob/master/src/random/randomIntegerInRange.js
[randomNumberInRange]:https://github.com/tyrocjh/my-util/blob/master/src/random/randomNumberInRange.js
[shuffle]:https://github.com/tyrocjh/my-util/blob/master/src/random/shuffle.js
[uuid]:https://github.com/tyrocjh/my-util/blob/master/src/random/uuid.js
[addCommaToNum]:https://github.com/tyrocjh/my-util/blob/master/src/regex/addCommaToNum.js
[capitalize]:https://github.com/tyrocjh/my-util/blob/master/src/regex/capitalize.js
[encryptIdentity]:https://github.com/tyrocjh/my-util/blob/master/src/regex/encryptIdentity.js
[encryptPhone]:https://github.com/tyrocjh/my-util/blob/master/src/regex/encryptPhone.js
[escapeHTML]:https://github.com/tyrocjh/my-util/blob/master/src/regex/escapeHTML.js
[filterCENum]:https://github.com/tyrocjh/my-util/blob/master/src/regex/filterCENum.js
[filterENum]:https://github.com/tyrocjh/my-util/blob/master/src/regex/filterENum.js
[filterFloate]:https://github.com/tyrocjh/my-util/blob/master/src/regex/filterFloate.js
[filterNum]:https://github.com/tyrocjh/my-util/blob/master/src/regex/filterNum.js
[isChinese]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isChinese.js
[isColor]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isColor.js
[isEmail]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isEmail.js
[isIdCard]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isIdCard.js
[isLandline]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isLandline.js
[isLetter]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isLetter.js
[isPhoneNum]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isPhoneNum.js
[isValidPwd]:https://github.com/tyrocjh/my-util/blob/master/src/regex/isValidPwd.js
[byteSize]:https://github.com/tyrocjh/my-util/blob/master/src/str/byteSize.js
[capitalize]:https://github.com/tyrocjh/my-util/blob/master/src/str/capitalize.js
[capitalizeEveryWord]:https://github.com/tyrocjh/my-util/blob/master/src/str/capitalizeEveryWord.js
[decapitalize]:https://github.com/tyrocjh/my-util/blob/master/src/str/decapitalize.js
[fNumber]:https://github.com/tyrocjh/my-util/blob/master/src/str/fNumber.js
[fNumberWithComma]:https://github.com/tyrocjh/my-util/blob/master/src/str/fNumberWithComma.js
[fromCamelCase]:https://github.com/tyrocjh/my-util/blob/master/src/str/fromCamelCase.js
[isEmoji]:https://github.com/tyrocjh/my-util/blob/master/src/str/isEmoji.js
[isNumber]:https://github.com/tyrocjh/my-util/blob/master/src/str/isNumber.js
[isUrl]:https://github.com/tyrocjh/my-util/blob/master/src/str/isUrl.js
[leftPad]:https://github.com/tyrocjh/my-util/blob/master/src/str/leftPad.js
[reverseString]:https://github.com/tyrocjh/my-util/blob/master/src/str/reverseString.js
[rgbToHex]:https://github.com/tyrocjh/my-util/blob/master/src/str/rgbToHex.js
[stripHTMLTags]:https://github.com/tyrocjh/my-util/blob/master/src/str/stripHTMLTags.js
[toCamelCase]:https://github.com/tyrocjh/my-util/blob/master/src/str/toCamelCase.js
[trim]:https://github.com/tyrocjh/my-util/blob/master/src/str/trim.js
[dayOfYear]:https://github.com/tyrocjh/my-util/blob/master/src/time/dayOfYear.js
[formatPassTime]:https://github.com/tyrocjh/my-util/blob/master/src/time/formatPassTime.js
[formatRemainTime]:https://github.com/tyrocjh/my-util/blob/master/src/time/formatRemainTime.js
[getDaysDiffBetweenDates]:https://github.com/tyrocjh/my-util/blob/master/src/time/getDaysDiffBetweenDates.js
[isLeapYear]:https://github.com/tyrocjh/my-util/blob/master/src/time/isLeapYear.js
[isSameDay]:https://github.com/tyrocjh/my-util/blob/master/src/time/isSameDay.js
[isWeekday]:https://github.com/tyrocjh/my-util/blob/master/src/time/isWeekday.js
[timeLeft]:https://github.com/tyrocjh/my-util/blob/master/src/time/timeLeft.js
[getQueryString]:https://github.com/tyrocjh/my-util/blob/master/src/url/getQueryString.js
[httpsRedirect]:https://github.com/tyrocjh/my-util/blob/master/src/url/httpsRedirect.js
[isIpv4]:https://github.com/tyrocjh/my-util/blob/master/src/url/isIpv4.js
[isUrl]:https://github.com/tyrocjh/my-util/blob/master/src/url/isUrl.js
[parseQueryToObj]:https://github.com/tyrocjh/my-util/blob/master/src/url/parseQueryToObj.js