好久沒寫簡書,今天看大神的代碼里面,好多封裝好的工具函數(shù),忍不住想偷師一下,所以在這里總結(jié)一下
1. 大數(shù)值轉(zhuǎn)換和保留n位有效數(shù)字
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. 大數(shù)字每隔三位加點
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.大小寫轉(zhuǎn)換
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.復(fù)制內(nèi)容到剪貼板
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,性能分析函數(shù)
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('白屏?xí)r間 :' + (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內(nèi)存使用占比:' + (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位時全為數(shù)字块蚌,18位前17位為數(shù)字,最后一位是校驗位,可能為數(shù)字或字符X。
if (!(/(^/d{15}$)|(^/d{17}([0-9]|X)$)/.test(num)))
{
alert('輸入的身份證號長度不對,或者號碼不符合規(guī)定腹忽!/n15位號碼應(yīng)全為數(shù)字,18位號碼末位可以為數(shù)字或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;
}