project-general-tools
Version:
项目开发通用工具类封装
418 lines (417 loc) • 14.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/*
* @Author: Mad Dragon 395548460@qq.com
* @Date: 2020年3月21日
* @explanatory: 通用工具类
*/
/* eslint-disable */
const VUE_APP_CDN = process.env.VUE_APP_CDN || '';
const utils = {
getUUID(len, radix) {
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
const uuid = [];
let i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i += 1)
uuid[i] = chars[0 | Math.random() * radix];
}
else {
let r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i += 1) {
if (!uuid[i]) {
r = 0 | Math.random() * 16;
uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
},
getResult(arr = [], id = '', find = 'id', result = 'value') {
let value = '';
if (id === undefined || id === null || !find || !result) {
return '';
}
arr.map((e) => {
if (e[find] === id) {
value = e[result];
}
return e;
});
return value;
},
url: {
build(url, param) {
let parStr = '';
if (param) {
const type = typeof param;
if (type === 'string') {
parStr = param;
}
else if (type === 'object') {
parStr = this.mapToParamString(param);
}
parStr = (url.indexOf('?') === -1 ? '?' : '&') + parStr;
}
return (url + parStr).replace(/(?<!:)\/\/+/g, '/');
},
getParam(name) {
const val = this.getParamMap()[name];
return utils.object.isNotNull(val) ? val : null;
},
getParamKeys() {
return utils.map.keys(this.getParamMap());
},
getParamVals() {
return utils.map.vals(this.getParamMap());
},
getParamMap() {
return this.paramStringToMap((window.location.search || '').replace(/^\?/, ''));
},
paramStringToMap: function (str) {
if (utils.string.isBlank(str)) {
return {};
}
const entrys = str.replace(/\+/g, ' ').split('&');
let entry;
const map = {};
let k;
let v;
for (const i in entrys) {
if (!Object.prototype.hasOwnProperty.call(entrys, i))
continue;
entry = entrys[i].split('=');
k = decodeURIComponent(entry[0]);
v = entry[1];
if (v)
(v = decodeURIComponent(v));
map[k] = v;
}
return map;
},
mapToParamString(m) {
if (utils.map.isEmpty(m)) {
return '';
}
const keys = utils.map.keys(m);
let url = '';
for (let i = 0, len = keys.length, key, val; i < len; i += 1) {
key = keys[i];
val = m[key];
if (i !== 0) {
url += '&';
}
url += encodeURIComponent(key);
if (typeof val !== 'undefined') {
url += `=${encodeURIComponent(val)}`;
}
}
return url;
}
},
object: {
isObject(obj) {
return typeof obj === 'object';
},
isFunction(obj) {
return typeof obj === 'function';
},
isArray(obj) {
return this.isNotNull(obj) && obj.constructor === Array;
},
isNull(obj) {
return typeof obj === 'undefined' || obj === null || this.length(obj) === 0;
},
isNotNull(obj) {
return !this.isNull(obj);
},
length(obj) {
let count = 0;
for (const i in obj) {
// 如果包含除它的原型本身之外的属性
if (Object.prototype.hasOwnProperty.call(obj, i)) {
count += 1;
}
}
return count;
},
getChildrenPath(obj, c, k) {
if (this.isNull(obj)) {
return null;
}
if (obj === c) {
return k;
}
if (this.isObject(obj)) {
let v;
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key))
continue;
v = this.getChildrenPath(obj[key], c, key);
if (utils.string.isNotBlank(v)) {
return `${utils.string.isNotBlank(k) ? `${k}.` : ''}${v}`;
}
}
}
return null;
},
merge(t, s, mergeArray = false) {
for (const k in s) {
if (!Object.prototype.hasOwnProperty.call(s, k) || typeof s[k] === 'undefined' || s[k] === null)
continue;
const item = s[k];
switch (item.constructor) {
case Object: {
if (t[k] && t[k].constructor === Object) {
this.merge(t[k], item);
}
else {
t[k] = item;
}
break;
}
case Array: {
if (item.length < 1) {
break;
}
if (mergeArray && t[k] && t[k].constructor === Array) {
t[k] = [...t[k], ...item];
}
else {
t[k] = item;
}
break;
}
case String: {
if (item.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '').length > 0) {
t[k] = item;
}
break;
}
default: {
t[k] = item;
}
}
}
return t;
}
},
string: {
trim(str) {
return utils.object.isNull(str) ? '' : (`${str}`).replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
},
isBlank(str) {
return utils.object.isNull(str) || this.trim(str).length === 0;
},
isNotBlank(str) {
return !this.isBlank(str);
},
isEmpty(str) {
return utils.object.isNull(str) || (`${str}`).length === 0;
},
isNotEmpty(str) {
return !this.isEmpty(str);
},
equalsIgnoreCase(a, b) {
if (utils.object.isNull(a) || utils.object.isNull(b)) {
return false;
}
return (`${a}`).toLowerCase() === (`${b}`).toLowerCase();
}
},
list: {
isEmpty(l) {
return utils.object.isNull(l) || l.length < 1;
},
isNotEmpty(l) {
return !utils.list.isEmpty(l);
},
stringToList(s) {
const txt = typeof s === 'string' ? s.split(',') : s;
return s && s.length > 0 ? txt : [];
},
find(l, k, v, j) {
const n = [];
if (utils.list.isNotEmpty(l)) {
for (let i = 0, len = l.length, r = l[i]; i < len; i += 1) {
if (j ? r[k] === v : `${r[k]}` === `${v}`)
n.push(r);
}
}
return n;
},
indexOf(l, k, v, b, j) {
let n = -1;
if (utils.list.isNotEmpty(l)) {
for (let i = b || 0, len = l.length, r = l[i]; i < len; i += 1) {
if (j ? r[k] === v : `${r[k]}` === `${v}`) {
n = i;
break;
}
}
}
return n;
}
},
map: {
mapsExtVal(maps, key) {
const list = [];
for (let i = 0, len = maps.length; i < len; i += 1) {
list.push(maps[i][key]);
}
return list;
},
listToMap(list, key) {
if (utils.object.isNull(list) || utils.string.isEmpty(key)) {
return null;
}
const map = {};
for (let i = 0, len = list.length; i < len; i += 1) {
const row = list[i];
map[row[key]] = row;
}
return map;
},
isEqualForString(a, b) {
return utils.map.isEqual(a, b, null, true);
},
isEmpty(m) {
return utils.object.isNull(m) || this.keys(m).length < 1;
},
isNotEmpty(m) {
return !this.isEmpty(m);
},
isEqual(a, b, isWeak, isString) {
if (utils.object.isNull(a) && utils.object.isNull(b)) {
return true;
}
if (utils.object.isNull(a) || utils.object.isNull(b)) {
return false;
}
const aks = this.keys(a);
const bks = this.keys(b);
const aksl = aks.length;
const bksl = bks.length;
if (aksl !== bksl) {
return false;
}
for (let i = 0; i < aksl; i += 1) {
if (isWeak || isString ? `${a[aks[i]]}` !== `${b[aks[i]]}` : a[aks[i]] !== b[aks[i]]) {
return false;
}
}
return true;
},
keys(m) {
const keys = [];
for (const key in m) {
if (Object.prototype.hasOwnProperty.call(m, key)) {
keys.push(key);
}
}
return keys;
},
vals(m) {
const l = [];
const keys = utils.map.keys(m);
for (let i = 0, len = keys.length; i < len; i += 1) {
l.push(m[keys[i]]);
}
return l;
}
},
// 格式化手机号 隐藏中间数据
formatPhoneNumber(phone) {
if (!phone)
return '';
return `${String(phone).slice(0, 3)}****${String(phone).slice(-4)}`;
},
// 数组转 百分比
numberToPercentage(percentage) {
if (percentage == null)
return '--';
const num = (Math.round(percentage * 10000) / 100).toFixed(2);
return Number(percentage) === 0 ? '0%' : `${Math.abs(num)}%`;
},
/**
* @description 绑定事件 on(element, event, handler)
*/
on() {
let a = document.removeEventListener;
if (a) {
return function (element, event, handler) {
if (element && event && handler) {
element.addEventListener(event, handler, false);
}
};
}
else {
return function (element, event, handler) {
if (element && event && handler) {
element.attachEvent('on' + event, handler);
}
};
}
},
/**
* @description 解绑事件 off(element, event, handler)
*/
off() {
let a = document.removeEventListener;
if (a) {
return function (element, event, handler) {
if (element && event) {
element.removeEventListener(event, handler, false);
}
};
}
else {
return function (element, event, handler) {
if (element && event) {
element.detachEvent('on' + event, handler);
}
};
}
},
CDN(name, cdn) {
const basePath = cdn || VUE_APP_CDN || '';
return basePath + name;
}
};
exports.default = utils;
/**
* _ooOoo_
* o8888888o
* 88' . '88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* .'' '< `.___\_<|>_/___.' >'''.
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永无BUG
* 佛曰:
* 写字楼里写字间,写字间里程序员;
* 程序人员写程序,又拿程序换酒钱。
* 酒醒只在网上坐,酒醉还来网下眠;
* 酒醉酒醒日复日,网上网下年复年。
* 但愿老死电脑间,不愿鞠躬老板前;
* 奔驰宝马贵者趣,公交自行程序员。
* 别人笑我忒疯癫,我笑自己命太贱;
* 不见满街漂亮妹,哪个归得程序员?
*
* Mad Dragon 395548460@qq.com
*/