tools-for-js 1.3.2 → 1.4.0
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/README.md +6 -3
- package/lib/index.js +26 -7
- package/lib/utils/common.js +1 -26
- package/lib/utils/random.js +100 -0
- package/lib/utils/string.js +1 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,7 +32,6 @@ import { API } from 'tools-for-js'
|
|
|
32
32
|
| 深拷贝变量 | deepCopy |
|
|
33
33
|
| 校验空值参数 | isEmpty |
|
|
34
34
|
| 校验两个参数是否相等 | isEqual |
|
|
35
|
-
| 生成 UUID | createUUID |
|
|
36
35
|
| 数组去重 | deWeightArray |
|
|
37
36
|
| 计算树的最后节点某字段的总和 | treeLastChildSum |
|
|
38
37
|
| 校验数组对象中是否存在指定的字段为空 | checkKeyEmpty |
|
|
@@ -55,8 +54,12 @@ import { API } from 'tools-for-js'
|
|
|
55
54
|
| 将路径中的反斜杠字符 \ 替换为斜杠 / | replacePath |
|
|
56
55
|
| 切割/获取文件名 | splitToFileName |
|
|
57
56
|
| 数组反选 | invertSelection |
|
|
58
|
-
| 生成随机颜色 | getRandomColor |
|
|
59
57
|
| 创建时间对象 | createDate |
|
|
60
58
|
| 获取结束时间 | getExpiresEndTime |
|
|
61
59
|
| 判断是否过期 | isExpires |
|
|
62
|
-
|
|
|
60
|
+
| 随机数字 | randomNum |
|
|
61
|
+
| 随机字符串 | randomCode |
|
|
62
|
+
| 随机颜色 | randomColor |
|
|
63
|
+
| 创建 UUID | createUUID |
|
|
64
|
+
| 随机 MAC 地址 | randomMac |
|
|
65
|
+
| 待更新 | function |
|
package/lib/index.js
CHANGED
|
@@ -66,7 +66,7 @@ Object.defineProperty(exports, "createDate", {
|
|
|
66
66
|
Object.defineProperty(exports, "createUUID", {
|
|
67
67
|
enumerable: true,
|
|
68
68
|
get: function get() {
|
|
69
|
-
return
|
|
69
|
+
return _random.createUUID;
|
|
70
70
|
}
|
|
71
71
|
});
|
|
72
72
|
Object.defineProperty(exports, "dateFormat", {
|
|
@@ -117,12 +117,6 @@ Object.defineProperty(exports, "getPagination", {
|
|
|
117
117
|
return _array.getPagination;
|
|
118
118
|
}
|
|
119
119
|
});
|
|
120
|
-
Object.defineProperty(exports, "getRandomColor", {
|
|
121
|
-
enumerable: true,
|
|
122
|
-
get: function get() {
|
|
123
|
-
return _string.getRandomColor;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
120
|
Object.defineProperty(exports, "getStrLength", {
|
|
127
121
|
enumerable: true,
|
|
128
122
|
get: function get() {
|
|
@@ -189,6 +183,30 @@ Object.defineProperty(exports, "objectToQs", {
|
|
|
189
183
|
return _object.objectToQs;
|
|
190
184
|
}
|
|
191
185
|
});
|
|
186
|
+
Object.defineProperty(exports, "randomCode", {
|
|
187
|
+
enumerable: true,
|
|
188
|
+
get: function get() {
|
|
189
|
+
return _random.randomCode;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
Object.defineProperty(exports, "randomColor", {
|
|
193
|
+
enumerable: true,
|
|
194
|
+
get: function get() {
|
|
195
|
+
return _random.randomColor;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
Object.defineProperty(exports, "randomMac", {
|
|
199
|
+
enumerable: true,
|
|
200
|
+
get: function get() {
|
|
201
|
+
return _random.randomMac;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
Object.defineProperty(exports, "randomNum", {
|
|
205
|
+
enumerable: true,
|
|
206
|
+
get: function get() {
|
|
207
|
+
return _random.randomNum;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
192
210
|
Object.defineProperty(exports, "replacePath", {
|
|
193
211
|
enumerable: true,
|
|
194
212
|
get: function get() {
|
|
@@ -261,4 +279,5 @@ var _array = require("./utils/array");
|
|
|
261
279
|
var _calculate = require("./utils/calculate");
|
|
262
280
|
var _common = require("./utils/common");
|
|
263
281
|
var _string = require("./utils/string");
|
|
282
|
+
var _random = require("./utils/random");
|
|
264
283
|
var _object = require("./utils/object");
|
package/lib/utils/common.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.isEqual = exports.isEmpty = exports.deepCopy = exports.
|
|
6
|
+
exports.isEqual = exports.isEmpty = exports.deepCopy = exports.IsType = void 0;
|
|
7
7
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
8
|
/*******
|
|
9
9
|
* @description: 变量类型判断
|
|
@@ -112,29 +112,4 @@ var isEqual = exports.isEqual = function isEqual(a, b) {
|
|
|
112
112
|
return false; // 其它类型,均返回false
|
|
113
113
|
}
|
|
114
114
|
return true;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
/*******
|
|
118
|
-
* @description: 生成UUID
|
|
119
|
-
* UUID(Universally Unique IDentifier) 全局唯一标识符。
|
|
120
|
-
* UUID是一种由算法生成的二进制长度为128位的数字标识符。
|
|
121
|
-
* UUID的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,
|
|
122
|
-
* 其中的 x 是 0-9 或 a-f 范围内的一个32位十六进制数。
|
|
123
|
-
* 在理想情况下,任何计算机和计算机集群都不会生成两个相同的UUID。
|
|
124
|
-
* @author: 琴时
|
|
125
|
-
* @param {String} symbol [分隔符] [默认为空]
|
|
126
|
-
* @return {String}
|
|
127
|
-
* @example: createUUID('-') // 生成UUID并使用[-]作为分隔符
|
|
128
|
-
*/
|
|
129
|
-
var createUUID = exports.createUUID = function createUUID() {
|
|
130
|
-
var symbol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
131
|
-
var s = [];
|
|
132
|
-
var hexDigits = '0123456789abcdef';
|
|
133
|
-
for (var i = 0; i < 36; i++) {
|
|
134
|
-
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
|
|
135
|
-
}
|
|
136
|
-
s[14] = '4'; // bits 12-15 of 时间和版本字段设置为0010
|
|
137
|
-
s[19] = hexDigits.substr(s[19] & 0x3 | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
|
|
138
|
-
s[8] = s[13] = s[18] = s[23] = '-';
|
|
139
|
-
return s.join('').replace(/-/g, symbol);
|
|
140
115
|
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.randomCode = exports.createUUID = void 0;
|
|
7
|
+
exports.randomColor = randomColor;
|
|
8
|
+
exports.randomNum = exports.randomMac = void 0;
|
|
9
|
+
/*******
|
|
10
|
+
* @description: 随机数字
|
|
11
|
+
* @author: 琴时
|
|
12
|
+
* @param {Number} length [长度] [默认为6]
|
|
13
|
+
* @return {String}
|
|
14
|
+
*/
|
|
15
|
+
var randomNum = exports.randomNum = function randomNum() {
|
|
16
|
+
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 6;
|
|
17
|
+
var result = '';
|
|
18
|
+
var decimalDigits = '0123456789';
|
|
19
|
+
for (var i = 0; i < length; i++) {
|
|
20
|
+
var index = Math.floor(Math.random() * 10); //随机生成一位0~9的数
|
|
21
|
+
result += decimalDigits.charAt(index);
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/*******
|
|
27
|
+
* @description: 随机字符串
|
|
28
|
+
* @author: 琴时
|
|
29
|
+
* @param {Number} length [长度] [默认为6]
|
|
30
|
+
* @return {String}
|
|
31
|
+
*/
|
|
32
|
+
var randomCode = exports.randomCode = function randomCode() {
|
|
33
|
+
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 6;
|
|
34
|
+
var result = '';
|
|
35
|
+
var num = '0123456789';
|
|
36
|
+
var lower = 'abcdefghijklmnopqrstuvwxyz';
|
|
37
|
+
var capital = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
38
|
+
var character = num + lower + capital;
|
|
39
|
+
for (var i = 0; i < length; i++) {
|
|
40
|
+
var index = Math.floor(Math.random() * character.length); //随机生成一位0~61的数
|
|
41
|
+
result += character.charAt(index);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/*******
|
|
47
|
+
* @description: 随机颜色
|
|
48
|
+
* @author: 琴时
|
|
49
|
+
* @param {Array} exclude [跳过颜色(可选参数),默认为空数组]
|
|
50
|
+
* @return {String}
|
|
51
|
+
*/
|
|
52
|
+
function randomColor() {
|
|
53
|
+
var exclude = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
54
|
+
var hexDigits = '0123456789ABCDEF';
|
|
55
|
+
var color = '#';
|
|
56
|
+
for (var i = 0; i < 6; i++) {
|
|
57
|
+
var index = Math.floor(Math.random() * 16); //随机生成一位0~15的数
|
|
58
|
+
color += hexDigits.charAt(index);
|
|
59
|
+
}
|
|
60
|
+
//如果生成的颜色和默认颜色重复了,则重新生成
|
|
61
|
+
if (exclude.includes(color)) randomColor(exclude);
|
|
62
|
+
return color;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*******
|
|
66
|
+
* @description: 创建UUID
|
|
67
|
+
* UUID(Universally Unique IDentifier) 全局唯一标识符。
|
|
68
|
+
* UUID是一种由算法生成的二进制长度为128位的数字标识符。
|
|
69
|
+
* UUID的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,
|
|
70
|
+
* 其中的 x 是 0-9 或 a-f 范围内的一个32位十六进制数。
|
|
71
|
+
* 在理想情况下,任何计算机和计算机集群都不会生成两个相同的UUID。
|
|
72
|
+
* @author: 琴时
|
|
73
|
+
* @param {String} symbol [分隔符] [默认为空]
|
|
74
|
+
* @return {String}
|
|
75
|
+
* @example: createUUID('-') // 生成UUID并使用[-]作为分隔符
|
|
76
|
+
*/
|
|
77
|
+
var createUUID = exports.createUUID = function createUUID() {
|
|
78
|
+
var symbol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '-';
|
|
79
|
+
var s = [];
|
|
80
|
+
var hexDigits = '0123456789abcdef';
|
|
81
|
+
for (var i = 0; i < 36; i++) {
|
|
82
|
+
s[i] = hexDigits.charAt(Math.floor(Math.random() * 16));
|
|
83
|
+
}
|
|
84
|
+
s[14] = '4'; // bits 12-15 of 时间和版本字段设置为0010
|
|
85
|
+
s[19] = hexDigits.charAt(s[19] & 3 | 8); // bits 6-7 of the clock_seq_hi_and_reserved to 01
|
|
86
|
+
s[8] = s[13] = s[18] = s[23] = '-';
|
|
87
|
+
return s.join('').replace(/-/g, symbol);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/*******
|
|
91
|
+
* @description: 随机MAC地址
|
|
92
|
+
* @author: 琴时
|
|
93
|
+
* @return {String}
|
|
94
|
+
*/
|
|
95
|
+
var randomMac = exports.randomMac = function randomMac() {
|
|
96
|
+
return 'XX:XX:XX:XX:XX:XX'.replace(/X/g, function () {
|
|
97
|
+
var hexDigits = '0123456789ABCDEF';
|
|
98
|
+
return hexDigits.charAt(Math.floor(Math.random() * 16));
|
|
99
|
+
});
|
|
100
|
+
};
|
package/lib/utils/string.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.encode = exports.decode = exports.checkPwdStrength = void 0;
|
|
7
|
-
exports.getRandomColor = getRandomColor;
|
|
8
|
-
exports.getStrLength = void 0;
|
|
6
|
+
exports.getStrLength = exports.encode = exports.decode = exports.checkPwdStrength = void 0;
|
|
9
7
|
exports.replacePath = replacePath;
|
|
10
8
|
exports.splitString = splitString;
|
|
11
9
|
exports.splitToFileName = splitToFileName;
|
|
@@ -208,22 +206,4 @@ function splitToFileName(path) {
|
|
|
208
206
|
var lastIndex = path.lastIndexOf('/');
|
|
209
207
|
if (lastIndex === -1) return path;
|
|
210
208
|
return path.substring(lastIndex + 1);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/*******
|
|
214
|
-
* @description: 生成随机颜色
|
|
215
|
-
* @author: 琴时
|
|
216
|
-
* @param {Array} defaultList [跳过颜色(可选参数),默认为空数组]
|
|
217
|
-
* @return {String}
|
|
218
|
-
*/
|
|
219
|
-
function getRandomColor() {
|
|
220
|
-
var defaultList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
221
|
-
var letters = '0123456789ABCDEF';
|
|
222
|
-
var color = '#';
|
|
223
|
-
for (var i = 0; i < 6; i++) {
|
|
224
|
-
color += letters[Math.floor(Math.random() * 16)];
|
|
225
|
-
}
|
|
226
|
-
//如果生成的颜色和默认颜色重复了,则重新生成
|
|
227
|
-
if (defaultList.includes(color)) getRandomColor(defaultList);
|
|
228
|
-
return color;
|
|
229
209
|
}
|