@haluo/util 1.0.32 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/index.js +33 -36
  2. package/dist/modules/cookie/index.js +54 -54
  3. package/dist/modules/date/index.js +192 -192
  4. package/dist/modules/dom/index.js +62 -62
  5. package/dist/modules/filter/index.js +44 -57
  6. package/dist/modules/format/index.js +21 -21
  7. package/dist/modules/match/index.js +31 -31
  8. package/dist/modules/monitor/index.js +17 -17
  9. package/dist/modules/monitor/lib/jsError.js +53 -61
  10. package/dist/modules/monitor/lib/timing.js +69 -69
  11. package/dist/modules/monitor/lib/xhr.js +48 -50
  12. package/dist/modules/monitor/utils/onload.js +11 -11
  13. package/dist/modules/monitor/utils/tracker.js +63 -84
  14. package/dist/modules/number/index.js +102 -102
  15. package/dist/modules/sentry/index.js +81 -78
  16. package/dist/modules/tools/index.js +393 -393
  17. package/dist/tsconfig.tsbuildinfo +1 -0
  18. package/dist/{index.d.ts → types/index.d.ts} +2 -3
  19. package/dist/types/index.js +2 -0
  20. package/dist/{modules → types/modules}/cookie/index.d.ts +1 -3
  21. package/dist/{modules → types/modules}/filter/index.d.ts +0 -1
  22. package/dist/types/modules/monitor/index.d.ts +3 -0
  23. package/dist/types/modules/monitor/lib/jsError.d.ts +1 -0
  24. package/dist/types/modules/monitor/lib/timing.d.ts +1 -0
  25. package/dist/types/modules/monitor/lib/xhr.d.ts +1 -0
  26. package/dist/types/modules/monitor/utils/onload.d.ts +1 -0
  27. package/dist/types/modules/monitor/utils/tracker.d.ts +7 -0
  28. package/dist/types/types/index.d.ts +3 -0
  29. package/package.json +5 -4
  30. package/.babelrc +0 -21
  31. package/.eslintrc.js +0 -216
  32. package/__tests__/unit/date/date.spec.js +0 -14
  33. package/__tests__/unit/jest.conf.js +0 -25
  34. package/__tests__/unit/specs/date.test.js +0 -11
  35. package/dist/index.cjs.js +0 -89
  36. package/dist/index.esm.js +0 -87
  37. package/dist/lib-list.d.ts +0 -2
  38. package/global.d.ts +0 -0
  39. package/publish.sh +0 -11
  40. package/specification/CSS.md +0 -25
  41. package/specification/JS.md +0 -9
  42. package/specification/VUE.md +0 -1
  43. package/src/consts/httpCode.js +0 -10
  44. package/src/index.ts +0 -54
  45. package/src/modules/cookie/index.ts +0 -69
  46. package/src/modules/date/index.ts +0 -196
  47. package/src/modules/dom/index.ts +0 -78
  48. package/src/modules/filter/index.ts +0 -57
  49. package/src/modules/format/index.ts +0 -19
  50. package/src/modules/match/index.ts +0 -31
  51. package/src/modules/monitor/index.ts +0 -13
  52. package/src/modules/monitor/lib/jsError.ts +0 -57
  53. package/src/modules/monitor/lib/timing.ts +0 -75
  54. package/src/modules/monitor/lib/xhr.ts +0 -42
  55. package/src/modules/monitor/utils/onload.ts +0 -8
  56. package/src/modules/monitor/utils/tracker.ts +0 -61
  57. package/src/modules/number/index.ts +0 -108
  58. package/src/modules/sentry/index.ts +0 -79
  59. package/src/modules/tools/index.ts +0 -427
  60. package/tsconfig.json +0 -34
  61. package/yarn-error.log +0 -9316
  62. /package/dist/{modules → types/modules}/date/index.d.ts +0 -0
  63. /package/dist/{modules → types/modules}/dom/index.d.ts +0 -0
  64. /package/dist/{modules → types/modules}/format/index.d.ts +0 -0
  65. /package/dist/{modules → types/modules}/match/index.d.ts +0 -0
  66. /package/dist/{modules → types/modules}/number/index.d.ts +0 -0
  67. /package/dist/{modules → types/modules}/sentry/index.d.ts +0 -0
  68. /package/dist/{modules → types/modules}/tools/index.d.ts +0 -0
@@ -1,427 +0,0 @@
1
- /**
2
- * @file: tools 常用的工具函数
3
- * @Author: wanghui
4
- */
5
- 'use strict'
6
-
7
- let previous = 0;
8
- let timeout: NodeJS.Timer | null = null
9
-
10
- import { IObjectKey } from '../../types/index'
11
- class ToolsClass {
12
- [key: string]: any;
13
-
14
- constructor() {
15
- this.__loaded__ = {} // 已加载的资源
16
- /**
17
- * 阻止事件传递
18
- * @param {Event} e
19
- * @return {Void}
20
- */
21
- this.__setDefault__ = (e: any) => {
22
- e && e.preventDefault()
23
- }
24
- }
25
-
26
-
27
- /**
28
- * 深拷贝 Object|Array
29
- * 深拷贝,支持 普通对象、数组,但是未解决Function、Date、RegExp,且1M以上数据性能不好
30
- * @param {String} data
31
- * @return {Object}
32
- */
33
- deepCopy(data: object): object {
34
- return JSON.parse(JSON.stringify(data));
35
- }
36
-
37
- /**
38
- * 深拷贝 Object|Array
39
- * 支持 普通对象、数组和函数的深复制,但是未解决循环引用、Date、RegExp
40
- * @param {Object|Array} data
41
- * @return {Object}
42
- */
43
- deepCopy2<T>(obj: T): T {
44
- const _obj: any = Array.isArray(obj) ? [] : {}
45
- for (const i in obj) {
46
- _obj[i] = typeof obj[i] === 'object' ? this.deepCopy2(obj[i]) : obj[i]
47
- }
48
- return _obj
49
- }
50
-
51
- /**
52
- * 深拷贝 Object|Array
53
- * 注意:需要 yarn add lodash
54
- * @param {Object|Array} data
55
- * @return {Object}
56
- */
57
- deepCopy3(obj: any): any {
58
- const deepcopy = require('lodash/cloneDeep')
59
- return deepcopy(obj)
60
- }
61
-
62
- /**
63
- * 防抖
64
- * demo:debounce(func, 300)(args)
65
- * @param {Function} func 执行函数
66
- * @param {Number} wait 节流时间,毫秒
67
- * @return {Function} delay
68
- */
69
- debounce(func: Function, wait: number) {
70
- const delay = function () {
71
- const args = arguments
72
-
73
- if (timeout) clearTimeout(timeout);
74
-
75
- timeout = setTimeout(() => {
76
- func.apply(delay, args)
77
- }, wait);
78
- }
79
- return delay
80
- }
81
-
82
- /**
83
- * 节流
84
- * demo:throttle(func, 300)(args)
85
- * @param {Object|Array} func 执行函数
86
- * @param {Number} wait 节流时间,毫秒
87
- * @return {Function} delay
88
- */
89
- throttle(func: Function, wait: number) {
90
- const delay = function () {
91
- const now = Date.now();
92
- if (now - previous > wait) {
93
- func.apply(delay, arguments);
94
- previous = now;
95
- }
96
- }
97
- return delay
98
- }
99
-
100
- /**
101
- * 获取路径中文件名称
102
- * @param {String} url
103
- * @return {String}
104
- */
105
- getUrlName(url: string) {
106
- return (
107
- url && url.split('?')[0].split('/').reverse()[0]
108
- )
109
- }
110
- /**
111
- * 动态加载脚本
112
- * tip:按需加载时用(多次调用,js不会重复加载)
113
- * @param {String} url
114
- * @return {Promise}
115
- */
116
- loadJs(url: string) {
117
- if (!(window && window.document)) {
118
- return new Error('仅支持浏览器')
119
- }
120
-
121
- const name = this.getUrlName(url)
122
- const id = 'js_' + name
123
-
124
- return new Promise((resolve: Function, reject: Function) => {
125
- if (this.__loaded__[id]) {
126
- return resolve()
127
- }
128
- const script = document.createElement('script')
129
- script.type = 'text/javascript'
130
- script.async = true
131
- script.src = url
132
- script.id = id
133
- script.onload = () => {
134
- this.__loaded__[id] = true
135
- resolve()
136
- }
137
- script.onerror = (e) => {
138
- reject(e)
139
- }
140
- document.body.appendChild(script)
141
- })
142
- }
143
- /**
144
- * 动态加载样式
145
- * @param {String} url
146
- * @return {Promise}
147
- */
148
- loadCss(url: string) {
149
- if (!(window && window.document)) {
150
- return new Error('仅支持浏览器')
151
- }
152
-
153
- const name = this.getUrlName(url)
154
- const id = 'css_' + name
155
-
156
- return new Promise((resolve: Function, reject: Function) => {
157
- if (this.__loaded__[id]) {
158
- return resolve()
159
- }
160
- const link = document.createElement('link')
161
- link.type = 'text/css'
162
- link.rel = 'stylesheet'
163
- link.href = url
164
- link.id = id
165
- link.onload = () => {
166
- this.__loaded__[id] = true
167
- resolve()
168
- }
169
- link.onerror = (e) => {
170
- reject(e)
171
- }
172
- document.head.appendChild(link)
173
- })
174
- }
175
-
176
- /**
177
- * 蒙层显示后,html禁止滚动操作
178
- * @param {String} className
179
- * @return {Void}
180
- */
181
- stopScroll = (className: string) => {
182
- if (!(window && window.document)) {
183
- return new Error('仅支持浏览器')
184
- }
185
-
186
- const html = document.documentElement
187
- html.style.overflow = 'hidden'
188
- html.style.height = '100%'
189
- const body = document.body
190
- body.style.overflow = 'hidden'
191
- body.style.height = '100%'
192
- if (className) {
193
- const dom = document.querySelector(`.${className}`)
194
- dom && dom.addEventListener('touchmove', this.__setDefault__)
195
- }
196
- }
197
- /**
198
- * 蒙层隐藏后,html开启滚动操作
199
- * @param {String} className
200
- * @return {Void}
201
- */
202
- startScroll = (className: string) => {
203
- if (!(window && window.document)) {
204
- return new Error('仅支持浏览器')
205
- }
206
-
207
- const html = document.documentElement
208
- html.style.overflow = 'visible'
209
- html.style.height = 'auto'
210
- const body = document.body
211
- body.style.overflow = 'visible'
212
- body.style.height = 'auto'
213
- if (className) {
214
- const dom = document.querySelector(`.${className}`)
215
- dom && dom.removeEventListener('touchmove', this.__setDefault__)
216
- }
217
- }
218
-
219
- /**
220
- * 字符串复制到剪贴板
221
- * 注意:需要 yarn add clipboard-copy
222
- * @param {String} str
223
- * @return {String}
224
- */
225
- clipboard(str: string) {
226
- if (!(window && window.document)) {
227
- return new Error('仅支持浏览器')
228
- }
229
-
230
- const copy = require('clipboard-copy')
231
- return copy(str)
232
- }
233
-
234
- /**
235
- * 首字母大写
236
- * @param {String} str
237
- * @return {String}
238
- */
239
- firstUpperCase(str: string): string {
240
- return str.charAt(0).toUpperCase() + str.toString().slice(1)
241
- }
242
-
243
- /**
244
- * 截取数组或字符串,示例:slice('1234', 3)
245
- * @param {Array|String} target 数组或字符串
246
- * @param {Number} length 截取长度,从0开始
247
- * @return {any}
248
- */
249
- slice(target: Array<any> | string = '', length: number = 0): string | any[] {
250
- return target.slice(0, length)
251
- }
252
-
253
- /**
254
- * 获取guid
255
- * @return {String}
256
- */
257
- guid(): string {
258
- function S4() {
259
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
260
- }
261
- return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
262
- }
263
-
264
- /**
265
- * 获取文本字节数(含中文)
266
- * @param {String} str
267
- * @return {String}
268
- */
269
- getBytesOfText(str: string = ''): number {
270
- return str.replace(/[^\u0000-\u00ff]/g, "aa").length
271
- }
272
-
273
- /**
274
- * object 对象转 array 数组
275
- * demo:
276
- * objectToArray({a:1,b:2}) 输出:["a=1", "b=2"]
277
- * @param {Object} obj
278
- * @returns {Array} arr
279
- */
280
- objectToArray = (obj: IObjectKey<string>): Array<string> => {
281
- var arr = []
282
-
283
- if (typeof obj === 'object') {
284
- for (var key in obj) {
285
- if (obj.hasOwnProperty(key)) {
286
- arr.push([key, obj[key]].join('='))
287
- }
288
- }
289
- }
290
-
291
- return arr
292
- }
293
-
294
- /**
295
- * convertEnum
296
- * 枚举键值互换
297
- * @param {Object} obj
298
- * convertKeyValueEnum({a: 1, b: 2}) // {1: "a", 2: "b"}
299
- * @returns {Object} result
300
- */
301
- convertKeyValueEnum = (obj: IObjectKey<string>): IObjectKey<string> => {
302
- const result: IObjectKey<string> = {}
303
-
304
- if (typeof obj === 'object') {
305
- for (const key in obj) {
306
- if (obj.hasOwnProperty(key)) {
307
- result[obj[key]] = key
308
- }
309
- }
310
- }
311
-
312
- return result
313
- }
314
-
315
- /**
316
- * 数组去重
317
- * @param {Array} arr
318
- * @returns {Array}
319
- */
320
- uniqueArr(arr: Array<any>): Array<any> {
321
- return Array.from(new Set(arr))
322
- }
323
-
324
- /**
325
- * 数组元素交换位置
326
- * index1和index2分别是两个数组的索引值,即是两个要交换元素位置的索引值,如1,5就是数组中下标为1和5的两个元素交换位置
327
- * @param {Array<string | number>} array 数组
328
- * @param {number} index1 添加项目的位置
329
- * @param {number} index2 删除项目的位置
330
- * swapArray([1, 2, 3, 4], 2, 3) // [1, 2, 4, 3]
331
- * @returns {Array<string | number>} array
332
- */
333
- swapArray(array: Array<string | number>, index1: number, index2: number): Array<string | number> {
334
- [array[index1], array[index2]] = [array[index2], array[index1]]
335
- return array
336
- }
337
-
338
- /**
339
- * 过滤表情符号
340
- * @param {String} str
341
- * @return {String}
342
- */
343
- filterEmoji(str: string): string {
344
- return str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg, '')
345
- }
346
-
347
- /**
348
- * 是否包含表情
349
- * @param {String} str
350
- * @return {boolean}
351
- */
352
- containsEmoji(str: string): boolean {
353
- const reg = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg
354
- return reg.test(str)
355
- }
356
-
357
- /**
358
- * 是否包含表汉字
359
- * @param {String} str
360
- * @return {boolean}
361
- */
362
- containsHanZi(str: string): boolean {
363
- const reg = /[\u4e00-\u9fa5]/mg
364
- return reg.test(str)
365
- }
366
-
367
- isEmpty(val: any) {
368
- // null or undefined
369
- if (val == null) return true;
370
-
371
- if (typeof val === 'boolean') return false;
372
-
373
- if (typeof val === 'number') return !val;
374
-
375
- if (val instanceof Error) return val.message === '';
376
-
377
- switch (Object.prototype.toString.call(val)) {
378
- // String or Array
379
- case '[object String]':
380
- case '[object Array]':
381
- return !val.length;
382
-
383
- // Map or Set or File
384
- case '[object File]':
385
- case '[object Map]':
386
- case '[object Set]': {
387
- return !val.size;
388
- }
389
- // Plain Object
390
- case '[object Object]': {
391
- return !Object.keys(val).length;
392
- }
393
- }
394
-
395
- return false;
396
- };
397
- isDefined = (val: any) => {
398
- return val !== undefined && val !== null;
399
- };
400
- /**
401
- * 字段脱敏处理
402
- * @param {String} field 未脱敏字段
403
- * @param {Int} before 开头未脱敏字符数
404
- * @param {Int} after 结尾未脱敏字符数
405
- * @return {String} 已脱敏字段
406
- */
407
- sensitiveField(field: string | number, before: number = 3, after: number = 4): string {
408
- if (!field) {
409
- return '';
410
- }
411
- field = String(field);
412
-
413
- let sensitiveLen = field.length - before - after;
414
- if (sensitiveLen < 0) {
415
- sensitiveLen = 0;
416
- }
417
-
418
- // 匹配中文、英文、数字
419
- const regItem = '[\u4e00-\u9fa5a-zA-Z0-9]';
420
- const regExp = `(${regItem}{${before}})${regItem}*(${regItem}{${after}})`;
421
- const reg = new RegExp(regExp);
422
-
423
- return field.replace(reg, `$1${"*".repeat(sensitiveLen)}$2`);
424
- }
425
- }
426
-
427
- module.exports = new ToolsClass()
package/tsconfig.json DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "moduleResolution": "node",
4
- "target": "ES5",
5
- "module": "commonjs",
6
- "lib": [
7
- "esnext",
8
- "dom"
9
- ],
10
- "allowJs": false,
11
- "strict": true,
12
- "noUnusedLocals": true, // 检查只声明、未使用的局部变量(只提示不报错)
13
- "sourceMap": false,
14
- "removeComments": false,
15
- // "allowSyntheticDefaultImports": true,
16
- // "experimentalDecorators": true,
17
- // "emitDecoratorMetadata": true,
18
- "esModuleInterop": true, // 允许export=导出,由import from 导入
19
- "resolveJsonModule": true,
20
- "declaration": true, // 生成声明文件,开启后会自动生成声明文件
21
- "incremental": true, // TS编译器在第一次编译之后会生成一个存储编译信息的文件,第二次编译会在第一次的基础上进行增量编译,可以提高编译的速度
22
- "diagnostics": true, // 打印诊断信息
23
- // "listEmittedFiles": true, // 打印输出文件
24
- // "listFiles": true, // 打印编译的文件(包括引用的声明文件)
25
- "declarationDir": "dist/types", // 指定生成声明文件存放目录
26
- "outDir": "dist/", // 指定输出目录
27
- "typeRoots": [
28
- "node_modules/@types"
29
- ]
30
- },
31
- "include": [
32
- "src"
33
- ]
34
- }