- 检测数据是不是除了symbol外的原始数据
| function isStatic(value) { |
| return ( |
| typeof value === 'string' || |
| typeof value === 'number' || |
| typeof value === 'boolean' || |
| typeof value === 'undefined' || |
| value === null |
| ) |
| } |
- 检查数据是否为有效的类数组长度
| function isLength(value) { |
| return ( |
| typeof value == 'number' && |
| value > -1 && |
| value % 1 == 0 && |
| value <= Number.MAX_SAFE_INTEGER |
| ) |
| } |
- 检查数据是否为函数
| function isFunction(value) { |
| return Object.prototype.toString.call(value) === '[object Function]' |
| } |
- 判断数据是否为时间对象
| function isDate(value) { |
| return Object.prototype.toString.call(value) === '[object Date]' |
| } |
- 判断数据是否为正则对象
| function isRegExp(value) { |
| return Object.prototype.toString.call(value) === '[object RegExp]' |
| } |
- 判断数据是否为数组类型的数据
| function isArray(arr) { |
| return Object.prototype.toString.call(arr) === '[object Array]' |
| } |
- 获取数组中非undefined数据中的最大值
| function max(arr) { |
| arr = arr.filter(item => !_isNaN(item)) |
| return arr.length ? Math.max.apply(null, arr) : undefined |
| } |
- 获取数组中非undefined数据中的最小值
| function min(arr) { |
| arr = arr.filter(item => !_isNaN(item)) |
| return arr.length ? Math.min.apply(null, arr) : undefined |
| } |
- 横线转驼峰命名
| let camelizeRE = /-(\w)/g |
| function camelize(str) { |
| return str.replace(camelizeRE, function (_, c) { |
| return c ? c.toUpperCase() : '' |
| }) |
| } |
- 驼峰命名转横线命名:拆分字符串,使用 - 相连,并且转换为小写
| let hyphenateRE = /\B([A-Z])/g |
| function hyphenate(str) { |
| return str.replace(hyphenateRE, '-$1').toLowerCase() |
| } |
- 字符串首位大写
| function capitalize(str) { |
| return str.charAt(0).toUpperCase() + str.slice(1) |
| } |
- 深拷贝
| function deepCopy(obj, hash = new WeakMap()) { |
| |
| if (obj.constructor === Date) return new Date(obj) |
| |
| if (obj.constructor === RegExp) return new RegExp(obj) |
| |
| if (hash.has(obj)) return hash.get(obj) |
| |
| |
| let allDesc = Object.getOwnPropertyDescriptor(obj) |
| |
| let copyObj = Object.create(Object.getPrototypeOf(obj), allDesc) |
| hash.set(obj, copyObj) |
| for (let key of Reflect.ownKeys(obj)) { |
| copyObj[key] = |
| isComplexDataType(obj[key]) && typeof obj[key] !== 'function' |
| ? deepCopy(obj[key], hash) |
| : obj[key] |
| } |
| function isComplexDataType(obj) { |
| return ( |
| (typeof obj === 'object' || typeof obj === 'function') && obj !== null |
| ) |
| } |
| return copyObj |
| } |
- 数据深、浅拷贝
| function deepCLone(obj) { |
| |
| let objClone = Array.isArray(obj) ? [] : {} |
| |
| if (obj && typeof obj === 'object' && obj != null) { |
| |
| for (let key in obj) { |
| |
| if (obj.hasOwnProperty(key)) { |
| |
| if (obj[key] && typeof obj[key] === 'object') { |
| |
| objClone[key] = deepCLone(obj[key]) |
| } else { |
| |
| objClone[key] = obj[key] |
| } |
| } |
| } |
| } |
| return objClone |
| } |
- 获取数据类型,返回结果为 Number、String、Object、Array等
| function getRawType(value) { |
| return Object.prototype.toString.call(value).slice(8, -1) |
| } |
- 格式化时间
| |
| |
| |
| |
| |
| |
| |
| function dateFormater(formater, t) { |
| let date = t ? new Date(t) : new Date(), |
| Y = date.getFullYear() + '', |
| M = date.getMonth() + 1, |
| D = date.getDate(), |
| H = date.getHours(), |
| m = date.getMinutes(), |
| s = date.getSeconds() |
| return formater |
| .replace(/YYYY|yyyy/g, Y) |
| .replace(/YY|yy/g, Y.substring(2, 2)) |
| .replace(/MM/g, (M < 10 ? '0' : '') + M) |
| .replace(/DD/g, (D < 10 ? '0' : '') + D) |
| .replace(/HH|hh/g, (H < 10 ? '0' : '') + H) |
| .replace(/mm/g, (m < 10 ? '0' : '') + m) |
| .replace(/ss/g, (s < 10 ? '0' : '') + s) |
| } |
- 获取url参数,返回一个对象
| function GetUrlParam() { |
| let url = document.location.toString() |
| let arrObj = url.split('?') |
| let params = Object.create(null) |
| if (arrObj.length > 1) { |
| arrObj = arrObj[1].split('&') |
| arrObj.forEach(item => { |
| item = item.split('=') |
| params[item[0]] = item[1] |
| }) |
| } |
| return params |
| } |
| |
| |
- base64转blob
| |
| |
| |
| |
| |
| function dataURLtoBlob(data) { |
| const dataAtob = atob(data) |
| let dataLength = dataAtob.length |
| const u8arr = new Uint8Array(dataLength) |
| while (dataLength--) { |
| u8arr[dataLength] = dataAtob.charCodeAt(dataLength) |
| } |
| return new Blob([u8arr]) |
| } |
- base64数据导出文件。文件下载
| function downloadFile(filename, data) { |
| let DownloadLink = document.createElement('a') |
| |
| if (DownloadLink) { |
| document.body.appendChild(DownloadLink) |
| DownloadLink.style = 'display: none' |
| DownloadLink.download = filename |
| DownloadLink.href = data |
| |
| if (document.createEvent) { |
| let DownloadEvt = document.createEvent('MouseEvents') |
| |
| DownloadEvt.initEvent('click', true, false) |
| DownloadLink.dispatchEvent(DownloadEvt) |
| } else if (document.createEventObject) DownloadLink.fireEvent('onclick') |
| else if (typeof DownloadLink.onclick == 'function') DownloadLink.onclick() |
| |
| document.body.removeChild(DownloadLink) |
| } |
| } |
- 下载文件
| |
| |
| |
| |
| |
| |
| export function download(data, fileName, transformBlob) { |
| const blob = !transformBlob ? data : dataURLtoBlob(data) |
| |
| const blobUrl = window.URL.createObjectURL(blob) |
| |
| |
| if (navigator.msSaveOrOpenBlob) { |
| navigator.msSaveOrOpenBlob(blob, fileName) |
| } else { |
| const a = document.createElement('a') |
| a.style.display = 'none' |
| a.href = blobUrl |
| a.download = fileName |
| document.body.appendChild(a) |
| a.click() |
| |
| document.body.removeChild(a) |
| window.URL.revokeObjectURL(blobUrl) |
| } |
| } |
- 页面全屏
| function toFullScreen() { |
| let el = document.documentElement |
| let rfs = |
| el.requestFullScreen || |
| el.webkitRequestFullScreen || |
| el.mozRequestFullScreen || |
| el.msRequestFullScreen |
| |
| |
| if (rfs) { |
| rfs.call(el) |
| } else if (typeof window.ActiveXObject !== 'undefined') { |
| |
| let wscript = new ActiveXObject('WScript.Shell') |
| if (wscript != null) { |
| wscript.SendKeys('{F11}') |
| } |
| } else { |
| alert('浏览器不支持全屏') |
| } |
| } |
- 退出全屏
| function exitFullscreen() { |
| let el = parent.document |
| let cfs = |
| el.cancelFullScreen || |
| el.webkitCancelFullScreen || |
| el.mozCancelFullScreen || |
| el.exitFullScreen |
| |
| |
| if (cfs) { |
| cfs.call(el) |
| } else if (typeof window.ActiveXObject !== 'undefined') { |
| |
| let wscript = new ActiveXObject('WScript.Shell') |
| if (wscript != null) { |
| wscript.SendKeys('{F11}') |
| } |
| } else { |
| alert('切换失败,可尝试Esc退出') |
| } |
| } |
- 利用performance.timing进行性能分析
| 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) + |
| '%' |
| ) |
| } |
| }) |
| } |
- 禁止某些键盘事件
| document.addEventListener('keydown', function (event) { |
| return ( |
| !( |
| ( |
| 112 == event.code || |
| 123 == event.code || |
| (event.ctrlKey && 82 == event.code) || |
| (event.ctrlKey && 78 == event.code) || |
| (event.shiftKey && 121 == event.code) || |
| (event.altKey && 115 == event.code) || |
| ('A' == event.srcElement.tagName && event.shiftKey) |
| ) |
| ) || (event.returnValue = false) |
| ) |
| }) |
- 禁止右键、选择、赋值
| ['contextmenu', 'selectstart', 'copy'].forEach(function (ev) { |
| document.addEventListener(ev, function (event) { |
| return (event.returnValue = false) |
| }) |
| }) |
常用js工具函数,长期维护更新