zhl-methods 1.0.3 → 1.0.4

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/js/index.js CHANGED
@@ -13,6 +13,20 @@ export const isPhone = (value) => {
13
13
  }
14
14
  return false;
15
15
  };
16
+ /**
17
+ * 身份证号校验
18
+ * @example isIDCode('410781199909090000') => true
19
+ * @param value 要校验的数据
20
+ * @returns true false 是 否
21
+ */
22
+ export const isIDCode = (value) => {
23
+ if (typeof value == "string") {
24
+ return /^[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)$/.test(
25
+ value
26
+ );
27
+ }
28
+ return false;
29
+ };
16
30
  /**
17
31
  * 对象转url参数
18
32
  * @example toUrlQuery({a:1}) => '?a=1'
@@ -31,7 +45,7 @@ export const toUrlQuery = (query) => {
31
45
  }
32
46
  };
33
47
  /**
34
- * 对象转url参数
48
+ * 将传入的数据转换成千位符
35
49
  * @example numberToPrice(1234.12) => '1,234.12'
36
50
  * @param {number} 要转换的数字
37
51
  * @returns {String} '1,234.12'
@@ -47,6 +61,17 @@ export const numberToPrice = (number) => {
47
61
  console.log(error, "numberToPrice");
48
62
  }
49
63
  };
64
+ /**
65
+ * 千位符格式转换数字
66
+ * @example moneyToNumber('123,456.789') => 123456.789
67
+ * @author zhl
68
+ * @date 2023/11/23
69
+ * @param data 要转换的数据
70
+ * @returns 转换完成的数据
71
+ */
72
+ export const priceToNumber = (data) => {
73
+ return Number(data.replace(/,/gi, ""));
74
+ };
50
75
  /**
51
76
  * 保留两位小数
52
77
  * @example setTwoDecimal(3.144) => '3.14'
@@ -71,7 +96,125 @@ export const setTwoDecimal = (number, isAdjust) => {
71
96
  * @param {max} 最大值
72
97
  * @returns {Number} 98
73
98
  */
74
-
75
99
  export const getRandomNumber = (min, max) => {
76
100
  return Math.floor(Math.random() * (max - min + 1)) + min;
77
101
  };
102
+ /**
103
+ * 检测数据格式
104
+ * @example isType({},object) => true
105
+ * @example isType({}) => object
106
+ * @author zhl
107
+ * @param data 要检测的数据
108
+ * @param type 类型 非必填
109
+ * @returns 不传type 返回数据的类型
110
+ * 传type 返回 true false
111
+ */
112
+ export const isType = (data, type) => {
113
+ if (type) {
114
+ return Object.prototype.toString.call(data) === type;
115
+ }
116
+ return Object.prototype.toString.call(data);
117
+ };
118
+ /**
119
+ * 时间戳转换成时间
120
+ * @example toTypeDate('1701139100',true) => 2023-11-28 10:38:20
121
+ * @author zhl
122
+ * @param date 要转换的数据 Date格式
123
+ * @param isSeconds 是否显示秒
124
+ * @returns 转换完成的数据
125
+ */
126
+ export const toTypeDate = (date, isSeconds) => {
127
+ if (date && typeof date == "number") {
128
+ String(date).length == 10
129
+ ? (date = new Date(date * 1000))
130
+ : (date = new Date(date));
131
+ } else {
132
+ date = new Date();
133
+ }
134
+ const year = date.getFullYear();
135
+ let month = date.getMonth() + 1;
136
+ let ri = date.getDate();
137
+ let hours = date.getHours();
138
+ let minutes = date.getMinutes();
139
+ let seconds = date.getSeconds();
140
+ if (month < 10) month = "0" + month;
141
+ if (ri < 10) ri = "0" + ri;
142
+ if (hours < 10) hours = "0" + hours;
143
+ if (minutes < 10) minutes = "0" + minutes;
144
+ if (seconds < 10) seconds = "0" + seconds;
145
+
146
+ return (
147
+ year +
148
+ "-" +
149
+ month +
150
+ "-" +
151
+ ri +
152
+ " " +
153
+ hours +
154
+ ":" +
155
+ minutes +
156
+ (isSeconds ? ":" + seconds : "")
157
+ );
158
+ };
159
+ /**
160
+ * 转换成整数
161
+ * @example toInt(123.123) => 123
162
+ * @param value 要转成整数的数据 字符串 数字
163
+ * @returns true false 是 否
164
+ */
165
+ export const toInt = (value) => {
166
+ if (typeof value == "number") {
167
+ return ~~value;
168
+ }
169
+ if (typeof value == "string") {
170
+ return parseInt(value, 10);
171
+ }
172
+ return 0;
173
+ };
174
+ /**
175
+ * 复制
176
+ * @example copyText(123) => Promise
177
+ * @param content 要复制的数据
178
+ * @returns Promise 复制成功 失败
179
+ */
180
+ export const copyText = (content) => {
181
+ return new Promise((resolve, reject) => {
182
+ try {
183
+ // 创建输入框元素
184
+ const input = document.createElement("input"); //不会保留文本格式
185
+ //如果要保留文本格式,比如保留换行符,或者多行文本,可以使用 textarea 标签,再配和模板字符串 ` `
186
+ //const input = document.createElement('textarea')
187
+ // 将想要复制的值
188
+ input.value = content;
189
+ // 页面底部追加输入框
190
+ document.body.appendChild(input);
191
+ // 选中输入框
192
+ input.select();
193
+ // 执行浏览器复制命令
194
+ document.execCommand("Copy");
195
+ // 弹出复制成功信息
196
+ //this.$message.success('复制成功');
197
+ // 复制后移除输入框
198
+ input.remove();
199
+ resolve(true);
200
+ } catch (err) {
201
+ reject(err);
202
+ }
203
+ });
204
+ };
205
+ /**
206
+ * 下载文件
207
+ * @example downloadFile('www.baidu.com','文件名称')
208
+ * @param url 要下载的文件 地址
209
+ * @param name 要下载的文件名称
210
+ * @returns 没有返回值
211
+ */
212
+ export const downloadFile = (url, name) => {
213
+ let a = document.createElement("a");
214
+ a.href = url;
215
+ a.download = name;
216
+ a.style.display = "none";
217
+ document.body.appendChild(a);
218
+ a.click();
219
+ a.remove();
220
+ };
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "zhl-methods",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
- "license": "ISC"
6
+ "license": "ISC",
7
+ "dependencies": {
8
+ "vue": "^3.5.12"
9
+ }
7
10
  }
package/vue3/index.js ADDED
@@ -0,0 +1,7 @@
1
+ import { getCurrentInstance } from "vue";
2
+
3
+ export function useEmitter() {
4
+ const internalInstance = getCurrentInstance();
5
+ const emitter = internalInstance.appContext.config.globalProperties.emitter;
6
+ return emitter;
7
+ }