@wzyjs/utils 0.0.17 → 0.0.21

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 (39) hide show
  1. package/dist/cjs/common/index.d.ts +2 -2
  2. package/dist/cjs/common/tools/index.d.ts +1 -0
  3. package/dist/cjs/common/tools/number.d.ts +3 -0
  4. package/dist/cjs/common/tools/number.js +34 -0
  5. package/dist/cjs/common/tools/object.d.ts +10 -1
  6. package/dist/cjs/common/tools/object.js +38 -3
  7. package/dist/cjs/common/tools/other.d.ts +14 -2
  8. package/dist/cjs/common/tools/other.js +66 -2
  9. package/dist/cjs/common/tools/string.d.ts +12 -1
  10. package/dist/cjs/common/tools/string.js +81 -1
  11. package/dist/cjs/index_all.d.ts +3 -0
  12. package/dist/cjs/{index_c.js → index_rd.js} +32 -1
  13. package/dist/cjs/rd/index.d.ts +2 -0
  14. package/dist/cjs/rd/jsonFile/index.d.ts +6 -0
  15. package/dist/cjs/rd/jsonFile/index.js +29 -0
  16. package/dist/cjs/rd/jsonFile.d.ts +6 -0
  17. package/dist/cjs/rd/jsonFile.js +29 -0
  18. package/dist/cjs/rd/mail/index.d.ts +1 -0
  19. package/dist/cjs/rd/mail/index.js +32 -0
  20. package/dist/esm/common/index.d.ts +2 -2
  21. package/dist/esm/common/tools/index.d.ts +1 -0
  22. package/dist/esm/common/tools/number.d.ts +3 -0
  23. package/dist/esm/common/tools/number.js +30 -0
  24. package/dist/esm/common/tools/object.d.ts +10 -1
  25. package/dist/esm/common/tools/object.js +36 -3
  26. package/dist/esm/common/tools/other.d.ts +14 -2
  27. package/dist/esm/common/tools/other.js +65 -3
  28. package/dist/esm/common/tools/string.d.ts +12 -1
  29. package/dist/esm/common/tools/string.js +71 -2
  30. package/dist/esm/index_all.d.ts +3 -0
  31. package/dist/esm/index_fe.js +16 -0
  32. package/dist/esm/rd/index.d.ts +1 -0
  33. package/dist/esm/rd/jsonFile.d.ts +6 -0
  34. package/package.json +7 -5
  35. package/dist/esm/index.js +0 -15
  36. /package/dist/cjs/{index.d.ts → index_fe.d.ts} +0 -0
  37. /package/dist/cjs/{index_c.d.ts → index_rd.d.ts} +0 -0
  38. /package/dist/esm/{index.d.ts → index_fe.d.ts} +0 -0
  39. /package/dist/esm/{index_c.d.ts → index_rd.d.ts} +0 -0
@@ -1,5 +1,5 @@
1
- import { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError } from 'lodash';
2
- export { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, };
1
+ import { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, find, groupBy } from 'lodash';
2
+ export { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, find, groupBy };
3
3
  export * as consola from 'consola';
4
4
  export * from './tools';
5
5
  export * from './types';
@@ -1,3 +1,4 @@
1
1
  export * from './string';
2
2
  export * from './object';
3
3
  export * from './other';
4
+ export * from './number';
@@ -0,0 +1,3 @@
1
+ export declare const getRandomNum: (min: number, max: number) => number;
2
+ export declare const limitDecimals: (v: string, num: number | undefined, isForce: boolean) => string;
3
+ export declare const getFileSize: (size: number) => string;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ // 取指定范围内的随机数
4
+ const getRandomNum = (min, max) => {
5
+ return Math.floor(Math.random() * (max - min + 1) + min);
6
+ };
7
+ // 指定小数点的位数
8
+ const limitDecimals = (v, num = 2, isForce) => {
9
+ let value = parseFloat(v);
10
+ if (isNaN(value)) {
11
+ if (isForce) {
12
+ value = 0;
13
+ }
14
+ else {
15
+ return '';
16
+ }
17
+ }
18
+ return value.toFixed(num).toString();
19
+ };
20
+ // 转换成更易读的尺寸单位
21
+ const getFileSize = (size) => {
22
+ let d = 'G';
23
+ let s = size / 1024 / 1024 / 1024;
24
+ if (s < 1) {
25
+ d = 'M';
26
+ s *= 1024;
27
+ }
28
+ // @ts-ignore
29
+ return `${(s.toFixed(1) / 1).toString()}${d}`;
30
+ };
31
+
32
+ exports.getFileSize = getFileSize;
33
+ exports.getRandomNum = getRandomNum;
34
+ exports.limitDecimals = limitDecimals;
@@ -1 +1,10 @@
1
- export declare const checkAttr: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => boolean;
1
+ export declare const findItem: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => I | undefined;
2
+ export declare const filterParams: (params: {
3
+ [key: string]: any;
4
+ }) => {};
5
+ export declare const watch: {
6
+ observe(obj: {
7
+ [key: string]: any;
8
+ }, key: string, watchFun: (value: any, val: any) => undefined): void;
9
+ setWatcher(data?: {}, watch?: {}): void;
10
+ };
@@ -1,8 +1,43 @@
1
1
  'use strict';
2
2
 
3
3
  // 判断是否有包含指定属性的对象,第三个参数可以指定具体的值
4
- const checkAttr = (list, attr, value) => {
5
- return list.some((item) => value === undefined ? item[attr] : item[attr] === value);
4
+ const findItem = (list, attr, value) => {
5
+ return list.find((item) => value === undefined ? item[attr] : item[attr] === value);
6
+ };
7
+ // 过滤对象中为undefined的属性
8
+ const filterParams = (params) => {
9
+ return Object.entries(params || {}).reduce((obj, [key, value]) => {
10
+ if (value !== undefined) {
11
+ obj[key] = value;
12
+ }
13
+ return obj;
14
+ }, {});
15
+ };
16
+ // 监听属性
17
+ const watch = {
18
+ observe(obj, key, watchFun) {
19
+ // 给该属性设默认值
20
+ const val = obj[key];
21
+ Object.defineProperty(obj, key, {
22
+ configurable: true,
23
+ enumerable: true,
24
+ set(value) {
25
+ obj[key] = value;
26
+ // 赋值(set)时,调用对应函数
27
+ watchFun(value, val);
28
+ },
29
+ get() {
30
+ return val;
31
+ },
32
+ });
33
+ },
34
+ setWatcher(data = {}, watch = {}) {
35
+ Object.keys(watch).forEach(v => {
36
+ this.observe(data, v, watch[v]); // 监听data内的v属性,传入watch内对应函数以调用
37
+ });
38
+ },
6
39
  };
7
40
 
8
- exports.checkAttr = checkAttr;
41
+ exports.filterParams = filterParams;
42
+ exports.findItem = findItem;
43
+ exports.watch = watch;
@@ -1,8 +1,11 @@
1
1
  import { KeyValue, OrderParams, Pagination, SortParams } from '../types';
2
2
  export declare const handleParams: (params?: Pagination & KeyValue, sort?: SortParams) => {
3
- page: Pagination;
3
+ page: {
4
+ size?: number | undefined;
5
+ current?: number | undefined;
6
+ };
4
7
  where: KeyValue<string, string | number | boolean | RegExp>;
5
- order?: OrderParams;
8
+ order?: OrderParams<string> | undefined;
6
9
  };
7
10
  export declare const handleRes2List: <D>(reqPromise: any) => Promise<{
8
11
  label: string;
@@ -12,3 +15,12 @@ export declare const executePromise: (promise: Promise<any>) => Promise<{
12
15
  result: any;
13
16
  time: number;
14
17
  }>;
18
+ export declare const locationFn: {
19
+ url2Params: (url?: string) => {};
20
+ params2Url: (params: {
21
+ [key: string]: any;
22
+ }) => string;
23
+ setUrlParams: (key: string, value: any, keepName: string) => void;
24
+ urlGetPath: (url?: string) => string;
25
+ };
26
+ export declare const delay: (time?: number) => Promise<unknown>;
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var lodash = require('lodash');
4
+
3
5
  // 处理 ProComponents 中 ProTable 参数中的 page 和 where 和 sort
4
6
  const handleParams = (params, sort) => {
5
7
  if (!params) {
@@ -12,7 +14,7 @@ const handleParams = (params, sort) => {
12
14
  return {
13
15
  page: {
14
16
  current: current || 1,
15
- pageSize: pageSize || 10,
17
+ size: pageSize || 10,
16
18
  },
17
19
  where: Object.entries(other).reduce((acc, [key, value]) => {
18
20
  if (key && value) {
@@ -31,7 +33,7 @@ const handleRes2List = async (reqPromise) => {
31
33
  const { data } = await reqPromise();
32
34
  return (data?.list || []).map((item) => ({
33
35
  label: item.name,
34
- value: item.key,
36
+ value: item._id || item.key,
35
37
  }));
36
38
  };
37
39
  // 计算执行 promise 花费的时间
@@ -43,8 +45,70 @@ const executePromise = async (promise) => {
43
45
  result,
44
46
  time,
45
47
  };
48
+ };
49
+ // 地址栏方面的操作
50
+ const locationFn = {
51
+ // url地址的query转为query对象
52
+ url2Params: (url = location.href) => {
53
+ url = decodeURIComponent(url);
54
+ let jsonList = {};
55
+ if (url.indexOf('?') > -1) {
56
+ let str = url.slice(url.indexOf('?') + 1);
57
+ let strs = str.split('&');
58
+ for (let i = 0; i < strs.length; i++) {
59
+ jsonList[strs[i].split('=')[0]] = strs[i].split('=')[1]; // 如果出现乱码的话,可以用decodeURI()进行解码
60
+ }
61
+ }
62
+ return jsonList || {};
63
+ },
64
+ // 参数对象转换为url
65
+ params2Url: (params) => {
66
+ let url = '';
67
+ for (let k in params) {
68
+ if (k) {
69
+ url += `${k}=${params[k]}&`;
70
+ }
71
+ }
72
+ return url.substr(0, url.length - 1);
73
+ },
74
+ // 设置地址栏参数
75
+ setUrlParams: (key, value, keepName) => {
76
+ if (key === undefined || value === undefined)
77
+ return;
78
+ // 给地址栏参数对象添加指定参数
79
+ let allParams = locationFn.url2Params();
80
+ if (keepName) {
81
+ allParams = lodash.pick(allParams, keepName) || {};
82
+ }
83
+ allParams[key] = encodeURIComponent(value);
84
+ // 域名和路径
85
+ const url = location.href;
86
+ const sliceEnd = url.indexOf('?') === -1 ? url.length : url.indexOf('?');
87
+ const domainPath = url.slice(0, sliceEnd);
88
+ // 参数
89
+ const params = locationFn.params2Url(allParams);
90
+ // 修改地址栏url
91
+ location.replace(`${domainPath}?${params}`);
92
+ },
93
+ // 取地址栏的path部分
94
+ urlGetPath: (url = location.href) => {
95
+ // 计算要截取的最后一位
96
+ let lastIndex = url.indexOf('?');
97
+ if (lastIndex === -1) {
98
+ lastIndex = url.length;
99
+ }
100
+ return url.slice(0, lastIndex);
101
+ },
102
+ };
103
+ // 延迟指定毫秒数
104
+ const delay = (time = 1000) => {
105
+ return new Promise(resolve => {
106
+ setTimeout(resolve, time);
107
+ });
46
108
  };
47
109
 
110
+ exports.delay = delay;
48
111
  exports.executePromise = executePromise;
49
112
  exports.handleParams = handleParams;
50
113
  exports.handleRes2List = handleRes2List;
114
+ exports.locationFn = locationFn;
@@ -2,4 +2,15 @@ export declare const getChineseByStr: (str: string) => string;
2
2
  export declare const getStrLength: (value: string) => number;
3
3
  export declare const replaceAll: (str: string, searchValue: string, replaceValue: string) => string;
4
4
  export declare const replaceByRules: (str: string, rules: [string, string][]) => string;
5
- export declare const jsonParse: (value: string | object) => object | any[];
5
+ export declare const getType: (value: any) => string;
6
+ export declare const amount: (str: string) => string;
7
+ export declare const jsonParse: (value: string | object) => object;
8
+ export declare const isJson: (str: string) => boolean;
9
+ export declare const toString: (value: any) => string;
10
+ export declare const getRandomColor: () => string;
11
+ export declare const getRandomString: (length?: number) => string;
12
+ export declare const getChinese: (str: string) => string;
13
+ export declare const getSliceStr: (str: string, before: string, after: string) => string;
14
+ export declare const getProxyUrl: (url: string) => string;
15
+ export declare const getLength: (value: string) => number;
16
+ export declare const getCookie: (name: string) => string | null;
@@ -37,7 +37,16 @@ const replaceByRules = (str, rules) => {
37
37
  });
38
38
  return str;
39
39
  };
40
- // JSON.parse
40
+ // 获取变量的类型
41
+ const getType = (value) => {
42
+ return Object.prototype.toString.call(value).slice(8, -1);
43
+ };
44
+ // 金钱格式化,每三位加,
45
+ const amount = (str) => {
46
+ const reg = /(?=(?!\b)(\d{3})+$)/g;
47
+ return str.replace(reg, ',');
48
+ };
49
+ // 更安全的 JSON.parse
41
50
  const jsonParse = (value) => {
42
51
  if (typeof value === 'object') {
43
52
  return value;
@@ -48,10 +57,81 @@ const jsonParse = (value) => {
48
57
  catch (err) {
49
58
  return {};
50
59
  }
60
+ };
61
+ // 是否为json字符串
62
+ const isJson = (str) => {
63
+ try {
64
+ if (getType(JSON.parse(str)) === 'Object') {
65
+ return true;
66
+ }
67
+ }
68
+ catch (e) {
69
+ }
70
+ return false;
71
+ };
72
+ // 将变量转为字符串
73
+ const toString = (value) => {
74
+ return Object.prototype.toString.call(value).slice(8, -1) === 'object' ? JSON.stringify(value) : String(value);
75
+ };
76
+ // 生成随机颜色
77
+ const getRandomColor = () => {
78
+ const color = Math.floor(Math.random() * 16777215).toString(16);
79
+ if (color.length === 6) {
80
+ return color;
81
+ }
82
+ else {
83
+ return getRandomColor();
84
+ }
85
+ };
86
+ // 生成随机字符串
87
+ const getRandomString = (length = 4) => {
88
+ return Math.random().toString(36).substr(2, length);
89
+ };
90
+ // 获取字符串内的中文
91
+ const getChinese = (str) => {
92
+ if (str == null || str === '') {
93
+ return '';
94
+ }
95
+ const res = str.match(/[\u4e00-\u9fa5]/g);
96
+ if (!res) {
97
+ return '';
98
+ }
99
+ return res.join('');
100
+ };
101
+ // 截取指定两个文本之间的字符串 (不好用)
102
+ const getSliceStr = (str, before, after) => {
103
+ return str.slice(str.indexOf(before) + before.length, str.lastIndexOf(after));
104
+ };
105
+ // 获取代理后的url
106
+ const getProxyUrl = (url) => {
107
+ const beforeUrl = 'https://1141871752167714.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/a.LATEST/proxy/?url=';
108
+ return beforeUrl + url;
109
+ };
110
+ // 获取字符串的长度
111
+ const getLength = (value) => {
112
+ const chineseLength = getChinese(value).length;
113
+ return (value.length - chineseLength) + chineseLength * 2;
114
+ };
115
+ // 获取指定cookie
116
+ const getCookie = (name) => {
117
+ const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
118
+ const arr = document.cookie.match(reg);
119
+ return arr ? unescape(arr[2]) : null;
51
120
  };
52
121
 
122
+ exports.amount = amount;
123
+ exports.getChinese = getChinese;
53
124
  exports.getChineseByStr = getChineseByStr;
125
+ exports.getCookie = getCookie;
126
+ exports.getLength = getLength;
127
+ exports.getProxyUrl = getProxyUrl;
128
+ exports.getRandomColor = getRandomColor;
129
+ exports.getRandomString = getRandomString;
130
+ exports.getSliceStr = getSliceStr;
54
131
  exports.getStrLength = getStrLength;
132
+ exports.getType = getType;
133
+ exports.isJson = isJson;
55
134
  exports.jsonParse = jsonParse;
56
135
  exports.replaceAll = replaceAll;
57
136
  exports.replaceByRules = replaceByRules;
137
+ exports.toString = toString;
@@ -0,0 +1,3 @@
1
+ export * from './common';
2
+ export * from './fe';
3
+ export * from './rd';
@@ -5,7 +5,10 @@ var consola = require('consola');
5
5
  var string = require('./common/tools/string.js');
6
6
  var object = require('./common/tools/object.js');
7
7
  var other = require('./common/tools/other.js');
8
+ var number = require('./common/tools/number.js');
8
9
  var Database = require('./rd/database/Database.js');
10
+ var index = require('./rd/jsonFile/index.js');
11
+ var index$1 = require('./rd/mail/index.js');
9
12
 
10
13
  function _interopNamespaceDefault(e) {
11
14
  var n = Object.create(null);
@@ -36,6 +39,14 @@ Object.defineProperty(exports, 'debounce', {
36
39
  enumerable: true,
37
40
  get: function () { return lodash.debounce; }
38
41
  });
42
+ Object.defineProperty(exports, 'find', {
43
+ enumerable: true,
44
+ get: function () { return lodash.find; }
45
+ });
46
+ Object.defineProperty(exports, 'groupBy', {
47
+ enumerable: true,
48
+ get: function () { return lodash.groupBy; }
49
+ });
39
50
  Object.defineProperty(exports, 'isBoolean', {
40
51
  enumerable: true,
41
52
  get: function () { return lodash.isBoolean; }
@@ -94,13 +105,33 @@ Object.defineProperty(exports, 'uniqWith', {
94
105
  get: function () { return lodash.uniqWith; }
95
106
  });
96
107
  exports.consola = consola__namespace;
108
+ exports.amount = string.amount;
109
+ exports.getChinese = string.getChinese;
97
110
  exports.getChineseByStr = string.getChineseByStr;
111
+ exports.getCookie = string.getCookie;
112
+ exports.getLength = string.getLength;
113
+ exports.getProxyUrl = string.getProxyUrl;
114
+ exports.getRandomColor = string.getRandomColor;
115
+ exports.getRandomString = string.getRandomString;
116
+ exports.getSliceStr = string.getSliceStr;
98
117
  exports.getStrLength = string.getStrLength;
118
+ exports.getType = string.getType;
119
+ exports.isJson = string.isJson;
99
120
  exports.jsonParse = string.jsonParse;
100
121
  exports.replaceAll = string.replaceAll;
101
122
  exports.replaceByRules = string.replaceByRules;
102
- exports.checkAttr = object.checkAttr;
123
+ exports.toString = string.toString;
124
+ exports.filterParams = object.filterParams;
125
+ exports.findItem = object.findItem;
126
+ exports.watch = object.watch;
127
+ exports.delay = other.delay;
103
128
  exports.executePromise = other.executePromise;
104
129
  exports.handleParams = other.handleParams;
105
130
  exports.handleRes2List = other.handleRes2List;
131
+ exports.locationFn = other.locationFn;
132
+ exports.getFileSize = number.getFileSize;
133
+ exports.getRandomNum = number.getRandomNum;
134
+ exports.limitDecimals = number.limitDecimals;
106
135
  exports.Database = Database.Database;
136
+ exports.JsonFile = index.JsonFile;
137
+ exports.sendMail = index$1.sendMail;
@@ -1 +1,3 @@
1
1
  export * from './database';
2
+ export * from './jsonFile';
3
+ export * from './mail';
@@ -0,0 +1,6 @@
1
+ export declare class JsonFile {
2
+ filePath: string;
3
+ constructor(filePath: string);
4
+ get(key: string): Promise<unknown>;
5
+ set(key: string, value: string): void;
6
+ }
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ // 实现一个文件系统读写数据库
6
+ class JsonFile {
7
+ filePath;
8
+ constructor(filePath) {
9
+ this.filePath = path.resolve(__dirname, filePath);
10
+ }
11
+ get(key) {
12
+ return new Promise((resolve) => {
13
+ fs.readFile(this.filePath, (err, data) => {
14
+ const json = data ? JSON.parse(data) : {};
15
+ resolve(json[key]);
16
+ });
17
+ });
18
+ }
19
+ set(key, value) {
20
+ fs.readFile(this.filePath, { encoding: 'utf-8' }, (err, data) => {
21
+ const json = data ? JSON.parse(data || '{}') : {};
22
+ json[key] = value;
23
+ fs.writeFile(this.filePath, JSON.stringify(json), () => {
24
+ });
25
+ });
26
+ }
27
+ }
28
+
29
+ exports.JsonFile = JsonFile;
@@ -0,0 +1,6 @@
1
+ export declare class JsonFile {
2
+ filePath: string;
3
+ constructor(filePath: string);
4
+ get(key: string): Promise<unknown>;
5
+ set(key: string, value: string): void;
6
+ }
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ // 实现一个文件系统读写数据库
6
+ class JsonFile {
7
+ filePath;
8
+ constructor(filePath) {
9
+ this.filePath = path.resolve(__dirname, filePath);
10
+ }
11
+ get(key) {
12
+ return new Promise((resolve) => {
13
+ fs.readFile(this.filePath, (err, data) => {
14
+ const json = data ? JSON.parse(data) : {};
15
+ resolve(json[key]);
16
+ });
17
+ });
18
+ }
19
+ set(key, value) {
20
+ fs.readFile(this.filePath, { encoding: 'utf-8' }, (err, data) => {
21
+ const json = data ? JSON.parse(data || '{}') : {};
22
+ json[key] = value;
23
+ fs.writeFile(this.filePath, JSON.stringify(json), () => {
24
+ });
25
+ });
26
+ }
27
+ }
28
+
29
+ exports.JsonFile = JsonFile;
@@ -0,0 +1 @@
1
+ export declare const sendMail: (title: string, content: string) => Promise<string>;
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ var nodemailer = require('nodemailer');
4
+
5
+ const fromEmail = '15835196981@163.com';
6
+ const fromPass = 'REGDSGDETFECIGKE';
7
+ const toEmail = '657189555@qq.com';
8
+ const transporter = nodemailer.createTransport({
9
+ host: 'smtp.163.com',
10
+ port: 465,
11
+ secure: true,
12
+ auth: {
13
+ user: fromEmail,
14
+ pass: fromPass
15
+ }
16
+ });
17
+ const sendMail = async (title, content) => {
18
+ try {
19
+ await transporter.sendMail({
20
+ from: fromEmail,
21
+ to: toEmail,
22
+ subject: title,
23
+ text: content // 邮件正文
24
+ });
25
+ return title + ' 邮件通知成功';
26
+ }
27
+ catch (error) {
28
+ return title + '邮件通知失败' + JSON.stringify(error);
29
+ }
30
+ };
31
+
32
+ exports.sendMail = sendMail;
@@ -1,5 +1,5 @@
1
- import { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError } from 'lodash';
2
- export { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, };
1
+ import { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, find, groupBy } from 'lodash';
2
+ export { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, find, groupBy };
3
3
  export * as consola from 'consola';
4
4
  export * from './tools';
5
5
  export * from './types';
@@ -1,3 +1,4 @@
1
1
  export * from './string';
2
2
  export * from './object';
3
3
  export * from './other';
4
+ export * from './number';
@@ -0,0 +1,3 @@
1
+ export declare const getRandomNum: (min: number, max: number) => number;
2
+ export declare const limitDecimals: (v: string, num: number | undefined, isForce: boolean) => string;
3
+ export declare const getFileSize: (size: number) => string;
@@ -0,0 +1,30 @@
1
+ // 取指定范围内的随机数
2
+ const getRandomNum = (min, max) => {
3
+ return Math.floor(Math.random() * (max - min + 1) + min);
4
+ };
5
+ // 指定小数点的位数
6
+ const limitDecimals = (v, num = 2, isForce) => {
7
+ let value = parseFloat(v);
8
+ if (isNaN(value)) {
9
+ if (isForce) {
10
+ value = 0;
11
+ }
12
+ else {
13
+ return '';
14
+ }
15
+ }
16
+ return value.toFixed(num).toString();
17
+ };
18
+ // 转换成更易读的尺寸单位
19
+ const getFileSize = (size) => {
20
+ let d = 'G';
21
+ let s = size / 1024 / 1024 / 1024;
22
+ if (s < 1) {
23
+ d = 'M';
24
+ s *= 1024;
25
+ }
26
+ // @ts-ignore
27
+ return `${(s.toFixed(1) / 1).toString()}${d}`;
28
+ };
29
+
30
+ export { getFileSize, getRandomNum, limitDecimals };
@@ -1 +1,10 @@
1
- export declare const checkAttr: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => boolean;
1
+ export declare const findItem: <I>(list: I[], attr: keyof I, value?: I[keyof I] | undefined) => I | undefined;
2
+ export declare const filterParams: (params: {
3
+ [key: string]: any;
4
+ }) => {};
5
+ export declare const watch: {
6
+ observe(obj: {
7
+ [key: string]: any;
8
+ }, key: string, watchFun: (value: any, val: any) => undefined): void;
9
+ setWatcher(data?: {}, watch?: {}): void;
10
+ };
@@ -1,6 +1,39 @@
1
1
  // 判断是否有包含指定属性的对象,第三个参数可以指定具体的值
2
- const checkAttr = (list, attr, value) => {
3
- return list.some((item) => value === undefined ? item[attr] : item[attr] === value);
2
+ const findItem = (list, attr, value) => {
3
+ return list.find((item) => value === undefined ? item[attr] : item[attr] === value);
4
+ };
5
+ // 过滤对象中为undefined的属性
6
+ const filterParams = (params) => {
7
+ return Object.entries(params || {}).reduce((obj, [key, value]) => {
8
+ if (value !== undefined) {
9
+ obj[key] = value;
10
+ }
11
+ return obj;
12
+ }, {});
13
+ };
14
+ // 监听属性
15
+ const watch = {
16
+ observe(obj, key, watchFun) {
17
+ // 给该属性设默认值
18
+ const val = obj[key];
19
+ Object.defineProperty(obj, key, {
20
+ configurable: true,
21
+ enumerable: true,
22
+ set(value) {
23
+ obj[key] = value;
24
+ // 赋值(set)时,调用对应函数
25
+ watchFun(value, val);
26
+ },
27
+ get() {
28
+ return val;
29
+ },
30
+ });
31
+ },
32
+ setWatcher(data = {}, watch = {}) {
33
+ Object.keys(watch).forEach(v => {
34
+ this.observe(data, v, watch[v]); // 监听data内的v属性,传入watch内对应函数以调用
35
+ });
36
+ },
4
37
  };
5
38
 
6
- export { checkAttr };
39
+ export { filterParams, findItem, watch };
@@ -1,8 +1,11 @@
1
1
  import { KeyValue, OrderParams, Pagination, SortParams } from '../types';
2
2
  export declare const handleParams: (params?: Pagination & KeyValue, sort?: SortParams) => {
3
- page: Pagination;
3
+ page: {
4
+ size?: number | undefined;
5
+ current?: number | undefined;
6
+ };
4
7
  where: KeyValue<string, string | number | boolean | RegExp>;
5
- order?: OrderParams;
8
+ order?: OrderParams<string> | undefined;
6
9
  };
7
10
  export declare const handleRes2List: <D>(reqPromise: any) => Promise<{
8
11
  label: string;
@@ -12,3 +15,12 @@ export declare const executePromise: (promise: Promise<any>) => Promise<{
12
15
  result: any;
13
16
  time: number;
14
17
  }>;
18
+ export declare const locationFn: {
19
+ url2Params: (url?: string) => {};
20
+ params2Url: (params: {
21
+ [key: string]: any;
22
+ }) => string;
23
+ setUrlParams: (key: string, value: any, keepName: string) => void;
24
+ urlGetPath: (url?: string) => string;
25
+ };
26
+ export declare const delay: (time?: number) => Promise<unknown>;
@@ -1,3 +1,5 @@
1
+ import { pick } from 'lodash';
2
+
1
3
  // 处理 ProComponents 中 ProTable 参数中的 page 和 where 和 sort
2
4
  const handleParams = (params, sort) => {
3
5
  if (!params) {
@@ -10,7 +12,7 @@ const handleParams = (params, sort) => {
10
12
  return {
11
13
  page: {
12
14
  current: current || 1,
13
- pageSize: pageSize || 10,
15
+ size: pageSize || 10,
14
16
  },
15
17
  where: Object.entries(other).reduce((acc, [key, value]) => {
16
18
  if (key && value) {
@@ -29,7 +31,7 @@ const handleRes2List = async (reqPromise) => {
29
31
  const { data } = await reqPromise();
30
32
  return (data?.list || []).map((item) => ({
31
33
  label: item.name,
32
- value: item.key,
34
+ value: item._id || item.key,
33
35
  }));
34
36
  };
35
37
  // 计算执行 promise 花费的时间
@@ -41,6 +43,66 @@ const executePromise = async (promise) => {
41
43
  result,
42
44
  time,
43
45
  };
46
+ };
47
+ // 地址栏方面的操作
48
+ const locationFn = {
49
+ // url地址的query转为query对象
50
+ url2Params: (url = location.href) => {
51
+ url = decodeURIComponent(url);
52
+ let jsonList = {};
53
+ if (url.indexOf('?') > -1) {
54
+ let str = url.slice(url.indexOf('?') + 1);
55
+ let strs = str.split('&');
56
+ for (let i = 0; i < strs.length; i++) {
57
+ jsonList[strs[i].split('=')[0]] = strs[i].split('=')[1]; // 如果出现乱码的话,可以用decodeURI()进行解码
58
+ }
59
+ }
60
+ return jsonList || {};
61
+ },
62
+ // 参数对象转换为url
63
+ params2Url: (params) => {
64
+ let url = '';
65
+ for (let k in params) {
66
+ if (k) {
67
+ url += `${k}=${params[k]}&`;
68
+ }
69
+ }
70
+ return url.substr(0, url.length - 1);
71
+ },
72
+ // 设置地址栏参数
73
+ setUrlParams: (key, value, keepName) => {
74
+ if (key === undefined || value === undefined)
75
+ return;
76
+ // 给地址栏参数对象添加指定参数
77
+ let allParams = locationFn.url2Params();
78
+ if (keepName) {
79
+ allParams = pick(allParams, keepName) || {};
80
+ }
81
+ allParams[key] = encodeURIComponent(value);
82
+ // 域名和路径
83
+ const url = location.href;
84
+ const sliceEnd = url.indexOf('?') === -1 ? url.length : url.indexOf('?');
85
+ const domainPath = url.slice(0, sliceEnd);
86
+ // 参数
87
+ const params = locationFn.params2Url(allParams);
88
+ // 修改地址栏url
89
+ location.replace(`${domainPath}?${params}`);
90
+ },
91
+ // 取地址栏的path部分
92
+ urlGetPath: (url = location.href) => {
93
+ // 计算要截取的最后一位
94
+ let lastIndex = url.indexOf('?');
95
+ if (lastIndex === -1) {
96
+ lastIndex = url.length;
97
+ }
98
+ return url.slice(0, lastIndex);
99
+ },
100
+ };
101
+ // 延迟指定毫秒数
102
+ const delay = (time = 1000) => {
103
+ return new Promise(resolve => {
104
+ setTimeout(resolve, time);
105
+ });
44
106
  };
45
107
 
46
- export { executePromise, handleParams, handleRes2List };
108
+ export { delay, executePromise, handleParams, handleRes2List, locationFn };
@@ -2,4 +2,15 @@ export declare const getChineseByStr: (str: string) => string;
2
2
  export declare const getStrLength: (value: string) => number;
3
3
  export declare const replaceAll: (str: string, searchValue: string, replaceValue: string) => string;
4
4
  export declare const replaceByRules: (str: string, rules: [string, string][]) => string;
5
- export declare const jsonParse: (value: string | object) => object | any[];
5
+ export declare const getType: (value: any) => string;
6
+ export declare const amount: (str: string) => string;
7
+ export declare const jsonParse: (value: string | object) => object;
8
+ export declare const isJson: (str: string) => boolean;
9
+ export declare const toString: (value: any) => string;
10
+ export declare const getRandomColor: () => string;
11
+ export declare const getRandomString: (length?: number) => string;
12
+ export declare const getChinese: (str: string) => string;
13
+ export declare const getSliceStr: (str: string, before: string, after: string) => string;
14
+ export declare const getProxyUrl: (url: string) => string;
15
+ export declare const getLength: (value: string) => number;
16
+ export declare const getCookie: (name: string) => string | null;
@@ -35,7 +35,16 @@ const replaceByRules = (str, rules) => {
35
35
  });
36
36
  return str;
37
37
  };
38
- // JSON.parse
38
+ // 获取变量的类型
39
+ const getType = (value) => {
40
+ return Object.prototype.toString.call(value).slice(8, -1);
41
+ };
42
+ // 金钱格式化,每三位加,
43
+ const amount = (str) => {
44
+ const reg = /(?=(?!\b)(\d{3})+$)/g;
45
+ return str.replace(reg, ',');
46
+ };
47
+ // 更安全的 JSON.parse
39
48
  const jsonParse = (value) => {
40
49
  if (typeof value === 'object') {
41
50
  return value;
@@ -46,6 +55,66 @@ const jsonParse = (value) => {
46
55
  catch (err) {
47
56
  return {};
48
57
  }
58
+ };
59
+ // 是否为json字符串
60
+ const isJson = (str) => {
61
+ try {
62
+ if (getType(JSON.parse(str)) === 'Object') {
63
+ return true;
64
+ }
65
+ }
66
+ catch (e) {
67
+ }
68
+ return false;
69
+ };
70
+ // 将变量转为字符串
71
+ const toString = (value) => {
72
+ return Object.prototype.toString.call(value).slice(8, -1) === 'object' ? JSON.stringify(value) : String(value);
73
+ };
74
+ // 生成随机颜色
75
+ const getRandomColor = () => {
76
+ const color = Math.floor(Math.random() * 16777215).toString(16);
77
+ if (color.length === 6) {
78
+ return color;
79
+ }
80
+ else {
81
+ return getRandomColor();
82
+ }
83
+ };
84
+ // 生成随机字符串
85
+ const getRandomString = (length = 4) => {
86
+ return Math.random().toString(36).substr(2, length);
87
+ };
88
+ // 获取字符串内的中文
89
+ const getChinese = (str) => {
90
+ if (str == null || str === '') {
91
+ return '';
92
+ }
93
+ const res = str.match(/[\u4e00-\u9fa5]/g);
94
+ if (!res) {
95
+ return '';
96
+ }
97
+ return res.join('');
98
+ };
99
+ // 截取指定两个文本之间的字符串 (不好用)
100
+ const getSliceStr = (str, before, after) => {
101
+ return str.slice(str.indexOf(before) + before.length, str.lastIndexOf(after));
102
+ };
103
+ // 获取代理后的url
104
+ const getProxyUrl = (url) => {
105
+ const beforeUrl = 'https://1141871752167714.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/a.LATEST/proxy/?url=';
106
+ return beforeUrl + url;
107
+ };
108
+ // 获取字符串的长度
109
+ const getLength = (value) => {
110
+ const chineseLength = getChinese(value).length;
111
+ return (value.length - chineseLength) + chineseLength * 2;
112
+ };
113
+ // 获取指定cookie
114
+ const getCookie = (name) => {
115
+ const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
116
+ const arr = document.cookie.match(reg);
117
+ return arr ? unescape(arr[2]) : null;
49
118
  };
50
119
 
51
- export { getChineseByStr, getStrLength, jsonParse, replaceAll, replaceByRules };
120
+ export { amount, getChinese, getChineseByStr, getCookie, getLength, getProxyUrl, getRandomColor, getRandomString, getSliceStr, getStrLength, getType, isJson, jsonParse, replaceAll, replaceByRules, toString };
@@ -0,0 +1,3 @@
1
+ export * from './common';
2
+ export * from './fe';
3
+ export * from './rd';
@@ -0,0 +1,16 @@
1
+ export { cloneDeep, debounce, find, groupBy, isBoolean, isEmpty, isEqual, isError, isFunction, isNumber, isObject, isString, default as lodash, merge, omit, pick, uniq, uniqBy, uniqWith } from 'lodash';
2
+ import * as consola from 'consola';
3
+ export { consola };
4
+ export { amount, getChinese, getChineseByStr, getCookie, getLength, getProxyUrl, getRandomColor, getRandomString, getSliceStr, getStrLength, getType, isJson, jsonParse, replaceAll, replaceByRules, toString } from './common/tools/string.js';
5
+ export { filterParams, findItem, watch } from './common/tools/object.js';
6
+ export { delay, executePromise, handleParams, handleRes2List, locationFn } from './common/tools/other.js';
7
+ export { getFileSize, getRandomNum, limitDecimals } from './common/tools/number.js';
8
+ export { localforage } from './fe/index.js';
9
+ import './fe/dayjs.js';
10
+ export { default as dayjs } from 'dayjs';
11
+ export { flashBackground, flashBorder, hexToRgba } from './fe/style.js';
12
+ export { getElement, isElementInViewport, scrollIntoView } from './fe/element.js';
13
+ export { default as copy } from 'copy-to-clipboard';
14
+ export { default as classnames } from 'classnames';
15
+ export { default as md5 } from 'md5';
16
+ export { default as anime } from 'animejs';
@@ -1 +1,2 @@
1
1
  export * from './database';
2
+ export * from './jsonFile';
@@ -0,0 +1,6 @@
1
+ export declare class JsonFile {
2
+ filePath: string;
3
+ constructor(filePath: string);
4
+ get(key: string): Promise<unknown>;
5
+ set(key: string, value: string): void;
6
+ }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@wzyjs/utils",
3
- "version": "0.0.17",
3
+ "version": "0.0.21",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "license": "ISC",
7
- "main": "dist/cjs/index_c.js",
8
- "module": "dist/esm/index.js",
9
- "typings": "dist/cjs/index_c.d.ts",
7
+ "main": "dist/cjs/index_rd.js",
8
+ "module": "dist/esm/index_fe.js",
9
+ "typings": "dist/esm/index_all.d.ts",
10
10
  "scripts": {
11
11
  "dev": "rollup -c -w --bundleConfigAsCjs",
12
12
  "build": "rollup -c --bundleConfigAsCjs"
@@ -17,6 +17,7 @@
17
17
  "dependencies": {
18
18
  "@cloudbase/node-sdk": "^2.9.1",
19
19
  "@types/animejs": "^3.1.6",
20
+ "@types/nodemailer": "^6.4.7",
20
21
  "animejs": "^3.2.1",
21
22
  "classnames": "^2.3.2",
22
23
  "consola": "^2.15.3",
@@ -25,6 +26,7 @@
25
26
  "localforage": "^1.10.0",
26
27
  "lodash": "^4.17.21",
27
28
  "md5": "^2.3.0",
29
+ "nodemailer": "^6.9.1",
28
30
  "tslib": "^2.4.1"
29
31
  },
30
32
  "devDependencies": {
@@ -44,5 +46,5 @@
44
46
  "type": "git",
45
47
  "url": "https://gitee.com/wang-zhenyu/app.git"
46
48
  },
47
- "gitHead": "f07be747da894f607f30c2be97c23abac5712bc2"
49
+ "gitHead": "e9f0222770f1a8a3b3365e9fadd8f65a069a3ef5"
48
50
  }
package/dist/esm/index.js DELETED
@@ -1,15 +0,0 @@
1
- export { cloneDeep, debounce, isBoolean, isEmpty, isEqual, isError, isFunction, isNumber, isObject, isString, default as lodash, merge, omit, pick, uniq, uniqBy, uniqWith } from 'lodash';
2
- import * as consola from 'consola';
3
- export { consola };
4
- export { getChineseByStr, getStrLength, jsonParse, replaceAll, replaceByRules } from './common/tools/string.js';
5
- export { checkAttr } from './common/tools/object.js';
6
- export { executePromise, handleParams, handleRes2List } from './common/tools/other.js';
7
- export { localforage } from './fe/index.js';
8
- import './fe/dayjs.js';
9
- export { default as dayjs } from 'dayjs';
10
- export { flashBackground, flashBorder, hexToRgba } from './fe/style.js';
11
- export { getElement, isElementInViewport, scrollIntoView } from './fe/element.js';
12
- export { default as copy } from 'copy-to-clipboard';
13
- export { default as classnames } from 'classnames';
14
- export { default as md5 } from 'md5';
15
- export { default as anime } from 'animejs';
File without changes
File without changes
File without changes
File without changes