一、class 类的工具函数
1、mix 混合类
适用于需要继承多个类,但是项目使用了eslint/tslint 不允许继承多个类的时候使用
// 复制类的属性function copyProperties(target, source) {for (let key of Reflect.ownKeys(source)) {if (key !== 'constructor' && key !== 'prototype' && key !== 'name' && key !== 'length') {let desc = Object.getOwnPropertyDescriptor(source, key)Object.defineProperty(target, key, desc)}}}// 混合类export default function mix(...mixins) {class Mix {constructor(...args) {for (let mixin of mixins) {copyProperties(this, new mixin(...args)); // 拷贝实例属性}}}for (let mixin of mixins) {copyProperties(Mix, mixin); // 拷贝静态属性copyProperties(Mix.prototype, mixin.prototype); // 拷贝原型属性}return Mix;}
二、基于 URL 的工具函数
1、获取地址栏参数
/*** 根据指定的key获取浏览器URL参数值* @param key 获取的指定参数* @param url 指定url 默认为当前地址* @returns {string | Object }*/function getUrlParams(key = '', url = window.location.href) {const regexP = /[^#&\?]+=[^#&\?]*/ig,res: any = {},ms = url.match(regexP);if (ms) {for (const temp of ms) {const arr = temp.split('=');res[arr[0]] = decodeURI(arr[1]);}}if (key) {return res[key]} else {return res;}}
三、时间类型的工具函数
1、按照指定格式 格式化时间
/*** 按照指定格式格式化时间* @param {String} formater 要格式的格式* @param {Date} t 要格式的时间,如果不传默认格式化当前时间* @param {Boolean} isDecimal 是否是十位* dateFormater('YYYYMMDD') ==> 20200306* dateFormater('YYMMDD') ==> "200306"*/function dateFormater(t, formater = 'YYYY-MM-DD HH:mm:ss', isDecimal = true) {const date = !isDecimal ? (t ? new Date(t) : new Date()) : (t ? new Date(t * 1000) : 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.substr(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)}
四、Array 类型的工具函数
1、按照指定的 key 值对指定的 对象数组去重
/*** 根据指定key去重数组* @param arr 去重的数组* @param type 去重数组根据的key*/function removedDuplicate(arr, type) {const newArr: any = [];const tArr: any = [];if (arr.length === 0) {return arr;} else {if (type) {for (const item of arr) {if (!tArr[item[type]]) {newArr.push(item);tArr[item[type]] = true;}}return newArr;} else {for (const temp of arr) {if (!tArr[temp]) {newArr.push(temp);tArr[temp] = true;}}return newArr;}}}
五、Number类型的工具函数
1、生成Min-Max的随机数
/*** 生成Min-Max的随机数* @param {*number} Min* @param {*number} Max*/function getRandomNum(Min, Max) {const Range = Max - Min;const Rand = Math.random();return (Min + Math.round(Rand * Range));}
六、适用于所有的工具函数
1、基于类的防抖函数
class CommonUtils {/*** 防抖函数* @param fn 需要节流的函数* @param contentThis 节流函数上下文this* @param delay 节流阀门时间*/debounce(fn: void, contentThis: object, delay = 1000) {let timer; // 维护一个 timerreturn function () {const args: any = arguments;if (timer) {clearTimeout(timer);}timer = setTimeout(function () {// @ts-ignore 忽略TS检查fn.apply(contentThis, args);}, delay);}.call(this);}}
七、String 类型的工具函数
1、用于替换字符串中的标签,避免脚本攻击
/*** 用于替换字符串中的标签,避免脚本攻击* @param str* @returns string*/function htmlEncodeByRegExp(str) {const tempStr = str || '';if (tempStr.length === 0) { return ''; }return tempStr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/ /g, ' ').replace(/\'/g, ''').replace(/\"/g, '"');}
