Skip to content

Commit d5bbd9d

Browse files
committed
函数优化
1 parent 328dee7 commit d5bbd9d

File tree

3 files changed

+113
-106
lines changed

3 files changed

+113
-106
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# MyUtils
2-
汇总常用的JavaScript工具函数
2+
平时汇总的JavaScript工具函数,在日常开发中可能会用到,希望对大家有所帮助~
33

4-
1.[主工具函数](https://github.com/XmanLin/MyUtils/tree/master/util)
4+
暂时分为大类
5+
1. [主工具函数](https://github.com/XmanLin/MyUtils/blob/master/util/util.js)
56

6-
2.[日期工具函数](https://github.com/XmanLin/MyUtils/tree/master/dateUtil)
7+
2. [日期工具函数](https://github.com/XmanLin/MyUtils/blob/master/dateUtil/dateUtil.js)
8+
9+
3. [浏览器相关工具函数](https://github.com/XmanLin/MyUtils/blob/master/browser/browserUtil.js)
10+
11+
4. [storage工具函数](https://github.com/XmanLin/MyUtils/blob/master/storage/storageUtil.js)
12+
13+
持续更新中...

browser/browserUtil.js

+11
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,15 @@ export function openWindow(url, windowName, width, height) {
271271
eval("try { win.resizeTo(width, height); } catch(e) { }");
272272
win.focus();
273273
}
274+
}
275+
276+
/**
277+
* 自适应页面(rem)
278+
* @param { number } width
279+
*/
280+
export function AutoResponse(width = 750) {
281+
const target = document.documentElement;
282+
target.clientWidth >= 600
283+
? (target.style.fontSize = "80px")
284+
: (target.style.fontSize = target.clientWidth / width * 100 + "px");
274285
}

util/util.js

+92-103
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function fileToBase64String(file, format = ['jpg', 'jpeg', 'png', 'gif'],
4040
let suffix = file.type.split('/')[1].toLowerCase();
4141
let inFormat = false;
4242
for (let i = 0; i < format.length; i++) {
43-
if (suffix == format[i]) {
43+
if (suffix === format[i]) {
4444
inFormat = true;
4545
break;
4646
}
@@ -63,6 +63,85 @@ export function fileToBase64String(file, format = ['jpg', 'jpeg', 'png', 'gif'],
6363
})
6464
}
6565

66+
/**
67+
* B转换到KB,MB,GB并保留两位小数
68+
* @param { number } fileSize
69+
*/
70+
export function formatFileSize(fileSize) {
71+
let temp;
72+
if (fileSize < 1024) {
73+
return fileSize + 'B';
74+
} else if (fileSize < (1024 * 1024)) {
75+
temp = fileSize / 1024;
76+
temp = temp.toFixed(2);
77+
return temp + 'KB';
78+
} else if (fileSize < (1024 * 1024 * 1024)) {
79+
temp = fileSize / (1024 * 1024);
80+
temp = temp.toFixed(2);
81+
return temp + 'MB';
82+
} else {
83+
temp = fileSize / (1024 * 1024 * 1024);
84+
temp = temp.toFixed(2);
85+
return temp + 'GB';
86+
}
87+
}
88+
89+
/**
90+
* base64转file
91+
* @param { base64 } base64
92+
* @param { string } filename 转换后的文件名
93+
*/
94+
export const base64ToFile = (base64, filename )=> {
95+
let arr = base64.split(',');
96+
let mime = arr[0].match(/:(.*?);/)[1];
97+
let suffix = mime.split('/')[1] ;// 图片后缀
98+
let bstr = atob(arr[1]);
99+
let n = bstr.length;
100+
let u8arr = new Uint8Array(n);
101+
while (n--) {
102+
u8arr[n] = bstr.charCodeAt(n)
103+
}
104+
return new File([u8arr], `${filename}.${suffix}`, { type: mime })
105+
};
106+
107+
/**
108+
* base64转blob
109+
* @param { base64 } base64
110+
*/
111+
export const base64ToBlob = base64 => {
112+
let arr = base64.split(','),
113+
mime = arr[0].match(/:(.*?);/)[1],
114+
bstr = atob(arr[1]),
115+
n = bstr.length,
116+
u8arr = new Uint8Array(n);
117+
while (n--) {
118+
u8arr[n] = bstr.charCodeAt(n);
119+
}
120+
return new Blob([u8arr], { type: mime });
121+
};
122+
123+
/**
124+
* blob转file
125+
* @param { blob } blob
126+
* @param { string } fileName
127+
*/
128+
export const blobToFile = (blob, fileName) => {
129+
blob.lastModifiedDate = new Date();
130+
blob.name = fileName;
131+
return blob;
132+
};
133+
134+
/**
135+
* file转base64
136+
* @param { * } file 图片文件
137+
*/
138+
export const fileToBase64 = file => {
139+
let reader = new FileReader();
140+
reader.readAsDataURL(file);
141+
reader.onload = function (e) {
142+
return e.target.result
143+
};
144+
};
66145

67146
/**
68147
* 递归生成树形结构
@@ -152,29 +231,6 @@ export function inArray(item, data) {
152231
}
153232

154233

155-
/**
156-
* B转换到KB,MB,GB并保留两位小数
157-
* @param { number } fileSize
158-
*/
159-
export function formatFileSize(fileSize) {
160-
if (fileSize < 1024) {
161-
return fileSize + 'B';
162-
} else if (fileSize < (1024 * 1024)) {
163-
var temp = fileSize / 1024;
164-
temp = temp.toFixed(2);
165-
return temp + 'KB';
166-
} else if (fileSize < (1024 * 1024 * 1024)) {
167-
var temp = fileSize / (1024 * 1024);
168-
temp = temp.toFixed(2);
169-
return temp + 'MB';
170-
} else {
171-
var temp = fileSize / (1024 * 1024 * 1024);
172-
temp = temp.toFixed(2);
173-
return temp + 'GB';
174-
}
175-
}
176-
177-
178234
/**
179235
* Windows根据详细版本号判断当前系统名称
180236
* @param { string } osVersion
@@ -262,10 +318,11 @@ export function debounce(func,wait,immediate) {
262318
* @param { number } type 1 表时间戳版,2 表定时器版
263319
*/
264320
export function throttle(func, wait ,type) {
321+
let previous, timeout;
265322
if(type===1){
266-
var previous = 0;
323+
previous = 0;
267324
}else if(type===2){
268-
var timeout;
325+
timeout = null;
269326
}
270327
return function() {
271328
let context = this;
@@ -295,8 +352,8 @@ export function throttle(func, wait ,type) {
295352
* @param {*} target
296353
*/
297354
export function type(target) {
298-
var ret = typeof(target);
299-
var template = {
355+
let ret = typeof(target);
356+
let template = {
300357
"[object Array]": "array",
301358
"[object Object]":"object",
302359
"[object Number]":"number - object",
@@ -307,7 +364,7 @@ export function type(target) {
307364
if(target === null) {
308365
return 'null';
309366
}else if(ret == "object"){
310-
var str = Object.prototype.toString.call(target);
367+
let str = Object.prototype.toString.call(target);
311368
return template[str];
312369
}else{
313370
return ret;
@@ -322,44 +379,32 @@ export function type(target) {
322379
export const RandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
323380

324381

325-
/**
326-
* 自适应页面(rem)
327-
* @param { number } width
328-
*/
329-
export function AutoResponse(width = 750) {
330-
const target = document.documentElement;
331-
target.clientWidth >= 600
332-
? (target.style.fontSize = "80px")
333-
: (target.style.fontSize = target.clientWidth / width * 100 + "px");
334-
}
335-
336-
337382
/**
338383
* 数组乱序
339384
* @param {array} arr
340385
*/
341-
export function shuffle(arr) {
386+
export function arrScrambling(arr) {
342387
let array = arr;
343388
let index = array.length;
344-
345389
while (index) {
346390
index -= 1;
347391
let randomIndex = Math.floor(Math.random() * index);
348392
let middleware = array[index];
349393
array[index] = array[randomIndex];
350394
array[randomIndex] = middleware
351395
}
352-
353396
return array
354397
}
355398

399+
356400
/**
357401
* 数组交集
358402
* @param { array} arr1
359403
* @param { array } arr2
360404
*/
361405
export const similarity = (arr1, arr2) => arr1.filter(v => arr2.includes(v));
362406

407+
363408
/**
364409
* 数组中某元素出现的次数
365410
* @param { array } arr
@@ -418,7 +463,7 @@ export function division(num1,num2){
418463
}
419464

420465
/**
421-
* 乘法函数
466+
* 乘法函数(精度丢失问题)
422467
* @param { number } num1
423468
* @param { number } num2
424469
*/
@@ -493,7 +538,7 @@ export function trim(str, type = 1) {
493538
/**
494539
* 大小写转换
495540
* @param { string } str 待转换的字符串
496-
* @param { number } type 1-全大写 2-全小写 3-首字母大写
541+
* @param { number } type 1-全大写 2-全小写 3-首字母大写 其他-不转换
497542
*/
498543

499544
export function turnCase(str, type) {
@@ -519,7 +564,7 @@ export function hexColor() {
519564
let str = '#';
520565
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
521566
for (let i = 0; i < 6; i++) {
522-
let index = Number.parseInt(Math.random() * 16);
567+
let index = Number.parseInt((Math.random() * 16).toString());
523568
str += arr[index]
524569
}
525570
return str;
@@ -596,62 +641,6 @@ export const outOfNum = (val, maxNum) =>{
596641
};
597642

598643

599-
/**
600-
* base64转file
601-
* @param { base64 } base64
602-
* @param { string } filename 转换后的文件名
603-
*/
604-
export const base64ToFile = (base64, filename )=> {
605-
let arr = base64.split(',');
606-
let mime = arr[0].match(/:(.*?);/)[1];
607-
let suffix = mime.split('/')[1] ;// 图片后缀
608-
let bstr = atob(arr[1]);
609-
let n = bstr.length;
610-
let u8arr = new Uint8Array(n);
611-
while (n--) {
612-
u8arr[n] = bstr.charCodeAt(n)
613-
}
614-
return new File([u8arr], `${filename}.${suffix}`, { type: mime })
615-
};
616-
617-
/**
618-
* base64转blob
619-
* @param { base64 } base64
620-
*/
621-
export const base64ToBlob = base64 => {
622-
let arr = base64.split(','),
623-
mime = arr[0].match(/:(.*?);/)[1],
624-
bstr = atob(arr[1]),
625-
n = bstr.length,
626-
u8arr = new Uint8Array(n);
627-
while (n--) {
628-
u8arr[n] = bstr.charCodeAt(n);
629-
}
630-
return new Blob([u8arr], { type: mime });
631-
};
632-
633-
/**
634-
* blob转file
635-
* @param { blob } blob
636-
* @param { string } fileName
637-
*/
638-
export const blobToFile = (blob, fileName) => {
639-
blob.lastModifiedDate = new Date();
640-
blob.name = fileName;
641-
return blob;
642-
};
643-
644-
/**
645-
* file转base64
646-
* @param { * } file 图片文件
647-
*/
648-
export const fileToBase64 = file => {
649-
let reader = new FileReader();
650-
reader.readAsDataURL(file);
651-
reader.onload = function (e) {
652-
return e.target.result
653-
};
654-
};
655644

656645

657646

0 commit comments

Comments
 (0)