jiezhangxin-utils 0.0.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/LICENSE +194 -0
- package/README.md +62 -0
- package/dist/jiezhangxin-utils.cjs.js +370 -0
- package/dist/jiezhangxin-utils.esm.js +348 -0
- package/dist/jiezhangxin-utils.umd.js +1 -0
- package/package.json +28 -0
- package/src/array/arrayUtils.js +103 -0
- package/src/date/dateUtils.js +75 -0
- package/src/index.js +14 -0
- package/src/storage/storageUtils.js +105 -0
- package/src/string/stringUtils.js +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
木兰宽松许可证,第2版
|
|
2
|
+
|
|
3
|
+
木兰宽松许可证,第2版
|
|
4
|
+
|
|
5
|
+
2020年1月 http://license.coscl.org.cn/MulanPSL2
|
|
6
|
+
|
|
7
|
+
您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束:
|
|
8
|
+
|
|
9
|
+
0. 定义
|
|
10
|
+
|
|
11
|
+
“软件” 是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
|
|
12
|
+
|
|
13
|
+
“贡献” 是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
|
|
14
|
+
|
|
15
|
+
“贡献者” 是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
|
|
16
|
+
|
|
17
|
+
“法人实体” 是指提交贡献的机构及其“关联实体”。
|
|
18
|
+
|
|
19
|
+
“关联实体” 是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是
|
|
20
|
+
指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
|
|
21
|
+
|
|
22
|
+
1. 授予版权许可
|
|
23
|
+
|
|
24
|
+
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可
|
|
25
|
+
以复制、使用、修改、分发其“贡献”,不论修改与否。
|
|
26
|
+
|
|
27
|
+
2. 授予专利许可
|
|
28
|
+
|
|
29
|
+
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定
|
|
30
|
+
撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡
|
|
31
|
+
献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软
|
|
32
|
+
件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“
|
|
33
|
+
关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或
|
|
34
|
+
其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权
|
|
35
|
+
行动之日终止。
|
|
36
|
+
|
|
37
|
+
3. 无商标许可
|
|
38
|
+
|
|
39
|
+
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定
|
|
40
|
+
的声明义务而必须使用除外。
|
|
41
|
+
|
|
42
|
+
4. 分发限制
|
|
43
|
+
|
|
44
|
+
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“
|
|
45
|
+
本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
|
|
46
|
+
|
|
47
|
+
5. 免责声明与责任限制
|
|
48
|
+
|
|
49
|
+
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对
|
|
50
|
+
任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于
|
|
51
|
+
何种法律理论,即使其曾被建议有此种损失的可能性。
|
|
52
|
+
|
|
53
|
+
6. 语言
|
|
54
|
+
|
|
55
|
+
“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文
|
|
56
|
+
版为准。
|
|
57
|
+
|
|
58
|
+
条款结束
|
|
59
|
+
|
|
60
|
+
如何将木兰宽松许可证,第2版,应用到您的软件
|
|
61
|
+
|
|
62
|
+
如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
|
|
63
|
+
|
|
64
|
+
1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
|
|
65
|
+
|
|
66
|
+
2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
|
|
67
|
+
|
|
68
|
+
3, 请将如下声明文本放入每个源文件的头部注释中。
|
|
69
|
+
|
|
70
|
+
Copyright (c) [Year] [name of copyright holder]
|
|
71
|
+
[Software Name] is licensed under Mulan PSL v2.
|
|
72
|
+
You can use this software according to the terms and conditions of the Mulan
|
|
73
|
+
PSL v2.
|
|
74
|
+
You may obtain a copy of Mulan PSL v2 at:
|
|
75
|
+
http://license.coscl.org.cn/MulanPSL2
|
|
76
|
+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
|
|
77
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
78
|
+
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
79
|
+
See the Mulan PSL v2 for more details.
|
|
80
|
+
|
|
81
|
+
Mulan Permissive Software License,Version 2
|
|
82
|
+
|
|
83
|
+
Mulan Permissive Software License,Version 2 (Mulan PSL v2)
|
|
84
|
+
|
|
85
|
+
January 2020 http://license.coscl.org.cn/MulanPSL2
|
|
86
|
+
|
|
87
|
+
Your reproduction, use, modification and distribution of the Software shall
|
|
88
|
+
be subject to Mulan PSL v2 (this License) with the following terms and
|
|
89
|
+
conditions:
|
|
90
|
+
|
|
91
|
+
0. Definition
|
|
92
|
+
|
|
93
|
+
Software means the program and related documents which are licensed under
|
|
94
|
+
this License and comprise all Contribution(s).
|
|
95
|
+
|
|
96
|
+
Contribution means the copyrightable work licensed by a particular
|
|
97
|
+
Contributor under this License.
|
|
98
|
+
|
|
99
|
+
Contributor means the Individual or Legal Entity who licenses its
|
|
100
|
+
copyrightable work under this License.
|
|
101
|
+
|
|
102
|
+
Legal Entity means the entity making a Contribution and all its
|
|
103
|
+
Affiliates.
|
|
104
|
+
|
|
105
|
+
Affiliates means entities that control, are controlled by, or are under
|
|
106
|
+
common control with the acting entity under this License, ‘control’ means
|
|
107
|
+
direct or indirect ownership of at least fifty percent (50%) of the voting
|
|
108
|
+
power, capital or other securities of controlled or commonly controlled
|
|
109
|
+
entity.
|
|
110
|
+
|
|
111
|
+
1. Grant of Copyright License
|
|
112
|
+
|
|
113
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
|
114
|
+
grants to you a perpetual, worldwide, royalty-free, non-exclusive,
|
|
115
|
+
irrevocable copyright license to reproduce, use, modify, or distribute its
|
|
116
|
+
Contribution, with modification or not.
|
|
117
|
+
|
|
118
|
+
2. Grant of Patent License
|
|
119
|
+
|
|
120
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
|
121
|
+
grants to you a perpetual, worldwide, royalty-free, non-exclusive,
|
|
122
|
+
irrevocable (except for revocation under this Section) patent license to
|
|
123
|
+
make, have made, use, offer for sale, sell, import or otherwise transfer its
|
|
124
|
+
Contribution, where such patent license is only limited to the patent claims
|
|
125
|
+
owned or controlled by such Contributor now or in future which will be
|
|
126
|
+
necessarily infringed by its Contribution alone, or by combination of the
|
|
127
|
+
Contribution with the Software to which the Contribution was contributed.
|
|
128
|
+
The patent license shall not apply to any modification of the Contribution,
|
|
129
|
+
and any other combination which includes the Contribution. If you or your
|
|
130
|
+
Affiliates directly or indirectly institute patent litigation (including a
|
|
131
|
+
cross claim or counterclaim in a litigation) or other patent enforcement
|
|
132
|
+
activities against any individual or entity by alleging that the Software or
|
|
133
|
+
any Contribution in it infringes patents, then any patent license granted to
|
|
134
|
+
you under this License for the Software shall terminate as of the date such
|
|
135
|
+
litigation or activity is filed or taken.
|
|
136
|
+
|
|
137
|
+
3. No Trademark License
|
|
138
|
+
|
|
139
|
+
No trademark license is granted to use the trade names, trademarks, service
|
|
140
|
+
marks, or product names of Contributor, except as required to fulfill notice
|
|
141
|
+
requirements in section 4.
|
|
142
|
+
|
|
143
|
+
4. Distribution Restriction
|
|
144
|
+
|
|
145
|
+
You may distribute the Software in any medium with or without modification,
|
|
146
|
+
whether in source or executable forms, provided that you provide recipients
|
|
147
|
+
with a copy of this License and retain copyright, patent, trademark and
|
|
148
|
+
disclaimer statements in the Software.
|
|
149
|
+
|
|
150
|
+
5. Disclaimer of Warranty and Limitation of Liability
|
|
151
|
+
|
|
152
|
+
THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY
|
|
153
|
+
KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR
|
|
154
|
+
COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT
|
|
155
|
+
LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING
|
|
156
|
+
FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO
|
|
157
|
+
MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF
|
|
158
|
+
THE POSSIBILITY OF SUCH DAMAGES.
|
|
159
|
+
|
|
160
|
+
6. Language
|
|
161
|
+
|
|
162
|
+
THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION
|
|
163
|
+
AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF
|
|
164
|
+
DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION
|
|
165
|
+
SHALL PREVAIL.
|
|
166
|
+
|
|
167
|
+
END OF THE TERMS AND CONDITIONS
|
|
168
|
+
|
|
169
|
+
How to Apply the Mulan Permissive Software License,Version 2
|
|
170
|
+
(Mulan PSL v2) to Your Software
|
|
171
|
+
|
|
172
|
+
To apply the Mulan PSL v2 to your work, for easy identification by
|
|
173
|
+
recipients, you are suggested to complete following three steps:
|
|
174
|
+
|
|
175
|
+
i. Fill in the blanks in following statement, including insert your software
|
|
176
|
+
name, the year of the first publication of your software, and your name
|
|
177
|
+
identified as the copyright owner;
|
|
178
|
+
|
|
179
|
+
ii. Create a file named "LICENSE" which contains the whole context of this
|
|
180
|
+
License in the first directory of your software package;
|
|
181
|
+
|
|
182
|
+
iii. Attach the statement to the appropriate annotated syntax at the
|
|
183
|
+
beginning of each source file.
|
|
184
|
+
|
|
185
|
+
Copyright (c) [Year] [name of copyright holder]
|
|
186
|
+
[Software Name] is licensed under Mulan PSL v2.
|
|
187
|
+
You can use this software according to the terms and conditions of the Mulan
|
|
188
|
+
PSL v2.
|
|
189
|
+
You may obtain a copy of Mulan PSL v2 at:
|
|
190
|
+
http://license.coscl.org.cn/MulanPSL2
|
|
191
|
+
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
|
|
192
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
193
|
+
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
194
|
+
See the Mulan PSL v2 for more details.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# jiezhangxin-utils
|
|
2
|
+
|
|
3
|
+
A utility toolkit for Vue 3 projects, providing common functions for date handling, string manipulation, array processing, and storage management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
npm install jiezhangxin-utils --save
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
### In Vue 3 project
|
|
10
|
+
// Import all utilities
|
|
11
|
+
import * as jiezhangxinUtils from 'jiezhangxin-utils';
|
|
12
|
+
|
|
13
|
+
// Or import specific utilities
|
|
14
|
+
import { formatDate, capitalize } from 'jiezhangxin-utils';
|
|
15
|
+
### Example
|
|
16
|
+
// Format date
|
|
17
|
+
console.log(formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss'));
|
|
18
|
+
|
|
19
|
+
// Capitalize string
|
|
20
|
+
console.log(capitalize('hello world')); // Output: 'Hello world'
|
|
21
|
+
|
|
22
|
+
// Array deduplication
|
|
23
|
+
console.log(uniqueArray([1, 2, 2, 3, 3, 3])); // Output: [1, 2, 3]
|
|
24
|
+
|
|
25
|
+
// Local storage
|
|
26
|
+
setLocalStorage('userInfo', { name: 'John', age: 30 });
|
|
27
|
+
console.log(getLocalStorage('userInfo'));
|
|
28
|
+
## API Documentation
|
|
29
|
+
|
|
30
|
+
### Date Utilities
|
|
31
|
+
|
|
32
|
+
- `formatDate(date, format)`: Format date into string
|
|
33
|
+
- `getDaysBetween(startDate, endDate)`: Get days between two dates
|
|
34
|
+
- `isToday(date)`: Check if date is today
|
|
35
|
+
|
|
36
|
+
### String Utilities
|
|
37
|
+
|
|
38
|
+
- `trim(str)`: Remove whitespace from both ends of string
|
|
39
|
+
- `capitalize(str)`: Capitalize the first letter of string
|
|
40
|
+
- `maskStr(str, startLen, endLen, replaceChar)`: Mask string for privacy
|
|
41
|
+
- `isEmail(str)`: Check if string is a valid email
|
|
42
|
+
- `isPhone(str)`: Check if string is a valid phone number
|
|
43
|
+
|
|
44
|
+
### Array Utilities
|
|
45
|
+
|
|
46
|
+
- `uniqueArray(arr)`: Remove duplicates from array
|
|
47
|
+
- `sortArray(arr, key, isAsc)`: Sort array
|
|
48
|
+
- `groupArray(arr, key)`: Group array by key
|
|
49
|
+
- `flattenArray(arr)`: Flatten nested array
|
|
50
|
+
|
|
51
|
+
### Storage Utilities
|
|
52
|
+
|
|
53
|
+
- `getLocalStorage(key)`: Get item from localStorage
|
|
54
|
+
- `setLocalStorage(key, value)`: Set item to localStorage
|
|
55
|
+
- `removeLocalStorage(key)`: Remove item from localStorage
|
|
56
|
+
- `getSessionStorage(key)`: Get item from sessionStorage
|
|
57
|
+
- `setSessionStorage(key, value)`: Set item to sessionStorage
|
|
58
|
+
- `removeSessionStorage(key)`: Remove item from sessionStorage
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 格式化日期
|
|
7
|
+
* @param {Date|string|number} date - 日期
|
|
8
|
+
* @param {string} format - 格式字符串,例如:'yyyy-MM-dd HH:mm:ss'
|
|
9
|
+
* @returns {string} 格式化后的日期字符串
|
|
10
|
+
*/
|
|
11
|
+
function formatDate(date, format = 'yyyy-MM-dd') {
|
|
12
|
+
if (!date) {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 处理日期参数
|
|
17
|
+
if (!(date instanceof Date)) {
|
|
18
|
+
date = new Date(date);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 检查日期是否有效
|
|
22
|
+
if (isNaN(date.getTime())) {
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
const o = {
|
|
26
|
+
'M+': date.getMonth() + 1,
|
|
27
|
+
// 月份
|
|
28
|
+
'd+': date.getDate(),
|
|
29
|
+
// 日
|
|
30
|
+
'H+': date.getHours(),
|
|
31
|
+
// 小时
|
|
32
|
+
'm+': date.getMinutes(),
|
|
33
|
+
// 分
|
|
34
|
+
's+': date.getSeconds(),
|
|
35
|
+
// 秒
|
|
36
|
+
'q+': Math.floor((date.getMonth() + 3) / 3),
|
|
37
|
+
// 季度
|
|
38
|
+
'S': date.getMilliseconds() // 毫秒
|
|
39
|
+
};
|
|
40
|
+
if (/(y+)/.test(format)) {
|
|
41
|
+
format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
|
42
|
+
}
|
|
43
|
+
for (const k in o) {
|
|
44
|
+
if (new RegExp('(' + k + ')').test(format)) {
|
|
45
|
+
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return format;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 获取两个日期之间的天数差
|
|
53
|
+
* @param {Date|string|number} startDate - 开始日期
|
|
54
|
+
* @param {Date|string|number} endDate - 结束日期
|
|
55
|
+
* @returns {number} 天数差
|
|
56
|
+
*/
|
|
57
|
+
function getDaysBetween(startDate, endDate) {
|
|
58
|
+
const start = new Date(startDate);
|
|
59
|
+
const end = new Date(endDate);
|
|
60
|
+
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
const oneDay = 24 * 60 * 60 * 1000; // 毫秒
|
|
64
|
+
return Math.round(Math.abs((end - start) / oneDay));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 判断是否为今天
|
|
69
|
+
* @param {Date|string|number} date - 日期
|
|
70
|
+
* @returns {boolean} 是否为今天
|
|
71
|
+
*/
|
|
72
|
+
function isToday(date) {
|
|
73
|
+
const target = new Date(date);
|
|
74
|
+
const today = new Date();
|
|
75
|
+
return target.getDate() === today.getDate() && target.getMonth() === today.getMonth() && target.getFullYear() === today.getFullYear();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 去除字符串首尾空格
|
|
80
|
+
* @param {string} str - 输入字符串
|
|
81
|
+
* @returns {string} 处理后的字符串
|
|
82
|
+
*/
|
|
83
|
+
function trim(str) {
|
|
84
|
+
if (typeof str !== 'string') {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
return str.trim();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 首字母大写
|
|
92
|
+
* @param {string} str - 输入字符串
|
|
93
|
+
* @returns {string} 处理后的字符串
|
|
94
|
+
*/
|
|
95
|
+
function capitalize(str) {
|
|
96
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 字符串脱敏处理
|
|
104
|
+
* @param {string} str - 输入字符串
|
|
105
|
+
* @param {number} startLen - 保留开始长度
|
|
106
|
+
* @param {number} endLen - 保留结束长度
|
|
107
|
+
* @param {string} replaceChar - 替换字符
|
|
108
|
+
* @returns {string} 脱敏后的字符串
|
|
109
|
+
*/
|
|
110
|
+
function maskStr(str, startLen = 3, endLen = 4, replaceChar = '*') {
|
|
111
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
112
|
+
return '';
|
|
113
|
+
}
|
|
114
|
+
const len = str.length;
|
|
115
|
+
if (startLen + endLen >= len) {
|
|
116
|
+
return str;
|
|
117
|
+
}
|
|
118
|
+
const maskLen = len - startLen - endLen;
|
|
119
|
+
const mask = replaceChar.repeat(maskLen);
|
|
120
|
+
return str.substring(0, startLen) + mask + str.substring(len - endLen);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 判断字符串是否为邮箱格式
|
|
125
|
+
* @param {string} str - 输入字符串
|
|
126
|
+
* @returns {boolean} 是否为邮箱
|
|
127
|
+
*/
|
|
128
|
+
function isEmail(str) {
|
|
129
|
+
if (typeof str !== 'string') {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
|
|
133
|
+
return reg.test(str);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 判断字符串是否为手机号格式
|
|
138
|
+
* @param {string} str - 输入字符串
|
|
139
|
+
* @returns {boolean} 是否为手机号
|
|
140
|
+
*/
|
|
141
|
+
function isPhone(str) {
|
|
142
|
+
if (typeof str !== 'string') {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
const reg = /^1[3-9]\d{9}$/;
|
|
146
|
+
return reg.test(str);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 数组去重
|
|
151
|
+
* @param {Array} arr - 输入数组
|
|
152
|
+
* @returns {Array} 去重后的数组
|
|
153
|
+
*/
|
|
154
|
+
function uniqueArray(arr) {
|
|
155
|
+
if (!Array.isArray(arr)) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
return [...new Set(arr)];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 数组排序
|
|
163
|
+
* @param {Array} arr - 输入数组
|
|
164
|
+
* @param {string} key - 排序的键名(对象数组时使用)
|
|
165
|
+
* @param {boolean} isAsc - 是否升序
|
|
166
|
+
* @returns {Array} 排序后的数组
|
|
167
|
+
*/
|
|
168
|
+
function sortArray(arr, key, isAsc = true) {
|
|
169
|
+
if (!Array.isArray(arr)) {
|
|
170
|
+
return [];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 复制数组避免修改原数组
|
|
174
|
+
const newArr = [...arr];
|
|
175
|
+
newArr.sort((a, b) => {
|
|
176
|
+
let valueA = a;
|
|
177
|
+
let valueB = b;
|
|
178
|
+
|
|
179
|
+
// 如果指定了key,说明是对象数组
|
|
180
|
+
if (key) {
|
|
181
|
+
valueA = a[key];
|
|
182
|
+
valueB = b[key];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 处理数字情况
|
|
186
|
+
if (typeof valueA === 'number' && typeof valueB === 'number') {
|
|
187
|
+
return isAsc ? valueA - valueB : valueB - valueA;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 处理字符串情况
|
|
191
|
+
const strA = String(valueA).toLowerCase();
|
|
192
|
+
const strB = String(valueB).toLowerCase();
|
|
193
|
+
if (strA < strB) {
|
|
194
|
+
return isAsc ? -1 : 1;
|
|
195
|
+
}
|
|
196
|
+
if (strA > strB) {
|
|
197
|
+
return isAsc ? 1 : -1;
|
|
198
|
+
}
|
|
199
|
+
return 0;
|
|
200
|
+
});
|
|
201
|
+
return newArr;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 数组分组
|
|
206
|
+
* @param {Array} arr - 输入数组
|
|
207
|
+
* @param {Function|string} key - 分组依据的函数或键名
|
|
208
|
+
* @returns {Object} 分组后的对象
|
|
209
|
+
*/
|
|
210
|
+
function groupArray(arr, key) {
|
|
211
|
+
if (!Array.isArray(arr)) {
|
|
212
|
+
return {};
|
|
213
|
+
}
|
|
214
|
+
return arr.reduce((result, item) => {
|
|
215
|
+
let groupKey;
|
|
216
|
+
if (typeof key === 'function') {
|
|
217
|
+
groupKey = key(item);
|
|
218
|
+
} else if (typeof key === 'string') {
|
|
219
|
+
groupKey = item[key];
|
|
220
|
+
} else {
|
|
221
|
+
groupKey = item;
|
|
222
|
+
}
|
|
223
|
+
if (!result[groupKey]) {
|
|
224
|
+
result[groupKey] = [];
|
|
225
|
+
}
|
|
226
|
+
result[groupKey].push(item);
|
|
227
|
+
return result;
|
|
228
|
+
}, {});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 数组扁平化
|
|
233
|
+
* @param {Array} arr - 输入数组
|
|
234
|
+
* @returns {Array} 扁平化后的数组
|
|
235
|
+
*/
|
|
236
|
+
function flattenArray(arr) {
|
|
237
|
+
if (!Array.isArray(arr)) {
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
return arr.reduce((result, item) => {
|
|
241
|
+
return result.concat(Array.isArray(item) ? flattenArray(item) : item);
|
|
242
|
+
}, []);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 从localStorage获取数据
|
|
247
|
+
* @param {string} key - 存储键名
|
|
248
|
+
* @returns {any} 存储的值
|
|
249
|
+
*/
|
|
250
|
+
function getLocalStorage(key) {
|
|
251
|
+
if (!key || typeof key !== 'string') {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
const value = localStorage.getItem(key);
|
|
256
|
+
if (value === null) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
// 尝试解析JSON
|
|
260
|
+
return JSON.parse(value);
|
|
261
|
+
} catch (e) {
|
|
262
|
+
// 如果解析失败,直接返回原始值
|
|
263
|
+
return localStorage.getItem(key);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 向localStorage存储数据
|
|
269
|
+
* @param {string} key - 存储键名
|
|
270
|
+
* @param {any} value - 存储的值
|
|
271
|
+
* @returns {boolean} 是否存储成功
|
|
272
|
+
*/
|
|
273
|
+
function setLocalStorage(key, value) {
|
|
274
|
+
if (!key || typeof key !== 'string') {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
// 转换为JSON字符串存储
|
|
279
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
280
|
+
return true;
|
|
281
|
+
} catch (e) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 从localStorage删除数据
|
|
288
|
+
* @param {string} key - 存储键名
|
|
289
|
+
*/
|
|
290
|
+
function removeLocalStorage(key) {
|
|
291
|
+
if (key && typeof key === 'string') {
|
|
292
|
+
localStorage.removeItem(key);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 从sessionStorage获取数据
|
|
298
|
+
* @param {string} key - 存储键名
|
|
299
|
+
* @returns {any} 存储的值
|
|
300
|
+
*/
|
|
301
|
+
function getSessionStorage(key) {
|
|
302
|
+
if (!key || typeof key !== 'string') {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
try {
|
|
306
|
+
const value = sessionStorage.getItem(key);
|
|
307
|
+
if (value === null) {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
// 尝试解析JSON
|
|
311
|
+
return JSON.parse(value);
|
|
312
|
+
} catch (e) {
|
|
313
|
+
// 如果解析失败,直接返回原始值
|
|
314
|
+
return sessionStorage.getItem(key);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 向sessionStorage存储数据
|
|
320
|
+
* @param {string} key - 存储键名
|
|
321
|
+
* @param {any} value - 存储的值
|
|
322
|
+
* @returns {boolean} 是否存储成功
|
|
323
|
+
*/
|
|
324
|
+
function setSessionStorage(key, value) {
|
|
325
|
+
if (!key || typeof key !== 'string') {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
// 转换为JSON字符串存储
|
|
330
|
+
sessionStorage.setItem(key, JSON.stringify(value));
|
|
331
|
+
return true;
|
|
332
|
+
} catch (e) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 从sessionStorage删除数据
|
|
339
|
+
* @param {string} key - 存储键名
|
|
340
|
+
*/
|
|
341
|
+
function removeSessionStorage(key) {
|
|
342
|
+
if (key && typeof key === 'string') {
|
|
343
|
+
sessionStorage.removeItem(key);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 导出日期工具
|
|
348
|
+
|
|
349
|
+
// 工具包版本信息
|
|
350
|
+
const version = '1.0.0';
|
|
351
|
+
|
|
352
|
+
exports.capitalize = capitalize;
|
|
353
|
+
exports.flattenArray = flattenArray;
|
|
354
|
+
exports.formatDate = formatDate;
|
|
355
|
+
exports.getDaysBetween = getDaysBetween;
|
|
356
|
+
exports.getLocalStorage = getLocalStorage;
|
|
357
|
+
exports.getSessionStorage = getSessionStorage;
|
|
358
|
+
exports.groupArray = groupArray;
|
|
359
|
+
exports.isEmail = isEmail;
|
|
360
|
+
exports.isPhone = isPhone;
|
|
361
|
+
exports.isToday = isToday;
|
|
362
|
+
exports.maskStr = maskStr;
|
|
363
|
+
exports.removeLocalStorage = removeLocalStorage;
|
|
364
|
+
exports.removeSessionStorage = removeSessionStorage;
|
|
365
|
+
exports.setLocalStorage = setLocalStorage;
|
|
366
|
+
exports.setSessionStorage = setSessionStorage;
|
|
367
|
+
exports.sortArray = sortArray;
|
|
368
|
+
exports.trim = trim;
|
|
369
|
+
exports.uniqueArray = uniqueArray;
|
|
370
|
+
exports.version = version;
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 格式化日期
|
|
3
|
+
* @param {Date|string|number} date - 日期
|
|
4
|
+
* @param {string} format - 格式字符串,例如:'yyyy-MM-dd HH:mm:ss'
|
|
5
|
+
* @returns {string} 格式化后的日期字符串
|
|
6
|
+
*/
|
|
7
|
+
function formatDate(date, format = 'yyyy-MM-dd') {
|
|
8
|
+
if (!date) {
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 处理日期参数
|
|
13
|
+
if (!(date instanceof Date)) {
|
|
14
|
+
date = new Date(date);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 检查日期是否有效
|
|
18
|
+
if (isNaN(date.getTime())) {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
const o = {
|
|
22
|
+
'M+': date.getMonth() + 1,
|
|
23
|
+
// 月份
|
|
24
|
+
'd+': date.getDate(),
|
|
25
|
+
// 日
|
|
26
|
+
'H+': date.getHours(),
|
|
27
|
+
// 小时
|
|
28
|
+
'm+': date.getMinutes(),
|
|
29
|
+
// 分
|
|
30
|
+
's+': date.getSeconds(),
|
|
31
|
+
// 秒
|
|
32
|
+
'q+': Math.floor((date.getMonth() + 3) / 3),
|
|
33
|
+
// 季度
|
|
34
|
+
'S': date.getMilliseconds() // 毫秒
|
|
35
|
+
};
|
|
36
|
+
if (/(y+)/.test(format)) {
|
|
37
|
+
format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
|
38
|
+
}
|
|
39
|
+
for (const k in o) {
|
|
40
|
+
if (new RegExp('(' + k + ')').test(format)) {
|
|
41
|
+
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return format;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 获取两个日期之间的天数差
|
|
49
|
+
* @param {Date|string|number} startDate - 开始日期
|
|
50
|
+
* @param {Date|string|number} endDate - 结束日期
|
|
51
|
+
* @returns {number} 天数差
|
|
52
|
+
*/
|
|
53
|
+
function getDaysBetween(startDate, endDate) {
|
|
54
|
+
const start = new Date(startDate);
|
|
55
|
+
const end = new Date(endDate);
|
|
56
|
+
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
const oneDay = 24 * 60 * 60 * 1000; // 毫秒
|
|
60
|
+
return Math.round(Math.abs((end - start) / oneDay));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 判断是否为今天
|
|
65
|
+
* @param {Date|string|number} date - 日期
|
|
66
|
+
* @returns {boolean} 是否为今天
|
|
67
|
+
*/
|
|
68
|
+
function isToday(date) {
|
|
69
|
+
const target = new Date(date);
|
|
70
|
+
const today = new Date();
|
|
71
|
+
return target.getDate() === today.getDate() && target.getMonth() === today.getMonth() && target.getFullYear() === today.getFullYear();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 去除字符串首尾空格
|
|
76
|
+
* @param {string} str - 输入字符串
|
|
77
|
+
* @returns {string} 处理后的字符串
|
|
78
|
+
*/
|
|
79
|
+
function trim(str) {
|
|
80
|
+
if (typeof str !== 'string') {
|
|
81
|
+
return '';
|
|
82
|
+
}
|
|
83
|
+
return str.trim();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 首字母大写
|
|
88
|
+
* @param {string} str - 输入字符串
|
|
89
|
+
* @returns {string} 处理后的字符串
|
|
90
|
+
*/
|
|
91
|
+
function capitalize(str) {
|
|
92
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
93
|
+
return '';
|
|
94
|
+
}
|
|
95
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 字符串脱敏处理
|
|
100
|
+
* @param {string} str - 输入字符串
|
|
101
|
+
* @param {number} startLen - 保留开始长度
|
|
102
|
+
* @param {number} endLen - 保留结束长度
|
|
103
|
+
* @param {string} replaceChar - 替换字符
|
|
104
|
+
* @returns {string} 脱敏后的字符串
|
|
105
|
+
*/
|
|
106
|
+
function maskStr(str, startLen = 3, endLen = 4, replaceChar = '*') {
|
|
107
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
108
|
+
return '';
|
|
109
|
+
}
|
|
110
|
+
const len = str.length;
|
|
111
|
+
if (startLen + endLen >= len) {
|
|
112
|
+
return str;
|
|
113
|
+
}
|
|
114
|
+
const maskLen = len - startLen - endLen;
|
|
115
|
+
const mask = replaceChar.repeat(maskLen);
|
|
116
|
+
return str.substring(0, startLen) + mask + str.substring(len - endLen);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 判断字符串是否为邮箱格式
|
|
121
|
+
* @param {string} str - 输入字符串
|
|
122
|
+
* @returns {boolean} 是否为邮箱
|
|
123
|
+
*/
|
|
124
|
+
function isEmail(str) {
|
|
125
|
+
if (typeof str !== 'string') {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
|
|
129
|
+
return reg.test(str);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 判断字符串是否为手机号格式
|
|
134
|
+
* @param {string} str - 输入字符串
|
|
135
|
+
* @returns {boolean} 是否为手机号
|
|
136
|
+
*/
|
|
137
|
+
function isPhone(str) {
|
|
138
|
+
if (typeof str !== 'string') {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
const reg = /^1[3-9]\d{9}$/;
|
|
142
|
+
return reg.test(str);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 数组去重
|
|
147
|
+
* @param {Array} arr - 输入数组
|
|
148
|
+
* @returns {Array} 去重后的数组
|
|
149
|
+
*/
|
|
150
|
+
function uniqueArray(arr) {
|
|
151
|
+
if (!Array.isArray(arr)) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
return [...new Set(arr)];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 数组排序
|
|
159
|
+
* @param {Array} arr - 输入数组
|
|
160
|
+
* @param {string} key - 排序的键名(对象数组时使用)
|
|
161
|
+
* @param {boolean} isAsc - 是否升序
|
|
162
|
+
* @returns {Array} 排序后的数组
|
|
163
|
+
*/
|
|
164
|
+
function sortArray(arr, key, isAsc = true) {
|
|
165
|
+
if (!Array.isArray(arr)) {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 复制数组避免修改原数组
|
|
170
|
+
const newArr = [...arr];
|
|
171
|
+
newArr.sort((a, b) => {
|
|
172
|
+
let valueA = a;
|
|
173
|
+
let valueB = b;
|
|
174
|
+
|
|
175
|
+
// 如果指定了key,说明是对象数组
|
|
176
|
+
if (key) {
|
|
177
|
+
valueA = a[key];
|
|
178
|
+
valueB = b[key];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// 处理数字情况
|
|
182
|
+
if (typeof valueA === 'number' && typeof valueB === 'number') {
|
|
183
|
+
return isAsc ? valueA - valueB : valueB - valueA;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 处理字符串情况
|
|
187
|
+
const strA = String(valueA).toLowerCase();
|
|
188
|
+
const strB = String(valueB).toLowerCase();
|
|
189
|
+
if (strA < strB) {
|
|
190
|
+
return isAsc ? -1 : 1;
|
|
191
|
+
}
|
|
192
|
+
if (strA > strB) {
|
|
193
|
+
return isAsc ? 1 : -1;
|
|
194
|
+
}
|
|
195
|
+
return 0;
|
|
196
|
+
});
|
|
197
|
+
return newArr;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 数组分组
|
|
202
|
+
* @param {Array} arr - 输入数组
|
|
203
|
+
* @param {Function|string} key - 分组依据的函数或键名
|
|
204
|
+
* @returns {Object} 分组后的对象
|
|
205
|
+
*/
|
|
206
|
+
function groupArray(arr, key) {
|
|
207
|
+
if (!Array.isArray(arr)) {
|
|
208
|
+
return {};
|
|
209
|
+
}
|
|
210
|
+
return arr.reduce((result, item) => {
|
|
211
|
+
let groupKey;
|
|
212
|
+
if (typeof key === 'function') {
|
|
213
|
+
groupKey = key(item);
|
|
214
|
+
} else if (typeof key === 'string') {
|
|
215
|
+
groupKey = item[key];
|
|
216
|
+
} else {
|
|
217
|
+
groupKey = item;
|
|
218
|
+
}
|
|
219
|
+
if (!result[groupKey]) {
|
|
220
|
+
result[groupKey] = [];
|
|
221
|
+
}
|
|
222
|
+
result[groupKey].push(item);
|
|
223
|
+
return result;
|
|
224
|
+
}, {});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 数组扁平化
|
|
229
|
+
* @param {Array} arr - 输入数组
|
|
230
|
+
* @returns {Array} 扁平化后的数组
|
|
231
|
+
*/
|
|
232
|
+
function flattenArray(arr) {
|
|
233
|
+
if (!Array.isArray(arr)) {
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
return arr.reduce((result, item) => {
|
|
237
|
+
return result.concat(Array.isArray(item) ? flattenArray(item) : item);
|
|
238
|
+
}, []);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 从localStorage获取数据
|
|
243
|
+
* @param {string} key - 存储键名
|
|
244
|
+
* @returns {any} 存储的值
|
|
245
|
+
*/
|
|
246
|
+
function getLocalStorage(key) {
|
|
247
|
+
if (!key || typeof key !== 'string') {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
const value = localStorage.getItem(key);
|
|
252
|
+
if (value === null) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
// 尝试解析JSON
|
|
256
|
+
return JSON.parse(value);
|
|
257
|
+
} catch (e) {
|
|
258
|
+
// 如果解析失败,直接返回原始值
|
|
259
|
+
return localStorage.getItem(key);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 向localStorage存储数据
|
|
265
|
+
* @param {string} key - 存储键名
|
|
266
|
+
* @param {any} value - 存储的值
|
|
267
|
+
* @returns {boolean} 是否存储成功
|
|
268
|
+
*/
|
|
269
|
+
function setLocalStorage(key, value) {
|
|
270
|
+
if (!key || typeof key !== 'string') {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
// 转换为JSON字符串存储
|
|
275
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
276
|
+
return true;
|
|
277
|
+
} catch (e) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* 从localStorage删除数据
|
|
284
|
+
* @param {string} key - 存储键名
|
|
285
|
+
*/
|
|
286
|
+
function removeLocalStorage(key) {
|
|
287
|
+
if (key && typeof key === 'string') {
|
|
288
|
+
localStorage.removeItem(key);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 从sessionStorage获取数据
|
|
294
|
+
* @param {string} key - 存储键名
|
|
295
|
+
* @returns {any} 存储的值
|
|
296
|
+
*/
|
|
297
|
+
function getSessionStorage(key) {
|
|
298
|
+
if (!key || typeof key !== 'string') {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const value = sessionStorage.getItem(key);
|
|
303
|
+
if (value === null) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
// 尝试解析JSON
|
|
307
|
+
return JSON.parse(value);
|
|
308
|
+
} catch (e) {
|
|
309
|
+
// 如果解析失败,直接返回原始值
|
|
310
|
+
return sessionStorage.getItem(key);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 向sessionStorage存储数据
|
|
316
|
+
* @param {string} key - 存储键名
|
|
317
|
+
* @param {any} value - 存储的值
|
|
318
|
+
* @returns {boolean} 是否存储成功
|
|
319
|
+
*/
|
|
320
|
+
function setSessionStorage(key, value) {
|
|
321
|
+
if (!key || typeof key !== 'string') {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
// 转换为JSON字符串存储
|
|
326
|
+
sessionStorage.setItem(key, JSON.stringify(value));
|
|
327
|
+
return true;
|
|
328
|
+
} catch (e) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* 从sessionStorage删除数据
|
|
335
|
+
* @param {string} key - 存储键名
|
|
336
|
+
*/
|
|
337
|
+
function removeSessionStorage(key) {
|
|
338
|
+
if (key && typeof key === 'string') {
|
|
339
|
+
sessionStorage.removeItem(key);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 导出日期工具
|
|
344
|
+
|
|
345
|
+
// 工具包版本信息
|
|
346
|
+
const version = '1.0.0';
|
|
347
|
+
|
|
348
|
+
export { capitalize, flattenArray, formatDate, getDaysBetween, getLocalStorage, getSessionStorage, groupArray, isEmail, isPhone, isToday, maskStr, removeLocalStorage, removeSessionStorage, setLocalStorage, setSessionStorage, sortArray, trim, uniqueArray, version };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jiezhangxinUtils={})}(this,function(t){"use strict";t.capitalize=function(t){return"string"!=typeof t||0===t.length?"":t.charAt(0).toUpperCase()+t.slice(1)},t.flattenArray=function t(e){return Array.isArray(e)?e.reduce((e,r)=>e.concat(Array.isArray(r)?t(r):r),[]):[]},t.formatDate=function(t,e="yyyy-MM-dd"){if(!t)return"";if(t instanceof Date||(t=new Date(t)),isNaN(t.getTime()))return"";const r={"M+":t.getMonth()+1,"d+":t.getDate(),"H+":t.getHours(),"m+":t.getMinutes(),"s+":t.getSeconds(),"q+":Math.floor((t.getMonth()+3)/3),S:t.getMilliseconds()};/(y+)/.test(e)&&(e=e.replace(RegExp.$1,(t.getFullYear()+"").substr(4-RegExp.$1.length)));for(const t in r)new RegExp("("+t+")").test(e)&&(e=e.replace(RegExp.$1,1===RegExp.$1.length?r[t]:("00"+r[t]).substr((""+r[t]).length)));return e},t.getDaysBetween=function(t,e){const r=new Date(t),n=new Date(e);return isNaN(r.getTime())||isNaN(n.getTime())?0:Math.round(Math.abs((n-r)/864e5))},t.getLocalStorage=function(t){if(!t||"string"!=typeof t)return null;try{const e=localStorage.getItem(t);return null===e?null:JSON.parse(e)}catch(e){return localStorage.getItem(t)}},t.getSessionStorage=function(t){if(!t||"string"!=typeof t)return null;try{const e=sessionStorage.getItem(t);return null===e?null:JSON.parse(e)}catch(e){return sessionStorage.getItem(t)}},t.groupArray=function(t,e){return Array.isArray(t)?t.reduce((t,r)=>{let n;return n="function"==typeof e?e(r):"string"==typeof e?r[e]:r,t[n]||(t[n]=[]),t[n].push(r),t},{}):{}},t.isEmail=function(t){return"string"==typeof t&&/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(t)},t.isPhone=function(t){return"string"==typeof t&&/^1[3-9]\d{9}$/.test(t)},t.isToday=function(t){const e=new Date(t),r=new Date;return e.getDate()===r.getDate()&&e.getMonth()===r.getMonth()&&e.getFullYear()===r.getFullYear()},t.maskStr=function(t,e=3,r=4,n="*"){if("string"!=typeof t||0===t.length)return"";const o=t.length;if(e+r>=o)return t;const s=o-e-r,i=n.repeat(s);return t.substring(0,e)+i+t.substring(o-r)},t.removeLocalStorage=function(t){t&&"string"==typeof t&&localStorage.removeItem(t)},t.removeSessionStorage=function(t){t&&"string"==typeof t&&sessionStorage.removeItem(t)},t.setLocalStorage=function(t,e){if(!t||"string"!=typeof t)return!1;try{return localStorage.setItem(t,JSON.stringify(e)),!0}catch(t){return!1}},t.setSessionStorage=function(t,e){if(!t||"string"!=typeof t)return!1;try{return sessionStorage.setItem(t,JSON.stringify(e)),!0}catch(t){return!1}},t.sortArray=function(t,e,r=!0){if(!Array.isArray(t))return[];const n=[...t];return n.sort((t,n)=>{let o=t,s=n;if(e&&(o=t[e],s=n[e]),"number"==typeof o&&"number"==typeof s)return r?o-s:s-o;const i=String(o).toLowerCase(),a=String(s).toLowerCase();return i<a?r?-1:1:i>a?r?1:-1:0}),n},t.trim=function(t){return"string"!=typeof t?"":t.trim()},t.uniqueArray=function(t){return Array.isArray(t)?[...new Set(t)]:[]},t.version="1.0.0",Object.defineProperty(t,"__esModule",{value:!0})});
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jiezhangxin-utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A utility toolkit for Vue 3 projects",
|
|
5
|
+
"main": "dist/jiezhangxin-utils.cjs.js",
|
|
6
|
+
"module": "dist/jiezhangxin-utils.esm.js",
|
|
7
|
+
"browser": "dist/jiezhangxin-utils.umd.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rollup -c",
|
|
10
|
+
"dev": "rollup -c -w",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"utils",
|
|
15
|
+
"tools",
|
|
16
|
+
"vue3",
|
|
17
|
+
"jiezhangxin"
|
|
18
|
+
],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@rollup/plugin-babel": "^5.3.1",
|
|
23
|
+
"@rollup/plugin-commonjs": "^22.0.2",
|
|
24
|
+
"@rollup/plugin-node-resolve": "^14.1.0",
|
|
25
|
+
"rollup": "^2.79.1",
|
|
26
|
+
"rollup-plugin-terser": "^7.0.2"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数组去重
|
|
3
|
+
* @param {Array} arr - 输入数组
|
|
4
|
+
* @returns {Array} 去重后的数组
|
|
5
|
+
*/
|
|
6
|
+
export function uniqueArray(arr) {
|
|
7
|
+
if (!Array.isArray(arr)) {
|
|
8
|
+
return [];
|
|
9
|
+
}
|
|
10
|
+
return [...new Set(arr)];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 数组排序
|
|
15
|
+
* @param {Array} arr - 输入数组
|
|
16
|
+
* @param {string} key - 排序的键名(对象数组时使用)
|
|
17
|
+
* @param {boolean} isAsc - 是否升序
|
|
18
|
+
* @returns {Array} 排序后的数组
|
|
19
|
+
*/
|
|
20
|
+
export function sortArray(arr, key, isAsc = true) {
|
|
21
|
+
if (!Array.isArray(arr)) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 复制数组避免修改原数组
|
|
26
|
+
const newArr = [...arr];
|
|
27
|
+
|
|
28
|
+
newArr.sort((a, b) => {
|
|
29
|
+
let valueA = a;
|
|
30
|
+
let valueB = b;
|
|
31
|
+
|
|
32
|
+
// 如果指定了key,说明是对象数组
|
|
33
|
+
if (key) {
|
|
34
|
+
valueA = a[key];
|
|
35
|
+
valueB = b[key];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 处理数字情况
|
|
39
|
+
if (typeof valueA === 'number' && typeof valueB === 'number') {
|
|
40
|
+
return isAsc ? valueA - valueB : valueB - valueA;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 处理字符串情况
|
|
44
|
+
const strA = String(valueA).toLowerCase();
|
|
45
|
+
const strB = String(valueB).toLowerCase();
|
|
46
|
+
|
|
47
|
+
if (strA < strB) {
|
|
48
|
+
return isAsc ? -1 : 1;
|
|
49
|
+
}
|
|
50
|
+
if (strA > strB) {
|
|
51
|
+
return isAsc ? 1 : -1;
|
|
52
|
+
}
|
|
53
|
+
return 0;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return newArr;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 数组分组
|
|
61
|
+
* @param {Array} arr - 输入数组
|
|
62
|
+
* @param {Function|string} key - 分组依据的函数或键名
|
|
63
|
+
* @returns {Object} 分组后的对象
|
|
64
|
+
*/
|
|
65
|
+
export function groupArray(arr, key) {
|
|
66
|
+
if (!Array.isArray(arr)) {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return arr.reduce((result, item) => {
|
|
71
|
+
let groupKey;
|
|
72
|
+
|
|
73
|
+
if (typeof key === 'function') {
|
|
74
|
+
groupKey = key(item);
|
|
75
|
+
} else if (typeof key === 'string') {
|
|
76
|
+
groupKey = item[key];
|
|
77
|
+
} else {
|
|
78
|
+
groupKey = item;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!result[groupKey]) {
|
|
82
|
+
result[groupKey] = [];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
result[groupKey].push(item);
|
|
86
|
+
return result;
|
|
87
|
+
}, {});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 数组扁平化
|
|
92
|
+
* @param {Array} arr - 输入数组
|
|
93
|
+
* @returns {Array} 扁平化后的数组
|
|
94
|
+
*/
|
|
95
|
+
export function flattenArray(arr) {
|
|
96
|
+
if (!Array.isArray(arr)) {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return arr.reduce((result, item) => {
|
|
101
|
+
return result.concat(Array.isArray(item) ? flattenArray(item) : item);
|
|
102
|
+
}, []);
|
|
103
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 格式化日期
|
|
3
|
+
* @param {Date|string|number} date - 日期
|
|
4
|
+
* @param {string} format - 格式字符串,例如:'yyyy-MM-dd HH:mm:ss'
|
|
5
|
+
* @returns {string} 格式化后的日期字符串
|
|
6
|
+
*/
|
|
7
|
+
export function formatDate(date, format = 'yyyy-MM-dd') {
|
|
8
|
+
if (!date) {
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 处理日期参数
|
|
13
|
+
if (!(date instanceof Date)) {
|
|
14
|
+
date = new Date(date);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 检查日期是否有效
|
|
18
|
+
if (isNaN(date.getTime())) {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const o = {
|
|
23
|
+
'M+': date.getMonth() + 1, // 月份
|
|
24
|
+
'd+': date.getDate(), // 日
|
|
25
|
+
'H+': date.getHours(), // 小时
|
|
26
|
+
'm+': date.getMinutes(), // 分
|
|
27
|
+
's+': date.getSeconds(), // 秒
|
|
28
|
+
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
|
|
29
|
+
'S': date.getMilliseconds() // 毫秒
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
if (/(y+)/.test(format)) {
|
|
33
|
+
format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (const k in o) {
|
|
37
|
+
if (new RegExp('(' + k + ')').test(format)) {
|
|
38
|
+
format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return format;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 获取两个日期之间的天数差
|
|
47
|
+
* @param {Date|string|number} startDate - 开始日期
|
|
48
|
+
* @param {Date|string|number} endDate - 结束日期
|
|
49
|
+
* @returns {number} 天数差
|
|
50
|
+
*/
|
|
51
|
+
export function getDaysBetween(startDate, endDate) {
|
|
52
|
+
const start = new Date(startDate);
|
|
53
|
+
const end = new Date(endDate);
|
|
54
|
+
|
|
55
|
+
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const oneDay = 24 * 60 * 60 * 1000; // 毫秒
|
|
60
|
+
return Math.round(Math.abs((end - start) / oneDay));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 判断是否为今天
|
|
65
|
+
* @param {Date|string|number} date - 日期
|
|
66
|
+
* @returns {boolean} 是否为今天
|
|
67
|
+
*/
|
|
68
|
+
export function isToday(date) {
|
|
69
|
+
const target = new Date(date);
|
|
70
|
+
const today = new Date();
|
|
71
|
+
|
|
72
|
+
return target.getDate() === today.getDate() &&
|
|
73
|
+
target.getMonth() === today.getMonth() &&
|
|
74
|
+
target.getFullYear() === today.getFullYear();
|
|
75
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// 导出日期工具
|
|
2
|
+
export * from './date/dateUtils';
|
|
3
|
+
|
|
4
|
+
// 导出字符串工具
|
|
5
|
+
export * from './string/stringUtils';
|
|
6
|
+
|
|
7
|
+
// 导出数组工具
|
|
8
|
+
export * from './array/arrayUtils';
|
|
9
|
+
|
|
10
|
+
// 导出存储工具
|
|
11
|
+
export * from './storage/storageUtils';
|
|
12
|
+
|
|
13
|
+
// 工具包版本信息
|
|
14
|
+
export const version = '1.0.0';
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从localStorage获取数据
|
|
3
|
+
* @param {string} key - 存储键名
|
|
4
|
+
* @returns {any} 存储的值
|
|
5
|
+
*/
|
|
6
|
+
export function getLocalStorage(key) {
|
|
7
|
+
if (!key || typeof key !== 'string') {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const value = localStorage.getItem(key);
|
|
13
|
+
if (value === null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
// 尝试解析JSON
|
|
17
|
+
return JSON.parse(value);
|
|
18
|
+
} catch (e) {
|
|
19
|
+
// 如果解析失败,直接返回原始值
|
|
20
|
+
return localStorage.getItem(key);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 向localStorage存储数据
|
|
26
|
+
* @param {string} key - 存储键名
|
|
27
|
+
* @param {any} value - 存储的值
|
|
28
|
+
* @returns {boolean} 是否存储成功
|
|
29
|
+
*/
|
|
30
|
+
export function setLocalStorage(key, value) {
|
|
31
|
+
if (!key || typeof key !== 'string') {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
// 转换为JSON字符串存储
|
|
37
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
38
|
+
return true;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 从localStorage删除数据
|
|
46
|
+
* @param {string} key - 存储键名
|
|
47
|
+
*/
|
|
48
|
+
export function removeLocalStorage(key) {
|
|
49
|
+
if (key && typeof key === 'string') {
|
|
50
|
+
localStorage.removeItem(key);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 从sessionStorage获取数据
|
|
56
|
+
* @param {string} key - 存储键名
|
|
57
|
+
* @returns {any} 存储的值
|
|
58
|
+
*/
|
|
59
|
+
export function getSessionStorage(key) {
|
|
60
|
+
if (!key || typeof key !== 'string') {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const value = sessionStorage.getItem(key);
|
|
66
|
+
if (value === null) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
// 尝试解析JSON
|
|
70
|
+
return JSON.parse(value);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
// 如果解析失败,直接返回原始值
|
|
73
|
+
return sessionStorage.getItem(key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 向sessionStorage存储数据
|
|
79
|
+
* @param {string} key - 存储键名
|
|
80
|
+
* @param {any} value - 存储的值
|
|
81
|
+
* @returns {boolean} 是否存储成功
|
|
82
|
+
*/
|
|
83
|
+
export function setSessionStorage(key, value) {
|
|
84
|
+
if (!key || typeof key !== 'string') {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
// 转换为JSON字符串存储
|
|
90
|
+
sessionStorage.setItem(key, JSON.stringify(value));
|
|
91
|
+
return true;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 从sessionStorage删除数据
|
|
99
|
+
* @param {string} key - 存储键名
|
|
100
|
+
*/
|
|
101
|
+
export function removeSessionStorage(key) {
|
|
102
|
+
if (key && typeof key === 'string') {
|
|
103
|
+
sessionStorage.removeItem(key);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 去除字符串首尾空格
|
|
3
|
+
* @param {string} str - 输入字符串
|
|
4
|
+
* @returns {string} 处理后的字符串
|
|
5
|
+
*/
|
|
6
|
+
export function trim(str) {
|
|
7
|
+
if (typeof str !== 'string') {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
return str.trim();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 首字母大写
|
|
15
|
+
* @param {string} str - 输入字符串
|
|
16
|
+
* @returns {string} 处理后的字符串
|
|
17
|
+
*/
|
|
18
|
+
export function capitalize(str) {
|
|
19
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 字符串脱敏处理
|
|
27
|
+
* @param {string} str - 输入字符串
|
|
28
|
+
* @param {number} startLen - 保留开始长度
|
|
29
|
+
* @param {number} endLen - 保留结束长度
|
|
30
|
+
* @param {string} replaceChar - 替换字符
|
|
31
|
+
* @returns {string} 脱敏后的字符串
|
|
32
|
+
*/
|
|
33
|
+
export function maskStr(str, startLen = 3, endLen = 4, replaceChar = '*') {
|
|
34
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const len = str.length;
|
|
39
|
+
if (startLen + endLen >= len) {
|
|
40
|
+
return str;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const maskLen = len - startLen - endLen;
|
|
44
|
+
const mask = replaceChar.repeat(maskLen);
|
|
45
|
+
|
|
46
|
+
return str.substring(0, startLen) + mask + str.substring(len - endLen);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 判断字符串是否为邮箱格式
|
|
51
|
+
* @param {string} str - 输入字符串
|
|
52
|
+
* @returns {boolean} 是否为邮箱
|
|
53
|
+
*/
|
|
54
|
+
export function isEmail(str) {
|
|
55
|
+
if (typeof str !== 'string') {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
|
|
59
|
+
return reg.test(str);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 判断字符串是否为手机号格式
|
|
64
|
+
* @param {string} str - 输入字符串
|
|
65
|
+
* @returns {boolean} 是否为手机号
|
|
66
|
+
*/
|
|
67
|
+
export function isPhone(str) {
|
|
68
|
+
if (typeof str !== 'string') {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
const reg = /^1[3-9]\d{9}$/;
|
|
72
|
+
return reg.test(str);
|
|
73
|
+
}
|