(function () { //处理函数 const dealarr = (sort) => { //排序 const sortarr = sort.sort((a, b) => { return a.startIndex - b.startIndex; });
if (sortarr.length === 1) {
return sortarr;
}
let temparr = [sortarr[0]];
for (let i = 1; i <= sortarr.length - 1; i++) {
if (sortarr[i].startIndex < temparr[temparr.length - 1].endIndex) {
if (sortarr[i].endIndex > temparr[temparr.length - 1].endIndex) {
temparr.splice(temparr.length - 1, 1, {
startIndex: temparr[temparr.length - 1].startIndex,
endIndex: sortarr[i].endIndex,
});
}
} else {
temparr.push({
startIndex: sortarr[i].startIndex,
endIndex: sortarr[i].endIndex,
});
}
}
let flag = true;
for (let i = 0; i < temparr.length - 1; i++) {
if (temparr[i + 1].startIndex < temparr[i].endIndex) {
flag = false;
}
}
if (temparr.length === 1 || flag) {
return temparr;
} else {
return dealarr(temparr);
}
};
//设置cookie: const setCookie = (name, value, day) => { if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除 var expires = day * 24 * 60 * 60 * 1000; var date = new Date(+new Date() + expires); document.cookie = name + '=' + value + ';expires=' + date.toUTCString(); } else { document.cookie = name + '=' + value; } }; //读取cookie: const getCookie = (name) => { var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); if ((arr = document.cookie.match(reg))) return arr[2]; else return null; }; //删除cookie: const delCookie = (name) => { var exp = new Date(); exp.setTime(exp.getTime() - 1); document.cookie = name + '=' + '' + ';expires=' + exp.toGMTString(); };
//实现局部打印: const Printpart = (className) => { //id-str 内容中的id var el = document.getElementsByClassName(className)[0];
var iframe = document.createElement('IFRAME');
var doc = null;
iframe.setAttribute(
'style',
'position:absolute;width:0px;height:0px;left:-500px;top:-500px;visibility:hidden;',
);
console.dir(el.innerHTML);
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
doc.write('<div>' + el.innerHTML + '</div>');
doc.close();
iframe.contentWindow.focus();
iframe.contentWindow.print();
if (navigator.userAgent.indexOf('MSIE') > 0) {
document.body.removeChild(iframe);
}
};
let class2type = {}, toString = class2type.toString, hasOwn = class2type.hasOwnProperty, fnToString = hasOwn.toString, ObjectFunctionString = fnToString.call(Object), getProto = Object.getPrototypeOf;
// 检测数据类型的通用方法 const toType = function toType(obj) { if (obj == null) return obj + ''; return typeof obj === 'object' || typeof obj === 'function' ? /^[object (\w+)]$/g.exec(toString.call(obj))[1].toLowerCase() : typeof obj; };
// 检测是否为函数 const isFunction = function isFunction(obj) { return ( typeof obj === 'function' && typeof obj.nodeType !== 'number' && typeof obj.item !== 'function' ); };
// 检测是否为window对象 const isWindow = function isWindow(obj) { return obj != null && obj === obj.window; };
// 检测是否为数组或者类数组 const isArrayLike = function isArrayLike(obj) { let length = !!obj && 'length' in obj && obj.length, type = toType(obj); if (isFunction(obj) || isWindow(obj)) return false; return ( type === 'array' || length === 0 || (typeof length === 'number' && length > 0 && length - 1 in obj) ); };
// 检测是否为纯粹的对象「标准普通对象 或者 直属类是Object」 const isPlainObject = function isPlainObject(obj) { let proto, Ctor; if (!obj || toString.call(obj) !== '[object Object]') return false; proto = getProto(obj); if (!proto) return true; Ctor = hasOwn.call(proto, 'constructor') && proto.constructor; return ( typeof Ctor === 'function' && fnToString.call(Ctor) === ObjectFunctionString ); };
// 检测是否为空对象 const isEmptyObj = function isEmptyObj(obj) { if (obj == null) return false; if (typeof obj !== 'object') return false; let keys = Object.keys(obj); keys = keys.concat(Object.getOwnPropertySymbols(obj)); return keys.length === 0; };
// 检测是不是有效数字 const isNumeric = function isNumeric(obj) { let type = toType(obj); return (type === 'number' || type === 'string') && !isNaN(obj); };
// 函数防抖 const debounce = function debounce(func, wait, immediate) { if (typeof func !== 'function') throw new TypeError('func is not a function~'); if (typeof wait === 'boolean') immediate = wait; if (typeof wait !== 'number') wait = 300; if (typeof immediate !== 'boolean') immediate = false; let timer = null; return function operate(...params) { let now = !timer && immediate, result; if (timer) { clearTimeout(timer); timer = null; } timer = setTimeout(() => { if (!immediate) func.call(this, ...params); clearTimeout(timer); timer = null; }, wait); if (now) result = func.call(this, ...params); return result; }; };
// 函数节流 const throttle = function throttle(func, wait) { if (typeof func !== 'function') throw new TypeError('func is not a function~'); if (typeof wait !== 'number') wait = 300; let timer = null, previous = 0; return function operate(...params) { let now = +new Date(), remaining = wait - (now - previous), result; if (remaining <= 0) { result = func.call(this, ...params); previous = +new Date(); timer = clearTimer(timer); } else if (!timer) { timer = setTimeout(() => { func.call(this, ...params); previous = +new Date(); timer = clearTimer(timer); }, remaining); } return result; }; };
// 深浅克隆 const clone = function clone(target, deep, handle) { let type = typeof target, isArray = Array.isArray(target), isPlain = isPlainObject(target), ctor, keys, obj; if (typeof deep !== 'boolean') deep = false; if (!Array.isArray(handle)) handle = []; if (handle.includes(target)) return target; handle.push(target); if (target == null || type !== 'object') return target; ctor = target.constructor; if (!isArray && !isPlain) return new ctor(target); keys = Reflect.ownKeys(target); obj = new ctor(); keys.forEach((key) => { let val = target[key]; if (deep) { obj[key] = clone(val, deep, handle); return; } obj[key] = val; }); return obj; };
// 日期格式化 const formatTime = function formatTime(time, template) { if (typeof time !== 'string') { time = new Date().toLocaleString('zh-CN', { hour12: false }); } if (typeof template !== 'string') { template = '{0}年{1}月{2}日 {3}:{4}:{5}'; } let arr = []; if (/^\d{8}/.test(time)) { let [, 1, 3] = /^(\d{4})(\d{2})(\d{2})1, 3); } else { arr = time.match(/\d+/g); } return template.replace(/{(\d+)}/g, (_, 1) => { let item = arr[1] || '00'; if (item.length < 2) item = '0' + item; return item; }); };
// const getHref= (content) => { // if (!content) { // return ""; // } // let urlPattern = /(https?://|www.)[a-zA-Z_0-9-@]+(.\w[a-zA-Z_0-9-:]+)+(/[()~#&-=?+%/.\w]+)?/g; // content = content.replace(urlPattern, function(match) { // var href = match; // if (match.indexOf("http") == -1) { // href = "http://" + match; // } // return "<a target="_blank" href="" + href + "">" + match + ""; // }); // return content; // }
let utils = { debounce, throttle, toType, isFunction, isWindow, isArrayLike, isPlainObject, isNumeric, clone, formatTime, dealarr, setCookie, getCookie, delCookie, isEmptyObj, Printpart, // getHref, };
/* 导出模块 */ if (typeof module === 'object' && typeof module.exports === 'object') module.exports = utils; if (typeof window !== 'undefined') window.utils = utils; })();