@haluo/util 1.0.3 → 1.0.6

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 (45) hide show
  1. package/.babelrc +21 -0
  2. package/.eslintrc.js +216 -0
  3. package/__tests__/unit/date/date.spec.js +14 -0
  4. package/__tests__/unit/jest.conf.js +25 -0
  5. package/__tests__/unit/specs/date.test.js +11 -0
  6. package/coverage/clover.xml +78 -0
  7. package/coverage/coverage-final.json +2 -0
  8. package/coverage/lcov-report/base.css +224 -0
  9. package/coverage/lcov-report/block-navigation.js +79 -0
  10. package/coverage/lcov-report/favicon.png +0 -0
  11. package/coverage/lcov-report/index.html +111 -0
  12. package/coverage/lcov-report/index.ts.html +668 -0
  13. package/coverage/lcov-report/prettify.css +1 -0
  14. package/coverage/lcov-report/prettify.js +2 -0
  15. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  16. package/coverage/lcov-report/sorter.js +170 -0
  17. package/coverage/lcov.info +170 -0
  18. package/dist/index.js +36 -0
  19. package/dist/modules/cookie/index.js +54 -0
  20. package/dist/modules/date/index.js +192 -0
  21. package/dist/modules/dom/index.js +62 -0
  22. package/dist/modules/filter/index.js +57 -0
  23. package/dist/modules/format/index.js +21 -0
  24. package/dist/modules/match/index.js +31 -0
  25. package/dist/modules/number/index.js +102 -0
  26. package/dist/modules/sentry/index.js +81 -0
  27. package/dist/modules/tools/index.js +393 -0
  28. package/global.d.ts +0 -0
  29. package/package.json +1 -2
  30. package/publish.sh +11 -0
  31. package/specification/CSS.md +25 -0
  32. package/specification/JS.md +9 -0
  33. package/specification/VUE.md +1 -0
  34. package/src/consts/httpCode.js +10 -0
  35. package/src/index.ts +54 -0
  36. package/src/modules/cookie/index.ts +69 -0
  37. package/src/modules/date/index.ts +196 -0
  38. package/src/modules/dom/index.ts +78 -0
  39. package/src/modules/filter/index.ts +57 -0
  40. package/src/modules/format/index.ts +19 -0
  41. package/src/modules/match/index.ts +31 -0
  42. package/src/modules/number/index.ts +108 -0
  43. package/src/modules/sentry/index.ts +82 -0
  44. package/src/modules/tools/index.ts +427 -0
  45. package/tsconfig.json +34 -0
@@ -0,0 +1,393 @@
1
+ /**
2
+ * @file: tools 常用的工具函数
3
+ * @Author: wanghui
4
+ */
5
+ 'use strict';
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ var previous = 0;
8
+ var timeout = null;
9
+ var ToolsClass = /** @class */ (function () {
10
+ function ToolsClass() {
11
+ var _this = this;
12
+ /**
13
+ * 蒙层显示后,html禁止滚动操作
14
+ * @param {String} className
15
+ * @return {Void}
16
+ */
17
+ this.stopScroll = function (className) {
18
+ if (!(window && window.document)) {
19
+ return new Error('仅支持浏览器');
20
+ }
21
+ var html = document.documentElement;
22
+ html.style.overflow = 'hidden';
23
+ html.style.height = '100%';
24
+ var body = document.body;
25
+ body.style.overflow = 'hidden';
26
+ body.style.height = '100%';
27
+ if (className) {
28
+ var dom = document.querySelector(".".concat(className));
29
+ dom && dom.addEventListener('touchmove', _this.__setDefault__);
30
+ }
31
+ };
32
+ /**
33
+ * 蒙层隐藏后,html开启滚动操作
34
+ * @param {String} className
35
+ * @return {Void}
36
+ */
37
+ this.startScroll = function (className) {
38
+ if (!(window && window.document)) {
39
+ return new Error('仅支持浏览器');
40
+ }
41
+ var html = document.documentElement;
42
+ html.style.overflow = 'visible';
43
+ html.style.height = 'auto';
44
+ var body = document.body;
45
+ body.style.overflow = 'visible';
46
+ body.style.height = 'auto';
47
+ if (className) {
48
+ var dom = document.querySelector(".".concat(className));
49
+ dom && dom.removeEventListener('touchmove', _this.__setDefault__);
50
+ }
51
+ };
52
+ /**
53
+ * object 对象转 array 数组
54
+ * demo:
55
+ * objectToArray({a:1,b:2}) 输出:["a=1", "b=2"]
56
+ * @param {Object} obj
57
+ * @returns {Array} arr
58
+ */
59
+ this.objectToArray = function (obj) {
60
+ var arr = [];
61
+ if (typeof obj === 'object') {
62
+ for (var key in obj) {
63
+ if (obj.hasOwnProperty(key)) {
64
+ arr.push([key, obj[key]].join('='));
65
+ }
66
+ }
67
+ }
68
+ return arr;
69
+ };
70
+ /**
71
+ * convertEnum
72
+ * 枚举键值互换
73
+ * @param {Object} obj
74
+ * convertKeyValueEnum({a: 1, b: 2}) // {1: "a", 2: "b"}
75
+ * @returns {Object} result
76
+ */
77
+ this.convertKeyValueEnum = function (obj) {
78
+ var result = {};
79
+ if (typeof obj === 'object') {
80
+ for (var key in obj) {
81
+ if (obj.hasOwnProperty(key)) {
82
+ result[obj[key]] = key;
83
+ }
84
+ }
85
+ }
86
+ return result;
87
+ };
88
+ this.isDefined = function (val) {
89
+ return val !== undefined && val !== null;
90
+ };
91
+ this.__loaded__ = {}; // 已加载的资源
92
+ /**
93
+ * 阻止事件传递
94
+ * @param {Event} e
95
+ * @return {Void}
96
+ */
97
+ this.__setDefault__ = function (e) {
98
+ e && e.preventDefault();
99
+ };
100
+ }
101
+ /**
102
+ * 深拷贝 Object|Array
103
+ * 深拷贝,支持 普通对象、数组,但是未解决Function、Date、RegExp,且1M以上数据性能不好
104
+ * @param {String} data
105
+ * @return {Object}
106
+ */
107
+ ToolsClass.prototype.deepCopy = function (data) {
108
+ return JSON.parse(JSON.stringify(data));
109
+ };
110
+ /**
111
+ * 深拷贝 Object|Array
112
+ * 支持 普通对象、数组和函数的深复制,但是未解决循环引用、Date、RegExp
113
+ * @param {Object|Array} data
114
+ * @return {Object}
115
+ */
116
+ ToolsClass.prototype.deepCopy2 = function (obj) {
117
+ var _obj = Array.isArray(obj) ? [] : {};
118
+ for (var i in obj) {
119
+ _obj[i] = typeof obj[i] === 'object' ? this.deepCopy2(obj[i]) : obj[i];
120
+ }
121
+ return _obj;
122
+ };
123
+ /**
124
+ * 深拷贝 Object|Array
125
+ * 注意:需要 yarn add lodash
126
+ * @param {Object|Array} data
127
+ * @return {Object}
128
+ */
129
+ ToolsClass.prototype.deepCopy3 = function (obj) {
130
+ var deepcopy = require('lodash/cloneDeep');
131
+ return deepcopy(obj);
132
+ };
133
+ /**
134
+ * 防抖
135
+ * demo:debounce(func, 300)(args)
136
+ * @param {Function} func 执行函数
137
+ * @param {Number} wait 节流时间,毫秒
138
+ * @return {Function} delay
139
+ */
140
+ ToolsClass.prototype.debounce = function (func, wait) {
141
+ var delay = function () {
142
+ var args = arguments;
143
+ if (timeout)
144
+ clearTimeout(timeout);
145
+ timeout = setTimeout(function () {
146
+ func.apply(delay, args);
147
+ }, wait);
148
+ };
149
+ return delay;
150
+ };
151
+ /**
152
+ * 节流
153
+ * demo:throttle(func, 300)(args)
154
+ * @param {Object|Array} func 执行函数
155
+ * @param {Number} wait 节流时间,毫秒
156
+ * @return {Function} delay
157
+ */
158
+ ToolsClass.prototype.throttle = function (func, wait) {
159
+ var delay = function () {
160
+ var now = Date.now();
161
+ if (now - previous > wait) {
162
+ func.apply(delay, arguments);
163
+ previous = now;
164
+ }
165
+ };
166
+ return delay;
167
+ };
168
+ /**
169
+ * 获取路径中文件名称
170
+ * @param {String} url
171
+ * @return {String}
172
+ */
173
+ ToolsClass.prototype.getUrlName = function (url) {
174
+ return (url && url.split('?')[0].split('/').reverse()[0]);
175
+ };
176
+ /**
177
+ * 动态加载脚本
178
+ * tip:按需加载时用(多次调用,js不会重复加载)
179
+ * @param {String} url
180
+ * @return {Promise}
181
+ */
182
+ ToolsClass.prototype.loadJs = function (url) {
183
+ var _this = this;
184
+ if (!(window && window.document)) {
185
+ return new Error('仅支持浏览器');
186
+ }
187
+ var name = this.getUrlName(url);
188
+ var id = 'js_' + name;
189
+ return new Promise(function (resolve, reject) {
190
+ if (_this.__loaded__[id]) {
191
+ return resolve();
192
+ }
193
+ var script = document.createElement('script');
194
+ script.type = 'text/javascript';
195
+ script.async = true;
196
+ script.src = url;
197
+ script.id = id;
198
+ script.onload = function () {
199
+ _this.__loaded__[id] = true;
200
+ resolve();
201
+ };
202
+ script.onerror = function (e) {
203
+ reject(e);
204
+ };
205
+ document.body.appendChild(script);
206
+ });
207
+ };
208
+ /**
209
+ * 动态加载样式
210
+ * @param {String} url
211
+ * @return {Promise}
212
+ */
213
+ ToolsClass.prototype.loadCss = function (url) {
214
+ var _this = this;
215
+ if (!(window && window.document)) {
216
+ return new Error('仅支持浏览器');
217
+ }
218
+ var name = this.getUrlName(url);
219
+ var id = 'css_' + name;
220
+ return new Promise(function (resolve, reject) {
221
+ if (_this.__loaded__[id]) {
222
+ return resolve();
223
+ }
224
+ var link = document.createElement('link');
225
+ link.type = 'text/css';
226
+ link.rel = 'stylesheet';
227
+ link.href = url;
228
+ link.id = id;
229
+ link.onload = function () {
230
+ _this.__loaded__[id] = true;
231
+ resolve();
232
+ };
233
+ link.onerror = function (e) {
234
+ reject(e);
235
+ };
236
+ document.head.appendChild(link);
237
+ });
238
+ };
239
+ /**
240
+ * 字符串复制到剪贴板
241
+ * 注意:需要 yarn add clipboard-copy
242
+ * @param {String} str
243
+ * @return {String}
244
+ */
245
+ ToolsClass.prototype.clipboard = function (str) {
246
+ if (!(window && window.document)) {
247
+ return new Error('仅支持浏览器');
248
+ }
249
+ var copy = require('clipboard-copy');
250
+ return copy(str);
251
+ };
252
+ /**
253
+ * 首字母大写
254
+ * @param {String} str
255
+ * @return {String}
256
+ */
257
+ ToolsClass.prototype.firstUpperCase = function (str) {
258
+ return str.charAt(0).toUpperCase() + str.toString().slice(1);
259
+ };
260
+ /**
261
+ * 截取数组或字符串,示例:slice('1234', 3)
262
+ * @param {Array|String} target 数组或字符串
263
+ * @param {Number} length 截取长度,从0开始
264
+ * @return {any}
265
+ */
266
+ ToolsClass.prototype.slice = function (target, length) {
267
+ if (target === void 0) { target = ''; }
268
+ if (length === void 0) { length = 0; }
269
+ return target.slice(0, length);
270
+ };
271
+ /**
272
+ * 获取guid
273
+ * @return {String}
274
+ */
275
+ ToolsClass.prototype.guid = function () {
276
+ function S4() {
277
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
278
+ }
279
+ return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
280
+ };
281
+ /**
282
+ * 获取文本字节数(含中文)
283
+ * @param {String} str
284
+ * @return {String}
285
+ */
286
+ ToolsClass.prototype.getBytesOfText = function (str) {
287
+ if (str === void 0) { str = ''; }
288
+ return str.replace(/[^\u0000-\u00ff]/g, "aa").length;
289
+ };
290
+ /**
291
+ * 数组去重
292
+ * @param {Array} arr
293
+ * @returns {Array}
294
+ */
295
+ ToolsClass.prototype.uniqueArr = function (arr) {
296
+ return Array.from(new Set(arr));
297
+ };
298
+ /**
299
+ * 数组元素交换位置
300
+ * index1和index2分别是两个数组的索引值,即是两个要交换元素位置的索引值,如1,5就是数组中下标为1和5的两个元素交换位置
301
+ * @param {Array<string | number>} array 数组
302
+ * @param {number} index1 添加项目的位置
303
+ * @param {number} index2 删除项目的位置
304
+ * swapArray([1, 2, 3, 4], 2, 3) // [1, 2, 4, 3]
305
+ * @returns {Array<string | number>} array
306
+ */
307
+ ToolsClass.prototype.swapArray = function (array, index1, index2) {
308
+ var _a;
309
+ _a = [array[index2], array[index1]], array[index1] = _a[0], array[index2] = _a[1];
310
+ return array;
311
+ };
312
+ /**
313
+ * 过滤表情符号
314
+ * @param {String} str
315
+ * @return {String}
316
+ */
317
+ ToolsClass.prototype.filterEmoji = function (str) {
318
+ return str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg, '');
319
+ };
320
+ /**
321
+ * 是否包含表情
322
+ * @param {String} str
323
+ * @return {boolean}
324
+ */
325
+ ToolsClass.prototype.containsEmoji = function (str) {
326
+ var reg = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg;
327
+ return reg.test(str);
328
+ };
329
+ /**
330
+ * 是否包含表汉字
331
+ * @param {String} str
332
+ * @return {boolean}
333
+ */
334
+ ToolsClass.prototype.containsHanZi = function (str) {
335
+ var reg = /[\u4e00-\u9fa5]/mg;
336
+ return reg.test(str);
337
+ };
338
+ ToolsClass.prototype.isEmpty = function (val) {
339
+ // null or undefined
340
+ if (val == null)
341
+ return true;
342
+ if (typeof val === 'boolean')
343
+ return false;
344
+ if (typeof val === 'number')
345
+ return !val;
346
+ if (val instanceof Error)
347
+ return val.message === '';
348
+ switch (Object.prototype.toString.call(val)) {
349
+ // String or Array
350
+ case '[object String]':
351
+ case '[object Array]':
352
+ return !val.length;
353
+ // Map or Set or File
354
+ case '[object File]':
355
+ case '[object Map]':
356
+ case '[object Set]': {
357
+ return !val.size;
358
+ }
359
+ // Plain Object
360
+ case '[object Object]': {
361
+ return !Object.keys(val).length;
362
+ }
363
+ }
364
+ return false;
365
+ };
366
+ ;
367
+ /**
368
+ * 字段脱敏处理
369
+ * @param {String} field 未脱敏字段
370
+ * @param {Int} before 开头未脱敏字符数
371
+ * @param {Int} after 结尾未脱敏字符数
372
+ * @return {String} 已脱敏字段
373
+ */
374
+ ToolsClass.prototype.sensitiveField = function (field, before, after) {
375
+ if (before === void 0) { before = 3; }
376
+ if (after === void 0) { after = 4; }
377
+ if (!field) {
378
+ return '';
379
+ }
380
+ field = String(field);
381
+ var sensitiveLen = field.length - before - after;
382
+ if (sensitiveLen < 0) {
383
+ sensitiveLen = 0;
384
+ }
385
+ // 匹配中文、英文、数字
386
+ var regItem = '[\u4e00-\u9fa5a-zA-Z0-9]';
387
+ var regExp = "(".concat(regItem, "{").concat(before, "})").concat(regItem, "*(").concat(regItem, "{").concat(after, "})");
388
+ var reg = new RegExp(regExp);
389
+ return field.replace(reg, "$1".concat("*".repeat(sensitiveLen), "$2"));
390
+ };
391
+ return ToolsClass;
392
+ }());
393
+ module.exports = new ToolsClass();
package/global.d.ts ADDED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haluo/util",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "main": "./dist/index.js",
5
5
  "author": "<wanghui@jddmoto.com>",
6
6
  "description": "哈罗摩托工具库",
@@ -13,7 +13,6 @@
13
13
  "precommit": "lint-staged"
14
14
  },
15
15
  "types": "dist/types/index.d.ts",
16
- "files": ["./dist/**/*", "./package.json"],
17
16
  "lint-staged": {
18
17
  "src/**/*.{js,vue}": [
19
18
  "eslint --fix",
package/publish.sh ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bash
2
+ # npm config get registry # 检查仓库镜像库
3
+ # npm config set registry=http://registry.npmjs.org
4
+ # echo '请进行登录相关操作:'
5
+ # npm login # 登陆
6
+ tsc
7
+ echo "-------publishing-------"
8
+ npm publish # 发布
9
+ # npm config set registry=https://registry.npm.taobao.org # 设置为淘宝镜像
10
+ echo "发布完成"
11
+ exit
@@ -0,0 +1,25 @@
1
+ ### CSS
2
+
3
+
4
+ #### [BEM](http://getbem.com/)
5
+ **BlockElementModifier其实是块(block)、元素(element)、修饰符(modifier)**
6
+ > * block 代表了更高级别的抽象或组件
7
+ > * block__element 代表 block 的后代,用于形成一个完整的 block 的整体
8
+ > * block--modifier代表 block 的不同状态或不同版本
9
+
10
+ ```css
11
+ /* demo */
12
+ person{} /* 人 */
13
+ person__hand{} /* 人的手 */
14
+ person--female{} /* 女人 */
15
+ person--female__hand{} /* 女人的手 */
16
+ person__hand--left{} /* 人的左手 */
17
+ ```
18
+
19
+ #### Z-Index
20
+ ```
21
+ 页面元素:1-99
22
+ fixed:100-199
23
+ 弹框:200-299
24
+ Toast:300-399
25
+ ```
@@ -0,0 +1,9 @@
1
+ ### [JS](http://alloyteam.github.io/CodeGuide/)
2
+
3
+ ```
4
+ 1、路由命名:name 大写,path和文件名 小写加中划线
5
+ 2、组件使用:组件文件、引用名、页面使用 名称保持一致
6
+ 3、JS
7
+ > 接口请求参数做解构
8
+ > 公共文件,多尝试用class
9
+ ```
@@ -0,0 +1 @@
1
+ ### [VUE](https://cn.vuejs.org/v2/style-guide/)
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 处理http返回的错误码等信息
3
+ * @Author: wanghui
4
+ */
5
+ export default {
6
+ SUCCESS: {
7
+ 'resultCode': '0',
8
+ 'resultDesc': '处理成功'
9
+ },
10
+ }
package/src/index.ts ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @Author: wanghui
3
+ * createBy: @2020.05.21
4
+ */
5
+ 'use strict'
6
+
7
+ const modules: any = {
8
+ cookie: require('./modules/cookie'),
9
+ date: require('./modules/date'),
10
+ dom: require('./modules/dom'),
11
+ filter: require('./modules/filter'),
12
+ format: require('./modules/format'),
13
+ match: require('./modules/match'),
14
+ number: require('./modules/number'),
15
+ tools: require('./modules/tools'),
16
+ }
17
+
18
+ class Utils {
19
+ [key: string]: Object
20
+
21
+ constructor() {
22
+ Object.assign(this, modules)
23
+ }
24
+
25
+ /**
26
+ * 挂载各组件
27
+ * 示例:this.$cookie、this.$date、this.$match、this.$number、this.$tools
28
+ * @param {Object} Vue 需要挂载的目标对象
29
+ */
30
+ install(Vue: any) {
31
+ Object.keys(modules).forEach((key) => {
32
+ if (key === 'filter') {
33
+ return modules[key].install(Vue)
34
+ }
35
+ Vue.prototype['$' + key] = modules[key]
36
+ Vue['$' + key] = modules[key]
37
+ })
38
+ }
39
+
40
+ // loadModules() {
41
+ // const modules = require['context']('./modules/', true, /.js$/)
42
+ // modules.keys().forEach((modulesKey: string) => {
43
+ // const attr: string = modulesKey.replace('./', '').replace('.js', '').replace('/index', '')
44
+ // const haveDefault = ['sentry']
45
+ // if (haveDefault.includes(attr)) {
46
+ // this[attr] = modules(modulesKey).default
47
+ // } else {
48
+ // this[attr] = modules(modulesKey)
49
+ // }
50
+ // })
51
+ // }
52
+ }
53
+
54
+ module.exports = new Utils()
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @file Cookie
3
+ * @Author: wanghui
4
+ * @createBy: @2021.01.21
5
+ */
6
+ 'use strict'
7
+ interface ICookie {
8
+ name: string,
9
+ value: string,
10
+ exdays: number,
11
+ path?: string,
12
+ domain?: string,
13
+ }
14
+
15
+ class CookieClass {
16
+ /**
17
+ * 获取cookie
18
+ * @param {String} name
19
+ * @return {String}
20
+ */
21
+ getCookie(name: string) {
22
+ const _name = name + '='
23
+ const ca = document.cookie.split(';')
24
+ for (let i = 0; i < ca.length; i++) {
25
+ let c = ca[i]
26
+ while (c.charAt(0) === ' ') c = c.substring(1)
27
+ if (c.includes(_name)) return c.substring(_name.length, c.length)
28
+ }
29
+ return ''
30
+ }
31
+
32
+ /**
33
+ * 设置cookie
34
+ * @param {Object} ICookie
35
+ */
36
+ setCookie({
37
+ name = '',
38
+ value = '',
39
+ exdays = -1,
40
+ path = '/',
41
+ domain = '.jddmoto.com',
42
+ }: ICookie) {
43
+ var d = new Date();
44
+ d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
45
+ var expires = `expires=${d.toUTCString()}`
46
+ document.cookie = `${name}=${value};${expires};path=${path};domain=${domain};`
47
+ }
48
+
49
+ /**
50
+ * 清除Cookie
51
+ * @param {String} name
52
+ */
53
+ clearCookie({
54
+ name = '',
55
+ path = '/',
56
+ domain = '.jddmoto.com',
57
+ }: ICookie) {
58
+ this.setCookie({
59
+ name,
60
+ value: '',
61
+ exdays: -1,
62
+ path,
63
+ domain
64
+ })
65
+ }
66
+ }
67
+
68
+ module.exports = new CookieClass()
69
+