# tyutil **Repository Path**: Alibb01/tyutil ## Basic Information - **Project Name**: tyutil - **Description**: js工具库,常用函数封装 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2019-04-11 - **Last Updated**: 2021-03-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # tyutil [![npm package](https://img.shields.io/badge/tyutil-0.1.1%20-green.svg)](https://www.npmjs.com/package/tyutil) ## 一个js工具库 整理了平常项目中常用的一些方法,正则验证,日期处理,金额处理,字符串处理判断,节流防抖等。 # 安装使用 ### 1. npm完整引入 `npm install tyutil --save-dev` 支持umd规范 ```js let tyutil = require('tyutil') let mValue = tyutil.money.toBig(12345) // mValue : 壹万贰仟叁佰肆拾伍元整 ``` ### 2. 部分引入 你可以只引入你需要的模块 ```js // let date = require('tyutil/module/模块名') let date = require('tyutil/module/date') let fTime = date.format(new Date(),'yyyy年MM月dd日') // fTime : 2017年12月06日 ``` ### 3. 浏览器 直接下载`dist`目录下的`tyutil.min.js`使用 ```html ``` # 模块 - common.js 封装了一些常用的函数 - date.js 日期处理 - dom.js 常用dom处理 - drawCircle.js 绘制动态环形进度条 - money.js 金额处理 - optimize.js 性能优化 - store.js 封装了sessionStorage/localStorage/cookie,方便操作 - regexp.js 一些常用的正则表达式 # API文档 ## Members
Regexp

正则表达式封装

sstore

sessionStorage封装

lstore

localStorage封装

cookie

cookie封装

## Functions
isNotEmpty(str)boolean

判断字符串是否不为空

isEmptyObject(obj)boolean

判断对象是否为空

isNumber(n)boolean

判断是否是数字

isArray(obj)void

判断是否是数组

clone(obj)object

克隆对象

getOS()string

获取操作系统

getExplore(versions)string

获取浏览器类型和版本

urlParamsToObj(url)object

url参数转对象

objToUrlParams(url, params)string

对象参数添加到url上

getParame(name)string

获取url参数

randomWord(flag, min, max)string

生成任意长度随机字母数字组合

UUID()string

生成uuid

ImgToBase64(url, callback, outputFormat)void

img转base64

getTime()number

获取当前时间戳

passedTime(startTime)string

距现在的已过时间

remainTime(endTime)string

距现在的剩余时间

format(dateTime, fmt)string

格式化日期

strToDate(dateStr)Date | ''

字符串转日期

compareDate(d1, d2)number

比较两个日期

addDay(date, dayNum)Date

日期添加天数

hasClass(ele, cls)boolean

是否有指定样式

addClass(ele, cls)void

为指定的dom元素添加样式

removeClass(ele, cls)void

删除指定dom元素的样式

toggleClass(ele, cls)void

如果存在(不存在),就删除(添加)一个样式

getScrollTop()number

获取滚动条距离顶部的距离

offset(ele)number

获取元素的距离文档的位置

setScrollTop(value)void

设置滚动条距顶部的距离

smoothScroll(to, duration)void

平滑滚动到指定位置

pin(ele)void

当元素滚动到顶部时固定元素

isDOMElement(obj)boolean

判断是否是dom对象

drawCircle(el, percent, style)void

绘制圆环进度条

format(num, precision, separator)string

格式化金额

toBig(n)string

金额转大写

isSupportWebP()boolean

判断浏览器是否支持webP格式图片

loadWebP(attr)void

替换webP

debounce(func, delay)function

防抖动

throttle(func, delay)function

节流

## Regexp 正则表达式封装 **Kind**: global variable * [Regexp](#Regexp) * [.isEmail(str)](#Regexp.isEmail) ⇒ boolean * [.isMobilePhone(str)](#Regexp.isMobilePhone) ⇒ boolean * [.idCard(str)](#Regexp.idCard) ⇒ boolean * [.isUrl(str)](#Regexp.isUrl) ⇒ boolean * [.isDate(str)](#Regexp.isDate) ⇒ boolean * [.isIncludeCn(str)](#Regexp.isIncludeCn) ⇒ boolean * [.isUserName(str)](#Regexp.isUserName) ⇒ boolean ### Regexp.isEmail(str) ⇒ boolean 判断是否为邮箱地址 **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ### Regexp.isMobilePhone(str) ⇒ boolean 判断是否是手机号码 **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ### Regexp.idCard(str) ⇒ boolean 判断是否是身份证 **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ### Regexp.isUrl(str) ⇒ boolean 判断是否是url **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ### Regexp.isDate(str) ⇒ boolean 判断是否是日期 **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ### Regexp.isIncludeCn(str) ⇒ boolean 判断是否包含中文 **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ### Regexp.isUserName(str) ⇒ boolean 判断用户名4-16位(字母,数字,下划线,减号) **Kind**: static method of [Regexp](#Regexp) | Param | Type | Description | | --- | --- | --- | | str | string | 待验证字符串 | ## sstore sessionStorage封装 **Kind**: global variable * [sstore](#sstore) * [.set(key, value)](#sstore.set) ⇒ void * [.get(key)](#sstore.get) ⇒ object * [.del(key)](#sstore.del) ⇒ void * [.clear()](#sstore.clear) ⇒ void ### sstore.set(key, value) ⇒ void 添加 **Kind**: static method of [sstore](#sstore) | Param | Type | Description | | --- | --- | --- | | key | string | key | | value | string | 值 | ### sstore.get(key) ⇒ object 获取 **Kind**: static method of [sstore](#sstore) | Param | Type | Description | | --- | --- | --- | | key | string | key | ### sstore.del(key) ⇒ void 删除 **Kind**: static method of [sstore](#sstore) | Param | Type | Description | | --- | --- | --- | | key | string | key | ### sstore.clear() ⇒ void 清除 **Kind**: static method of [sstore](#sstore) ## lstore localStorage封装 **Kind**: global variable * [lstore](#lstore) * [.set(key, value)](#lstore.set) ⇒ void * [.get(key)](#lstore.get) ⇒ object * [.del(key)](#lstore.del) ⇒ void * [.clear()](#lstore.clear) ⇒ void ### lstore.set(key, value) ⇒ void 添加 **Kind**: static method of [lstore](#lstore) | Param | Type | Description | | --- | --- | --- | | key | string | key | | value | object | 值 | ### lstore.get(key) ⇒ object 获取 **Kind**: static method of [lstore](#lstore) | Param | Description | | --- | --- | | key | key | ### lstore.del(key) ⇒ void 删除 **Kind**: static method of [lstore](#lstore) | Param | Description | | --- | --- | | key | key | ### lstore.clear() ⇒ void 清除 **Kind**: static method of [lstore](#lstore) ## cookie cookie封装 **Kind**: global variable * [cookie](#cookie) * [.set(key, value, time)](#cookie.set) ⇒ void * [.get(key)](#cookie.get) ⇒ string * [.del(key)](#cookie.del) ⇒ void ### cookie.set(key, value, time) ⇒ void 设置cookie key的值 **Kind**: static method of [cookie](#cookie) | Param | Type | Description | | --- | --- | --- | | key | string | key | | value | string | 值 | | time | number | 失效时间 | ### cookie.get(key) ⇒ string 获取cookie指定key值 **Kind**: static method of [cookie](#cookie) | Param | Type | Description | | --- | --- | --- | | key | string | key | ### cookie.del(key) ⇒ void 删除cookie **Kind**: static method of [cookie](#cookie) | Param | Type | Description | | --- | --- | --- | | key | string | key | ## isNotEmpty(str) ⇒ boolean 判断字符串是否不为空 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | str | string | 字符串 | ## isEmptyObject(obj) ⇒ boolean 判断对象是否为空 **Kind**: global function | Param | Type | | --- | --- | | obj | object | ## isNumber(n) ⇒ boolean 判断是否是数字 **Kind**: global function | Param | Type | | --- | --- | | n | object | ## isArray(obj) ⇒ void 判断是否是数组 **Kind**: global function | Param | Type | | --- | --- | | obj | object | ## clone(obj) ⇒ object 克隆对象 **Kind**: global function | Param | Type | | --- | --- | | obj | object | ## getOS() ⇒ string 获取操作系统 **Kind**: global function ## getExplore(versions) ⇒ string 获取浏览器类型和版本 **Kind**: global function | Param | Type | | --- | --- | | versions | boolean | ## urlParamsToObj(url) ⇒ object url参数转对象 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | url | String | url | ## objToUrlParams(url, params) ⇒ string 对象参数添加到url上 **Kind**: global function **Returns**: string - url | Param | Type | Description | | --- | --- | --- | | url | string | url | | params | object | 参数 | ## getParame(name) ⇒ string 获取url参数 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | name | string | 参数名 | ## randomWord(flag, min, max) ⇒ string 生成任意长度随机字母数字组合 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | flag | boolean | 是否任意长度 | | min | number | 最小长度 | | max | number | 最大长度 | ## UUID() ⇒ string 生成uuid **Kind**: global function ## ImgToBase64(url, callback, outputFormat) ⇒ void img转base64 **Kind**: global function | Param | Type | | --- | --- | | url | string | | callback | function | | outputFormat | string | ## getTime() ⇒ number 获取当前时间戳 **Kind**: global function ## passedTime(startTime) ⇒ string 距现在的已过时间 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | startTime | string | 开始日期字符串 | ## remainTime(endTime) ⇒ string 距现在的剩余时间 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | endTime | string | 结束日期字符串 | ## format(dateTime, fmt) ⇒ string 格式化日期 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | dateTime | string | 日期/日期字符串 | | fmt | object | 格式 | ## strToDate(dateStr) ⇒ Date \| '' 字符串转日期 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | dateStr | string | 日期字符串 | ## compareDate(d1, d2) ⇒ number 比较两个日期 **Kind**: global function **Returns**: number - 1:大于;0:等于;-1:小于 | Param | Type | Description | | --- | --- | --- | | d1 | Date | 第一个日期 | | d2 | Date | 第二个日期 | ## addDay(date, dayNum) ⇒ Date 日期添加天数 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | date | Date | 日期 | | dayNum | number | 添加的天数 | ## hasClass(ele, cls) ⇒ boolean 是否有指定样式 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | ele | object | dom节点 | | cls | string | class名称 | ## addClass(ele, cls) ⇒ void 为指定的dom元素添加样式 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | ele | object | dom节点 | | cls | string | class名称 | ## removeClass(ele, cls) ⇒ void 删除指定dom元素的样式 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | ele | object | dom节点 | | cls | string | class名称 | ## toggleClass(ele, cls) ⇒ void 如果存在(不存在),就删除(添加)一个样式 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | ele | object | dom节点 | | cls | string | class名称 | ## getScrollTop() ⇒ number 获取滚动条距离顶部的距离 **Kind**: global function ## offset(ele) ⇒ number 获取元素的距离文档的位置 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | ele | object | dom节点 | ## setScrollTop(value) ⇒ void 设置滚动条距顶部的距离 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | value | number | 距离 | ## smoothScroll(to, duration) ⇒ void 平滑滚动到指定位置 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | to | number | 距离顶部的距离 | | duration | number | 滚动的时间 | ## pin(ele) ⇒ void 当元素滚动到顶部时固定元素 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | ele | object | dom节点 | ## isDOMElement(obj) ⇒ boolean 判断是否是dom对象 **Kind**: global function | Param | Type | | --- | --- | | obj | object | ## drawCircle(el, percent, style) ⇒ void 绘制圆环进度条 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | el | string | canvas节点 | | percent | number | 进度 | | style | object | 样式配置 | | style.width | number | 圆环宽度 | | style.height | number | 圆环高度 | | style.bgColor | string | 背景圆环的颜色 | | style.foreColor | string | 运动圆环的颜色 | | style.lineWidth | number | 环的宽度 | | style.fontSize | number | 字体大小 | | style.fontFamily | string | 字体 | | style.lineCap | boolean | 运动圆环是否圆角 | ## format(num, precision, separator) ⇒ string 格式化金额 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | num | number | 金额 | | precision | number | 精度 | | separator | \* | 分隔符 | ## toBig(n) ⇒ string 金额转大写 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | n | number | 金额 | ## isSupportWebP() ⇒ boolean 判断浏览器是否支持webP格式图片 **Kind**: global function ## loadWebP(attr) ⇒ void 替换webP **Kind**: global function | Param | Type | | --- | --- | | attr | string | ## debounce(func, delay) ⇒ function 防抖动 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | func | function | 要执行的函数 | | delay | number | 间隔时间 | ## throttle(func, delay) ⇒ function 节流 **Kind**: global function | Param | Type | Description | | --- | --- | --- | | func | function | 要执行的函数 | | delay | number | 间隔时间 |