好久没写简书,今天看大神的代码里面,好多封装好的工具函数,忍不住想偷师一下,所以在这里总结一下
1. 大数值转换和保留n位有效数字
function numberFormatter(num, digits) {
const si = [
{ value: 1E13, symbol: '亿亿' },
{ value: 1E12, symbol: '万亿' },
{ value: 1E11, symbol: '千亿' },
{ value: 1E10, symbol: '百亿' },
{ value: 1E9, symbol: '十亿' },
{ value: 1E8, symbol: '亿' },
{ value: 1E7, symbol: '千万' },
{ value: 1E6, symbol: '百万' },
{ value: 1E5, symbol: '十万' },
{ value: 1E4, symbol: '万' },
{ value: 1E3, symbol: '千' }
]
for (let i = 0; i < si.length; i++) {
if (num >= si[i].value) {
return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
}
}
return num.toString()
}
console.log(numberFormatter(1234, 2));//1.23千
console.log(numberFormatter(12345, 2));//1.23万
console.log(numberFormatter(123456, 2));//1.23十万
console.log(numberFormatter(1234567, 2));//1.23百万
console.log(numberFormatter(12345678, 2));//1.23千万
2. 大数字每隔三位加点
function toThousandFilter(num) {
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
console.log(toThousandFilter(1234));//1,234
console.log(toThousandFilter(12345));//12,345
console.log(toThousandFilter(123456));//123,456
console.log(toThousandFilter(1234567));//1,234,567
console.log(toThousandFilter(12345678));//12,345,678
3.大小写转换
function uppercaseFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
console.log(String)//string
4.uuid的生成
getUUID = function () {
const s = []
var hexDigits = '0123456789abcdef'
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4'
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
s[8] = s[13] = s[18] = s[23] = '-'
var uuid = s.join('')
return uuid
}
console.log(getUUID());//38673f6b-bacc-4d9b-9330-dd97b7ae238f
4.复制内容到剪贴板
copyText = function (txt) {
const input = document.createElement('input')
input.value = txt
document.body.appendChild(input)
input.select()
document.execCommand('Copy')
document.body.removeChild(input)
}
这个请自己尝试,亲测有效
5.cookie清空
clearCookie = function () {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g)
if (keys) {
for (var i = keys.length; i--;)
document.cookie = keys[i] + '=0;expires=' + new Date( 0).toUTCString()
}
}
6,性能分析函数
window.onload = function() {
setTimeout(function() {
let t = performance.timing;
console.log('DNS查询耗时 :' + (t.domainLookupEnd - t.domainLookupStart).toFixed(0))
console.log('TCP链接耗时 :' + (t.connectEnd - t.connectStart).toFixed(0))
console.log('request请求耗时 :' + (t.responseEnd - t.responseStart).toFixed(0))
console.log('解析dom树耗时 :' + (t.domComplete - t.domInteractive).toFixed(0))
console.log('白屏时间 :' + (t.responseStart - t.navigationStart).toFixed(0))
console.log('domready时间 :' + (t.domContentLoadedEventEnd - t.navigationStart).toFixed(0))
console.log('onload时间 :' + (t.loadEventEnd - t.navigationStart).toFixed(0))
if (t = performance.memory) {
console.log('js内存使用占比:' + (t.usedJSHeapSize / t.totalJSHeapSize * 100).toFixed(2) + '%')
}
})
}
7.点击全屏
function toFullScreen() {
let elem = document.body;
elem.webkitRequestFullScreen
? elem.webkitRequestFullScreen()
: elem.mozRequestFullScreen
? elem.mozRequestFullScreen()
: elem.msRequestFullscreen
? elem.msRequestFullscreen()
: elem.requestFullScreen
? elem.requestFullScreen()
: alert("浏览器不支持全屏");
}
8.退出全屏
function exitFullscreen() {
let elem = parent.document;
elem.webkitCancelFullScreen
? elem.webkitCancelFullScreen()
: elem.mozCancelFullScreen
? elem.mozCancelFullScreen()
: elem.cancelFullScreen
? elem.cancelFullScreen()
: elem.msExitFullscreen
? elem.msExitFullscreen()
: elem.exitFullscreen
? elem.exitFullscreen()
: alert("切换失败,可尝试Esc退出");
}
9.获取浏览器信息
function getExplorerInfo() {
let t = navigator.userAgent.toLowerCase();
return 0 <= t.indexOf("msie") ? { //ie < 11
type: "IE",
version: Number(t.match(/msie ([\d]+)/)[1])
} : !!t.match(/trident\/.+?rv:(([\d.]+))/) ? { // ie 11
type: "IE",
version: 11
} : 0 <= t.indexOf("edge") ? {
type: "Edge",
version: Number(t.match(/edge\/([\d]+)/)[1])
} : 0 <= t.indexOf("firefox") ? {
type: "Firefox",
version: Number(t.match(/firefox\/([\d]+)/)[1])
} : 0 <= t.indexOf("chrome") ? {
type: "Chrome",
version: Number(t.match(/chrome\/([\d]+)/)[1])
} : 0 <= t.indexOf("opera") ? {
type: "Opera",
version: Number(t.match(/opera.([\d]+)/)[1])
} : 0 <= t.indexOf("Safari") ? {
type: "Safari",
version: Number(t.match(/version\/([\d]+)/)[1])
} : {
type: t,
version: -1
}
}
10.匹配身份证(15位或18位)
function isIdCardNo(num) {
num = num.toUpperCase();
//身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X。
if (!(/(^/d{15}$)|(^/d{17}([0-9]|X)$)/.test(num)))
{
alert('输入的身份证号长度不对,或者号码不符合规定!/n15位号码应全为数字,18位号码末位可以为数字或X。');
return false;
}
}
11.移动电话
function checkMobile(str) {
if (!(/^1[3|5|8][0-9]\d{4,8}$/.test(str))) {
return false;
}
return true;
}
12.判断输入是否是有效的电子邮件
function isemail(str) {
var result = str.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
if (result == null) return false;
return true;
}