chartUtil.js
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 生成随机图表id
*/
function generateRandomKey(prefix) {
if (prefix == null) {
prefix = 'chart'
}
var userAgent = window.navigator.userAgent
.replace(/[^a-zA-Z0-9]/g, '')
.split('')
var mid = ''
for (var i = 0; i < 12; i++) {
mid += userAgent[Math.round(Math.random() * (userAgent.length - 1))]
}
var time = new Date().getTime()
return prefix + '_' + mid + '_' + time
}
/**
* 深度克隆数据,包括对象,数组,map
* @param {*} obj 对象,数组,map
*/
function deepCopy(obj) {
if (!isObject(obj) && !isMap(obj)) {
return obj;
}
let cloneObj;
if (isMap(obj)) {
cloneObj = new Map();
for (let key of obj.keys()) {
let value = obj.get(key);
if (isMap(value) || isObject(value) || Array.isArray(obj)) {
let copyVal = deepCopy(value);
cloneObj.set(key, copyVal);
} else {
cloneObj.set(key, value);
}
}
} else if (typeof obj === "function") {
cloneObj = obj
} else {
cloneObj = Array.isArray(obj) ? [] : {};
if (obj instanceof HTMLElement) {
cloneObj = obj.cloneNode(true)
} else {
for (let key in obj) {
// if (obj.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
cloneObj[key] =
isMap(obj[key]) || isObject(obj[key])
? deepCopy(obj[key])
: obj[key];
}
}
}
}
return cloneObj;
}
/**
* 判断参数是否是Object类型
* @param {*} o
*/
function isObject(o) {
return (
!isMap(o) &&
(typeof o === 'object' || typeof o === 'function') &&
o !== null
);
}
/**
* 判断参数是否是Map类型
* @param {*} obj
*/
function isMap(obj) {
if (obj instanceof Map) {
return true;
} else {
return false;
}
}
// 替换temp中的${xxx}为指定内容 ,temp:字符串,这里指html代码,dataarry:一个对象{"xxx":"替换的内容"}
// 例:luckysheet.replaceHtml("${image}",{"image":"abc","jskdjslf":"abc"}) ==> abc
function replaceHtml(temp, dataarry) {
return temp.replace(/\$\{([\w]+)\}/g, function (s1, s2) { var s = dataarry[s2]; if (typeof (s) != "undefined") { return s; } else { return s1; } });
}
function hasChinaword(s) {
var patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
if (!patrn.exec(s)) {
return false;
}
else {
return true;
}
}
export {
isMap,
isObject,
deepCopy,
generateRandomKey,
replaceHtml
}