Global

Methods

browserInfo() → {String}

Source:
获取到浏览器信息
Example
browserInfo()
"chrome 91.0.4472.164"
Returns:
浏览器信息
Type
String

byteLength(str) → {number}

Source:
获取字节长度
Example
byteLength('123')
3
byteLength('好')
3
byteLength('a')
1
Parameters:
Name Type Description
str string 输入字符串
Returns:
output 输出字节长度
Type
number

dateAndTimestampConversion(time, isDisplayDate) → {string|number}

Source:
时间戳和日期相互转换
Example
dateAndTimestampConversion(1660120671762) // '2022-08-10'
dateAndTimestampConversion(1660120671762,false) // '2022-08-10 16:37:51'
dateAndTimestampConversion('2022-08-10 16:37:51') // 1660120671000
dateAndTimestampConversion('2022/08/10 16:37:51') // 1660120671000
Parameters:
Name Type Default Description
time number | string 时间戳或日期(日期示例:2022-08-10或者2022/08/10 16:37:51)
isDisplayDate boolean true 是否仅展示年月日 默认true
Returns:
Type
string | number

debounce(callback, time) → {function}

Source:
防抖
Example
window.addEventListener('keyup',debounce(function(e){console.log(e);},500))
Parameters:
Name Type Description
callback function 回调函数
time Number 延迟时间
Returns:
返回函数
Type
function

deepClone(data) → {Object|*}

Source:
深拷贝数据
Example
const originData=[{name:'lc',age:18},[1,2,3]]
 const clonedData=deepClone(originData)
 clonedData[0].age=28
 clonedData[1]=[9,9,6]
 console.log("clonedData",clonedData)
 console.log('originData',originData)
 //  clonedData [ { name: 'lc', age: 28 }, [ 9, 9, 6 ] ]
 //  originData [ { name: 'lc', age: 18 }, [ 1, 2, 3 ] ]
Parameters:
Name Type Description
data Object | * 待拷贝的数据
Returns:
拷贝后的新数据
Type
Object | *

digitUppercase(n)

Source:
人民币小写金额转换为大写
Example
digitUppercase(123456789)
"壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整"
digitUppercase(12.12)
"壹拾贰元壹角贰分"
Parameters:
Name Type Description
n String/number 金额
Returns:
大写金额

floatObj() → {Object}

Source:
浮点数的加减乘除,解决浮点数问题(如果运算数值比较大如123456.789*123456.789,建议使用decimal.js库)
Examples
console.log(floatObj().plus(0.1,0.2))  // 0.3
 
console.log(floatObj().times(19.9,100))  // 1990
Returns:
Type
Object

formatNumberZero(n) → {String}

Source:
生成小于 10 的数字前加 0
Example
formatNumberZero('1') => '01'
formatNumberZero(10) => '10'
Parameters:
Name Type Description
n String | Number 数字或字符串
Returns:
Type
String

formatTime(time, option) → {string}

Source:
与当前时间的间隔显示
Example
formatTime(new Date())
"刚刚"
formatTime(new Date() - 60 * 2 * 1000)
"2分钟前"
formatTime(new Date() - 60 * 60 * 2 * 1000)
"2小时前"
formatTime(new Date() - 60 * 60 * 24 * 1 * 1000)
"1天前"
formatTime(new Date('2021-07-20 15:01') , '{y}-{m}-{d} {h}:{i}')
"2021-07-20 15:01"
formatTime(new Date('2021-07-20 15:01') , '{y}/{m}/{d} {h}-{i}')
"2021/07/20 15-01"
 formatTime(new Date('2021-07-20 15:01') , '{y}/{m}/{d} {h}:{i}')
"2021/07/20 15:01"
Parameters:
Name Type Description
time number 时间 new Date()
option string 需要输出的日期格式
Returns:
Type
string

getDaysInOneMonth(year, month) → {Number}

Source:
获取某月份的天数
Example
getDaysInOneMonth({year: 2020, month: 5}) => 31
Parameters:
Name Type Description
year String
month String
Returns:
某月份的天数
Type
Number

getNowDate(seperator) → {String}

Source:
获取当前日期
Example
getNowDate() => "2020-05-18"
Parameters:
Name Type Default Description
seperator String - 连接字符 默认 -
Returns:
当前日期
Type
String

getObjVal(object, path) → {any}

Source:
获取对象的属性值
Example
const object = { 'a': { 'b': { 'c': 3 } } }
getObjVal(object, "a.b.c") => 3
Parameters:
Name Type Description
object object 需要获取值的对象
path string 需要获取值的属性路径
Returns:
任意值
Type
any

getRemovedQueryStringInUrl(queryKeyopt, urlopt) → {string}

Source:
移除url中某个查询字符串及其对应的值(支持hash及history模式)
Example
getRemovedQueryStringInUrl('name','http://127.0.0.1:8081/?name=aaa') // 'http://127.0.0.1:8081/'
getRemovedQueryStringInUrl('','http://127.0.0.1:8082/?token=aaa') // 'http://127.0.0.1:8082/'
Parameters:
Name Type Attributes Default Description
queryKey string <optional>
token 某一个查询字符串
url string <optional>
window.location.href 网址
Returns:
Type
string

getSearchParam(name, urlopt) → {string}

Source:
获取查询字符串中某个字段的值
Example
console.log(getSearchParam('id','https://h5.qq.com/matchrank/index.html?name=你好&id=20180727'))
 console.log(getSearchParam('name','https://h5.qq.com/matchrank/index.html?name=你好&id=20180727'))
// 20180727
// 你好
Parameters:
Name Type Attributes Default Description
name string 查询字符串中的某个字段
url string <optional>
window.location.href 网址(不填,则为当前网址)
Returns:
Type
string

isArrayEqual(arr1, arr2) → {Boolean}

Source:
判断数组是否相等
Example
isArrayEqual([1,2,3],[1,3,2])
true
isArrayEqual([1, { a: 1 }],[1, { a: 1 }])
true
Parameters:
Name Type Description
arr1 Array 数组1
arr2 Array 数组2
Returns:
Type
Boolean

isExternalPath(path) → {Boolean}

Source:
判断路径是否为http、mailto或tel
Parameters:
Name Type Description
path string 路径值
Returns:
Type
Boolean

isInteger(number) → {boolean}

Source:
是否为整数
Example
isInteger(3) => true
isInteger(3.3) => false
isInteger('') => false
isInteger('3') => false
isInteger(true) => false
isInteger([]) => false
Parameters:
Name Type Description
number any 需要判断的参数
Returns:
true | false
Type
boolean

isObjHasOwnProperty(obj, key) → {boolean}

Source:
判断obj是否为自身属性
Parameters:
Name Type Description
obj
key
Returns:
Type
boolean

isWechatBrowser() → {boolean}

Source:
判断当前浏览器是否为微信内置浏览器
Example
isWechatBrowser() // false
Returns:
Type
boolean

lotteryRandom(list, probs) → {any}

Source:
不同奖项的获取概率不同
Example
lotteryRandom([1, 2, 3], [0.5, 0.35, 0.15]) => 1
Parameters:
Name Type Description
list Array 需要概率性的事物集合
probs Array 概率数组集合
Returns:
概率性的事物
Type
any

paramToObj(url) → {Object}

Source:
url 获取url上的所有查询参数
Example
paramToObj("https://test.com?a=1&b=2") => { a: '1', b: '2' }
paramToObj('http://127.0.0.1:8080/?a=sdds&token=taa&user=787#/gamelist') => {"a":"sdds","token":"taa","user":"787"}
Parameters:
Name Type Description
url String
Returns:
? 后的参数对象
Type
Object

scrollAnimation(currentY, targetY)

Source:
动画滚动至目标位置
Example
scrollAnimation(0, 100)
Parameters:
Name Type Description
currentY Number 当前位置
targetY Number 目标位置

sequenceParam(obj) → {String}

Source:
序列化对象
Example
sequenceParam({a: 1, num: 20}) => "a=1&num=20"
Parameters:
Name Type Description
obj Object 需要序列化的参数对象
Returns:
序列化后的字符串
Type
String

thousandthPlace(value) → {String}

Source:
数字千分位分隔法
Example
thousandthPlace(123456.12)
"123,456.12"
Parameters:
Name Type Description
value Number 需要分隔数字
Returns:
返回千分位,分隔的字符串 如果传入的是字符串,直接返回该字符串
Type
String

throttle(callback, wait) → {function}

Source:
节流
Example
window.addEventListener('resize',throttle(function(e){console.log(e);},500))
Parameters:
Name Type Description
callback function 回调函数
wait Number 等候时间
Returns:
返回函数
Type
function

toFixed(number, precision) → {String}

Source:
对数字保留小数点后几位
Example
toFixed(0.105,2) => "0.11"
toFixed('0.105',4) => "0.1050"
toFixed(0.105) => "0.11"
Parameters:
Name Type Default Description
number String | Number 需要保留小数点的数值
precision Number 2 保留小数点后几位 默认 2 位
Returns:
保留后的数值字符串
Type
String