@wzyjs/utils 0.0.16 → 0.0.18

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 (42) hide show
  1. package/dist/cjs/common/index.d.ts +3 -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 +9 -0
  8. package/dist/cjs/common/tools/other.js +65 -1
  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/fe/dayjs.d.ts +2 -0
  12. package/dist/cjs/fe/element.d.ts +9 -0
  13. package/dist/cjs/fe/index.d.ts +5 -2
  14. package/dist/cjs/fe/style.d.ts +10 -0
  15. package/dist/cjs/index.d.ts +3 -2
  16. package/dist/cjs/index_c.js +31 -1
  17. package/dist/cjs/rd/index.d.ts +1 -0
  18. package/dist/cjs/rd/jsonFile.d.ts +6 -0
  19. package/dist/cjs/rd/jsonFile.js +29 -0
  20. package/dist/esm/common/index.d.ts +3 -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 +9 -0
  27. package/dist/esm/common/tools/other.js +64 -2
  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/fe/dayjs.d.ts +2 -0
  31. package/dist/esm/fe/dayjs.js +9 -0
  32. package/dist/esm/fe/element.d.ts +9 -0
  33. package/dist/esm/fe/element.js +36 -0
  34. package/dist/esm/fe/index.d.ts +5 -2
  35. package/dist/esm/fe/index.js +2 -1
  36. package/dist/esm/fe/style.d.ts +10 -0
  37. package/dist/esm/fe/style.js +45 -0
  38. package/dist/esm/index.d.ts +3 -2
  39. package/dist/esm/index.js +10 -5
  40. package/dist/esm/rd/index.d.ts +1 -0
  41. package/dist/esm/rd/jsonFile.d.ts +6 -0
  42. package/package.json +4 -2
@@ -1,5 +1,6 @@
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';
6
+ export { default as lodash } from 'lodash';
@@ -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;
@@ -12,3 +12,12 @@ export declare const executePromise: (promise: Promise<any>) => Promise<{
12
12
  result: any;
13
13
  time: number;
14
14
  }>;
15
+ export declare const locationFn: {
16
+ url2Params: (url?: string) => {};
17
+ params2Url: (params: {
18
+ [key: string]: any;
19
+ }) => string;
20
+ setUrlParams: (key: string, value: any, keepName: string) => void;
21
+ urlGetPath: (url?: string) => string;
22
+ };
23
+ 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) {
@@ -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,2 @@
1
+ import dayjs from 'dayjs';
2
+ export default dayjs;
@@ -0,0 +1,9 @@
1
+ export declare const scrollIntoView: (el: Element, option?: ScrollIntoViewOptions) => void;
2
+ export declare const getElement: (el: string | Element) => {
3
+ element: null;
4
+ originalStyle: {};
5
+ } | {
6
+ element: Element;
7
+ originalStyle: CSSStyleDeclaration;
8
+ };
9
+ export declare const isElementInViewport: (el: string | Element) => boolean | undefined;
@@ -1,8 +1,8 @@
1
1
  import md5 from 'md5';
2
- import dayjs from 'dayjs';
3
2
  import classnames from 'classnames';
4
3
  import copy from 'copy-to-clipboard';
5
- export { copy, classnames, dayjs, md5 };
4
+ import anime from 'animejs';
5
+ export { copy, classnames, md5, anime };
6
6
  export declare const localforage: {
7
7
  config: {
8
8
  (options: LocalForageOptions): boolean;
@@ -13,3 +13,6 @@ export declare const localforage: {
13
13
  getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
14
14
  removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
15
15
  };
16
+ export * from './style';
17
+ export * from './element';
18
+ export { default as dayjs } from './dayjs';
@@ -0,0 +1,10 @@
1
+ export declare const hexToRgba: (hexColor: string, a?: number) => {
2
+ nums: {
3
+ red: number;
4
+ green: number;
5
+ blue: number;
6
+ };
7
+ text: string;
8
+ };
9
+ export declare const flashBackground: (el: string | Element, color: string, a?: number) => void;
10
+ export declare const flashBorder: (el: string | Element, color: string, a?: number) => void;
@@ -1,2 +1,3 @@
1
- export * from './common';
2
- export * from './fe';
1
+ export * from './common';
2
+ export * from './fe';
3
+ export * from './rd';
@@ -5,7 +5,9 @@ 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 jsonFile = require('./rd/jsonFile.js');
9
11
 
10
12
  function _interopNamespaceDefault(e) {
11
13
  var n = Object.create(null);
@@ -36,6 +38,14 @@ Object.defineProperty(exports, 'debounce', {
36
38
  enumerable: true,
37
39
  get: function () { return lodash.debounce; }
38
40
  });
41
+ Object.defineProperty(exports, 'find', {
42
+ enumerable: true,
43
+ get: function () { return lodash.find; }
44
+ });
45
+ Object.defineProperty(exports, 'groupBy', {
46
+ enumerable: true,
47
+ get: function () { return lodash.groupBy; }
48
+ });
39
49
  Object.defineProperty(exports, 'isBoolean', {
40
50
  enumerable: true,
41
51
  get: function () { return lodash.isBoolean; }
@@ -68,6 +78,7 @@ Object.defineProperty(exports, 'isString', {
68
78
  enumerable: true,
69
79
  get: function () { return lodash.isString; }
70
80
  });
81
+ exports.lodash = lodash;
71
82
  Object.defineProperty(exports, 'merge', {
72
83
  enumerable: true,
73
84
  get: function () { return lodash.merge; }
@@ -93,13 +104,32 @@ Object.defineProperty(exports, 'uniqWith', {
93
104
  get: function () { return lodash.uniqWith; }
94
105
  });
95
106
  exports.consola = consola__namespace;
107
+ exports.amount = string.amount;
108
+ exports.getChinese = string.getChinese;
96
109
  exports.getChineseByStr = string.getChineseByStr;
110
+ exports.getCookie = string.getCookie;
111
+ exports.getLength = string.getLength;
112
+ exports.getProxyUrl = string.getProxyUrl;
113
+ exports.getRandomColor = string.getRandomColor;
114
+ exports.getRandomString = string.getRandomString;
115
+ exports.getSliceStr = string.getSliceStr;
97
116
  exports.getStrLength = string.getStrLength;
117
+ exports.getType = string.getType;
118
+ exports.isJson = string.isJson;
98
119
  exports.jsonParse = string.jsonParse;
99
120
  exports.replaceAll = string.replaceAll;
100
121
  exports.replaceByRules = string.replaceByRules;
101
- exports.checkAttr = object.checkAttr;
122
+ exports.toString = string.toString;
123
+ exports.filterParams = object.filterParams;
124
+ exports.findItem = object.findItem;
125
+ exports.watch = object.watch;
126
+ exports.delay = other.delay;
102
127
  exports.executePromise = other.executePromise;
103
128
  exports.handleParams = other.handleParams;
104
129
  exports.handleRes2List = other.handleRes2List;
130
+ exports.locationFn = other.locationFn;
131
+ exports.getFileSize = number.getFileSize;
132
+ exports.getRandomNum = number.getRandomNum;
133
+ exports.limitDecimals = number.limitDecimals;
105
134
  exports.Database = Database.Database;
135
+ exports.JsonFile = jsonFile.JsonFile;
@@ -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
+ }
@@ -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;
@@ -1,5 +1,6 @@
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';
6
+ export { default as lodash } from 'lodash';
@@ -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 };
@@ -12,3 +12,12 @@ export declare const executePromise: (promise: Promise<any>) => Promise<{
12
12
  result: any;
13
13
  time: number;
14
14
  }>;
15
+ export declare const locationFn: {
16
+ url2Params: (url?: string) => {};
17
+ params2Url: (params: {
18
+ [key: string]: any;
19
+ }) => string;
20
+ setUrlParams: (key: string, value: any, keepName: string) => void;
21
+ urlGetPath: (url?: string) => string;
22
+ };
23
+ 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) {
@@ -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,2 @@
1
+ import dayjs from 'dayjs';
2
+ export default dayjs;
@@ -0,0 +1,9 @@
1
+ import dayjs from 'dayjs';
2
+ export { default } from 'dayjs';
3
+ import isBetween from 'dayjs/plugin/isBetween';
4
+ import weekday from 'dayjs/plugin/weekday';
5
+ import localeData from 'dayjs/plugin/localeData';
6
+
7
+ dayjs.extend(isBetween);
8
+ dayjs.extend(weekday);
9
+ dayjs.extend(localeData);
@@ -0,0 +1,9 @@
1
+ export declare const scrollIntoView: (el: Element, option?: ScrollIntoViewOptions) => void;
2
+ export declare const getElement: (el: string | Element) => {
3
+ element: null;
4
+ originalStyle: {};
5
+ } | {
6
+ element: Element;
7
+ originalStyle: CSSStyleDeclaration;
8
+ };
9
+ export declare const isElementInViewport: (el: string | Element) => boolean | undefined;
@@ -0,0 +1,36 @@
1
+ // 滚动到让指定元素显示出来的位置
2
+ const scrollIntoView = (el, option) => {
3
+ if (!el) {
4
+ return;
5
+ }
6
+ el.scrollIntoView({ behavior: 'smooth', block: 'start', ...option });
7
+ };
8
+ // 获取指定元素 已经元素原始的样式
9
+ const getElement = (el) => {
10
+ const element = typeof el === 'string' ? document.querySelector(el) : el;
11
+ if (!element) {
12
+ return {
13
+ element: null,
14
+ originalStyle: {},
15
+ };
16
+ }
17
+ // 获取元素原始的样式
18
+ return {
19
+ element,
20
+ originalStyle: window.getComputedStyle(element),
21
+ };
22
+ };
23
+ // 判断元素是否在可见范围内
24
+ const isElementInViewport = (el) => {
25
+ const { element } = getElement(el);
26
+ if (!element) {
27
+ return;
28
+ }
29
+ const rect = element.getBoundingClientRect();
30
+ return (rect.top >= 0 &&
31
+ rect.left >= 0 &&
32
+ rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
33
+ rect.right <= (window.innerWidth || document.documentElement.clientWidth));
34
+ };
35
+
36
+ export { getElement, isElementInViewport, scrollIntoView };
@@ -1,8 +1,8 @@
1
1
  import md5 from 'md5';
2
- import dayjs from 'dayjs';
3
2
  import classnames from 'classnames';
4
3
  import copy from 'copy-to-clipboard';
5
- export { copy, classnames, dayjs, md5 };
4
+ import anime from 'animejs';
5
+ export { copy, classnames, md5, anime };
6
6
  export declare const localforage: {
7
7
  config: {
8
8
  (options: LocalForageOptions): boolean;
@@ -13,3 +13,6 @@ export declare const localforage: {
13
13
  getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
14
14
  removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
15
15
  };
16
+ export * from './style';
17
+ export * from './element';
18
+ export { default as dayjs } from './dayjs';
@@ -1,8 +1,9 @@
1
1
  export { default as md5 } from 'md5';
2
- export { default as dayjs } from 'dayjs';
3
2
  export { default as classnames } from 'classnames';
4
3
  export { default as copy } from 'copy-to-clipboard';
5
4
  import { config, setItem, getItem, removeItem } from 'localforage';
5
+ export { default as anime } from 'animejs';
6
+ import './dayjs.js';
6
7
 
7
8
  const localforage = {
8
9
  config,
@@ -0,0 +1,10 @@
1
+ export declare const hexToRgba: (hexColor: string, a?: number) => {
2
+ nums: {
3
+ red: number;
4
+ green: number;
5
+ blue: number;
6
+ };
7
+ text: string;
8
+ };
9
+ export declare const flashBackground: (el: string | Element, color: string, a?: number) => void;
10
+ export declare const flashBorder: (el: string | Element, color: string, a?: number) => void;
@@ -0,0 +1,45 @@
1
+ import anime from 'animejs';
2
+ import { getElement } from './element.js';
3
+
4
+ // 颜色值转换
5
+ const hexToRgba = (hexColor, a = 1) => {
6
+ let red = parseInt(hexColor.substr(1, 2), 16);
7
+ let green = parseInt(hexColor.substr(3, 2), 16);
8
+ let blue = parseInt(hexColor.substr(5, 2), 16);
9
+ return {
10
+ nums: { red, green, blue },
11
+ text: `rgba(${red}, ${green}, ${blue}, ${a})`,
12
+ };
13
+ };
14
+ // 背景颜色闪动
15
+ const flashBackground = (el, color, a = 1) => {
16
+ const { element, originalStyle } = getElement(el);
17
+ if (!element) {
18
+ return;
19
+ }
20
+ anime({
21
+ targets: element,
22
+ backgroundColor: [hexToRgba(color, a).text, originalStyle.backgroundColor],
23
+ duration: 1500,
24
+ easing: 'easeInOutSine',
25
+ direction: 'alternate',
26
+ loop: false,
27
+ });
28
+ };
29
+ // 边框闪动
30
+ const flashBorder = (el, color, a = 1) => {
31
+ const { element } = getElement(el);
32
+ if (!element) {
33
+ return;
34
+ }
35
+ anime({
36
+ targets: element,
37
+ border: [`1px solid ${hexToRgba(color, a).text}`, 'none'],
38
+ duration: 1500,
39
+ easing: 'easeInOutSine',
40
+ direction: 'alternate',
41
+ loop: false,
42
+ });
43
+ };
44
+
45
+ export { flashBackground, flashBorder, hexToRgba };
@@ -1,2 +1,3 @@
1
- export * from './common';
2
- export * from './fe';
1
+ export * from './common';
2
+ export * from './fe';
3
+ export * from './rd';
package/dist/esm/index.js CHANGED
@@ -1,11 +1,16 @@
1
- export { cloneDeep, debounce, isBoolean, isEmpty, isEqual, isError, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith } from 'lodash';
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
2
  import * as consola from 'consola';
3
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';
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';
7
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';
8
13
  export { default as copy } from 'copy-to-clipboard';
9
14
  export { default as classnames } from 'classnames';
10
- export { default as dayjs } from 'dayjs';
11
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@wzyjs/utils",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "license": "ISC",
@@ -16,6 +16,8 @@
16
16
  ],
17
17
  "dependencies": {
18
18
  "@cloudbase/node-sdk": "^2.9.1",
19
+ "@types/animejs": "^3.1.6",
20
+ "animejs": "^3.2.1",
19
21
  "classnames": "^2.3.2",
20
22
  "consola": "^2.15.3",
21
23
  "copy-to-clipboard": "^3.3.3",
@@ -42,5 +44,5 @@
42
44
  "type": "git",
43
45
  "url": "https://gitee.com/wang-zhenyu/app.git"
44
46
  },
45
- "gitHead": "06ef413d0febd5cf016833ca123e1ac31f52f3cc"
47
+ "gitHead": "7ebb80ef50a4df3d262a1da82d7967e628698b10"
46
48
  }