@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,75 +0,0 @@
1
- import onload from '../utils/onload'
2
- import tracker from '../utils/tracker';
3
- export default function timing() {
4
- let FMP:any, LCP:any
5
- new PerformanceObserver((entryList, observer) => {
6
- const perfEntries = entryList.getEntries()
7
- FMP = perfEntries[0]
8
- observer.disconnect() // 不再观察
9
- }).observe({ entryTypes: ['element'] }) // 观察页面中有意义的元素
10
- new PerformanceObserver((entryList, observer) => {
11
- const perfEntries = entryList.getEntries()
12
- LCP = perfEntries[0]
13
- observer.disconnect() // 不再观察
14
- }).observe({ entryTypes: ['largest-contentful-paint'] }) // 观察页面中最大的元素
15
- new PerformanceObserver((entryList, observer) => {
16
- const firstInput:any = entryList.getEntries()
17
- console.log('FID', firstInput);
18
- if(firstInput) {
19
- let inputDelay = firstInput.processingStart - firstInput.startTime
20
- const duration = firstInput.duration
21
- if(inputDelay > 0 || duration >0) {
22
- tracker.send({
23
- kind: 'experience',
24
- type: 'firstInputDelay',
25
- inputDelay, // 延迟时间
26
- duration, // 处理时间
27
- startTime: firstInput.startTime
28
- })
29
- }
30
- }
31
- observer.disconnect() // 不再观察
32
- }).observe({ type: 'first-input', buffered: true }) // 观察页面中最大的元素
33
- onload(function() {
34
- setTimeout(() => {
35
- console.dir(window.performance);
36
- const {
37
- fetchStart,
38
- connectStart,
39
- connectEnd,
40
- requestStart,
41
- responseStart,
42
- responseEnd,
43
- domInteractive,
44
- domContentLoadedEventStart,
45
- domContentLoadedEventEnd,
46
- loadEventStart,
47
- // loadEventEnd
48
- } = performance.getEntriesByType('navigation')[0] as any
49
- tracker.send({
50
- kind: 'experience',
51
- type: 'timing',
52
- connectTime: connectEnd - connectStart, // 连接时间
53
- ttfbTime: responseStart - requestStart, // 首字节到达时间
54
- responseTime: responseEnd - responseStart, // 响应的读取时间
55
- domContentLoadedTime: domContentLoadedEventEnd - domContentLoadedEventStart,
56
- timeToInteractive: domInteractive - fetchStart,// 首次可交互时间
57
- loadTime: loadEventStart - fetchStart // 完整的页面加载时间
58
- })
59
- let FP = performance.getEntriesByName('first-paint')[0]
60
- let FCP = performance.getEntriesByName('first-contentful-paint')[0]
61
- console.log('FP', FP);
62
- console.log('FCP', FCP);
63
- console.log('FMP', FMP);
64
- console.log('LCP', LCP);
65
- tracker.send({
66
- kind: 'experience',
67
- type: 'paint',
68
- FP: FP.startTime,
69
- FCP: FCP.startTime,
70
- FMP: FMP.startTime,
71
- LCP: LCP.startTime,
72
- })
73
- }, 3000);
74
- })
75
- }
@@ -1,42 +0,0 @@
1
- import tracker from "../utils/tracker"
2
-
3
- export default function injectXHR(data:any={}){
4
- let XMLHttpRequest:any = window.XMLHttpRequest
5
- let oldOpen = XMLHttpRequest.prototype.open as any
6
- XMLHttpRequest.prototype.open = function(method: string, url:string, async:boolean) {
7
- this.logData = { method, url, async }
8
- return oldOpen.apply(this, arguments)
9
- }
10
- let oldSend = XMLHttpRequest.prototype.send
11
- XMLHttpRequest.prototype.send = function(body:any) {
12
- if(this.logData) {
13
- const startTime = Date.now()
14
- const handler = (type:string) => (event:any) => {
15
- if(this.logData.url.indexOf('app/collect/original/info/report/v2') > -1 ) return
16
- // status = 0 是在跨域与404的情况下
17
- // 当前都是在readyState为4(请求已完成)的情况下,进行处理
18
- if(type !== 'error' || this.status === 0) return
19
- let duration = Date.now() - startTime
20
- let status = this.status
21
- let statusText = this.statusText
22
- tracker.send({
23
- kind: 'stability',
24
- type: 'xhr',
25
- eventType: event.type,
26
- pathname: this.logData.url,
27
- status: status + '-'+statusText,
28
- duration,
29
- response: this.response ? JSON.stringify(this.response) : '',
30
- params: body || '',
31
- title: 'xhr',
32
- reason: status + '-'+statusText+` ${this.logData.url} ${this.response ? JSON.stringify(this.response) : ''}`,
33
- ...data
34
- })
35
- }
36
- this.addEventListener('load', handler('load'), false)
37
- this.addEventListener('error', handler('error'), false)
38
- this.addEventListener('abort', handler('abort'), false)
39
- }
40
- return oldSend.apply(this, arguments)
41
- }
42
- }
@@ -1,8 +0,0 @@
1
- export default function onload(cb:any) {
2
- if(document.readyState) {
3
- cb()
4
- } else {
5
-
6
- window.addEventListener('load', cb)
7
- }
8
- }
@@ -1,61 +0,0 @@
1
- const { v4: uuidv4 } = require('uuid')
2
- // const Parser = require('ua-parser-js');
3
- const reg = /((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/g
4
-
5
- class SendTracker {
6
- url: string
7
- constructor() {
8
- this.url = ''
9
- }
10
- verifyRequest(reason:string):boolean {
11
- if(!reason) return false
12
- if(reg.test(reason)) return false
13
- if(reason.startsWith('halomoto')) return false
14
- if(reason.startsWith('file:')) return false
15
- if(reason.includes('log-center.58moto.com')) return false
16
- if(reason.includes('s23.cnzz.com')) return false
17
- if(reason.includes('g.alicdn.com')) return false
18
- if(reason.includes('vendors')) return false
19
- return true
20
- }
21
- send(data:any = {}) {
22
- const host = window.location.host
23
- if(host.indexOf('localhost') > -1) return
24
- if(reg.test(host)) return
25
- if(!this.verifyRequest(data.reason)) return
26
- data.reason = window.location.href+' '+data.reason
27
- const log = { ...getExtraData(), ...data}
28
- let formBody: any = [];
29
- for (var property in log) {
30
- var encodedKey = encodeURIComponent(property);
31
- var encodedValue = encodeURIComponent(log[property]);
32
- formBody.push(encodedKey + "=" + encodedValue);
33
- }
34
- formBody = formBody.join("&");
35
- fetch('https://apm-collect.58moto.com/app/collect/original/info/report/v2', {
36
- method: 'POST',
37
- headers: {
38
- "Content-Type":"application/x-www-form-urlencoded"
39
- },
40
- body: formBody,
41
- }).then(res => {
42
- // console.log(res)
43
- }).catch((e) => {
44
- console.log(e)
45
- })
46
- }
47
- }
48
- function getExtraData() {
49
- return {
50
- deviceId: uuidv4(),
51
- platform: 3,
52
- errorType: 3,
53
- channel: 'h5',
54
- bundle: 'com.jdd.motorfans',
55
- title: '',
56
- frontStatus: 3,
57
- occurTimeStamp: Date.now(),
58
- // userAgent: new Parser().getResult()
59
- }
60
- }
61
- export default new SendTracker()
@@ -1,108 +0,0 @@
1
- /**
2
- * @file number 格式化
3
- * @Author: wanghui
4
- * @createBy: @2020.05.26
5
- */
6
- 'use strict'
7
-
8
- class NumberClass {
9
- /**
10
- * 个位数前面补0
11
- * @param {Number} num 需要格式化的数字
12
- * @return {String} '01'
13
- */
14
- formatNumber(num: number) {
15
- const res = num.toString();
16
- return res[1] ? res : '0' + res;
17
- }
18
-
19
- /**
20
- * 将手机号中间部分替换为星号
21
- * @param {String} phone 手机号码
22
- * @return {String} 131****1111
23
- */
24
- formatPhone(phone: string): string {
25
- return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
26
- }
27
-
28
- /**
29
- * 格式化数字 万
30
- * @param {Number} num
31
- * @return {String} 12.3万
32
- */
33
- convertToWan(num: number): string | number {
34
- let result: string | number = ''
35
- if (num < 10000) {
36
- result = num;
37
- }
38
-
39
- if (num >= 10000) {
40
- result = (num / 10000).toFixed(1) + '万';
41
- }
42
- return result;
43
- }
44
-
45
- /**
46
- * 格式化数字 k
47
- * @param {Number} num
48
- * @return {String} 1.2k
49
- */
50
- convertToThousand(num: number): string | number {
51
- let result: string | number = ''
52
- if (num < 1000) {
53
- result = num;
54
- }
55
-
56
- if (num >= 1000) {
57
- result = (num / 1000).toFixed(1) + 'k';
58
- }
59
- return result;
60
- }
61
-
62
- /**
63
- * 随机数,指定范围
64
- * @param {Number} min 开始
65
- * @param {Number} max 结束
66
- * @return {Number|Object}
67
- */
68
- random(min: number, max: number) {
69
- if (arguments.length === 2) {
70
- return Math.floor(min + Math.random() * ((max + 1) - min))
71
- } else {
72
- return null;
73
- }
74
- }
75
-
76
- /**
77
- * 格式化金额
78
- * @param {Number} num
79
- * @return {String} 123,456
80
- */
81
- formatMoney(money: number | string, signal: string): string {
82
- let result = '';
83
- if (money === '') {
84
- return result;
85
- }
86
- money = String(money).replace('.00', '');
87
- money = money.substring(money.length - 2) === '.0' ? money.replace('.0', '') : money;
88
- // 小于3位数,直接返回
89
- if (Number(money) < 1000) {
90
- return result = money;
91
- }
92
- if (money.split('.')[0].length < 3) {
93
- return result = money;
94
- }
95
- signal = signal === '' ? '' : ','
96
- let price = money.split('.')[0] + '';
97
- const pricePoint = money.split('.')[1];
98
- result = price.length > 6 ?
99
- `${price.substring(0, price.length - 6)}${signal}${price.substring(price.length - 6, price.length - 3)},${price.substring(price.length - 3, price.length)}`
100
- :
101
- `${price.substring(0, price.length - 3)}${signal}${price.substring(price.length - 3, price.length)}`
102
- result = pricePoint ? `${result}${signal}${pricePoint}` : result
103
- return result;
104
- }
105
- }
106
-
107
- module.exports = new NumberClass()
108
-
@@ -1,79 +0,0 @@
1
- /**
2
- * 异常上报日志监控类
3
- * @Author: wanghui
4
- * 常用配置 option:https://docs.sentry.io/clients/javascript/config/
5
- * 1.自动捕获vue组件内异常
6
- * 2.自动捕获promise内的异常
7
- * 3.自动捕获没有被catch的运行异常
8
- */
9
- 'use strict'
10
-
11
- import Raven from '@sentry/browser';
12
-
13
- class Report {
14
- static instance: any;
15
- constructor(Vue: Object, options = {}) {
16
- if (process.env.NODE_ENV !== 'development') {
17
- // todo
18
- }
19
- this.vue = Vue;
20
- this.options = options;
21
- }
22
-
23
- [key: string]: any;
24
-
25
- static getInstance(Vue: Object, Option: Object) {
26
- if (!(this.instance instanceof this)) {
27
- this.instance = new this(Vue, Option);
28
- this.instance.install();
29
- }
30
- return this.instance;
31
- }
32
-
33
- install() {
34
- if (process.env.NODE_ENV !== 'development') {
35
- // Raven.config(this.options.dsn, {
36
- // environment: process.env.NODE_ENV,
37
- // }).addPlugin(RavenVue, this.Vue).install();
38
- // raven内置了vue插件,会通过vue.config.errorHandler来捕获vue组件内错误并上报sentry服务
39
-
40
- // 记录用户信息
41
- Raven.setUser({ user: this.options.user || '' });
42
-
43
- // 设置全局tag标签
44
- Raven.setTag('environment', this.options.env || '');
45
- }
46
- }
47
-
48
- /**
49
- * 主动上报
50
- * @param {String} data
51
- * @param {String} type 'info','warning','error'
52
- * @param {Object} options
53
- */
54
- log(data: any = null, type: any = 'error', options: any = {}) {
55
- // 添加面包屑
56
- Raven.addBreadcrumb({
57
- message: data,
58
- category: 'manual message',
59
- });
60
- // 异常上报
61
- if (data instanceof Error) {
62
- Raven.captureException(data, {
63
- level: type,
64
- tags: { options },
65
- });
66
- } else {
67
- Raven.captureException('error', {
68
- level: type,
69
- extra: {
70
- data,
71
- options: this.options,
72
- date: new Date(),
73
- },
74
- });
75
- }
76
- }
77
- }
78
-
79
- export default Report;