/** * 常用工具库 * @author andy */ const _util = { /*** * 类型检测 * @param {*} item 待检测数据 */ checkType(item) { const classType = {}; const toString = Object.prototype.toString; // 生成classType映射 ['Boolean', 'Number', 'String', 'Function', 'Array', 'Date', 'RegExp', 'Object', 'Error', 'Symbol', 'Map', 'Set', 'WeakSet', 'WeakMap' ].map((item) => { classType[`[object ${item}]`] = item.toLowerCase(); }); const _checkType = obj => { if (obj == null) { return `${obj}`; } return typeof obj === 'object' || typeof obj === 'function' ? classType[toString.call(obj)] || 'object' : typeof obj; }; return _checkType(item); }, /** * 深复制 * @param {目标值} target * @param {原始值} source */ assign(target, source) { target = target || {}; for (let item in source) { if (typeof source[item] === 'object' && source[item] !== null) { target[item] = (toString.apply(source[item]) === '[object Array]') ? [] : {}; this.assign(target[item], source[item]); } else { target[item] = source[item]; } } return target; }, /** * 获取url的query参数 */ getURLParms(url) { const result = {}; let str = url; const start = str.indexOf('?'); str = str.substr(start + 1); const arr = str.split('&'); for (let item of arr) { let tmp = item.split('='); result[tmp[0]] = tmp[1]; } return result; } }; export default _util;