-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
220 lines (205 loc) · 5.05 KB
/
utils.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* @description 删除对象中值为: null, undefined, NaN, ''的属性
* @param {any} obj
* @return {*}
*/
export const deleteUseLessObjectKey = (obj: any) => {
Object.keys(obj).forEach((key) => {
if ([null, undefined, NaN, ''].includes(obj[key])) {
delete obj[key];
}
});
};
/**
* @description 替换占位符
* @param {string} str
* @param {object} obj
* @return {*} string
* @example replaceKeyFromValue('Hello {name}',{name:'Word'}) => Hello Word
*/
export const replaceKeyFromValue = (str: string, obj: object) => {
let res = str;
Object.keys(obj).forEach((v) => {
res = res.replace(new RegExp(`{${v}}`, 'ig'), obj[v]);
});
return res;
};
/**
* @description 判断数据类型
* @return {*}
*/
export const judgeType = (
obj: any
):
| 'boolean'
| 'number'
| 'string'
| 'function'
| 'array'
| 'date'
| 'regExp'
| 'undefined'
| 'null'
| 'object' => {
const map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object',
};
return map[Object.prototype.toString.call(obj)];
};
/**
* @description myName转化为my-name
* @copy https://github.com/vueComponent/ant-design-vue/blob/HEAD/antd-tools/generator-types/src/utils.ts
* @param {string} input
* @return {*}
*/
export const toKebabCase = (input: string): string =>
input.replace(
/[A-Z]/g,
(val, index) => (index === 0 ? '' : '-') + val.toLowerCase()
);
/**
* @description my-name转化为myName
* @param {string} input
* @return {*}
*/
export const toCamelCased = (input: string): string =>
input.replace(/\-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
/**
* @description my-name转化为MyName
* @param {string} input
* @return {*}
*/
export const toPascalCase = (input: string): string => {
input.replace(input[0], input[0].toUpperCase());
return input.replace(/\-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
};
/**
* @description 使用json进行深克隆
* @param {*} obj
* @return {*}
*/
export const deepCloneByJson = <T>(obj: T): T =>
JSON.parse(JSON.stringify(obj));
/**
* @description 手写深拷贝,解决循环引用
* @param {*} object
* @return {*}
*/
export const deepClone = <T>(object: T): T => {
function clone(obj: any, hash: any) {
const newobj: any = Array.isArray(obj) ? [] : {};
// eslint-disable-next-line
hash = hash || new WeakMap();
if (hash.has(obj)) {
return hash.get(obj);
}
hash.set(obj, newobj);
Object.keys(obj).forEach((i) => {
if (obj[i] instanceof Object) {
newobj[i] = clone(obj[i], hash);
} else {
newobj[i] = obj[i];
}
});
return newobj;
}
return clone(object, undefined);
};
/**
* @description 防抖函数(Promise)
* @param {Function} fn 函数
* @param {number} delay 延迟时间
* @param {boolean} leading 首次立即执行
* @return {Promise}
*/
export const debounce = (fn: Function, delay: number, leading = false) => {
let timer;
const debounceFn = function () {
if (timer) {
clearTimeout(timer);
}
// @ts-ignore
const that = this;
const args = arguments;
return new Promise((resolve) => {
if (leading) {
let isFirst = false;
if (!timer) {
resolve(fn.apply(that, args));
isFirst = true;
}
timer = setTimeout(() => {
timer = null;
if (!isFirst) {
resolve(fn.apply(that, args));
}
}, delay);
} else {
timer = setTimeout(() => {
resolve(fn.apply(that, args));
}, delay);
}
});
};
debounceFn.cancel = function () {
clearTimeout(timer);
timer = null;
};
return debounceFn;
};
/**
* @description 节流函数(Promise)
* @param {Function} fn 函数
* @param {number} interval 间隔
* @param {boolean} trailing 最后一次执行
* @return {Promise}
*/
export const throttle = (fn: Function, interval: number, trailing = false) => {
let lastTime = 0;
let timer;
return function () {
// @ts-ignore
const that = this;
const args = arguments;
const newTime = new Date().getTime();
if (timer) {
clearTimeout(timer);
}
let result;
return new Promise((resolve) => {
if (newTime - lastTime > interval) {
result = fn.apply(that, args);
resolve(result);
lastTime = newTime;
} else if (trailing) {
timer = setTimeout(() => {
result = fn.apply(that, args);
resolve(result);
}, interval);
}
});
};
};
/**
* @description 生成uuid
* @return {*}
*/
export const generateUuid = () => {
const tempUrl = URL.createObjectURL(new Blob());
const uuid = tempUrl.toString(); // blob:null/9d24f135-3e33-46b7-b51f-dc5b8121d60a
URL.revokeObjectURL(tempUrl);
return uuid.split('/')[1];
};