sculp-js 1.6.1 → 1.7.1
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 +12 -2
- package/lib/cjs/array.js +2 -2
- package/lib/cjs/async.js +2 -2
- package/lib/cjs/base64.js +2 -2
- package/lib/cjs/clipboard.js +2 -2
- package/lib/cjs/cookie.js +2 -2
- package/lib/cjs/date.js +2 -2
- package/lib/cjs/dom.js +2 -2
- package/lib/cjs/download.js +2 -2
- package/lib/cjs/easing.js +2 -2
- package/lib/cjs/file.js +2 -2
- package/lib/cjs/func.js +2 -2
- package/lib/cjs/index.js +26 -3
- package/lib/cjs/math.js +2 -2
- package/lib/cjs/number.js +2 -2
- package/lib/cjs/object.js +67 -20
- package/lib/cjs/path.js +2 -2
- package/lib/cjs/qs.js +2 -2
- package/lib/cjs/random.js +2 -2
- package/lib/cjs/string.js +3 -3
- package/lib/cjs/tooltip.js +2 -2
- package/lib/cjs/tree.js +10 -8
- package/lib/cjs/type.js +8 -8
- package/lib/cjs/unique.js +2 -2
- package/lib/cjs/url.js +2 -2
- package/lib/cjs/validator.js +147 -0
- package/lib/cjs/variable.js +118 -0
- package/lib/cjs/watermark.js +2 -2
- package/lib/cjs/we-decode.js +4 -4
- package/lib/es/array.js +2 -2
- package/lib/es/async.js +2 -2
- package/lib/es/base64.js +2 -2
- package/lib/es/clipboard.js +2 -2
- package/lib/es/cookie.js +2 -2
- package/lib/es/date.js +2 -2
- package/lib/es/dom.js +2 -2
- package/lib/es/download.js +2 -2
- package/lib/es/easing.js +2 -2
- package/lib/es/file.js +2 -2
- package/lib/es/func.js +2 -2
- package/lib/es/index.js +5 -3
- package/lib/es/math.js +2 -2
- package/lib/es/number.js +2 -2
- package/lib/es/object.js +67 -20
- package/lib/es/path.js +2 -2
- package/lib/es/qs.js +2 -2
- package/lib/es/random.js +2 -2
- package/lib/es/string.js +3 -3
- package/lib/es/tooltip.js +2 -2
- package/lib/es/tree.js +10 -8
- package/lib/es/type.js +8 -8
- package/lib/es/unique.js +2 -2
- package/lib/es/url.js +2 -2
- package/lib/es/validator.js +130 -0
- package/lib/es/variable.js +112 -0
- package/lib/es/watermark.js +2 -2
- package/lib/es/we-decode.js +4 -4
- package/lib/index.d.ts +154 -16
- package/lib/umd/index.js +327 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,16 @@
|
|
|
9
9
|
|
|
10
10
|
[API文档](https://chandq.github.io/sculp-js/)
|
|
11
11
|
|
|
12
|
-
> TS + Rollup, native implementation, without relying on any third-party libraries, outputs products of three module modes: ESM, CJS, and UMD
|
|
12
|
+
> TS + Rollup, native implementation, without relying on any third-party libraries, outputs products of three module modes: ESM, CJS, and UMD. sculp-js only used to Web environment, @sculp/core can be used to Web、Node.js、Mini Program.
|
|
13
|
+
|
|
14
|
+
## Quickstart
|
|
15
|
+
|
|
16
|
+
- Via CDN: `<script src="https://unpkg.com/sculp-js"></script>`
|
|
17
|
+
- Via npm:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
npm i sculp-js
|
|
21
|
+
```
|
|
13
22
|
|
|
14
23
|
## Features
|
|
15
24
|
|
|
@@ -41,7 +50,7 @@
|
|
|
41
50
|
- Tree
|
|
42
51
|
|
|
43
52
|
- forEachDeep 高性能的深度优先遍历函数, 支持continue、break,可定制id、children
|
|
44
|
-
-
|
|
53
|
+
- mapDeep 高性能的深度优先遍历的Map函数, 支持continue、break,可定制id、children
|
|
45
54
|
- searchTreeById 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
|
|
46
55
|
- formatTree 高性能的数组转树函数
|
|
47
56
|
- flatTree 树转数组
|
|
@@ -55,6 +64,7 @@
|
|
|
55
64
|
- objectEach
|
|
56
65
|
- objectEachAsync
|
|
57
66
|
- objectGet
|
|
67
|
+
- cloneDeep
|
|
58
68
|
|
|
59
69
|
- Number
|
|
60
70
|
|
package/lib/cjs/array.js
CHANGED
package/lib/cjs/async.js
CHANGED
package/lib/cjs/base64.js
CHANGED
package/lib/cjs/clipboard.js
CHANGED
package/lib/cjs/cookie.js
CHANGED
package/lib/cjs/date.js
CHANGED
package/lib/cjs/dom.js
CHANGED
package/lib/cjs/download.js
CHANGED
package/lib/cjs/easing.js
CHANGED
package/lib/cjs/file.js
CHANGED
package/lib/cjs/func.js
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
3
|
-
* (c) 2023-
|
|
2
|
+
* sculp-js v1.7.0
|
|
3
|
+
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -30,6 +30,8 @@ var tree = require('./tree.js');
|
|
|
30
30
|
var math = require('./math.js');
|
|
31
31
|
var weDecode = require('./we-decode.js');
|
|
32
32
|
var base64 = require('./base64.js');
|
|
33
|
+
var validator = require('./validator.js');
|
|
34
|
+
var variable = require('./variable.js');
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
|
|
@@ -139,9 +141,9 @@ exports.tooltipEvent = tooltip.tooltipEvent;
|
|
|
139
141
|
exports.buildTree = tree.buildTree;
|
|
140
142
|
exports.flatTree = tree.flatTree;
|
|
141
143
|
exports.forEachDeep = tree.forEachDeep;
|
|
142
|
-
exports.forEachMap = tree.forEachMap;
|
|
143
144
|
exports.formatTree = tree.formatTree;
|
|
144
145
|
exports.fuzzySearchTree = tree.fuzzySearchTree;
|
|
146
|
+
exports.mapDeep = tree.mapDeep;
|
|
145
147
|
exports.searchTreeById = tree.searchTreeById;
|
|
146
148
|
exports.add = math.add;
|
|
147
149
|
exports.divide = math.divide;
|
|
@@ -152,3 +154,24 @@ exports.weAtob = weDecode.weAtob;
|
|
|
152
154
|
exports.weBtoa = weDecode.weBtoa;
|
|
153
155
|
exports.decodeFromBase64 = base64.decodeFromBase64;
|
|
154
156
|
exports.encodeToBase64 = base64.encodeToBase64;
|
|
157
|
+
exports.EMAIL_REGEX = validator.EMAIL_REGEX;
|
|
158
|
+
exports.HTTP_URL_REGEX = validator.HTTP_URL_REGEX;
|
|
159
|
+
exports.IPV4_REGEX = validator.IPV4_REGEX;
|
|
160
|
+
exports.IPV6_REGEX = validator.IPV6_REGEX;
|
|
161
|
+
exports.PHONE_REGEX = validator.PHONE_REGEX;
|
|
162
|
+
exports.URL_REGEX = validator.URL_REGEX;
|
|
163
|
+
exports.isDigit = validator.isDigit;
|
|
164
|
+
exports.isEmail = validator.isEmail;
|
|
165
|
+
exports.isFloat = validator.isFloat;
|
|
166
|
+
exports.isIdNo = validator.isIdNo;
|
|
167
|
+
exports.isInteger = validator.isInteger;
|
|
168
|
+
exports.isIpV4 = validator.isIpV4;
|
|
169
|
+
exports.isIpV6 = validator.isIpV6;
|
|
170
|
+
exports.isNumerical = validator.isNumerical;
|
|
171
|
+
exports.isPhone = validator.isPhone;
|
|
172
|
+
exports.isUrl = validator.isUrl;
|
|
173
|
+
exports.escapeRegExp = variable.escapeRegExp;
|
|
174
|
+
exports.executeInScope = variable.executeInScope;
|
|
175
|
+
exports.parseVarFromString = variable.parseVarFromString;
|
|
176
|
+
exports.replaceVarFromString = variable.replaceVarFromString;
|
|
177
|
+
exports.uniqueSymbol = variable.uniqueSymbol;
|
package/lib/cjs/math.js
CHANGED
package/lib/cjs/number.js
CHANGED
package/lib/cjs/object.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
3
|
-
* (c) 2023-
|
|
2
|
+
* sculp-js v1.7.0
|
|
3
|
+
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -198,26 +198,73 @@ function objectGet(obj, path, strict = false) {
|
|
|
198
198
|
}
|
|
199
199
|
/**
|
|
200
200
|
* 深拷贝堪称完全体 即:任何类型的数据都会被深拷贝
|
|
201
|
-
* @param {
|
|
201
|
+
* @param {T} source
|
|
202
202
|
* @param {WeakMap} map
|
|
203
|
-
* @returns {
|
|
203
|
+
* @returns {T}
|
|
204
204
|
*/
|
|
205
|
-
function cloneDeep(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
205
|
+
function cloneDeep(source, map = new WeakMap()) {
|
|
206
|
+
// 处理原始类型(非对象/数组)和 null/undefined
|
|
207
|
+
if (source === null || typeof source !== 'object') {
|
|
208
|
+
return source;
|
|
209
|
+
}
|
|
210
|
+
// 处理循环引用
|
|
211
|
+
if (map.has(source)) {
|
|
212
|
+
return map.get(source);
|
|
213
|
+
}
|
|
214
|
+
// 处理 Date 类型
|
|
215
|
+
if (source instanceof Date) {
|
|
216
|
+
const copy = new Date(source.getTime());
|
|
217
|
+
map.set(source, copy);
|
|
218
|
+
return copy;
|
|
219
|
+
}
|
|
220
|
+
// 处理 RegExp 类型
|
|
221
|
+
if (source instanceof RegExp) {
|
|
222
|
+
const copy = new RegExp(source.source, source.flags);
|
|
223
|
+
map.set(source, copy);
|
|
224
|
+
return copy;
|
|
225
|
+
}
|
|
226
|
+
// 处理数组类型
|
|
227
|
+
if (Array.isArray(source)) {
|
|
228
|
+
const copy = [];
|
|
229
|
+
map.set(source, copy);
|
|
230
|
+
for (const item of source) {
|
|
231
|
+
copy.push(cloneDeep(item, map));
|
|
232
|
+
}
|
|
233
|
+
return copy;
|
|
234
|
+
}
|
|
235
|
+
// 处理 Map 类型
|
|
236
|
+
if (source instanceof Map) {
|
|
237
|
+
const copy = new Map();
|
|
238
|
+
map.set(source, copy);
|
|
239
|
+
source.forEach((value, key) => {
|
|
240
|
+
copy.set(cloneDeep(key, map), cloneDeep(value, map));
|
|
241
|
+
});
|
|
242
|
+
return copy;
|
|
243
|
+
}
|
|
244
|
+
// 处理 Set 类型
|
|
245
|
+
if (source instanceof Set) {
|
|
246
|
+
const copy = new Set();
|
|
247
|
+
map.set(source, copy);
|
|
248
|
+
source.forEach(value => {
|
|
249
|
+
copy.add(cloneDeep(value, map));
|
|
250
|
+
});
|
|
251
|
+
return copy;
|
|
252
|
+
}
|
|
253
|
+
// 处理 ArrayBuffer 类型
|
|
254
|
+
if (source instanceof ArrayBuffer) {
|
|
255
|
+
const copy = new ArrayBuffer(source.byteLength);
|
|
256
|
+
new Uint8Array(copy).set(new Uint8Array(source));
|
|
257
|
+
map.set(source, copy);
|
|
258
|
+
return copy;
|
|
259
|
+
}
|
|
260
|
+
// 处理普通对象(包括原型链继承)
|
|
261
|
+
const copy = Object.create(Object.getPrototypeOf(source));
|
|
262
|
+
map.set(source, copy);
|
|
263
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
264
|
+
const value = source[key];
|
|
265
|
+
copy[key] = cloneDeep(value, map);
|
|
266
|
+
}
|
|
267
|
+
return copy;
|
|
221
268
|
}
|
|
222
269
|
|
|
223
270
|
exports.cloneDeep = cloneDeep;
|
package/lib/cjs/path.js
CHANGED
package/lib/cjs/qs.js
CHANGED
package/lib/cjs/random.js
CHANGED
package/lib/cjs/string.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
3
|
-
* (c) 2023-
|
|
2
|
+
* sculp-js v1.7.0
|
|
3
|
+
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -125,7 +125,7 @@ const stringFill = (length, value = ' ') => new Array(length).fill(value).join('
|
|
|
125
125
|
/**
|
|
126
126
|
* 解析URL查询参数
|
|
127
127
|
* @param {string} searchStr
|
|
128
|
-
* @
|
|
128
|
+
* @returns {Record<string, string | string[]>}
|
|
129
129
|
*/
|
|
130
130
|
function parseQueryParams(searchStr = location.search) {
|
|
131
131
|
const queryObj = {};
|
package/lib/cjs/tooltip.js
CHANGED
package/lib/cjs/tree.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
3
|
-
* (c) 2023-
|
|
2
|
+
* sculp-js v1.7.0
|
|
3
|
+
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
+
var object = require('./object.js');
|
|
10
|
+
|
|
9
11
|
const defaultFieldOptions = { keyField: 'key', childField: 'children', pidField: 'pid' };
|
|
10
12
|
const defaultSearchTreeOptions = {
|
|
11
13
|
childField: 'children',
|
|
@@ -74,7 +76,7 @@ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
|
74
76
|
* @param {boolean} isReverse 是否反向遍历
|
|
75
77
|
* @returns {any[]} 新的一棵树
|
|
76
78
|
*/
|
|
77
|
-
function
|
|
79
|
+
function mapDeep(tree, iterator, children = 'children', isReverse = false) {
|
|
78
80
|
let isBreak = false;
|
|
79
81
|
const newTree = [];
|
|
80
82
|
const walk = (arr, parent, newTree, level = 0) => {
|
|
@@ -91,7 +93,7 @@ function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
|
91
93
|
else if (re === true) {
|
|
92
94
|
continue;
|
|
93
95
|
}
|
|
94
|
-
newTree.push(re);
|
|
96
|
+
newTree.push(object.objectOmit(re, [children]));
|
|
95
97
|
// @ts-ignore
|
|
96
98
|
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
97
99
|
newTree[newTree.length - 1][children] = [];
|
|
@@ -117,7 +119,7 @@ function forEachMap(tree, iterator, children = 'children', isReverse = false) {
|
|
|
117
119
|
else if (re === true) {
|
|
118
120
|
continue;
|
|
119
121
|
}
|
|
120
|
-
newTree.push(re);
|
|
122
|
+
newTree.push(object.objectOmit(re, [children]));
|
|
121
123
|
// @ts-ignore
|
|
122
124
|
if (arr[i] && Array.isArray(arr[i][children])) {
|
|
123
125
|
newTree[newTree.length - 1][children] = [];
|
|
@@ -280,7 +282,7 @@ function formatTree(list, options = defaultFieldOptions) {
|
|
|
280
282
|
* 树形结构转扁平化
|
|
281
283
|
* @param {any} treeList
|
|
282
284
|
* @param {IFieldOptions} options
|
|
283
|
-
* @
|
|
285
|
+
* @returns {*}
|
|
284
286
|
*/
|
|
285
287
|
function flatTree(treeList, options = defaultFieldOptions) {
|
|
286
288
|
const { childField, keyField, pidField } = options;
|
|
@@ -306,7 +308,7 @@ function flatTree(treeList, options = defaultFieldOptions) {
|
|
|
306
308
|
* @param {any[]} nodes
|
|
307
309
|
* @param {string} query
|
|
308
310
|
* @param {ISearchTreeOpts} options
|
|
309
|
-
* @
|
|
311
|
+
* @returns {any[]}
|
|
310
312
|
*/
|
|
311
313
|
function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
|
|
312
314
|
const result = [];
|
|
@@ -352,7 +354,7 @@ function fuzzySearchTree(nodes, query, options = defaultSearchTreeOptions) {
|
|
|
352
354
|
exports.buildTree = buildTree;
|
|
353
355
|
exports.flatTree = flatTree;
|
|
354
356
|
exports.forEachDeep = forEachDeep;
|
|
355
|
-
exports.forEachMap = forEachMap;
|
|
356
357
|
exports.formatTree = formatTree;
|
|
357
358
|
exports.fuzzySearchTree = fuzzySearchTree;
|
|
359
|
+
exports.mapDeep = mapDeep;
|
|
358
360
|
exports.searchTreeById = searchTreeById;
|
package/lib/cjs/type.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.
|
|
3
|
-
* (c) 2023-
|
|
2
|
+
* sculp-js v1.7.0
|
|
3
|
+
* (c) 2023-present chandq
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -68,7 +68,7 @@ const isRegExp = (any) => typeIs(any) === 'RegExp';
|
|
|
68
68
|
/**
|
|
69
69
|
* 判断一个字符串是否为有效的 JSON, 若有效则返回有效的JSON对象,否则false
|
|
70
70
|
* @param {string} str
|
|
71
|
-
* @
|
|
71
|
+
* @returns {Object | boolean}
|
|
72
72
|
*/
|
|
73
73
|
function isJsonString(str) {
|
|
74
74
|
try {
|
|
@@ -93,19 +93,19 @@ function isJsonString(str) {
|
|
|
93
93
|
* @returns {boolean} Returns `true` if `value` is empty, else `false`.
|
|
94
94
|
* @example
|
|
95
95
|
*
|
|
96
|
-
*
|
|
96
|
+
* isEmpty(null);
|
|
97
97
|
* // => true
|
|
98
98
|
*
|
|
99
|
-
*
|
|
99
|
+
* isEmpty(true);
|
|
100
100
|
* // => true
|
|
101
101
|
*
|
|
102
|
-
*
|
|
102
|
+
* isEmpty(1);
|
|
103
103
|
* // => true
|
|
104
104
|
*
|
|
105
|
-
*
|
|
105
|
+
* isEmpty([1, 2, 3]);
|
|
106
106
|
* // => false
|
|
107
107
|
*
|
|
108
|
-
*
|
|
108
|
+
* isEmpty({ 'a': 1 });
|
|
109
109
|
* // => false
|
|
110
110
|
*/
|
|
111
111
|
function isEmpty(value) {
|
package/lib/cjs/unique.js
CHANGED
package/lib/cjs/url.js
CHANGED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* sculp-js v1.7.0
|
|
3
|
+
* (c) 2023-present chandq
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
// 邮箱
|
|
10
|
+
const EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
11
|
+
/**
|
|
12
|
+
* 判断字符串是否为邮箱格式,不对邮箱真实性做验证,如域名是否正确等
|
|
13
|
+
* @param {string} value
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
const isEmail = (value) => EMAIL_REGEX.test(value);
|
|
17
|
+
// 手机号码 (中国大陆)
|
|
18
|
+
// reference: https://www.runoob.com/regexp/regexp-syntax.html (?: 是非捕获元之一)
|
|
19
|
+
const PHONE_REGEX = /^(?:(?:\+|00)86)?1\d{10}$/;
|
|
20
|
+
/**
|
|
21
|
+
* 判断字符串是否为宽松手机格式,即首位为 1 的 11 位数字都属于手机号
|
|
22
|
+
* @param {string} value
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
*/
|
|
25
|
+
const isPhone = (value) => PHONE_REGEX.test(value);
|
|
26
|
+
// 身份证号码
|
|
27
|
+
// http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/
|
|
28
|
+
// ["北京市", "天津市", "河北省", "山西省", "内蒙古自治区",
|
|
29
|
+
// "辽宁省", "吉林省", "黑龙江省",
|
|
30
|
+
// "上海市", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省",
|
|
31
|
+
// "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省",
|
|
32
|
+
// "重庆市", "四川省", "贵州省", "云南省", "西藏自治区",
|
|
33
|
+
// "陕西省", "甘肃省", "青海省","宁夏回族自治区", "新疆维吾尔自治区",
|
|
34
|
+
// "台湾省",
|
|
35
|
+
// "香港特别行政区", "澳门特别行政区"]
|
|
36
|
+
// ["11", "12", "13", "14", "15",
|
|
37
|
+
// "21", "22", "23",
|
|
38
|
+
// "31", "32", "33", "34", "35", "36", "37",
|
|
39
|
+
// "41", "42", "43", "44", "45", "46",
|
|
40
|
+
// "50", "51", "52", "53", "54",
|
|
41
|
+
// "61", "62", "63", "64", "65",
|
|
42
|
+
// "71",
|
|
43
|
+
// "81", "82"]
|
|
44
|
+
// 91 国外
|
|
45
|
+
const IDNO_RE = /^(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|7[1]|8[1-2]|9[1])\d{4}(18|19|20)\d{2}[01]\d[0123]\d{4}[\dxX]$/;
|
|
46
|
+
/**
|
|
47
|
+
* 判断字符串是否为身份证号码格式
|
|
48
|
+
* @param {string} value
|
|
49
|
+
* @returns {boolean}
|
|
50
|
+
*/
|
|
51
|
+
const isIdNo = (value) => {
|
|
52
|
+
const isSameFormat = IDNO_RE.test(value);
|
|
53
|
+
if (!isSameFormat)
|
|
54
|
+
return false;
|
|
55
|
+
const year = Number(value.slice(6, 10));
|
|
56
|
+
const month = Number(value.slice(10, 12));
|
|
57
|
+
const date = Number(value.slice(12, 14));
|
|
58
|
+
const d = new Date(year, month - 1, date);
|
|
59
|
+
const isSameDate = d.getFullYear() === year && d.getMonth() + 1 === month && d.getDate() === date;
|
|
60
|
+
if (!isSameDate)
|
|
61
|
+
return false;
|
|
62
|
+
// 将身份证号码前面的17位数分别乘以不同的系数;
|
|
63
|
+
// 从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2
|
|
64
|
+
// 将这17位数字和系数相乘的结果相加;
|
|
65
|
+
// 用加出来和除以11,看余数是多少;
|
|
66
|
+
// 余数只可能有0-1-2-3-4-5-6-7-8-9-10这11个数字;
|
|
67
|
+
// 其分别对应的最后一位身份证的号码为1-0-X-9-8-7-6-5-4-3-2
|
|
68
|
+
// 通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。
|
|
69
|
+
const coefficientList = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
|
70
|
+
const residueList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
|
|
71
|
+
let sum = 0;
|
|
72
|
+
for (let start = 0; start < 17; start++) {
|
|
73
|
+
sum += Number(value.slice(start, start + 1)) * coefficientList[start];
|
|
74
|
+
}
|
|
75
|
+
return residueList[sum % 11] === value.slice(-1);
|
|
76
|
+
};
|
|
77
|
+
const URL_REGEX = /^(https?|ftp):\/\/([^\s/$.?#].[^\s]*)$/i;
|
|
78
|
+
const HTTP_URL_REGEX = /^https?:\/\/([^\s/$.?#].[^\s]*)$/i;
|
|
79
|
+
/**
|
|
80
|
+
* 判断字符串是否为 url 格式,支持 http、https、ftp 协议,支持域名或者 ipV4
|
|
81
|
+
* @param {string} value
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
const isUrl = (url, includeFtp = false) => {
|
|
85
|
+
const regex = includeFtp ? URL_REGEX : HTTP_URL_REGEX;
|
|
86
|
+
return regex.test(url);
|
|
87
|
+
};
|
|
88
|
+
// ipv4
|
|
89
|
+
const IPV4_REGEX = /^(?:(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/;
|
|
90
|
+
// ipv6
|
|
91
|
+
const IPV6_REGEX = /^(([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}|([\da-fA-F]{1,4}:){1,7}:|([\da-fA-F]{1,4}:){1,6}:[\da-fA-F]{1,4}|([\da-fA-F]{1,4}:){1,5}(:[\da-fA-F]{1,4}){1,2}|([\da-fA-F]{1,4}:){1,4}(:[\da-fA-F]{1,4}){1,3}|([\da-fA-F]{1,4}:){1,3}(:[\da-fA-F]{1,4}){1,4}|([\da-fA-F]{1,4}:){1,2}(:[\da-fA-F]{1,4}){1,5}|[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,6})|:((:[\da-fA-F]{1,4}){1,7}|:)|fe80:(:[\da-fA-F]{0,4}){0,4}%[\da-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?\d)?\d)\.){3}(25[0-5]|(2[0-4]|1?\d)?\d)|([\da-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?\d)?\d)\.){3}(25[0-5]|(2[0-4]|1?\d)?\d))$/i;
|
|
92
|
+
/**
|
|
93
|
+
* 判断字符串是否为 IPV4 格式,不对 ip 真实性做验证
|
|
94
|
+
* @param {string} value
|
|
95
|
+
* @returns {boolean}
|
|
96
|
+
*/
|
|
97
|
+
const isIpV4 = (value) => IPV4_REGEX.test(value);
|
|
98
|
+
/**
|
|
99
|
+
* 判断字符串是否为 IPV6 格式,不对 ip 真实性做验证
|
|
100
|
+
* @param {string} value
|
|
101
|
+
* @returns {boolean}
|
|
102
|
+
*/
|
|
103
|
+
const isIpV6 = (value) => IPV6_REGEX.test(value);
|
|
104
|
+
const INTEGER_RE = /^(-?[1-9]\d*|0)$/;
|
|
105
|
+
/**
|
|
106
|
+
* 判断字符串是否为整数(自然数),即 ...,-3,-2,-1,0,1,2,3,...
|
|
107
|
+
* @param {string} value
|
|
108
|
+
* @returns {boolean}
|
|
109
|
+
*/
|
|
110
|
+
const isInteger = (value) => INTEGER_RE.test(value);
|
|
111
|
+
const FLOAT_RE = /^-?([1-9]\d*|0)\.\d*[1-9]$/;
|
|
112
|
+
/**
|
|
113
|
+
* 判断字符串是否为浮点数,即必须有小数点的有理数
|
|
114
|
+
* @param {string} value
|
|
115
|
+
* @returns {boolean}
|
|
116
|
+
*/
|
|
117
|
+
const isFloat = (value) => FLOAT_RE.test(value);
|
|
118
|
+
/**
|
|
119
|
+
* 判断字符串是否为正确数值,包括整数和浮点数
|
|
120
|
+
* @param {string} value
|
|
121
|
+
* @returns {boolean}
|
|
122
|
+
*/
|
|
123
|
+
const isNumerical = (value) => isInteger(value) || isFloat(value);
|
|
124
|
+
const DIGIT_RE = /^\d+$/;
|
|
125
|
+
/**
|
|
126
|
+
* 判断字符串是否为数字,例如六位数字短信验证码(093031)
|
|
127
|
+
* @param {string} value
|
|
128
|
+
* @returns {boolean}
|
|
129
|
+
*/
|
|
130
|
+
const isDigit = (value) => DIGIT_RE.test(value);
|
|
131
|
+
|
|
132
|
+
exports.EMAIL_REGEX = EMAIL_REGEX;
|
|
133
|
+
exports.HTTP_URL_REGEX = HTTP_URL_REGEX;
|
|
134
|
+
exports.IPV4_REGEX = IPV4_REGEX;
|
|
135
|
+
exports.IPV6_REGEX = IPV6_REGEX;
|
|
136
|
+
exports.PHONE_REGEX = PHONE_REGEX;
|
|
137
|
+
exports.URL_REGEX = URL_REGEX;
|
|
138
|
+
exports.isDigit = isDigit;
|
|
139
|
+
exports.isEmail = isEmail;
|
|
140
|
+
exports.isFloat = isFloat;
|
|
141
|
+
exports.isIdNo = isIdNo;
|
|
142
|
+
exports.isInteger = isInteger;
|
|
143
|
+
exports.isIpV4 = isIpV4;
|
|
144
|
+
exports.isIpV6 = isIpV6;
|
|
145
|
+
exports.isNumerical = isNumerical;
|
|
146
|
+
exports.isPhone = isPhone;
|
|
147
|
+
exports.isUrl = isUrl;
|