一、对象清空 resetData
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
export const resetData = (valueObj: any) => { if (Object.prototype.toString.call(valueObj) === '[object Object]') { const keys = Object.keys(valueObj) const obj: { [name: string]: string | Array<any> | boolean | number } = {} keys.forEach(item => { obj[item] = Object.prototype.toString.call(valueObj[item]) === '[object Array]' ? [] : typeof valueObj[item] === 'number' ? 0 : typeof valueObj[item] === 'boolean' ? false : '' }) Object.assign(valueObj, obj) } else { const keys = Object.keys(valueObj[0]) const obj: { [name: string]: string | Array<any> | boolean | number } = {} keys.forEach(item => { obj[item] = Object.prototype.toString.call(valueObj[item]) === '[object Array]' ? [] : typeof valueObj[item] === 'number' ? 0 : typeof valueObj[item] === 'boolean' ? false : '' }) Object.assign(valueObj[0], obj) } }
|
二、js浮点数乘法处理
1 2 3 4 5 6 7 8
|
export const accMul = (arg1: number, arg2?: number) => { return arg2 ? Number(Number(arg1 * 100).toFixed(arg2)) : Number(Number(arg1 * 100).toFixed(2)) }
|
三、js浮点数除法处理
1 2 3 4 5 6 7 8
|
export const accDiv = (arg1: number, arg2?: number) => { return arg2 ? Number(Number(arg1 / 100).toFixed(arg2)) : Number(Number(arg1 / 100).toFixed(2)) }
|
四、数字转金额格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
export const numberToMoney = (num: number | BigNumber, decimals = 2, thousandsSep = ','): string => { if (isNaN(num as any)) { return '' } const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals) const sep = typeof thousandsSep === 'undefined' ? ',' : thousandsSep const s = new BigNumber(num).toString().replace(/,/g, '') const p = new BigNumber(s) const n = p.isNaN() ? 1 : p const formatNum = n .toFixed(prec) .toString() .replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) { return $1 + sep }) return num ? formatNum : '' }
|
五、时间戳转日期格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| type dateFormats = 'y-M-d' | 'y-M-d H:m:s' | 'y年M月d日' | 'y年M月d日 H时i分'
export const dateFormat = (timestape: number, formats: dateFormats = 'y-M-d H:m:s'): string => { if (!timestape) { return '' } const zero = (val: number): string | number => { return val < 10 ? `0${val}` : val } const myDate = new Date(timestape) const year = myDate.getFullYear() const month = zero(myDate.getMonth() + 1) const day = zero(myDate.getDate()) const hour = zero(myDate.getHours()) const minute = zero(myDate.getMinutes()) const second = zero(myDate.getSeconds()) const replaceDate = (match: string): string => { return ( { y: year, M: month, d: day, H: hour, m: minute, s: second } as any )[match] } return formats.replace(/Y|M|d|H|m|s/gi, replaceDate) }
|
六、数组去重
1 2 3 4 5
| const uniqueArr = (arr) => [...new Set(arr)];
console.log(uniqueArr(["1","2","1","2","3","4"]));
|
七、获取当前h5页面的url参数
一般是h5嵌套到其他如(app,小程序等),双方之前的交互时用到的,在main.ts中直接使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
export const getQueryString = (url = '') => { const baseUrl = url || window.location.href; const _pa = baseUrl.substring(baseUrl.indexOf('?') + 1); const _arrS = _pa.split('&'); const _rs: any = {}; for (let i = 0; i < _arrS.length; i++) { const pos = _arrS[i].indexOf('='); if (pos === -1) { continue; } const key = _arrS[i].substring(0, pos); const value = decodeURIComponent(_arrS[i].substring(pos + 1)); _rs[key] = value; } return _rs; };
const {number,string}: any = getQueryString(); console.log(number,string);
|
七、从url获取参数并转为对象
和上面不同的时需传入一个url
1 2 3 4 5 6
| const getParameters = URL => JSON.parse(`{"${decodeURI(URL.split("?")[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`)
getParameters("https://www.google.com.hk/search?number=666&string=money"); // {number: '666', string: 'money'}
|
八、检查对象是否为空
检查对象是否为空,实际上并不那么简单,即使对象为空,每次检查对象是否等于 {}
也会返回 false
1 2 3
| const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object; isEmpty({}) isEmpty({a:"not empty"})
|
九、文字复制到剪贴板
Clipboard API
它的所有操作都是异步的,返回 Promise
对象,不会造成页面卡顿。而且,它可以将任意内容(比如图片)放入剪贴板。
1 2
| const copyText = async (text) => await navigator.clipboard.writeText(text) copyText('单行代码 前端世界')
|