@polyv/utils 1.6.0 → 2.0.0-beta.2
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 +34 -19
- package/dist/cjs/boolean.d.ts +45 -0
- package/dist/cjs/boolean.js +1 -0
- package/dist/cjs/countdown.d.ts +97 -0
- package/dist/cjs/countdown.js +1 -0
- package/dist/cjs/date.d.ts +44 -0
- package/dist/cjs/date.js +1 -0
- package/dist/cjs/json.d.ts +43 -0
- package/dist/cjs/json.js +1 -0
- package/dist/cjs/net.d.ts +32 -0
- package/dist/cjs/net.js +1 -0
- package/dist/cjs/string.d.ts +94 -0
- package/dist/cjs/string.js +1 -0
- package/dist/cjs/validate.d.ts +26 -0
- package/dist/cjs/validate.js +1 -0
- package/dist/es/boolean.d.ts +45 -0
- package/dist/es/boolean.js +1 -0
- package/dist/es/countdown.d.ts +97 -0
- package/dist/es/countdown.js +1 -0
- package/dist/es/date.d.ts +44 -0
- package/dist/es/date.js +1 -0
- package/dist/es/json.d.ts +43 -0
- package/dist/es/json.js +1 -0
- package/dist/es/net.d.ts +32 -0
- package/dist/es/net.js +1 -0
- package/dist/es/string.d.ts +94 -0
- package/dist/es/string.js +1 -0
- package/dist/es/validate.d.ts +26 -0
- package/dist/es/validate.js +1 -0
- package/package.json +29 -24
- package/dist/boolean.js +0 -1
- package/dist/browser.js +0 -1
- package/dist/cookie.js +0 -1
- package/dist/countdown.js +0 -1
- package/dist/date.js +0 -1
- package/dist/lang.js +0 -1
- package/dist/net.js +0 -1
- package/dist/polling.js +0 -1
- package/dist/querystring.js +0 -1
- package/dist/storage.js +0 -1
- package/dist/string.js +0 -1
- package/dist/validate.js +0 -1
- package/src/boolean.js +0 -67
- package/src/browser.js +0 -113
- package/src/cookie.js +0 -115
- package/src/countdown.js +0 -91
- package/src/date.js +0 -92
- package/src/internal/core.js +0 -59
- package/src/internal/timeunit.js +0 -63
- package/src/lang.js +0 -121
- package/src/net.js +0 -54
- package/src/polling.js +0 -142
- package/src/querystring.js +0 -121
- package/src/storage.js +0 -106
- package/src/string.js +0 -190
- package/src/validate.js +0 -42
package/src/querystring.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本模块提供 URL 查询字符串的操作方法。
|
|
3
|
-
* @module querystring
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { hasOwnProp } from './lang';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 把查询字符串反序列化为键值对集合。
|
|
10
|
-
* @author luoliquan
|
|
11
|
-
* @param {string} str 查询字符串。
|
|
12
|
-
* @return {Object} 键值对集合。
|
|
13
|
-
* @example
|
|
14
|
-
* parse('a=1&%E9%94%AE=%E5%80%BC'); // { a: 1, '键': '值' }
|
|
15
|
-
* parse('a=1&a=2&b=3'); // { a: [1, 2], b: 3 }
|
|
16
|
-
*/
|
|
17
|
-
export function parse(str) {
|
|
18
|
-
if (typeof str !== 'string') {
|
|
19
|
-
throw new Error('The str argument must be a string type');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const result = {};
|
|
23
|
-
|
|
24
|
-
str.split('&').forEach(function(pair) {
|
|
25
|
-
if (!pair) { return; }
|
|
26
|
-
pair = pair.split('=');
|
|
27
|
-
const key = decodeURIComponent(pair[0]);
|
|
28
|
-
const value = decodeURIComponent(pair[1] || '');
|
|
29
|
-
|
|
30
|
-
if (hasOwnProp(result, key)) {
|
|
31
|
-
// 出现重复 key 值时解析为数组
|
|
32
|
-
if (!Array.isArray(result[key])) {
|
|
33
|
-
result[key] = [result[key]];
|
|
34
|
-
}
|
|
35
|
-
result[key].push(value);
|
|
36
|
-
} else {
|
|
37
|
-
result[key] = value;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 把键值对集合序列化为查询字符串。
|
|
46
|
-
* @author luoliquan
|
|
47
|
-
* @param {Object} data 键值对集合。
|
|
48
|
-
* @param {Object} [options] 参数。
|
|
49
|
-
* @param {Boolean} [options.ignoreEmpty=false] 是否忽略空值(包括null、undefined、空字符串)。
|
|
50
|
-
* @return {string} 序列化结果。
|
|
51
|
-
* @example
|
|
52
|
-
* stringify({ a: 1, '键': '值' }); // 'a=1&%E9%94%AE=%E5%80%BC'
|
|
53
|
-
* stringify({ a: [1, 2], b: 3 }); // 'a=1&a=2&b=3'
|
|
54
|
-
*/
|
|
55
|
-
export function stringify(data, options) {
|
|
56
|
-
options = options || {};
|
|
57
|
-
|
|
58
|
-
const result = [];
|
|
59
|
-
function addToResult(key, value) {
|
|
60
|
-
if (value == null) { value = ''; }
|
|
61
|
-
// 忽略空值的情况
|
|
62
|
-
if (value === '' && options.ignoreEmpty) { return; }
|
|
63
|
-
|
|
64
|
-
result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let key, value;
|
|
68
|
-
|
|
69
|
-
// 避免在循环中生成匿名函数,提到循环外
|
|
70
|
-
function loopItem(item) { addToResult(key, item); }
|
|
71
|
-
|
|
72
|
-
for (key in data) {
|
|
73
|
-
if (hasOwnProp(data, key)) {
|
|
74
|
-
value = data[key];
|
|
75
|
-
if (Array.isArray(value)) {
|
|
76
|
-
value.forEach(loopItem);
|
|
77
|
-
} else {
|
|
78
|
-
addToResult(key, value);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return result.join('&');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 把键值对集合序列化为查询字符串后拼接到指定URL。
|
|
88
|
-
* @author luoliquan
|
|
89
|
-
* @param {string} url 指定URL。
|
|
90
|
-
* @param {Object|string} data 键值对集合。
|
|
91
|
-
* @param {Object} [options] 参数。
|
|
92
|
-
* @param {Boolean} [options.ignoreEmpty] 序列化时是否忽略空值(包括null、undefined、空字符串)。
|
|
93
|
-
* @return {String} 处理后的URL。
|
|
94
|
-
* @example
|
|
95
|
-
* append('http://abc.com?a=1', { b: 2, c: 3 }); // 'http://abc.com?a=1&b=2&c=3'
|
|
96
|
-
* append('http://abc.com', { a: 1, b: 2 }); // 'http://abc.com?a=1&b=2'
|
|
97
|
-
*/
|
|
98
|
-
export function append(url, data, options) {
|
|
99
|
-
if (url == null) { return url; }
|
|
100
|
-
url = String(url);
|
|
101
|
-
|
|
102
|
-
// 如果url中包含hash,要先剪出来
|
|
103
|
-
const temp = url.indexOf('#');
|
|
104
|
-
let hash = '';
|
|
105
|
-
if (temp !== -1) {
|
|
106
|
-
hash = url.substring(temp, url.length);
|
|
107
|
-
url = url.substring(0, temp);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// 移除位于末尾的?和&,方便拼接
|
|
111
|
-
url = url.replace(/[?&]$/, '');
|
|
112
|
-
|
|
113
|
-
if (typeof data !== 'string') {
|
|
114
|
-
data = stringify(data, options);
|
|
115
|
-
} else {
|
|
116
|
-
// 移除位于开头的?和&,方便拼接
|
|
117
|
-
data = data.replace(/^[?&]/, '');
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return url + (url.indexOf('?') !== -1 ? '&' : '?') + data + hash;
|
|
121
|
-
}
|
package/src/storage.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本模块提供本地存储相关方法的封装。
|
|
3
|
-
* @module storage
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { tryParseJSON } from './lang';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 本地存储调用封装。
|
|
10
|
-
* @author luoliquan
|
|
11
|
-
* @param {Object} storageType 存储类型。
|
|
12
|
-
*/
|
|
13
|
-
export class StorageWrap {
|
|
14
|
-
constructor(storageType) {
|
|
15
|
-
this._storageType = storageType || {
|
|
16
|
-
getItem() { return null; },
|
|
17
|
-
setItem() {},
|
|
18
|
-
removeItem() {}
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 获取指定存储项的值。
|
|
24
|
-
* @param {string} key 存储项键名。
|
|
25
|
-
* @return {string} 存储项的值。
|
|
26
|
-
*/
|
|
27
|
-
get(key) {
|
|
28
|
-
return this._storageType.getItem(key);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 获取指定存储项的值并解析为 JSON。
|
|
33
|
-
* @param {string} key 存储项键名。
|
|
34
|
-
* @return {Any} 解析结果。
|
|
35
|
-
*/
|
|
36
|
-
getAsJSON(key) {
|
|
37
|
-
return tryParseJSON(this.get(key));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 写入指定存储项的值。
|
|
42
|
-
* @param {string} key 存储项键名。
|
|
43
|
-
* @param {Any} value 存储项的值。
|
|
44
|
-
* @return {boolean} 写入是否成功。
|
|
45
|
-
*/
|
|
46
|
-
set(key, value) {
|
|
47
|
-
// 在浏览器的隐私模式下 setItem 会抛出异常
|
|
48
|
-
// 但 getItem 和 removeItem 都不会
|
|
49
|
-
try {
|
|
50
|
-
this._storageType.setItem(key, value);
|
|
51
|
-
return true;
|
|
52
|
-
} catch (e) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 把指定值序列化为 JSON 字符串后写入到指定存储项。
|
|
59
|
-
* @param {string} key 存储项键名。
|
|
60
|
-
* @param {Any} value 存储项的值。
|
|
61
|
-
* @return {boolean} 写入是否成功。
|
|
62
|
-
*/
|
|
63
|
-
setAsJSON(key, value) {
|
|
64
|
-
return this.set(key, JSON.stringify(value));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 移除存储项。
|
|
69
|
-
* @param {string} key 存储项键名。
|
|
70
|
-
*/
|
|
71
|
-
remove(key) {
|
|
72
|
-
this._storageType.removeItem(key);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
let sessionStorage, localStorage;
|
|
78
|
-
if (typeof window !== 'undefined') {
|
|
79
|
-
// Chrome 隐私模式,跨域 iframe 内访问本地存储的相关对象会抛出异常
|
|
80
|
-
try {
|
|
81
|
-
sessionStorage = window.sessionStorage;
|
|
82
|
-
localStorage = window.localStorage;
|
|
83
|
-
} catch (e) {
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* sessionStorage 存取方法(通过 StorageWrap 包装了 sessionStorage)。
|
|
89
|
-
* @example
|
|
90
|
-
* const user = { name: 'Tom', pet: 'cat' };
|
|
91
|
-
* session.setAsJSON('user', user);
|
|
92
|
-
* typeof session.get('user'); // 'string'
|
|
93
|
-
* typeof session.getAsJSON('user'); // 'object'
|
|
94
|
-
*/
|
|
95
|
-
export const session = new StorageWrap(sessionStorage);
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* localStorage 存取方法(通过 StorageWrap 包装了 localStorage)。
|
|
99
|
-
* @example
|
|
100
|
-
* const user = { name: 'Tom', pet: 'cat' };
|
|
101
|
-
* local.setAsJSON('user', user);
|
|
102
|
-
* local.getAsJSON('user');
|
|
103
|
-
* typeof local.get('user'); // 'string'
|
|
104
|
-
* typeof local.getAsJSON('user'); // 'object'
|
|
105
|
-
*/
|
|
106
|
-
export const local = new StorageWrap(localStorage);
|
package/src/string.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本模块提供字符串处理相关方法。
|
|
3
|
-
* @module string
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { extend } from './lang';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 计算字符串长度(英文字符按 1 算,非英文字符可指定单位长度)。
|
|
10
|
-
* @param {string} str 字符串。
|
|
11
|
-
* @param {number} [nonEnLen=2] 非英文字符的单位长度。
|
|
12
|
-
* @return {number} 字符串长度。
|
|
13
|
-
* @example
|
|
14
|
-
* strLen('abcde;'); // 6
|
|
15
|
-
* strLen('abc测试;'); // 9
|
|
16
|
-
* strLen('abc测试;', 1); // 6
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* 计算字符串长度(可分别指定英文字符和非英文字符的单位长度)。
|
|
20
|
-
* @param {string} str 字符串。
|
|
21
|
-
* @param {number|Object} [options] 为数字时表示非英文字符单位长度(此时英文字符单位长度为 1);为 Object 时表示选项。
|
|
22
|
-
* @param {number} [options.enLen=1] 英文字符单位长度。
|
|
23
|
-
* @param {number} [options.nonEnLen=2] 非英文字符单位长度。
|
|
24
|
-
* @return {number} 字符串长度。
|
|
25
|
-
* @example
|
|
26
|
-
* strLen('abcde;'); // 6
|
|
27
|
-
* strLen('abc测试;'); // 9
|
|
28
|
-
* strLen('abc测试;', { enLen: 0.5, nonEnLen: 1 }); // 4.5
|
|
29
|
-
*/
|
|
30
|
-
export function strLen(str, options) {
|
|
31
|
-
// 函数重载
|
|
32
|
-
if (options == null) { options = 2; }
|
|
33
|
-
if (typeof options === 'number') {
|
|
34
|
-
options = {
|
|
35
|
-
enLen: 1,
|
|
36
|
-
nonEnLen: options
|
|
37
|
-
};
|
|
38
|
-
} else {
|
|
39
|
-
options = extend({
|
|
40
|
-
enLen: 1,
|
|
41
|
-
nonEnLen: 2
|
|
42
|
-
}, options);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let result = 0;
|
|
46
|
-
for (let i = str.length - 1; i >= 0; i--) {
|
|
47
|
-
result += str.charCodeAt(i) > 255 ?
|
|
48
|
-
options.nonEnLen :
|
|
49
|
-
options.enLen;
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 如果目标字符串超出限制长度,则进行截断并拼接省略符号;否则返回目标字符串。
|
|
56
|
-
* @param {string} str 目标字符串。
|
|
57
|
-
* @param {number} length 限制的长度。
|
|
58
|
-
* @param {Object} [options] 选项。
|
|
59
|
-
* @param {number} [options.mode=2] 非英文字符的单位长度。已废弃,请使用 nonEnLen。
|
|
60
|
-
* @param {number} [options.enLen=1] 英文字符的单位长度。
|
|
61
|
-
* @param {number} [options.nonEnLen=2] 非英文字符的单位长度。
|
|
62
|
-
* @param {string} [options.ellipsis='...'] 省略符号。
|
|
63
|
-
* @return {string} 截断后的字符串。
|
|
64
|
-
* @example
|
|
65
|
-
* cutStr('测试一下', 5); // '测试...'
|
|
66
|
-
* cutStr('测试一下', 8); // '测试一下'
|
|
67
|
-
* curStr('1测试2测试3', 3.5, { enLen: 0.5, nonEnLen: 1 }); // 1测...
|
|
68
|
-
*/
|
|
69
|
-
export function cutStr(str, length, options) {
|
|
70
|
-
options = extend({
|
|
71
|
-
ellipsis: '...',
|
|
72
|
-
enLen: 1
|
|
73
|
-
}, options);
|
|
74
|
-
if (options.nonEnLen == null) {
|
|
75
|
-
options.nonEnLen = options.mode == null ? 2 : options.mode;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
str = String(str);
|
|
79
|
-
const len = strLen(str, options);
|
|
80
|
-
|
|
81
|
-
// 未超出长度,直接返回传入的字符串
|
|
82
|
-
if (len <= length) { return str; }
|
|
83
|
-
|
|
84
|
-
// 减去省略符长度
|
|
85
|
-
length -= strLen(options.ellipsis, options);
|
|
86
|
-
|
|
87
|
-
let result = '', i = -1;
|
|
88
|
-
while (length > 0 && ++i < len) {
|
|
89
|
-
length -= str.charCodeAt(i) > 255 ?
|
|
90
|
-
options.nonEnLen :
|
|
91
|
-
options.enLen;
|
|
92
|
-
if (length >= 0) { result += str.charAt(i); }
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
result += options.ellipsis;
|
|
96
|
-
|
|
97
|
-
return result;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 把指定字符串中的 HTML 预留字符替换成 HTML 实体。
|
|
102
|
-
* @param {string} str 指定字符串。
|
|
103
|
-
* @return {string} 替换后的字符串。
|
|
104
|
-
*/
|
|
105
|
-
export function escapeHTML(str) {
|
|
106
|
-
if (str == null) { return str; }
|
|
107
|
-
const map = {
|
|
108
|
-
'"': '"',
|
|
109
|
-
'\'': ''',
|
|
110
|
-
'&': '&',
|
|
111
|
-
'<': '<',
|
|
112
|
-
'>': '>'
|
|
113
|
-
};
|
|
114
|
-
return String(str).replace(/["'&<>]/g, (match) => {
|
|
115
|
-
return map[match];
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 移除指定字符串中的 HTML 标签。
|
|
121
|
-
* @param {string} str 指定字符串。
|
|
122
|
-
* @return {string} 处理后的字符串。
|
|
123
|
-
*/
|
|
124
|
-
export function removeTags(str) {
|
|
125
|
-
if (str == null) { return str; }
|
|
126
|
-
return String(str).replace(/<.+?>/g, '');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* 把指定字符串中的换行符替换成 <br />。
|
|
131
|
-
* @param {string} str 指定字符串。
|
|
132
|
-
* @return {string} 替换后的字符串。
|
|
133
|
-
*/
|
|
134
|
-
export function nl2br(str) {
|
|
135
|
-
if (str == null) { return str; }
|
|
136
|
-
return String(str).replace(/\r?\n/g, '<br />');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* 生成随机字符串。
|
|
141
|
-
* @since 1.6.0
|
|
142
|
-
* @param {number} length 字符串长度。
|
|
143
|
-
* @param {string} [prefix] 字符串前缀(不计入长度)。
|
|
144
|
-
* @returns {string} 生成的随机字符串。
|
|
145
|
-
*/
|
|
146
|
-
export function randomStr(length, prefix) {
|
|
147
|
-
length = parseInt(length);
|
|
148
|
-
if (!length || length < 0) {
|
|
149
|
-
throw new Error('"length" must be a number greater than 0');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
let result = '';
|
|
153
|
-
do {
|
|
154
|
-
result += Math.random().toString(36).substr(2);
|
|
155
|
-
} while (result.length < length);
|
|
156
|
-
|
|
157
|
-
// 拼接的长度可能大于指定长度,进行裁剪
|
|
158
|
-
result = result.substr(0, length);
|
|
159
|
-
|
|
160
|
-
if (prefix != null) { result = prefix + result; }
|
|
161
|
-
|
|
162
|
-
return result;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* 版本号对比。
|
|
167
|
-
* @since 1.6.0
|
|
168
|
-
* @param {string} versionA 待比较版本 A。
|
|
169
|
-
* @param {string} versionB 待比较版本 B。
|
|
170
|
-
* @return {number} 大于 0 时,表示版本 A 大于版本 B;
|
|
171
|
-
* 小于 0 时,表示版本 B 大于版本 A;
|
|
172
|
-
* 等于 0 时,表示两个版本号一致。
|
|
173
|
-
*/
|
|
174
|
-
export function compareVersions(versionA, versionB) {
|
|
175
|
-
if (!versionA || !versionB) {
|
|
176
|
-
throw new Error('Please specify both version-a and verson-b');
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// 去掉末尾的 .000
|
|
180
|
-
const reg = /(\.0+)+$/;
|
|
181
|
-
versionA = String(versionA).replace(reg, '').split('.');
|
|
182
|
-
versionB = String(versionB).replace(reg, '').split('.');
|
|
183
|
-
|
|
184
|
-
const len = Math.min(versionA.length, versionB.length);
|
|
185
|
-
for (let i = 0; i < len; i++) {
|
|
186
|
-
const diff = parseInt(versionA[i]) - parseInt(versionB[i]);
|
|
187
|
-
if (diff) { return diff; }
|
|
188
|
-
}
|
|
189
|
-
return versionA.length - versionB.length;
|
|
190
|
-
}
|
package/src/validate.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本模块提供字符串格式验证方法。
|
|
3
|
-
* @module validate
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 检查目标字符串是否手机号码。
|
|
8
|
-
* @author liumin
|
|
9
|
-
* @param {string} str 目标字符串。
|
|
10
|
-
* @return {boolean} 目标字符串是否手机号码。
|
|
11
|
-
* @example
|
|
12
|
-
* isPhoneNO('13800138000'); // true
|
|
13
|
-
* isPhoneNO('a13800138000c'); // false
|
|
14
|
-
*/
|
|
15
|
-
export function isPhoneNO(str) {
|
|
16
|
-
return /^1[3-9]\d{9}$/.test(str);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 检查目标字符串是否电子邮箱地址。
|
|
21
|
-
* @author luoliquan
|
|
22
|
-
* @param {string} str 目标字符串。
|
|
23
|
-
* @return {boolean} 目标字符串是否电子邮箱地址。
|
|
24
|
-
* @example
|
|
25
|
-
* isEmail('me@polyv.net'); // true
|
|
26
|
-
* isEmail('me@polyv_.net-'); // false
|
|
27
|
-
*/
|
|
28
|
-
export function isEmail(str) {
|
|
29
|
-
let temp = /^[\w-]+(?:\.[\w-]+)*@[\w-]+(?:\.[\w-]+)*\.[a-zA-Z]{2,}$/.test(str);
|
|
30
|
-
if (temp) {
|
|
31
|
-
temp = str.replace('@', '.').split('.');
|
|
32
|
-
for (let i = temp.length - 1; i >= 0; i--) {
|
|
33
|
-
// 每一段的开头和结尾都不能是连字符或下划线
|
|
34
|
-
if (/^[-_]/.test(temp[i]) || /[_-]$/.test(temp[i])) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return true;
|
|
39
|
-
} else {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|