Global

Methods

accAdd(arg1, arg2) → {number}

Source:
Example
accAdd(0.1, 0.2)
// => 0.3
Parameters:
Name Type Description
arg1 number
arg2 number
Returns:

arg1加上arg2的精确结果

Type
number

accDiv(arg1, arg2) → {number}

Source:

除法函数,用来得到精确的除法结果
javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。

Example
accDiv(0.2, 0.3)
// => 0.6666666666666666
Parameters:
Name Type Description
arg1 number
arg2 number
Returns:

arg1除以arg2的精确结果

Type
number

accMul(arg1, arg2) → {number}

Source:

乘法函数,用来得到精确的乘法结果
javascript的乘法结果会由误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果

Example
accMul(0.222, 0.3333)
// => 0.0739926
Parameters:
Name Type Description
arg1 number
arg2 number
Returns:

arg1乘arg2的精确结果

Type
number

accSub(arg1, arg2) → {number}

Source:

减法函数,用来得到精确的减法结果
javascript的减法结果会有误差,在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。

Example
accSub(0.3, 0.2)
// => 0.1
Parameters:
Name Type Description
arg1 number
arg2 number
Returns:

arg1减去arg2的精确结果

Type
number

addChineseUnit(number, decimalDigit) → {string|*}

Source:

为数字加上单位:万或亿

Example
addChineseUnit(1000.01)
// => 1000.01

addChineseUnit(10000)
// => 1万

addChineseUnit(99000)
// => 9.9万

addChineseUnit(566000)
// => 56.6万

addChineseUnit(5660000)
// => 566万

addChineseUnit(44440000)
// => 4444万

addChineseUnit(11111000)
// => 1111.1万

addChineseUnit(444400000)
// => 4.44亿

addChineseUnit(400000000000000000000000)
// => 3999.99万亿亿

addChineseUnit(4000000000000000000000000)
// => 4亿亿亿
Parameters:
Name Type Description
number number

输入数字.

decimalDigit number

返回的小数点后最多的位数,默认为 2

Returns:

加上单位后的数字

Type
string | *

addClass(el, cls)

Source:

dom操作,元素添加某个class

Example
<div class="box flex"></div>
addClass(document.querySelector('.box'), 'flex1');
// => <div class="box flex flex1"></div>
Parameters:
Name Type Description
el

HTML元素

cls

class名称

anagrams(str) → {*}

Source:

Anagrams of string(带有重复项) 使用递归。对于给定字符串中的每个字母,为字母创建字谜。使用map()将字母与每部分字谜组合,然后使用reduce()将所有字谜组合到一个数组中,最基本情况是字符串长度等于2或1。

Example
anagrams('abc');
// => ['abc','acb','bac','bca','cab','cba']
Parameters:
Name Type Description
str
Returns:
Type
*

bytesToSize(bytes) → {string}

Source:

字节转换为B,KB,MB

Example
bytesToSize(10000)
// => 9.8 KB
Parameters:
Name Type Description
bytes Number
Returns:
Type
string

capitalizeEveryWord(str) → {string}

Source:

大写每个单词的首字母

Example
capitalizeEveryWord('hello world!');
// => 'Hello World!'
Parameters:
Name Type Description
str
Returns:
Type
string

changeMoneyToChinese(n) → {string}

Source:

数字金额大写转换,最多支持千亿

Example
changeMoneyToChinese(100111);
=> "壹拾万零壹佰壹拾壹元整"

changeMoneyToChinese(7.52);
=> "柒元伍角贰分"

changeMoneyToChinese(951434677682.00);
=> "玖仟伍佰壹拾肆亿叁仟肆佰陆拾柒万柒仟陆佰捌拾贰元整"
Parameters:
Name Type Description
n number

数字金额

Returns:
Type
string

combineURLs(baseURL, relativeURLs) → {string}

Source:

url字符串拼接

Parameters:
Name Type Description
baseURL string
relativeURLs string
Returns:

The combined URL

Type
string

dataURLToBlob(dataURL) → {*}

Source:

base64转blob

Example
const URI = 'data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMDAiIGhlaWdodD0iMjAwIj48ZGVmcz48c3R5bGUvPjwvZGVmcz48cGF0aCBkPSJNOTU5LjQzNiAyNDMuNUwzNzcuNDEzIDg3MC4yNyA2NC4wMiA0NjcuMzQ0bDExNC43MjctOTcuOTMgMTk4LjY2NiAyMDcuMDYgNDkyLjQ4Mi00MjIuNTEgODkuNTQgODkuNTM3em0wIDAiIGZpbGw9IiM0M2E5ZjEiLz48L3N2Zz4=';

dataURLToBlob(URI);
// => Blob {size: 248, type: "image/svg+xml"}
Parameters:
Name Type Description
dataURL string
Returns:
Type
*

deepClone(obj) → {*}

Source:

深层克隆对象

Example
const a = { foo: 'bar', obj: { a: 1, b: 2 } };
const b = deepClone(a);
// => a !== b, a.obj !== b.obj
Parameters:
Name Type Description
obj
Returns:
Type
*

deepMapKeys(obj, fn) → {Object}

Source:

深层映射对象键

Example
const obj = {
  foo: '1',
  nested: {
    child: {
      withArray: [
        {
          grandChild: ['hello']
        }
      ]
    }
  }
};

 const upperKeysObj = deepMapKeys(obj, key => key.toUpperCase());

 // =>
 {
  "FOO":"1",
  "NESTED":{
    "CHILD":{
      "WITHARRAY":[
        {
          "GRANDCHILD":[ 'hello' ]
        }
      ]
    }
  }
}
Parameters:
Name Type Description
obj
fn
Returns:
Type
Object

dig(obj, target) → {any}

Source:

基于给定的键返回嵌套JSON对象中的目标值

https://30secondsofcode.org/object#dig

Example
const data = {
 level1: {
   level2: {
     level3: 'some data'
   }
 }
};
dig(data, 'level3');
// => 'some data'
dig(data, 'level4');
// => undefined
Parameters:
Name Type Description
obj
target
Returns:
Type
any

encrypt(params)

Source:

加密算法 1.所有入参加入集合M,参数名做key, 值做value 2.提供的密钥1(字段名appid)与密钥2(字段名secret)两项,以及当前时间戳(字段名time)也加入集合M, 3.将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序) 4.集合M所有值拼接成字符串,转化成UTF-8编码格式的字节数组, 最后需要取MD5码(signature摘要值)

Example
const params = { mobile: '15858264900', nickname: 'windraxb', appkey: 'ertfgdf345435568123454rtoiko5=' };

md5(encrypt(params).toUpperCase());
// => md5('APPKEY=ERTFGDF34543545=&MOBILE=15858264903&NICKNAME=windraxb')
Parameters:
Name Type Description
params object

equals(a, b) → {*}

Source:

两个值之间的深入比较,以确定它们是否相等

Example
equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' });

// => true
Parameters:
Name Type Description
a Object
b Object
Returns:
Type
*

filterEmptyPropObj(obj) → {*}

Source:

过滤对象中为空的属性

Example
filterEmptyPropObj({name: 'foo', sex: ''})
// => {name: 'foo'}
Parameters:
Name Type Description
obj
Returns:
Type
*

formatBankCard(val) → {*}

Source:

格式化银行卡
用户在输入银行卡号时,需要以4位4位的形式显示,就是每隔4位加个空格,方便用户校对输入的银行卡是否正确
**注:**一般数据库里面存的都是不带格式的原始数据,所以提交的时候记得过滤下空格再提交哦。毕竟格式化这种算是表现层,前端展示的时候处理下就好,业务逻辑什么用到的卡号可不是格式化后的呢。
还原val.replace(/\s/g, '');

Example
formatBankCard('6225365271562822');
// => 6225 3652 7156 2822
Parameters:
Name Type Description
val string
Returns:
Type
*

formatDate(date, fmt) → {string}

Source:

Date 转化为指定格式的String
月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)可以用 1-2 个占位符
年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)

Example
formatDate(Date.now(), 'yyyy-MM-dd hh:mm:ss.S');
// => 2006-07-02 08:09:04.423

formatDate(Date.now(), 'yyyy-MM-dd E HH:mm:ss');
// => 2009-03-10 二 20:09:04

formatDate(Date.now(), 'yyyy-MM-dd EE hh:mm:ss');
// => 2009-03-10 周二 08:09:04

formatDate(Date.now(), 'yyyy-MM-dd EEE hh:mm:ss');
// => 2009-03-10 星期二 08:09:04

formatDate(Date.now(), 'yyyy-M-d h:m:s.S')
// => 2006-7-2 8:9:4.18
Parameters:
Name Type Description
date string | number

string支持形式:20160126 12:00:00,2016-01-26 12:00:00,2016.01.26 12:00:00,20160126,2016-01-26 12:00:00.0

fmt string
Returns:
Type
string

formatDateToTimeStamp(time) → {number}

Source:

获取指定时间unix时间戳

Example
formatDateToTimeStamp('20160126 12:00:00');
// => 1453780800000

formatDateToTimeStamp('2016-01-26 12:00:00');
// => 1453780800000

formatDateToTimeStamp('2016.01.26 12:00:00');
// => 1453780800000

formatDateToTimeStamp('20160126');
// => 1453737600000

formatDateToTimeStamp('2016-01-26 12:00:00.0');
// => 1453780800000
Parameters:
Name Type Description
time string
Returns:
Type
number

formatMoney(val, symbol) → {string|*|XML|void}

Source:

用符号(默认为逗号)格式化金钱

Example
formatMoney('1234567890');
// => 1,234,567,890
Parameters:
Name Type Description
val string
symbol string

默认,

Returns:
Type
string | * | XML | void

formatNumber(number, decimals, decPoint, thousandsSep, roundTag) → {XML|void|*|string}

Source:

格式化数字、金额、千分位、保留几位小数、舍入舍去

Example
formatNumber(2, 2, '.', ',');
// => 2.00
Parameters:
Name Type Description
number

要格式化的数字

decimals

保留几位小数

decPoint

小数点符号

thousandsSep

千分位符号

roundTag

舍入参数,默认 'ceil' 向上取,'floor'向下取,'round' 四舍五入

Returns:
Type
XML | void | * | string

formatPhone(phone, symbol) → {string|*|XML|void}

Source:

手机号码中间部分替换成指定符号

Example
formatPhone('15858264903');
// => 158****4903
Parameters:
Name Type Description
phone string
symbol string

默认为*

Returns:
Type
string | * | XML | void

formatTimeAgo(ms) → {*}

Source:

将时间转化为几天前,几小时前,几分钟前

Example
formatTimeAgo(1505232000000);
// => 1天前
Parameters:
Name Type Description
ms number
Returns:
Type
*

getBrowser() → {Object}

Source:

获取浏览器的类型和版本号

Example
getBrowser();
// => {type: "chrome", version: "60.0.3112.101"}
Returns:
Type
Object

getDayOfYear(time) → {number}

Source:

获取某个日期是当年中的第几天

Example
getDayOfYear('2014-01-10')
=> 10
Parameters:
Name Type Description
time
Returns:
Type
number

getDayOfYearWeek(time) → {number}

Source:

获取某个日期在这一年的第几周

Example
getDayOfYearWeek('2014-01-10')
=> 2
Parameters:
Name Type Description
time
Returns:
Type
number

getDays(时间, 长度, 方向:) → {Array}

Source:

返回指定长度的天数集合 摘自:https://segmentfault.com/a/1190000013041329

Example
getDays('2018-1-29', 6, 1)
// => ["2018-1-26", "2018-1-27", "2018-1-28", "2018-1-29", "2018-1-30", "2018-1-31", "2018-2-1"]
Parameters:
Name Type Description
时间 time
长度 len
方向: direction

1: 前几天; 2: 后几天; 3:前后几天 默认 3

Returns:

数组

Type
Array

getDevice() → {Object}

Source:

获取移动设备信息,如是否是iOS,android等

Example
getDevice();
// => {"androidChrome":false,"ipad":false,"iphone":true,"android":false,"ios":true,"os":"ios","osVersion":"9.1","webView":null}
Returns:
Type
Object

getDiffDay(startDay, endDay) → {number}

Source:

得到两个时间的时间差(返回天数)

Example
getDiffDay(1501516800000, 1504195200000);
// => 31
Parameters:
Name Type Description
startDay number

开始时间戳

endDay number

结束时间戳

Returns:
Type
number

getImgBase64(url) → {Promise}

Source:

获取图片的base64 url

Parameters:
Name Type Description
url string

图片url

Returns:

图片base64信息

Type
Promise

getIn(p, o) → {*}

Source:

主动防御 对于我们操作的数据,尤其是由 API 接口返回的,时常会有一个很复杂的深层嵌套的数据结构。为了代码的健壮性,很多时候需要对每一层访问都作空值判断,就像这样: props.user && props.user.posts && props.user.posts[0] && props.user.posts[0].comments && props.user.posts[0].comments[0] 代码看起来相当不美观,因此提供了一个非常简洁明了的原生的方式。

Example
var props = {
 user: {
   post: [{
     comments: 'test'
   }]
 }
};
getIn(['user', 'post', 0, 'comments'], props);
// => test
Parameters:
Name Type Description
p

属性列表

o

对象

Returns:

如果正常访问到,则返回对应的值,否则返回 null。

Type
*

getLocationHrefParam(name) → {*}

Source:

获取location.href参数

Example
window.location.href = 'http://www.baidu.com/?a=1&b=2';

getLocationHrefParam('a');
// => 1
Parameters:
Name Type Description
name string
Returns:
Type
*

getLocationSearchParam(name) → {*}

Source:

获取location.search的参数

Example
window.location.href = 'http://www.baidu.com/?a=1&b=2';

getLocationSearchParam('a');
// => 1
Parameters:
Name Type Description
name string
Returns:
Type
*

getMonthOfDay(时间) → {number}

Source:

获取某月有多少天 摘自:https://segmentfault.com/a/1190000013041329

Example
getMonthOfDay('2018-1-29')
// => 31
Parameters:
Name Type Description
时间 time
Returns:

天数

Type
number

getPixelRatio() → {number}

Source:

获取设备像素比

Example
// window.navigator.appVersion(5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1)
getPixelRatio();
// => 2
Returns:
Type
number

getURLParameters(url) → {Object}

Source:

获取网址参数

Example
getURLParameters('http://url.com/page?name=Adam&surname=Smith');
=> // {name: 'Adam', surname: 'Smith'}
Parameters:
Name Type Description
url string
Returns:

返回包含当前URL参数的对象。

Type
Object

getYearOfDay(time) → {number}

Source:

获取某年有多少天

Example
getYearOfDay('2014')
=> 365
Parameters:
Name Type Description
time
Returns:
Type
number

hasClass(el, cls) → {boolean}

Source:

Dom操作,元素是否包含某个class

Example
<div class="box flex"></div>
hasClass(document.querySelector('.box'), 'flex');
// => true
Parameters:
Name Type Description
el

HTML元素

cls

class类名

Returns:
Type
boolean

htmlDecode(str) → {string}

Source:

html字符解码

Example
htmlDecode('&lt;script&gt;');
// => <script>
Parameters:
Name Type Description
str string
Returns:
Type
string

htmlEncode(str) → {string}

Source:

html字符编码

Example
htmlEncode('<script>');
// => &lt;script&gt;
Parameters:
Name Type Description
str string
Returns:
Type
string

inAlipay() → {boolean}

Source:

是否是支付宝内核

Example
inAlipay();
// => false
Returns:
Type
boolean

inQQBrowser() → {boolean}

Source:

是否是QQ浏览器内核

Example
inQQBrowser();
// => false
Returns:
Type
boolean

inUCBrowser() → {boolean}

Source:

是否是UC浏览器内核

Example
inUCBrowser();
// => false
Returns:
Type
boolean

inWeibo() → {boolean}

Source:

是否是微博内核

Example
inWeibo();
// => false
Returns:
Type
boolean

inWeixin() → {boolean}

Source:

是否是微信内核

Example
inWeixin();
// => false
Returns:
Type
boolean

isCardId(val) → {boolean}

Source:

是否为有效的身份证号,支持1/2代(15位/18位数字)

Example
isCardId('411423198807127834');
// => true
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isChinese(str) → {boolean}

Source:

是否为中文

Example
isChinese('中文');
// => true
Parameters:
Name Type Description
str string
Returns:
Type
boolean

isDigit(val) → {boolean}

Source:

是否为数字

Example
isDigit('abc');
// => false
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isEmpty(val) → {boolean}

Source:

是否为空 如果a值是空对象,集合,没有可枚举属性或任何不被视为集合的类型,则返回true。

https://30secondsofcode.org/type#isempty

Example
isEmpty([]);
// => true
isEmpty({});
// => true
isEmpty('');
// => true
isEmpty([1, 2]);
// => false
isEmpty({ a: 1, b: 2 });
// => false
isEmpty('text');
// => false
isEmpty(123);
// => true - type is not considered a collection
isEmpty(true);
// => true - type is not considered a collection
Parameters:
Name Type Description
val
Returns:
Type
boolean

isEmptyObject(val) → {boolean}

Source:

是否为空对象

Example
isEmptyObject({});
// => true
Parameters:
Name Type Description
val
Returns:
Type
boolean

isHTML(str) → {boolean}

Source:

是否为 HTML 标签

Example
isHTML('<p>123</p>');
// => true
Parameters:
Name Type Description
str string
Returns:
Type
boolean

isLeapYear(val) → {boolean}

Source:

是否为闰年

Example
isLeapYear(2000);
// => true
Parameters:
Name Type Description
val number
Returns:
Type
boolean

isLetters(val) → {boolean}

Source:

是否为字母

Example
isLetters('1234');
// => false
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isMobileLoose(val) → {boolean}

Source:

是否为有效的手机号,中国手机号(最宽松), 只要是1开头即可, 如果你的手机号是用来接收短信, 优先建议选择这一条

Example
isMobileLoose('008618311006933');
// => true

isMobileLoose('+8617888829981');
// => true

isMobileLoose('19119255642');
// => true
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isPhoneX() → {boolean}

Source:

判断 iPhone X Series 机型,刘海屏

Example
isPhoneX()
=> true
Returns:
Type
boolean

isValidBankNo(val) → {boolean}

Source:

是否为有效的银行卡号(10到30位, 覆盖对公/私账户, 参考微信支付

Example
isValidBankNo('6234567890');
// => true

isValidBankNo('6222026006705354217');
// => true
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isValidBase64Format(val) → {boolean}

Source:

是否为有效的 base64格式

Example
isValidBase64Format('data:image/gif;base64,xxxx==')
=> true
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isValidEmail(val) → {boolean}

Source:

是否为有效的邮箱地址
名称允许汉字、字母、数字,域名只允许英文域名
中文如:杨元庆001Abc@lenovo.com.cn

Example
isValidEmail('123456@qq.com');
// => true
Parameters:
Name Type Description
val string
Returns:
Type
boolean

isValidUserName(val, minLength, maxLength) → {boolean}

Source:

是否为有效的用户名,4到16位(字母,数字,下划线,减号)

Example
isValidUserName('xiaohua_qq');
// => true
Parameters:
Name Type Description
val string
minLength number
maxLength number
Returns:
Type
boolean

last(array) → {boolean}

Source:

获取数组的最后一项

Example
last(['1,2,3']);
// => '3';
Parameters:
Name Type Description
array
Returns:
Type
boolean

mapKeys(obj, fn) → {Object}

Source:

根据提供函数生成的键生成一个新对象 使用 Object.keys(obj) 来迭代对象的键。 使用 Array.reduce() 创建一个具有相同值的新对象,并使用 fn 来映射键。

Example
mapKeys({ a: 1, b: 2 }, (val, key) => key + val);
// => { a1: 1, b2: 2 }
Parameters:
Name Type Description
obj
fn
Returns:
Type
Object

mapValues(obj, fn) → {Object}

Source:

根据提供函数返回的值映射一个新对象

Example
const users = {
  fred: { user: 'fred', age: 40 },
  pebbles: { user: 'pebbles', age: 1 }
};
mapValues(users, u => u.age);
// => { fred: 40, pebbles: 1 }
Parameters:
Name Type Description
obj
fn
Returns:
Type
Object

merge(objs) → {*}

Source:

从两个或多个对象的组合中创建一个新对象

https://30secondsofcode.org/object#merge

Example
merge(
 {
  a: [{ x: 2 }, { y: 4 }],
  b: 1
},
 {
  a: { z: 3 },
  b: [2, 3],
  c: 'foo'
});
// => { a: [ { x: 2 }, { y: 4 }, { z: 3 } ], b: [ 1, 2, 3 ], c: 'foo' }
Parameters:
Name Type Description
objs
Returns:
Type
*

numberToChinese(num) → {string}

Source:

数字转换成中文的大写数字

Example
numberToChinese(10001010);
=> "一千万一千一十"
Parameters:
Name Type Description
num
Returns:
Type
string

preZeroFill(num, size) → {string}

Source:

对整数进行前置补0

Example
preZeroFill(12, 3);
// => 012
Parameters:
Name Type Description
num number

数值

size number

要补0的位数

Returns:
Type
string

removeClass(el, cls)

Source:

Dom 操作,元素删除某个 class

Example
<div class="box flex"></div>
removeClass(document.querySelector('.box'), 'flex');
// => <div class="box"></div>
Parameters:
Name Type Description
el

HTML元素

cls

css类名

scrollToTop()

Source:

滚动到顶部 使用document.documentElement.scrollTop或document.body.scrollTop获取到顶部的距离。从顶部滚动一小部分距离。 使用window.requestAnimationFrame()来滚动。

Example
scrollToTop();

timeTaken(callback) → {*}

Source:

测试函数所花费的时间

Example
timeTaken(() => Math.pow(2, 10));
// => 1024
Parameters:
Name Type Description
callback
Returns:
Type
*

toNonExponential(num) → {string}

Source:

科学计数法转化为数值字符串形式

Example
toNonExponential(3.3e-7);
=> // "0.00000033"

toNonExponential(3e-7);
=> // "0.0000003"

toNonExponential(1.401e10);
=> // "14010000000"

toNonExponential(0.0004);
=> // "0.0004"
Parameters:
Name Type Description
num number
Returns:
Type
string

trim$1(str, type) → {*}

Source:

清除空格

Example
trim(' 123 ');
// => 123
Parameters:
Name Type Description
str
type

1-所有空格 2-前后空格 3-前空格 4-后空格

Returns:
Type
*