一些常用的JS方法
npm install @saqqdy/common```通过npm安装
npm install --save @saqqdy/common或者通过yarn安装
yarn add @saqqdy/common
#### 通过import引入模块的方式
``在你的.vue或者main.js里面写上import
import { trim, getOsVersion, ... } from '@saqqdy/common'使用
trim(somestring, type) // 返回清理空格后的字符串
getOsVersion() // 返回系统版本
...
#### 使用文件引入的方式
1. 通过require引入
``在你的main.js文件里面加上下面这一行
require('@saqqdy/common')
2. html静态页直接使用
``在你的html代码上加上script标签,使用CDN链接引入
// 全局参数
client, // client方法返回一个浏览器判断结果对象
pattern, // pattern返回一些常用的正则// String扩展、数组方法
trim, // 根据传参来去除空格
clearAttr, // 去除HTML标签所有属性
clearBr, // 去除换行
clearHtml, // 去除HTML标签
clearHtmlExpSN, // 去除HTML标签保留空格、换行
clearHtmlN, // 去除HTML标签及换行
clearHtmlNS, // 去除HTML标签及空格、换行
clearHtmlTag, // 去除HTML标签及标签里面的文字
getNumber, // 获取字符串中的数字
imgAdapt, // 扩展图片自动适应多种分辨率small original
imgChoose, // 扩展图片自动适应多种分辨率@2x @3x
getRandomNum, // 获取随机整数
getRandomStr, // 获取随机字符串
getRandomStrWidthSpecialChar, // 获取随机字符串带特殊符号
getCHSLength, // 获取字符串长度,中文算2个字符
cutCHSString, // 截取字符串,中文算2个字节
textareaInsertText, // textarea或input对象在指定的光标位置插入文字
textareaMoveToEnd, // textarea或input对象将光标定位到文字尾部
// 获取一下状态
isDigitals, // 是否为由数字组成的字符串
isExitsFunction, // 是否存在指定函数
isExitsVariable, // 是否存在指定变量
getWindowSize, // getWindowSize获取窗口大小
getAppVersion, // 获取APP版本号
getOsVersion, // 获取手机系统版本
getIsAppVersionLastest, // 版本号大小对比
getDirParam, // 获取目录形式URL参数
getParameter, // 获取单个URL参数
getFileType, // 文件后缀名
getUrlParam, // 获取URL参数
// 日期格式化
formatTime, // 日期格式化插件
formatTimeStr, // 格式化时间成:刚刚、几分钟前
// 缓存、cookie、session
setCookie, // setCookie写入cookie的方法
setLocal, // 写localStorage
setSession, // 写sessionStorage
getCookie, // 读取cookies
getLocal, // 读取localStorage
getSession, // 读取sessionStorage
delCookie, // 删除cookie
delLocal, // 删除localStorage
delSession, // 删除sessionStorage
// 编码与解码
encodeBase64, // 字符串、数字转base64
encodeUtf8, // 编码Utf8
decodeBase64, // base64解码
decodeUtf8, // 解码Utf8
enWxJumpLink, // 用*替换= 用!替换& 转码成微信跳转链接
enWxJumpLinkOld, // 用~替换= 用^替换& 转码成微信跳转链接
deWxJumpLink, // 用=替换* 用&替换! 解码成微信跳转链接
deWxJumpLinkOld, // 用=替换~ 用&替换^ 解码成微信跳转链接
// 防抖与限流
debounce, // 空闲控制 返回函数连续调用时,空闲时间必须大于或等于 delay,fn 才会执行
throttle, // 频率控制 返回函数连续调用时,fn 执行频率限定为每多少时间执行一次
// 事件委托、其他事件方法
stopBubble, // 阻止冒泡
stopDefault, // 阻止默认事件
addEvent, // 事件委托,支持多次委托
removeEvent, // removeEvent移除由addEvent创建的事件委托
getScrollPosition, // 获取滑动到顶部和底部 返回'top' 'bottom',建议使用限流
`$3
- addEvent(element, type, handler)
addEvent()事件委托,支持多次委托
- handleEvent(event) ⇒
Boolean
handleEvent()执行事件
- fixEvent(event) ⇒
Object
为IE的事件对象添加一些“缺失的”函数
- clearAttr(string) ⇒
String
去除HTML标签所有属性
- clearBr(string) ⇒
String
去除换行
- clearHtml(string) ⇒
String
去除HTML标签
- clearHtmlExpSN(string) ⇒
String
去除HTML标签保留空格、换行
- clearHtmlN(string) ⇒
String
去除HTML标签及换行
- clearHtmlNS(string) ⇒
String
去除HTML标签及空格、换行
- clearHtmlTag(string) ⇒
String
去除HTML标签及标签里面的文字
- client(name, userAgent) ⇒
Object | Boolean
client方法返回一个浏览器判断结果:{ ANDROID: true, GECKO: true, GLSH_APP: false, IE: false, IOS: false, IPAD: false, IPHONE: false, MOBILE: true, MOBILEDEVICE: true, OPERA: false, QQ: false, QQBROWSER: false, TRIDENT: false, WEBKIT: true, WEIXIN: false }
- cutCHSString(str, len, hasDot) ⇒
String
截取字符串,中文算2个字节
- deWxJumpLink(string) ⇒
String
用=替换 用&替换! 解码成微信跳转链接
nameexMall-detail-goodsInfoId!params(goodsInfoId)*8866 转成 name=exMall-detail-goodsInfoId¶ms[goodsInfoId]=8866
- deWxJumpLinkOld(string) ⇒
String
用=替换~ 用&替换^ 解码成微信跳转链接
- debounce(fn, delay, immediate) ⇒
function
空闲控制 返回函数连续调用时,空闲时间必须大于或等于 delay,fn 才会执行
- decodeBase64(input) ⇒
String
base64解码
- decodeUtf8(input) ⇒
String
解码Utf8
- delCookie(name)
删除cookie
- delLocal(name)
删除localStorage
- delSession(name)
删除sessionStorage
- enWxJumpLink(string) ⇒
String
用替换= 用!替换& 转码成微信跳转链接
name=exMall-detail-goodsInfoId¶ms[goodsInfoId]=8866 转成 nameexMall-detail-goodsInfoId!params(goodsInfoId)*8866
- enWxJumpLinkOld(string) ⇒
String
用~替换= 用^替换& 转码成微信跳转链接
- encodeBase64(input) ⇒
String
字符串、数字转base64
- encodeUtf8(input) ⇒
String
编码Utf8
- formatTime(time, fmt) ⇒
String
日期格式化插件
使用方式:formatTime(new Date(), "yyyy-MM-dd")
- formatTimeStr(time, fmt) ⇒
String
格式化时间成:刚刚、几分钟前
- getAppVersion(appName, withosstr, userAgent) ⇒
Boolean | null
获取APP版本号
- getCHSLength(str) ⇒
Number
获取文本长度,中文算2个字节
- getCookie(name) ⇒
String
读取cookies
- getDirParam(url) ⇒
Object
获取目录形式URL参数
- getFileType(url) ⇒
String
文件后缀名
- getIsAppVersionLastest(appName, compareVer, userAgent) ⇒
Boolean | null
版本号大小对比
- getLocal(name) ⇒
String
读取localStorage
- getNumber(string) ⇒
String
获取字符串中的数字
- getOsVersion(osName, withosstr, userAgent) ⇒
Boolean | null
获取手机系统版本
- getParameter(name) ⇒
获取单个URL参数
- getRandomNum(min, max) ⇒
Number
获取随机整数
- getRandomStr(len, widthSpecialChar) ⇒
String
获取随机字符串
- getRandomStrWidthSpecialChar(len) ⇒
String
获取随机字符串带特殊符号
- getScrollPosition() ⇒
String
获取滑动到顶部和底部 返回'top' 'bottom',建议使用限流
- getSession(name) ⇒
String
读取sessionStorage
- getUrlParam(url) ⇒
Object
获取URL参数
- getWindowSize() ⇒
Object
getWindowSize获取窗口大小
- imgAdapt(imgurl, size) ⇒
String
扩展图片自动适应多种分辨率small original
- imgChoose(imgurl) ⇒
String
扩展图片自动适应多种分辨率@2x @3x
- isDigitals(str) ⇒
Boolean
是否为由数字组成的字符串
- isExitsFunction(funcName) ⇒
Boolean
是否存在指定函数
- isExitsVariable(variableName) ⇒
Boolean
是否存在指定变量
- pattern() ⇒
Object
pattern返回一些常用的正则:any, arrjson, chinese, email, float, isjson, json, mobile, number, pass, postcode, qq, string, tel, textarea, url, username
- removeEvent(element, type, handler)
removeEvent移除由addEvent创建的事件委托
- setCookie(name, value, seconds)
setCookie写入cookie的方法
- setLocal(name, value, seconds)
写localStorage
- setSession(name, value, seconds)
写sessionStorage
- stopBubble(e) ⇒
Boolean
阻止冒泡
- stopDefault(e) ⇒
Boolean
阻止默认事件
- textareaInsertText(obj, str)
textarea或input对象在指定的光标位置插入文字
- textareaMoveToEnd(obj)
textarea或input对象将光标定位到文字尾部
- throttle(fn, delay, immediate) ⇒
function
频率控制 返回函数连续调用时,fn 执行频率限定为每多少时间执行一次
- trim(string, type) ⇒
String
trim()根据传参来去除空格
addEvent(element, type, handler)
addEvent()事件委托,支持多次委托Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| element | Object | js dom对象 |
| type | String | 事件类型。不需要加on |
| handler | function | 回调方法 |
handleEvent(event) ⇒ Boolean
handleEvent()执行事件Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| event | String | 事件类型 |
fixEvent(event) ⇒ Object
为IE的事件对象添加一些“缺失的”函数Kind: global function
Returns: Object - 返回补齐了缺失方法的的event
| Param | Type | Description |
| --- | --- | --- |
| event | String | 事件类型 |
clearAttr(string) ⇒ String
去除HTML标签所有属性Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入字符串 |
clearBr(string) ⇒ String
去除换行Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 带html标签的字符串 |
clearHtml(string) ⇒ String
去除HTML标签Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 带html标签的字符串 |
clearHtmlExpSN(string) ⇒ String
去除HTML标签保留空格、换行Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 带html标签的字符串 |
clearHtmlN(string) ⇒ String
去除HTML标签及换行Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 带html标签的字符串 |
clearHtmlNS(string) ⇒ String
去除HTML标签及空格、换行Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 带html标签的字符串 |
clearHtmlTag(string) ⇒ String
去除HTML标签及标签里面的文字Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| string | String | 带html标签的字符串 |
client(name, userAgent) ⇒ Object \| Boolean
client方法返回一个浏览器判断结果:{ ANDROID: true, GECKO: true, GLSH_APP: false, IE: false, IOS: false, IPAD: false, IPHONE: false, MOBILE: true, MOBILEDEVICE: true, OPERA: false, QQ: false, QQBROWSER: false, TRIDENT: false, WEBKIT: true, WEIXIN: false }Kind: global function
Returns: Object \| Boolean - 返回常用ua匹配表,如果传了name,那么返回是否匹配该终端true/false
| Param | Type | Description |
| --- | --- | --- |
| name | String | 可选,比如传入MicroMessenger,返回是否为微信内置浏览器 |
| userAgent | String | 可选,传入自定义的ua,默认取浏览器的navigator.appVersion |
cutCHSString(str, len, hasDot) ⇒ String
截取字符串,中文算2个字节Kind: global function
Returns: String - 返回截取后的字符串
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| str | String | | 要截取的字符串 |
| len | Number | | |
| hasDot | Boolean | false | |
deWxJumpLink(string) ⇒ String
用=替换* 用&替换! 解码成微信跳转链接
nameexMall-detail-goodsInfoId!params(goodsInfoId)8866 转成 name=exMall-detail-goodsInfoId¶ms[goodsInfoId]=8866Kind: global function
Returns: String - 返回解码结果
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入字符串 |
deWxJumpLinkOld(string) ⇒ String
用=替换~ 用&替换^ 解码成微信跳转链接Kind: global function
Returns: String - 返回解码结果
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入字符串 |
debounce(fn, delay, immediate) ⇒ function
空闲控制 返回函数连续调用时,空闲时间必须大于或等于 delay,fn 才会执行Kind: global function
Returns: function - 实际调用函数
| Param | Type | Description |
| --- | --- | --- |
| fn | function | 要调用的函数 |
| delay | number | 空闲时间 |
| immediate | bool | 给 immediate参数传递false 绑定的函数先执行,而不是delay后后执行。 |
decodeBase64(input) ⇒ String
base64解码Kind: global function
Returns: String - 解码后的字符串
| Param | Type | Description |
| --- | --- | --- |
| input | String | 需要解码的字符串 |
decodeUtf8(input) ⇒ String
解码Utf8Kind: global function
Returns: String - 解码后的字符串
| Param | Type | Description |
| --- | --- | --- |
| input | String | 需要解码的字符串 |
delCookie(name)
删除cookieKind: global function
| Param | Type | Description |
| --- | --- | --- |
| name | String | cookie名称 |
delLocal(name)
删除localStorageKind: global function
| Param | Type | Description |
| --- | --- | --- |
| name | String | 名称 |
delSession(name)
删除sessionStorageKind: global function
| Param | Type | Description |
| --- | --- | --- |
| name | String | 名称 |
enWxJumpLink(string) ⇒ String
用*替换= 用!替换& 转码成微信跳转链接
name=exMall-detail-goodsInfoId¶ms[goodsInfoId]=8866 转成 nameexMall-detail-goodsInfoId!params(goodsInfoId)8866Kind: global function
Returns: String - 返回转码结果
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入字符串 |
enWxJumpLinkOld(string) ⇒ String
用~替换= 用^替换& 转码成微信跳转链接Kind: global function
Returns: String - 返回转码结果
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入字符串 |
encodeBase64(input) ⇒ String
字符串、数字转base64Kind: global function
Returns: String - 返回BASE64编码
| Param | Type | Description |
| --- | --- | --- |
| input | String | 需要编码的字符串 |
encodeUtf8(input) ⇒ String
编码Utf8Kind: global function
Returns: String - 返回UTF-8编码
| Param | Type | Description |
| --- | --- | --- |
| input | String | 需要编码的字符串 |
formatTime(time, fmt) ⇒ String
日期格式化插件
使用方式:formatTime(new Date(), "yyyy-MM-dd")Kind: global function
Returns: String - 返回字符串
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| time | Date/String | | 时间对象或者字符串 |
| fmt | String | yyyy-MM-dd | 格式化风格 |
formatTimeStr(time, fmt) ⇒ String
格式化时间成:刚刚、几分钟前Kind: global function
Returns: String - 返回字符串
| Param | Type | Description |
| --- | --- | --- |
| time | Date/String | 时间对象或者字符串 |
| fmt | String | 格式化风格 |
getAppVersion(appName, withosstr, userAgent) ⇒ Boolean \| null
获取APP版本号Kind: global function
Returns: Boolean \| null - null/true/false
| Param | Type | Description |
| --- | --- | --- |
| appName | String | app名称 |
| withosstr | Boolean | 是否需要带上名称 |
| userAgent | String | ua,可不传,默认取navigator.appVersion |
getCHSLength(str) ⇒ Number
获取文本长度,中文算2个字节Kind: global function
Returns: Number - 返回长度
| Param | Type | Description |
| --- | --- | --- |
| str | String | 字符串 |
getCookie(name) ⇒ String
读取cookiesKind: global function
Returns: String - 返回cookie字符串
| Param | Type | Description |
| --- | --- | --- |
| name | String | cookie名称 |
getDirParam(url) ⇒ Object
获取目录形式URL参数Kind: global function
Returns: Object - 返回参数对象
| Param | Type | Description |
| --- | --- | --- |
| url | String | 传入url地址 |
getFileType(url) ⇒ String
文件后缀名Kind: global function
Returns: String - 返回文件后缀
| Param | Type | Description |
| --- | --- | --- |
| url | String | 文件名 |
getIsAppVersionLastest(appName, compareVer, userAgent) ⇒ Boolean \| null
版本号大小对比Kind: global function
Returns: Boolean \| null - null/true/false
| Param | Type | Description |
| --- | --- | --- |
| appName | String | app名称 |
| compareVer | String | 必传 需要对比的版本号 |
| userAgent | String | ua,可不传,默认取navigator.appVersion |
getLocal(name) ⇒ String
读取localStorageKind: global function
Returns: String - 返回localStorage
| Param | Type | Description |
| --- | --- | --- |
| name | String | 名称 |
getNumber(string) ⇒ String
获取字符串中的数字Kind: global function
Returns: String - 返回纯数字字符串
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入带数字的字符串 |
getOsVersion(osName, withosstr, userAgent) ⇒ Boolean \| null
获取手机系统版本Kind: global function
Returns: Boolean \| null - null/true/false
| Param | Type | Description |
| --- | --- | --- |
| osName | String | 系统类型字符串Android、iPod、iWatch或iPhone |
| withosstr | Boolean | 是否需要带上名称 |
| userAgent | String | ua,可不传,默认取navigator.appVersion |
getParameter(name) ⇒
获取单个URL参数Kind: global function
Returns: 返回参数值
| Param | Type | Description |
| --- | --- | --- |
| name | String | 参数名称 |
getRandomNum(min, max) ⇒ Number
获取随机整数Kind: global function
Returns: Number - 返回数字
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| min | Number | 1 | 随机数的最小值 |
| max | Number | 10 | 随机数的最大值 |
getRandomStr(len, widthSpecialChar) ⇒ String
获取随机字符串Kind: global function
Returns: String - 随机串
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| len | Number | 32 | 需要获取随机字符串的长度 |
| widthSpecialChar | Boolean | false | 可选,是否需要生成带特殊字符的串 |
getRandomStrWidthSpecialChar(len) ⇒ String
获取随机字符串带特殊符号Kind: global function
Returns: String - 随机串
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| len | Number | 32 | 需要获取随机字符串的长度 |
getScrollPosition() ⇒ String
获取滑动到顶部和底部 返回'top' 'bottom',建议使用限流Kind: global function
Returns: String - 返回位置
getSession(name) ⇒ String
读取sessionStorageKind: global function
Returns: String - 返回sessionStorage
| Param | Type | Description |
| --- | --- | --- |
| name | String | 名称 |
getUrlParam(url) ⇒ Object
获取URL参数Kind: global function
Returns: Object - 返回参数列表
| Param | Type | Description |
| --- | --- | --- |
| url | String | 传入url参数 |
getWindowSize() ⇒ Object
getWindowSize获取窗口大小Kind: global function
Returns: Object - 返回宽高
imgAdapt(imgurl, size) ⇒ String
扩展图片自动适应多种分辨率small originalKind: global function
Returns: String - 返回新地址
| Param | Type | Description |
| --- | --- | --- |
| imgurl | String | 图片url |
| size | String | 图片规格 |
imgChoose(imgurl) ⇒ String
扩展图片自动适应多种分辨率@2x @3xKind: global function
Returns: String - 返回新地址
| Param | Type | Description |
| --- | --- | --- |
| imgurl | String | 图片地址 |
isDigitals(str) ⇒ Boolean
是否为由数字组成的字符串Kind: global function
Returns: Boolean - 返回true/false
| Param | Type | Description |
| --- | --- | --- |
| str | String | 待检测的字符串 |
isExitsFunction(funcName) ⇒ Boolean
是否存在指定函数Kind: global function
Returns: Boolean - 返回true/false
| Param | Type | Description |
| --- | --- | --- |
| funcName | String | 传入函数名 |
isExitsVariable(variableName) ⇒ Boolean
是否存在指定变量Kind: global function
Returns: Boolean - 返回true/false
| Param | Type | Description |
| --- | --- | --- |
| variableName | String | 传入变量名称 |
pattern() ⇒ Object
pattern返回一些常用的正则:any, arrjson, chinese, email, float, isjson, json, mobile, number, pass, postcode, qq, string, tel, textarea, url, usernameKind: global function
Returns: Object - 返回对象
removeEvent(element, type, handler)
removeEvent移除由addEvent创建的事件委托Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| element | Object | js dom对象 |
| type | String | 事件类型。不需要加on |
| handler | function | 回调方法 |
setCookie(name, value, seconds)
setCookie写入cookie的方法Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| name | String | cookie名称 |
| value | \* | 设置要存储的值,可以是对象或字符串 |
| seconds | Number | cookie有效时间 |
setLocal(name, value, seconds)
写localStorageKind: global function
| Param | Type | Description |
| --- | --- | --- |
| name | String | 名称 |
| value | \* | 设置要存储的值,可以是对象或字符串 |
| seconds | Number | 有效时间 |
setSession(name, value, seconds)
写sessionStorageKind: global function
| Param | Type | Description |
| --- | --- | --- |
| name | String | 名称 |
| value | \* | 设置要存储的值,可以是对象或字符串 |
| seconds | Number | 有效时间 |
stopBubble(e) ⇒ Boolean
阻止冒泡Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| e | Object | dom的event对象 |
stopDefault(e) ⇒ Boolean
阻止默认事件Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| e | Object | dom的event对象 |
textareaInsertText(obj, str)
textarea或input对象在指定的光标位置插入文字Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| obj | Object | dom对象 |
| str | String | 要插入的文字 |
textareaMoveToEnd(obj)
textarea或input对象将光标定位到文字尾部Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| obj | Object | dom对象 |
throttle(fn, delay, immediate) ⇒ function
频率控制 返回函数连续调用时,fn 执行频率限定为每多少时间执行一次Kind: global function
Returns: function - 实际调用函数
| Param | Type | Description |
| --- | --- | --- |
| fn | function | 需要调用的函数 |
| delay | number | 延迟时间,单位毫秒 |
| immediate | bool | 给 immediate参数传递false 绑定的函数先执行,而不是delay后后执行。 |
trim(string, type) ⇒ String
trim()根据传参来去除空格Kind: global function
Returns: String - 返回新字符串
| Param | Type | Description |
| --- | --- | --- |
| string | String | 传入字符串 |
| type | string | 可选,去除空格的类型l:去除开头空格 r:去除尾部空格 lr:去除两端空格,为空的话去除所有空格 |
$3
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
$3
- 使用 _Readme\_XXX.md_ 来支持不同的语言,
例如` _Readme\_en.md, Readme\_zh.md_