1. 文件
1.1 blob 转为 base64
export const blobToBase64 = (blob, cb) => {
const fileReader = new FileReader();
fileReader.onload = (e) => {
return cb(e.target.result);
};
fileReader.readAsDataURL(blob);
};
1.2 file 转为 base64
export const fileToBase64 = (file, cb) => {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
return cb(e.target.result);
};
};
2. 格式 相关
function strIsJSON(str){
if (typeof str === 'string'){
try {
var obj = JSON.parse(str);
if(typeof obj === 'object' && obj){
return true;
}else{
return false;
}
} catch (e) {
return false;
}
}
return false;
}
function formatJson(str){
if(strIsJSON(str)) {
return JSON.stringify(JSON.parse(str), null, 4);
} else {
return str;
}
}
function transferTime(val, oldUnit, newUnit){
if(!oldUnit || !newUnit){
if(val >= 3600000){
return `${(val/3600000).toFixed(2)}h`;
}else if(val >= 60000){
return `${(val/60000).toFixed(2)}min`;
}else if(val >= 1000){
return `${(val/1000).toFixed(2)}s`;
}
return `${val}ms`;
}
return `${val}ms`;
}
function timestampToTime(timestamp) {
var date = new Date(timestamp);
var Y = date.getFullYear() + '-';
var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
var D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
var h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
var m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
var s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
return Y+M+D+h+m+s;
}
function transferDuration(duration){
let result = '';
if(duration/86400000 > 1){
result += `${Math.floor(duration/86400000)}D`;
}
if((duration%86400000)/3600000 > 1){
result += `${Math.floor((duration%86400000)/3600000)}h`;
}
if(((duration%86400000)%3600000)/60000 > 1){
result += `${Math.floor(((duration%86400000)%3600000)/60000)}m`;
}
if((((duration%86400000)%3600000)%60000)/1000 > 1){
result += `${Math.floor((((duration%86400000)%3600000)%60000)/1000)}s`;
}
if(duration < 60000 && duration > 1000){
result += `${duration%1000}ms`;
}
if(duration < 1000) {
result += `${duration}ms`;
}
return result;
}
3. DOM相关
function siblingElems(elem, siblingClassName) {
var nodes = [];
var _elem = elem;
while ((elem = elem.previousSibling)) {
if (elem.nodeType == 1 && siblingClassName && domNodeToArray(elem.classList).indexOf(siblingClassName) > -1) {
nodes.push(elem);
} else if (elem.nodeType == 1 && !siblingClassName) {
nodes.push(elem);
}
}
var elem = _elem;
while ((elem = elem.nextSibling)) {
if (elem.nodeType == 1 && siblingClassName && domNodeToArray(elem.classList).indexOf(siblingClassName) > -1) {
nodes.push(elem);
} else if (elem.nodeType == 1 && !siblingClassName) {
nodes.push(elem);
}
}
return nodes;
}
function getChildNode(pNode, childClassName) {
if (pNode.className.search(childClassName) !== -1) {
return [pNode];
}
var nodes = pNode.childNodes;
nodes = domNodeToArray(nodes).filter(function (item) {
if (item.nodeType !== 3) {
return item;
}
});
if (nodes.length) {
nodes = nodes.filter(function (item) {
if (item.className.search(childClassName) !== -1) {
return item
}
});
} return nodes;
}
function domNodeToArray(nodes) {
var array = null;
try {
array = Array.prototype.slice.call(elem.classList, 0);
} catch (error) {
array = new Array();
for (var i = 0, len = nodes.length; i < len; i++) {
array.push(nodes[i]);
}
}
return array;
}
4. 随机 相关
export const getRandomNum = (n,m) => {
let result = Math.random()*(m+1-n)+n;
while(result>m) {
result = Math.random()*(m+1-n)+n;
}
return result;
}
export const createRandomCn = (count) => {
const start = parseInt('4e00', 16)
const end = parseInt('9fa5', 16)
let name = ''
for(let i = 0; i < count; i++) {
const cha = Math.floor(Math.random() * (end - start))
name += '\\u' + (start + cha).toString(16)
}
return eval(`'${name}'`)
}
export const getRandomEnStr = (count) => {
let str = '';
let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
for(const i=0; i<count; i++){
pos = Math.round(Math.random() * (arr.length-1));
str += arr[pos];
}
return str;
}