common-utils-kit 1.1.4 → 1.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "common-utils-kit",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/files.js CHANGED
@@ -107,3 +107,131 @@ export function downloadFiles(url, filename) {
107
107
  xhr.send();
108
108
  });
109
109
  }
110
+ /**
111
+ * File转Base64
112
+ * @param {File} file - File对象格式文件数据
113
+ * @example
114
+ * fileToBase64(file).then((base64) => {
115
+ * console.log(base64); // base64格式的文件
116
+ * }).catch((error) => {
117
+ * console.error(error); // 转换失败或其他错误信息
118
+ * });
119
+ */
120
+ export function fileToBase64(file) {
121
+ return new Promise((resolve, reject) => {
122
+ const reader = new FileReader();
123
+ reader.readAsDataURL(file);
124
+ this.aa = reader.result;
125
+ reader.onload = () => resolve(reader.result);
126
+ reader.onerror = (error) => reject(error);
127
+ });
128
+ }
129
+ /**
130
+ * Base64转File
131
+ * @param {String} base64 - base64格式文件数据
132
+ * @example
133
+ * base64ToFile(base64).then((file) => {
134
+ * console.log(file); // File格式的文件
135
+ * }).catch((error) => {
136
+ * console.error(error); // 转换失败或其他错误信息
137
+ * });
138
+ */
139
+ export function base64ToFile(base64, filename = "file") { //将base64转换为文件
140
+ return new Promise((resolve, reject) => {
141
+ const arr = base64.split(',')
142
+ const mime = arr[0].match(/:(.*?);/)[1]
143
+ const suffix = mime.split('/')[1]
144
+ const bstr = atob(arr[1])
145
+ let n = bstr.length
146
+ const u8arr = new Uint8Array(n)
147
+ while (n--) {
148
+ u8arr[n] = bstr.charCodeAt(n)
149
+ }
150
+ const fileData = new File([u8arr], `${filename}.${suffix}`, { type: mime })
151
+ resolve(fileData)
152
+ })
153
+ }
154
+ /**
155
+ * Base64转Blob
156
+ * @param {String} base64 - base64格式文件数据
157
+ * @example
158
+ * base64ToFile(base64).then((blob) => {
159
+ * console.log(blob); // blob格式的文件
160
+ * }).catch((error) => {
161
+ * console.error(error); // 转换失败或其他错误信息
162
+ * });
163
+ */
164
+ export function base64ToFileBlob(dataurl) {
165
+ return new Promise((resolve, reject) => {
166
+ var arr = dataurl.split(','),
167
+ mime = arr[0].match(/:(.*?);/)[1],
168
+ bstr = atob(arr[1]),
169
+ n = bstr.length,
170
+ u8arr = new Uint8Array(n)
171
+ while (n--) {
172
+ u8arr[n] = bstr.charCodeAt(n)
173
+ }
174
+ const blobData = new Blob([u8arr], { type: mime })
175
+ resolve(blobData)
176
+ })
177
+ }
178
+ // 使用canvas压缩图片
179
+ export function uploadCompress(file, config) {
180
+ return new Promise((resolve, reject) => {
181
+ const image = new Image();
182
+ image.src = URL.createObjectURL(file);
183
+ image.onload = () => {
184
+ let resultBlob = "";
185
+ // 压缩图片
186
+ resultBlob = compressUpload(image, file, config);
187
+ resolve(resultBlob);
188
+ };
189
+ image.onerror = (err) => { reject(err) };
190
+ /* 图片压缩-canvas压缩 */
191
+ function compressUpload(image, file, compressConfig={}) {
192
+ const canvas = document.createElement("canvas");
193
+ const ctx = canvas.getContext("2d");
194
+ let width = compressConfig.width || image.width;
195
+ let height = compressConfig.height || image.height;
196
+
197
+ // 只设置宽度时,等比计算高度
198
+ if (compressConfig.width && !compressConfig.height) {
199
+ height = (compressConfig.width / image.width) * image.height;
200
+ }
201
+ // 只设置高度时,等比计算宽度
202
+ if (compressConfig.height && !compressConfig.width) {
203
+ width = (compressConfig.height / image.height) * image.width;
204
+ }
205
+
206
+ canvas.width = width;
207
+ canvas.height = height;
208
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
209
+ ctx.drawImage(image, 0, 0, width, height);
210
+
211
+ // 进行最小压缩0.1
212
+ const compressData = canvas.toDataURL(
213
+ file.type || "image/jpeg",
214
+ compressConfig.rate || 0.5
215
+ );
216
+ // base64转Blob
217
+ const blobImg = dataURItoBlob(compressData);
218
+ console.log(" ", (blobImg.size / 1024 / 1024).toFixed(2));
219
+ return blobImg;
220
+ }
221
+ /* 图片格式转换——base64转Blob对象 */
222
+ function dataURItoBlob(data) {
223
+ let byteString;
224
+ if (data.split(",")[0].indexOf("base64") >= 0) {
225
+ byteString = data.split(",")[1];
226
+ } else {
227
+ byteString = unescape(data.split(",")[1]);
228
+ }
229
+ const mimeString = data.split(",")[0].split(":")[1].split(";")[0];
230
+ const ia = new Uint8Array(byteString.length);
231
+ for (let i = 0; i < byteString.length; i += 1) {
232
+ ia[i] = byteString.charCodeAt(i);
233
+ }
234
+ return new Blob([ia], { type: mimeString });
235
+ }
236
+ });
237
+ }
package/src/format.js CHANGED
@@ -126,9 +126,7 @@ export function flatToTree(flatArray, rootValue = null, bind = "pid") {
126
126
  flatArray.forEach(item => {
127
127
  if (item[bind] === rootValue) {
128
128
  const children = flatToTree(flatArray, item.id);
129
- if (children.length) {
130
- item.children = children;
131
- }
129
+ item.children = children && children.length ? children : []
132
130
  tree.push(item);
133
131
  }
134
132
  });
package/src/tool.js CHANGED
@@ -19,26 +19,55 @@ export function debounce(time = 500) {
19
19
  })
20
20
  }
21
21
 
22
-
23
- let wait = null
24
22
  /**
25
23
  * @description 节流
26
24
  * @param {number} time 每time秒调用一次 默认0.5秒
25
+ * @param {boolean} immediately 是否立即执行一次 默认true
27
26
  * @example
28
- * throttle(time).then(res => {
27
+ * throttle(time,immediately).then(res => {
29
28
  * 执行方法
30
29
  * })
31
30
  */
32
- export function throttle(time = 500) { //防抖 // 默认0.2秒后执行事件
31
+
32
+ let wait = null;
33
+ export function throttle(time = 500, immediately=true) {
33
34
  return new Promise((resolve) => {
34
- if (wait) { return }
35
- wait = setTimeout(() => {
36
- resolve(true)
37
- wait = null
38
- }, time)
39
- })
35
+ if (immediately && (!wait || Date.now() - wait >= time)) {
36
+ resolve(true);
37
+ wait = Date.now();
38
+ setTimeout(() => {
39
+ wait = null;
40
+ }, time);
41
+ }
42
+ if (!immediately && !wait) {
43
+ wait = setTimeout(() => {
44
+ resolve(true);
45
+ wait = null;
46
+ }, time);
47
+ }
48
+ });
40
49
  }
41
50
 
51
+
52
+ // /**
53
+ // * @description 节流
54
+ // * @param {number} time 每time秒调用一次 默认0.5秒
55
+ // * @example
56
+ // * throttle(time).then(res => {
57
+ // * 执行方法
58
+ // * })
59
+ // */
60
+ // let wait = null
61
+ // export function throttle(time = 500) { //防抖 // 默认0.2秒后执行事件
62
+ // return new Promise((resolve) => {
63
+ // if (wait) { return }
64
+ // wait = setTimeout(() => {
65
+ // resolve(true)
66
+ // wait = null
67
+ // }, time)
68
+ // })
69
+ // }
70
+
42
71
  /**
43
72
  * @description 进行阻塞延时,以达到可以简写代码的目的
44
73
  * @param {number} value 堵塞时间 单位ms 毫秒