@haluo/util 2.0.0 → 2.0.2
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.
- package/dist/index.js +2 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.js +2 -0
- package/dist/types/modules/cookie/index.d.ts +25 -0
- package/dist/types/modules/date/index.d.ts +1 -0
- package/dist/types/modules/dom/index.d.ts +1 -0
- package/dist/types/modules/filter/index.d.ts +25 -0
- package/dist/types/modules/format/index.d.ts +13 -0
- package/dist/types/modules/match/index.d.ts +1 -0
- package/dist/types/modules/monitor/index.d.ts +3 -0
- package/dist/types/modules/monitor/lib/jsError.d.ts +1 -0
- package/dist/types/modules/monitor/lib/timing.d.ts +1 -0
- package/dist/types/modules/monitor/lib/xhr.d.ts +1 -0
- package/dist/types/modules/monitor/utils/onload.d.ts +1 -0
- package/dist/types/modules/monitor/utils/tracker.d.ts +7 -0
- package/dist/types/modules/number/index.d.ts +39 -0
- package/dist/types/modules/sentry/index.d.ts +15 -0
- package/dist/types/modules/tools/index.d.ts +1 -0
- package/dist/types/types/index.d.ts +3 -0
- package/package.json +2 -1
- package/.babelrc +0 -21
- package/.eslintrc.js +0 -216
- package/__tests__/unit/date/date.spec.js +0 -14
- package/__tests__/unit/jest.conf.js +0 -25
- package/__tests__/unit/specs/date.test.js +0 -11
- package/global.d.ts +0 -0
- package/publish.sh +0 -11
- package/specification/CSS.md +0 -25
- package/specification/JS.md +0 -9
- package/specification/VUE.md +0 -1
- package/src/consts/httpCode.js +0 -10
- package/src/index.ts +0 -51
- package/src/modules/cookie/index.ts +0 -69
- package/src/modules/date/index.ts +0 -196
- package/src/modules/dom/index.ts +0 -78
- package/src/modules/filter/index.ts +0 -43
- package/src/modules/format/index.ts +0 -19
- package/src/modules/match/index.ts +0 -31
- package/src/modules/monitor/index.ts +0 -13
- package/src/modules/monitor/lib/jsError.ts +0 -57
- package/src/modules/monitor/lib/timing.ts +0 -75
- package/src/modules/monitor/lib/xhr.ts +0 -40
- package/src/modules/monitor/utils/onload.ts +0 -8
- package/src/modules/monitor/utils/tracker.ts +0 -47
- package/src/modules/number/index.ts +0 -108
- package/src/modules/sentry/index.ts +0 -82
- package/src/modules/tools/index.ts +0 -427
- package/tsconfig.json +0 -34
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
const { v4: uuidv4 } = require('uuid')
|
|
2
|
-
// const Parser = require('ua-parser-js');
|
|
3
|
-
class SendTracker {
|
|
4
|
-
url: string
|
|
5
|
-
constructor() {
|
|
6
|
-
this.url = ''
|
|
7
|
-
}
|
|
8
|
-
send(data:any = {}) {
|
|
9
|
-
const host = window.location.host
|
|
10
|
-
if(host.indexOf('localhost') > -1) return
|
|
11
|
-
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
|
|
12
|
-
if(reg.test(host)) return
|
|
13
|
-
const log = { ...getExtraData(), ...data}
|
|
14
|
-
let formBody: any = [];
|
|
15
|
-
for (var property in log) {
|
|
16
|
-
var encodedKey = encodeURIComponent(property);
|
|
17
|
-
var encodedValue = encodeURIComponent(log[property]);
|
|
18
|
-
formBody.push(encodedKey + "=" + encodedValue);
|
|
19
|
-
}
|
|
20
|
-
formBody = formBody.join("&");
|
|
21
|
-
fetch('https://apm-collect.58moto.com/app/collect/original/info/report/v2', {
|
|
22
|
-
method: 'POST',
|
|
23
|
-
headers: {
|
|
24
|
-
"Content-Type":"application/x-www-form-urlencoded"
|
|
25
|
-
},
|
|
26
|
-
body: formBody,
|
|
27
|
-
}).then(res => {
|
|
28
|
-
// console.log(res)
|
|
29
|
-
}).catch((e) => {
|
|
30
|
-
console.log(e)
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function getExtraData() {
|
|
35
|
-
return {
|
|
36
|
-
deviceId: uuidv4(),
|
|
37
|
-
platform: 3,
|
|
38
|
-
errorType: 3,
|
|
39
|
-
channel: 'h5',
|
|
40
|
-
bundle: 'com.jdd.motorfans',
|
|
41
|
-
title: '',
|
|
42
|
-
frontStatus: 3,
|
|
43
|
-
occurTimeStamp: Date.now(),
|
|
44
|
-
// userAgent: new Parser().getResult()
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
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,82 +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 'raven-js';
|
|
12
|
-
// import RavenVue from 'raven-js/plugins/vue';
|
|
13
|
-
|
|
14
|
-
class Report {
|
|
15
|
-
static instance: any;
|
|
16
|
-
constructor(Vue: Object, options = {}) {
|
|
17
|
-
if (process.env.NODE_ENV !== 'development') {
|
|
18
|
-
// todo
|
|
19
|
-
}
|
|
20
|
-
this.vue = Vue;
|
|
21
|
-
this.options = options;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
[key: string]: any;
|
|
25
|
-
|
|
26
|
-
static getInstance(Vue: Object, Option: Object) {
|
|
27
|
-
if (!(this.instance instanceof this)) {
|
|
28
|
-
this.instance = new this(Vue, Option);
|
|
29
|
-
this.instance.install();
|
|
30
|
-
}
|
|
31
|
-
return this.instance;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
install() {
|
|
35
|
-
if (process.env.NODE_ENV !== 'development') {
|
|
36
|
-
// Raven.config(this.options.dsn, {
|
|
37
|
-
// environment: process.env.NODE_ENV,
|
|
38
|
-
// }).addPlugin(RavenVue, this.Vue).install();
|
|
39
|
-
// raven内置了vue插件,会通过vue.config.errorHandler来捕获vue组件内错误并上报sentry服务
|
|
40
|
-
|
|
41
|
-
// 记录用户信息
|
|
42
|
-
Raven.setUserContext({ user: this.options.user || '' });
|
|
43
|
-
|
|
44
|
-
// 设置全局tag标签
|
|
45
|
-
Raven.setTagsContext({ environment: this.options.env || '' });
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 主动上报
|
|
51
|
-
* @param {String} data
|
|
52
|
-
* @param {String} type 'info','warning','error'
|
|
53
|
-
* @param {Object} options
|
|
54
|
-
*/
|
|
55
|
-
log(data: any = null, type: any = 'error', options: any = {}) {
|
|
56
|
-
// 添加面包屑
|
|
57
|
-
Raven.captureBreadcrumb({
|
|
58
|
-
message: data,
|
|
59
|
-
category: 'manual message',
|
|
60
|
-
});
|
|
61
|
-
// 异常上报
|
|
62
|
-
if (data instanceof Error) {
|
|
63
|
-
Raven.captureException(data, {
|
|
64
|
-
level: type,
|
|
65
|
-
logger: 'manual exception',
|
|
66
|
-
tags: { options },
|
|
67
|
-
});
|
|
68
|
-
} else {
|
|
69
|
-
Raven.captureException('error', {
|
|
70
|
-
level: type,
|
|
71
|
-
logger: 'manual data',
|
|
72
|
-
extra: {
|
|
73
|
-
data,
|
|
74
|
-
options: this.options,
|
|
75
|
-
date: new Date(),
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export default Report;
|
|
@@ -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
|
-
}
|