zhl-easy 0.0.1 → 0.0.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/LICENSE +21 -0
- package/README.md +57 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/utils/index.d.ts +99 -0
- package/dist/utils/index.js +221 -0
- package/package.json +48 -4
- package/VERSION.md +0 -10
- package/index.ts +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zhl-easy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
# zhl-easy
|
|
2
|
-
|
|
3
|
-
JS
|
|
1
|
+
# zhl-easy
|
|
2
|
+
|
|
3
|
+
JS 工具类以及 `vue` `react` 组件。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install zhl-easy
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { isPhone, toUrlQuery } from "zhl-easy";
|
|
15
|
+
// 或者只引入工具函数
|
|
16
|
+
import { debounce } from "zhl-easy/utils";
|
|
17
|
+
|
|
18
|
+
isPhone("15093676561"); // true
|
|
19
|
+
toUrlQuery({ a: 1 }); // '?a=1'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 工具函数
|
|
23
|
+
|
|
24
|
+
| 方法 | 说明 |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `tel` | 手机号正则 |
|
|
27
|
+
| `idCard` | 身份证号正则 |
|
|
28
|
+
| `isPhone(value)` | 手机号校验 |
|
|
29
|
+
| `isIDCode(value)` | 身份证号校验 |
|
|
30
|
+
| `toUrlQuery(query)` | 对象转 url 参数 |
|
|
31
|
+
| `urlQueryToObject(str)` | url 参数转对象 |
|
|
32
|
+
| `numberToPrice(number)` | 数字转千位符 |
|
|
33
|
+
| `priceToNumber(data)` | 千位符转数字 |
|
|
34
|
+
| `setTwoDecimal(number, isAdjust)` | 保留两位小数 |
|
|
35
|
+
| `getRandomNumber(min, max)` | 生成区间内随机数 |
|
|
36
|
+
| `isType(data, type?)` | 检测数据类型 |
|
|
37
|
+
| `toInt(value)` | 转换成整数 |
|
|
38
|
+
| `copyText(content)` | 复制文本(浏览器环境) |
|
|
39
|
+
| `downloadFile(url, name)` | 下载文件(浏览器环境) |
|
|
40
|
+
| `debounce(fn, wait)` | 防抖 |
|
|
41
|
+
| `throttle(fn, threshhold, scope?)` | 节流 |
|
|
42
|
+
|
|
43
|
+
## 开发
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install # 安装依赖
|
|
47
|
+
npm run build # 编译到 dist/
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 发布
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm login
|
|
54
|
+
npm publish
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
发布前会自动执行 `npm run build`(见 `prepublishOnly`)。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./utils/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export declare const tel: RegExp;
|
|
2
|
+
export declare const idCard: RegExp;
|
|
3
|
+
/**
|
|
4
|
+
* 手机号校验
|
|
5
|
+
* @example isPhone('15093676561') => true
|
|
6
|
+
* @param value 要校验的数据
|
|
7
|
+
* @returns true false 是 否
|
|
8
|
+
*/
|
|
9
|
+
export declare const isPhone: (value: number | string) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 身份证号校验
|
|
12
|
+
* @example isIDCode('410781199909090000') => true
|
|
13
|
+
* @param value 要校验的数据
|
|
14
|
+
* @returns true false 是 否
|
|
15
|
+
*/
|
|
16
|
+
export declare const isIDCode: (value: number | string) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* 对象转url参数
|
|
19
|
+
* @example toUrlQuery({a:1}) => '?a=1'
|
|
20
|
+
* @param query 要转换的对象
|
|
21
|
+
*/
|
|
22
|
+
export declare const toUrlQuery: (query: Record<string, unknown>) => string;
|
|
23
|
+
/**
|
|
24
|
+
* url参数转对象
|
|
25
|
+
* @example urlQueryToObject('?a=1') => {a:1}
|
|
26
|
+
* @param str 要转换的url参数
|
|
27
|
+
*/
|
|
28
|
+
export declare const urlQueryToObject: (str: string) => Record<string, string>;
|
|
29
|
+
/**
|
|
30
|
+
* 将传入的数据转换成千位符
|
|
31
|
+
* @example numberToPrice(1234.12) => '1,234.12'
|
|
32
|
+
* @param number 要转换的数字
|
|
33
|
+
*/
|
|
34
|
+
export declare const numberToPrice: (number: number | string) => string;
|
|
35
|
+
/**
|
|
36
|
+
* 千位符格式转换数字
|
|
37
|
+
* @example moneyToNumber('123,456.789') => 123456.789
|
|
38
|
+
* @param data 要转换的数据
|
|
39
|
+
*/
|
|
40
|
+
export declare const priceToNumber: (data: string) => number;
|
|
41
|
+
/**
|
|
42
|
+
* 保留两位小数
|
|
43
|
+
* @example setTwoDecimal(3.144) => '3.14'
|
|
44
|
+
* @param number 要保留两位小数的数字
|
|
45
|
+
* @param isAdjust 是否四舍五入
|
|
46
|
+
*/
|
|
47
|
+
export declare const setTwoDecimal: (number: number | string, isAdjust: boolean) => string | number;
|
|
48
|
+
/**
|
|
49
|
+
* 生成随机数
|
|
50
|
+
* @example getRandomNumber(1,100) => 98
|
|
51
|
+
* @param min 最小值
|
|
52
|
+
* @param max 最大值
|
|
53
|
+
*/
|
|
54
|
+
export declare const getRandomNumber: (min: number, max: number) => number;
|
|
55
|
+
/**
|
|
56
|
+
* 检测数据格式
|
|
57
|
+
* @example isType({},Object) => true
|
|
58
|
+
* @example isType({}) => Object
|
|
59
|
+
* @param data 要检测的数据
|
|
60
|
+
* @param type 类型 非必填
|
|
61
|
+
* @returns 不传type 返回数据的类型 传type 返回 true false
|
|
62
|
+
*/
|
|
63
|
+
export declare const isType: (data: unknown, type?: string) => boolean | string;
|
|
64
|
+
/**
|
|
65
|
+
* 转换成整数
|
|
66
|
+
* @example toInt(123.123) => 123
|
|
67
|
+
* @param value 要转成整数的数据
|
|
68
|
+
* @returns true false 是 否
|
|
69
|
+
*/
|
|
70
|
+
export declare const toInt: (value: number | string) => number;
|
|
71
|
+
/**
|
|
72
|
+
* 复制
|
|
73
|
+
* @example copyText(123) => Promise
|
|
74
|
+
* @param content 要复制的数据
|
|
75
|
+
* @returns Promise true false 复制成功 失败
|
|
76
|
+
*/
|
|
77
|
+
export declare const copyText: (content: any) => Promise<boolean>;
|
|
78
|
+
/**
|
|
79
|
+
* 下载文件
|
|
80
|
+
* @example downloadFile('www.baidu.com','文件名称')
|
|
81
|
+
* @param url 要下载的文件 地址
|
|
82
|
+
* @param name 要下载的文件名称
|
|
83
|
+
*/
|
|
84
|
+
export declare const downloadFile: (url: string, name: string) => void;
|
|
85
|
+
/**
|
|
86
|
+
* 防抖
|
|
87
|
+
* @example debounce(() => {}, 250) => Function
|
|
88
|
+
* @param fn 防抖的函数
|
|
89
|
+
* @param wait 防抖时间 毫秒
|
|
90
|
+
*/
|
|
91
|
+
export declare const debounce: (fn: (...args: unknown[]) => void, wait?: number) => (this: unknown, ...args: unknown[]) => void;
|
|
92
|
+
/**
|
|
93
|
+
* 节流
|
|
94
|
+
* @example throttle(()=>{}) => Function
|
|
95
|
+
* @param fn 节流的函数
|
|
96
|
+
* @param threshhold 节流时间
|
|
97
|
+
* @param scope this
|
|
98
|
+
*/
|
|
99
|
+
export declare const throttle: (fn: (...args: unknown[]) => void, threshhold?: number, scope?: unknown) => (this: unknown, ...args: unknown[]) => void;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// 手机号
|
|
2
|
+
export const tel = /^1[3456789]\d{9}$/;
|
|
3
|
+
// 身份证
|
|
4
|
+
export const idCard = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
|
|
5
|
+
/**
|
|
6
|
+
* 手机号校验
|
|
7
|
+
* @example isPhone('15093676561') => true
|
|
8
|
+
* @param value 要校验的数据
|
|
9
|
+
* @returns true false 是 否
|
|
10
|
+
*/
|
|
11
|
+
export const isPhone = (value) => {
|
|
12
|
+
if (typeof value == "number") {
|
|
13
|
+
value = String(value);
|
|
14
|
+
}
|
|
15
|
+
return tel.test(value);
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 身份证号校验
|
|
19
|
+
* @example isIDCode('410781199909090000') => true
|
|
20
|
+
* @param value 要校验的数据
|
|
21
|
+
* @returns true false 是 否
|
|
22
|
+
*/
|
|
23
|
+
export const isIDCode = (value) => {
|
|
24
|
+
if (typeof value == "string") {
|
|
25
|
+
return idCard.test(value);
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* 对象转url参数
|
|
31
|
+
* @example toUrlQuery({a:1}) => '?a=1'
|
|
32
|
+
* @param query 要转换的对象
|
|
33
|
+
*/
|
|
34
|
+
export const toUrlQuery = (query) => {
|
|
35
|
+
let str = "?";
|
|
36
|
+
Object.keys(query).forEach((key) => {
|
|
37
|
+
str += `${key}=${query[key] ? String(query[key]) : ""}&`;
|
|
38
|
+
});
|
|
39
|
+
return str.slice(0, -1);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* url参数转对象
|
|
43
|
+
* @example urlQueryToObject('?a=1') => {a:1}
|
|
44
|
+
* @param str 要转换的url参数
|
|
45
|
+
*/
|
|
46
|
+
export const urlQueryToObject = (str) => {
|
|
47
|
+
const params = new URLSearchParams(str);
|
|
48
|
+
const obj = {};
|
|
49
|
+
params.forEach((item, index) => {
|
|
50
|
+
obj[index] = item;
|
|
51
|
+
});
|
|
52
|
+
return obj;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* 将传入的数据转换成千位符
|
|
56
|
+
* @example numberToPrice(1234.12) => '1,234.12'
|
|
57
|
+
* @param number 要转换的数字
|
|
58
|
+
*/
|
|
59
|
+
export const numberToPrice = (number) => {
|
|
60
|
+
return Number(number).toLocaleString("zh", {
|
|
61
|
+
minimumFractionDigits: 2,
|
|
62
|
+
maximumFractionDigits: 2,
|
|
63
|
+
style: "decimal",
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* 千位符格式转换数字
|
|
68
|
+
* @example moneyToNumber('123,456.789') => 123456.789
|
|
69
|
+
* @param data 要转换的数据
|
|
70
|
+
*/
|
|
71
|
+
export const priceToNumber = (data) => {
|
|
72
|
+
return Number(data.replace(/,/gi, ""));
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* 保留两位小数
|
|
76
|
+
* @example setTwoDecimal(3.144) => '3.14'
|
|
77
|
+
* @param number 要保留两位小数的数字
|
|
78
|
+
* @param isAdjust 是否四舍五入
|
|
79
|
+
*/
|
|
80
|
+
export const setTwoDecimal = (number, isAdjust) => {
|
|
81
|
+
if (isAdjust) {
|
|
82
|
+
return Math.round(Number(number) * Math.pow(10, 2)) / Math.pow(10, 2);
|
|
83
|
+
}
|
|
84
|
+
return (Math.floor(Number(number) * 100) / 100).toFixed(2);
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* 生成随机数
|
|
88
|
+
* @example getRandomNumber(1,100) => 98
|
|
89
|
+
* @param min 最小值
|
|
90
|
+
* @param max 最大值
|
|
91
|
+
*/
|
|
92
|
+
export const getRandomNumber = (min, max) => {
|
|
93
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* 检测数据格式
|
|
97
|
+
* @example isType({},Object) => true
|
|
98
|
+
* @example isType({}) => Object
|
|
99
|
+
* @param data 要检测的数据
|
|
100
|
+
* @param type 类型 非必填
|
|
101
|
+
* @returns 不传type 返回数据的类型 传type 返回 true false
|
|
102
|
+
*/
|
|
103
|
+
export const isType = (data, type) => {
|
|
104
|
+
const regex = /\[object (\w+)\]/;
|
|
105
|
+
const str = Object.prototype.toString.call(data);
|
|
106
|
+
const match = regex.exec(str);
|
|
107
|
+
if (!match)
|
|
108
|
+
return "";
|
|
109
|
+
return type ? match[1] === type : match[1];
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* 转换成整数
|
|
113
|
+
* @example toInt(123.123) => 123
|
|
114
|
+
* @param value 要转成整数的数据
|
|
115
|
+
* @returns true false 是 否
|
|
116
|
+
*/
|
|
117
|
+
export const toInt = (value) => {
|
|
118
|
+
if (typeof value == "number") {
|
|
119
|
+
return ~~value;
|
|
120
|
+
}
|
|
121
|
+
if (typeof value == "string") {
|
|
122
|
+
return parseInt(value, 10);
|
|
123
|
+
}
|
|
124
|
+
return 0;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* 复制
|
|
128
|
+
* @example copyText(123) => Promise
|
|
129
|
+
* @param content 要复制的数据
|
|
130
|
+
* @returns Promise true false 复制成功 失败
|
|
131
|
+
*/
|
|
132
|
+
export const copyText = (content) => {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
try {
|
|
135
|
+
// 创建输入框元素
|
|
136
|
+
const input = document.createElement("input"); //不会保留文本格式
|
|
137
|
+
//如果要保留文本格式,比如保留换行符,或者多行文本,可以使用 textarea 标签,再配和模板字符串 ` `
|
|
138
|
+
//const input = document.createElement('textarea')
|
|
139
|
+
// 将想要复制的值
|
|
140
|
+
input.value = content;
|
|
141
|
+
// 页面底部追加输入框
|
|
142
|
+
document.body.appendChild(input);
|
|
143
|
+
// 选中输入框
|
|
144
|
+
input.select();
|
|
145
|
+
// 执行浏览器复制命令
|
|
146
|
+
document.execCommand("Copy");
|
|
147
|
+
// 弹出复制成功信息
|
|
148
|
+
//this.$message.success('复制成功');
|
|
149
|
+
// 复制后移除输入框
|
|
150
|
+
input.remove();
|
|
151
|
+
resolve(true);
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
reject(false);
|
|
155
|
+
console.log("复制失败", err);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* 下载文件
|
|
161
|
+
* @example downloadFile('www.baidu.com','文件名称')
|
|
162
|
+
* @param url 要下载的文件 地址
|
|
163
|
+
* @param name 要下载的文件名称
|
|
164
|
+
*/
|
|
165
|
+
export const downloadFile = (url, name) => {
|
|
166
|
+
let a = document.createElement("a");
|
|
167
|
+
a.href = url;
|
|
168
|
+
a.download = name;
|
|
169
|
+
a.target = "_blank";
|
|
170
|
+
a.style.display = "none";
|
|
171
|
+
document.body.appendChild(a);
|
|
172
|
+
a.click();
|
|
173
|
+
a.remove();
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* 防抖
|
|
177
|
+
* @example debounce(() => {}, 250) => Function
|
|
178
|
+
* @param fn 防抖的函数
|
|
179
|
+
* @param wait 防抖时间 毫秒
|
|
180
|
+
*/
|
|
181
|
+
export const debounce = (fn, wait = 250) => {
|
|
182
|
+
let timeout = null;
|
|
183
|
+
return function (...args) {
|
|
184
|
+
if (timeout)
|
|
185
|
+
clearTimeout(timeout);
|
|
186
|
+
let callNow = !timeout;
|
|
187
|
+
timeout = setTimeout(() => {
|
|
188
|
+
timeout = null;
|
|
189
|
+
}, wait);
|
|
190
|
+
if (callNow)
|
|
191
|
+
fn.apply(this, args);
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* 节流
|
|
196
|
+
* @example throttle(()=>{}) => Function
|
|
197
|
+
* @param fn 节流的函数
|
|
198
|
+
* @param threshhold 节流时间
|
|
199
|
+
* @param scope this
|
|
200
|
+
*/
|
|
201
|
+
export const throttle = (fn, threshhold = 250, scope) => {
|
|
202
|
+
let last = null;
|
|
203
|
+
let timer = null;
|
|
204
|
+
return function (...args) {
|
|
205
|
+
let context = scope || this;
|
|
206
|
+
let now = +new Date();
|
|
207
|
+
if (last !== null && now < last + threshhold) {
|
|
208
|
+
// 如果在节流时间内,则取消之前的延迟调用
|
|
209
|
+
clearTimeout(timer);
|
|
210
|
+
// 设定一个新的延迟调用
|
|
211
|
+
timer = setTimeout(function () {
|
|
212
|
+
last = now;
|
|
213
|
+
fn.apply(context, args);
|
|
214
|
+
}, threshhold);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
last = now;
|
|
218
|
+
fn.apply(context, args);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
};
|
package/package.json
CHANGED
|
@@ -1,4 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "zhl-easy",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "zhl-easy",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "JS 工具函数库,以及 Vue / React 组件",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./utils": {
|
|
15
|
+
"types": "./dist/utils/index.d.ts",
|
|
16
|
+
"import": "./dist/utils/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"VERSION.md"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.json",
|
|
27
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
28
|
+
"prepublishOnly": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"zhl-easy",
|
|
32
|
+
"utils",
|
|
33
|
+
"tools",
|
|
34
|
+
"typescript"
|
|
35
|
+
],
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"typescript": "^5.9.3"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "gitee",
|
|
42
|
+
"url": "git+https://github.com/你的用户名/zhl-easy.git"
|
|
43
|
+
},
|
|
44
|
+
"author": {
|
|
45
|
+
"name": "zhl",
|
|
46
|
+
"email": "tuochu@qq.com"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/VERSION.md
DELETED
package/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
console.log('1')
|