参考: https://github.com/RayAiden/ts-utils
各种常用的 JS 工具集
npm i @mtjs/utils -S
- formatDate
- formatISODate
- formatISOTime
- formatISODateTime
- insureDate
- getLastDayOfMonth
- getBeginningOfDate
- getEnddingOfDate
- 参数: dateValue 时间戳 | 日期字符串
- 参数: formatString yyyyMMdd mm:hh:ss
formatDate(new Date().getTime(), "yyyy/MM/dd hh:mm:ss"); // "2019/11/06 15:31:50"
date: 时间戳 | 日期字符串 | 日期对象
formatISODate(new Date()); // 2019-11-06
date: 时间戳 | 日期字符串 | 日期对象
formatISOTime(new Date()); // 07:27:33
date: 时间戳 | 日期字符串 | 日期对象
formatISODateTime(new Date()); // 2019-11-06 07:27:33
date: 时间戳 | 日期字符串 | 日期对象
insureDate(new Date()); // 2020-08-25T16:00:00.000Z
year: 时间戳 month: 时间戳
getLastDayOfMonth('2020','8'); // '2020-8-31'
date: 时间戳 | 日期字符串 | 日期对象 ISOString: 是否使用 ISO 标准返回 Date 对象的字符串格式 | Boolean
getBeginningOfDate(1598343480735, true) // "2020-08-24T16:00:00.000Z"
getBeginningOfDate(1598343480735, false) // '2020-08-25 00:00:00'
date: 时间戳 | 日期字符串 | 日期对象 ISOString: 是否使用 ISO 标准返回 Date 对象的字符串格式 | Boolean
getEnddingOfDate(1598343480735, true) // "2020-08-24T16:00:00.000Z"
getEnddingOfDate(1598343480735, false) // '2020-08-25 23:59:59'
- scrollTop
- raf
- cancelRaf
滚动到顶部 scrollTop(el: HTMLElement | Window, from = 0, to: number, duration = 500, endCallback: Function)
el: 需要滚动的元素 | HTMLElement | Window, from: 滚动开始的位置 | 0, to: 滚动终止的位置, duration: 滚动持续的时间 | 500, endCallback: 滚动完成之后的回调函数
- numeral
- randomNum
- toFixedNum
- toDecimalMark
- epsEqDecimal
- px2vw
- vw2Px
- kb2mb
- formatPrice
- unFormat
- formatNum
- add
- sub
- mul
- div
- byte2mb
- toPercent
- formatMoney
- formatWeek
min: 最小值 max: 最大值
randomNum(5,10) // => 5 || 6 || 7 || 8 || 9 || 10
n: 操作的数字 decimals: 精确到几位小数
round(12.555,2) // => 12.56
num: 操作数 mark: 操作符 (如:¥、$)
toDecimalMark(12345674654.123,'¥') // => "¥12,345,674,654.123"
x: 操作数(小数) y: 操作数(小数)
epsEqDecimal(1.2, 1.2) // true
v px数值 useUnit: 是否添加 vw 单位
px2vw(33) // 4.4vw
v: vw数值 useUnit: 是否添加 px 单位
vw2Px(33) // 247.5px
num: kb数值
kb2mb(15960000) // 15.96M
num:金额(分) formatStr:格式化的字符(如:¥、$)
formatPrice(2300, '$') // $23
num:操作数
unFormat(2.10) // 2.1
unFormat(2.00) // 2
v: 操作数 formatStr:?
formatNum(123.123) // '123'
x: 操作数(小数) y: 操作数(小数)
add(1.1, 2.2) // 3.3
x: 操作数(小数) y: 操作数(小数)
sub(2.1, 1.1) // 1
x: 操作数(小数) y: 操作数(小数)
mul(2.2, 1.1) // 2.42
x: 操作数(小数) y: 操作数(小数)
div(6.6, 3.3) // 2
n: 数值 fixed: 保留小数位数
toPercent(0.85633, 1) // '85.6%'
price:价格 symbol:价格符号($、¥、'') fixed:保留小数位数(0、1、2)
formatMoney(5050, '$', 1) // '+$50.5'
num: 第几天 fmtStr: w 周, ww 星期 startOfZero: true 下标为 0 是 '一', false 下标为 1 是 '日'
formatWeek(2, 'w', false) // '周二'
formatWeek(2, 'w', true) // '星期三'
- isAndroid
- isIphone
- isIpad
- isWx
- isAli
- isPhone
- isObject
- isFunction
- isString
- isBoolean
- isPlainObject
- isUndefined
- isArray
- isNull
- isNullOrUndefined
- isEmpty
- deepClone
- deepMerge
返回值是 Boolean
isAndroid // true: 是安卓, false: 不是安卓
返回值是 Boolean
isIphone // true: 是iphone 或 ipad, false: 不是
返回值是 Boolean
isIpad // true: 是 ipad, false: 不是 ipad
返回值是 Boolean
isWx // true: 是 微信环境, false: 不是 微信环境
返回值是 Boolean
isAli // true: 是 支付宝环境, false: 不是 支付宝环境
返回值是 Boolean
isPhone // true: 是 手机端, false: 不是 手机端
返回值是 Boolean
isObject({name: 'mike'}) // true: 是 对象
isObject('nihao') // false: 不是 对象
返回值是 Boolean
isFunction // true: 是 方法, false: 不是 方法
返回值是 Boolean
isString // true: 是 字符串, false: 不是 字符串
返回值是 Boolean
isNumber // true: 是 Number, false: 不是 Number
返回值是 Boolean
isObject // true: 是 Boolean, false: 不是 Boolean
返回值是 Boolean
isPlainObject // true: 是 字面量定义的对象, false: 不是 字面量定义的对象
返回值是 Boolean
isUndefined // true: 是 undefined, false: 不是 undefined
返回值是 Boolean
isArray // true: 是 数组, false: 不是 数组
返回值是 Boolean
isDate // true: 是 Date, false: 不是 Date
返回值是 Boolean
isUndefined // true: 是 null, false: 不是 null
返回值是 Boolean
isNullOrUndefined // true: 是 null 或 undefined, false: 不是 null 或 undefined
obj: 任意对象
const nu = null, undef = undefined
isEmptyObj({}) // true
isEmptyObj([]) // true
isEmptyObj('') // true
isEmptyObj(nu) // true
isEmptyObj(undef) // true
isEmptyObj('hello') // false
obj: 任意对象
const a = { foo: 0 }, b = { foo: 0, bar: 1 }, arr = [a, b], noop = () => {};
deepClone(a) // { foo: 0 };
deepClone(b) // { foo: 0, bar: 1 };
deepClone(noop) // () => {};
deepClone(arr) // [ { foo: 0 }, { foo: 0, bar: 1 } ];
deepClone(undefined) // undefined;
object: 合并的目标对象 sources:源对象
let object = {name: 'hello'}, sources = ['w','o']
deepMerge( object, sources ) // { "0": "w", "1": "o", "name": "hello" }
- phoneReg
- priceReg
- idCardReg
- officeCertReg
- passportReg
- hmCardReg
- chineseReg
- intReg
- gtZeroIntReg
- emailReg
- telReg
- englishReg
- numberOrLetterReg
- urlReg
- hasSpaceReg
- nameReg
- specialCharReg
- imageReg
- lenLess6Reg
- lenMore20Reg
- allNumReg
- allLetterReg
- allSpecialCharsReg
- hasOtherCharReg
- repeatedMore3Reg
phoneReg.test('13155695875') // true
phoneReg.test('123456789') // fasle
phoneRule.pattern.test('123456789') // fasle
phoneRule.message // '请输入正确的手机号码'
最多保留两们小数
priceReg.test('2.33') // true
priceReg.test('2.3333') // fasle
priceRule.pattern.test('2.3333') // fasle
priceRule.message // '请输入最多两位小数的金额'
idCardReg.test('360622198502025523') // true
idCardReg.test('123456789') // fasle
idCardRule.pattern.test('123456789') // fasle
idCardRule.message // '身份证格式不正确'
officeCertReg.test('政字第00111206号') // true
officeCertReg.test('123456789') // fasle
officeCertRule.pattern.test('123456789') // fasle
officeCertRule.message // '军官证格式不正确'
passportReg.test('H280013(6)') // true
passportReg.test('123456789') // fasle
passportReg.pattern.test('123456789') // fasle
passportReg.message // '护照格式不正确'
hmCardReg.test('G07643214') // true
hmCardReg.test('123456789') // fasle
hmCardRule.pattern.test('123456789') // fasle
hmCardRule.message // '港澳通行证格式不正确'
chineseReg.test('你好') // true
chineseReg.test('123456789') // fasle
chineseRule.pattern.test('123456789') // fasle
chineseRule.message // '请输入中文字符'
within0IntReg.test('0') // true
within0IntReg.test('-2') // fasle
within0IntRule.pattern.test('-2') // fasle
within0IntRule.message // '请输入大于等于0的正整数'
more0IntReg.test('12') // true
more0IntReg.test('0') // fasle
more0IntRule.pattern.test('0') // fasle
more0IntRule.message // '请输入大于0的整数'
emailReg.test('156@qq.com') // true
emailReg.test('123456789') // fasle
emailRule.pattern.test('13155695875') // false
emailRule.message // '邮箱格式不正确'
telReg.test('015-1454514') // true
telReg.test('123456789') // fasle
telRule.pattern.test('13155695875') // fasle
telRule.message // '座机号格式不正确'
englishReg.test('abcdefg') // true
englishReg.test('123456789') // fasle
englishRule.pattern.test('13155695875') // fasle
englishRule.message // '请输入英文字符'
numberOrLetterReg.test('123abc') // true
numberOrLetterReg.test('你哈') // fasle
numberOrLetterRule.pattern.test('你哈') // fasle
numberOrLetterRule.message // '请输入数字或字母'
urlReg.test('https://hello.com') // fasle
urlReg.test('你哈') // fasle
urlRule.pattern.test('你哈') // true
urlRule.message // '请输入以 http: 或 https: 开头的链接地址'
hasSpaceReg.test('1 2') // true
hasSpaceReg.test('你哈') // fasle
hasSpaceRule.pattern.test('你哈') // fasle
hasSpaceRule.message // '不能含有空格'
nameReg.test('李好好') // true
nameReg.test('123') // fasle
nameRule.pattern.test('123') // fasle
nameRule.message // '请输入正确姓名'
specialCharReg.test('!@#$%^&') // true
specialCharReg.test('你哈') // fasle
specialCharRule.pattern.test('!@#$%^&') // true
specialCharRule.message // '不能包含特殊字符'
imageReg.test('test.jpg') // true
imageReg.test('你哈') // fasle
imageRule.pattern.test('你哈') // fasle
imageRule.message // '不是图片文件'
lenLess6Reg.test('123') // true
lenLess6Reg.test('123456') // fasle
lenLess6Rule.pattern.test('123') // true
lenLess6Rule.message // '密码长度为6-20个字符'
lenMore20Reg.test('1234567890abcdefghijklmn') // true
lenMore20Reg.test('123456') // fasle
lenMore20Rule.pattern.test('1234567890abcdefghijklmn') // true
lenMore20Rule.message // '密码长度为6-20个字符'
allNumReg.test('123456') // true
allNumReg.test('123456aa') // fasle
allNumRule.pattern.test('123456') // true
allNumRule.message // '密码由数字、特殊字符(! # $ % ^ & * @)、字母中的至少两种组成'
allLetterReg.test('abcdefg') // true
allLetterReg.test('123456aaa') // fasle
allLetterRule.pattern.test('abcdefg') // true
allLetterRule.message // '密码由数字、特殊字符(! # $ % ^ & * @)、字母中的至少两种组成'
allSpecialCharsReg.test('#$%^*@') // true
allSpecialCharsReg.test('123456%%$') // fasle
allSpecialCharRule.pattern.test('#$%^*@') // true
allSpecialCharRule.message // '密码由数字、特殊字符(! # $ % ^ & * @)、字母中的至少两种组成'
hasOtherCharReg.test('123++@@') // true
hasOtherCharReg.test('123456@@') // fasle
hasOtherCharRule.pattern.test('123++@@') // true
hasOtherCharRule.message // '密码由数字、特殊字符(! # $ % ^ & * @)、字母中的至少两种组成'
repeatedMore3Reg.test('12311111') // true
repeatedMore3Reg.test('123456') // fasle
repeatedMore3Rule.pattern.test('12311111') // true
repeatedMore3Rule.message // '密码不能有3位连续或重复的字符'
- numberMask
- getFileSuffix
- delWhitespace
- nameMask
- randomId
- idCardAndPhoneMask
- getInfoByIdCard
str:字符串 startIndex: 起始索引 endIndex: 结束索引 _mask: 遮蔽字符
utilscore.mask('12398765432',3,7) // => "123****5432"
fileName: 文件名
getFileSuffix('1.jpg') // .jpg
input: 字符串
deleteWhitespace('我是 测试 的 字符串 '); // '我是测试的字符串'
deleteWhitespace(''); //''
length: id的长度,不包含前缀长度 | 默认是 6 prefix 前缀 | 默认为空
randomId() // 05bbfd
val: 证件号 | string ISOString: 是否使用 ISO 标准返回 Date 对象的字符串格式 | Boolean
idCardAndPhoneMask('110101199003074813') // 110********4813
idCardAndPhoneMask('南字第3044444号') // 南字第****444号
idCardAndPhoneMask('G07643214') // G0***3214
idCardAndPhoneMask('H280013(6)') // H2****3(6)
idCardAndPhoneMask('13028829998') // 130****9998
name: 名称
nameMask('张三') // 张*
nameMask('张小三') // 张*三
nameMask('张标准三') // 张*三
根据身份证获取身份信息(年龄、性别、生日): getInfoByIdCard( idCard: String ): {age: number, gender: number, birthday: string}
idCard: 身份证号码
return: {age: number, gender: number, birthday: string}
getInfoByIdCard('360622199606296621') --> { "age": 24, "birthYear": "1996", "gender": 1 }
- url
- getUrlParam
- resolve
- urlToList
- removeParam
- addParam
- updateParam
- obj2pms
- buildUrl
- push
- replace
- go
- doWxAuth
key:获取对应key的值 | 默认获取所有 key 值 path:url地址 | 默认当前地址
getUrlParam('name', 'http://hh.com?name=aaa') // 'aaa'
href: url地址 path: 路径
resolve('http://foo.com/a/', 'b/c') => http://foo.com/a/b/c
url: url地址
urlToList('http://foo.com/a/') => ["/http:", "/http:/foo.com", "/http:/foo.com/a"]
keys: 要删除的 key 的集合 path: 地址 | 默认为当前地址
removeParam(['name'],'http://foo.com/a?name=mike') // 'http://foo.com/a'
params 要添加的键值对 path: 地址 | 默认为当前地址
addParam({name: 'mike'}, 'http://foo.com/a') // 'http://foo.com/a?name=mike'
params 要添加的键值对 path: 地址 | 默认为当前地址
updateParam({name: 'mike'}, 'http://foo.com/a?name=july') // 'http://foo.com/a?name=mike'
obj: JSON对象
obj2pms({a:1, b:2}) => 'a=1&b=2'
path: 路径 params: JSON对象
buildUrl('/login', {from: 'home'}) => '/login?from=home'
options: {path: '', query: {}} | url: string
push('/login') 或 push('/login',{name: 'a'}) // window.location.href = '/login?name=a'
options: {path: '', query: {}} | url: string
replace('/login') 或 replace('/login',{name: 'a'})
num:number
go(-1) // 浏览器地址回退 1步
appId:微信appId
if(!sessionStorage.getItem('code')) doWxAuth('xxxx')