Files
kiss-translator/src/libs/utils.js

305 lines
5.7 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
/**
* 限制数字大小
* @param {*} num
* @param {*} min
* @param {*} max
* @returns
*/
export const limitNumber = (num, min = 0, max = 100) => {
const number = parseInt(num);
if (Number.isNaN(number) || number < min) {
return min;
} else if (number > max) {
return max;
}
return number;
};
export const limitFloat = (num, min = 0, max = 100) => {
const number = parseFloat(num);
if (Number.isNaN(number) || number < min) {
return min;
} else if (number > max) {
return max;
}
return number;
};
2023-07-20 13:45:41 +08:00
/**
* 匹配是否为数组中的值
* @param {*} arr
* @param {*} val
* @returns
*/
export const matchValue = (arr, val) => {
if (arr.length === 0 || arr.includes(val)) {
return val;
}
return arr[0];
};
2023-08-09 17:33:51 +08:00
/**
* 等待
* @param {*} delay
* @returns
*/
export const sleep = (delay) =>
2023-08-31 13:38:06 +08:00
new Promise((resolve) => {
const timer = setTimeout(() => {
clearTimeout(timer);
resolve();
}, delay);
});
2023-08-16 22:13:07 +08:00
/**
* 防抖函数
* @param {*} func
* @param {*} delay
* @returns
*/
export const debounce = (func, delay = 200) => {
2023-09-08 10:52:42 +08:00
let timer = null;
2023-08-16 22:13:07 +08:00
return (...args) => {
timer && clearTimeout(timer);
timer = setTimeout(() => {
func(...args);
2023-09-08 10:32:44 +08:00
clearTimeout(timer);
timer = null;
2023-08-16 22:13:07 +08:00
}, delay);
};
};
2023-08-18 13:16:17 +08:00
2023-09-08 10:32:44 +08:00
/**
* 节流函数
* @param {*} func
* @param {*} delay
* @returns
*/
export const throttle = (func, delay = 200) => {
2023-09-08 10:52:42 +08:00
let timer = null;
let cache = null;
2023-09-08 10:32:44 +08:00
return (...args) => {
if (!timer) {
func(...args);
2023-09-08 10:52:42 +08:00
cache = null;
2023-09-08 10:32:44 +08:00
timer = setTimeout(() => {
2023-09-08 10:52:42 +08:00
if (cache) {
func(...cache);
cache = null;
}
2023-09-08 10:32:44 +08:00
clearTimeout(timer);
timer = null;
}, delay);
2023-09-08 10:52:42 +08:00
} else {
cache = args;
2023-09-08 10:32:44 +08:00
}
};
};
2023-09-07 10:20:08 +08:00
/**
* 判断字符串全是某个字符
* @param {*} s
* @param {*} c
* @param {*} i
* @returns
*/
export const isAllchar = (s, c, i = 0) => {
while (i < s.length) {
if (s[i] !== c) {
return false;
}
i++;
}
return true;
};
2023-08-18 13:16:17 +08:00
/**
* 字符串通配符(*)匹配
* @param {*} s
* @param {*} p
* @returns
*/
export const isMatch = (s, p) => {
if (s.length === 0 || p.length === 0) {
return false;
}
2023-09-05 16:11:33 +08:00
p = "*" + p + "*";
2023-08-18 13:16:17 +08:00
let [sIndex, pIndex] = [0, 0];
let [sRecord, pRecord] = [-1, -1];
while (sIndex < s.length && pRecord < p.length) {
if (p[pIndex] === "*") {
pIndex++;
[sRecord, pRecord] = [sIndex, pIndex];
} else if (s[sIndex] === p[pIndex]) {
sIndex++;
pIndex++;
} else if (sRecord + 1 < s.length) {
sRecord++;
[sIndex, pIndex] = [sRecord, pRecord];
} else {
return false;
}
}
if (p.length === pIndex) {
return true;
}
2023-09-07 10:20:08 +08:00
return isAllchar(p, "*", pIndex);
2023-08-18 13:16:17 +08:00
};
2023-08-20 19:27:29 +08:00
/**
* 类型检查
2023-08-20 23:30:08 +08:00
* @param {*} o
* @returns
2023-08-20 19:27:29 +08:00
*/
export const type = (o) => {
const s = Object.prototype.toString.call(o);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
};
2023-08-20 23:30:08 +08:00
/**
* sha256
* @param {*} text
* @returns
*/
export const sha256 = async (text, salt) => {
const data = new TextEncoder().encode(text + salt);
const digest = await crypto.subtle.digest({ name: "SHA-256" }, data);
return [...new Uint8Array(digest)]
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
};
2023-09-02 14:14:27 +08:00
/**
* 生成随机事件名称
* @returns
*/
export const genEventName = () => btoa(Math.random()).slice(3, 11);
2023-09-07 18:12:45 +08:00
/**
* 判断两个 Set 是否相同
* @param {*} a
* @param {*} b
* @returns
*/
export const isSameSet = (a, b) => {
const s = new Set([...a, ...b]);
return s.size === a.size && s.size === b.size;
};
2023-09-14 10:59:50 +08:00
/**
* 去掉字符串末尾某个字符
* @param {*} s
* @param {*} c
* @param {*} count
* @returns
*/
export const removeEndchar = (s, c, count = 1) => {
let i = s.length;
while (i > s.length - count && s[i - 1] === c) {
i--;
}
return s.slice(0, i);
};
2023-09-14 14:45:22 +08:00
/**
* 匹配字符串及语言标识
* @param {*} str
* @param {*} sign
* @returns
*/
export const matchInputStr = (str, sign) => {
switch (sign) {
case "//":
2024-04-11 10:44:25 +08:00
return str.match(/\/\/([\w-]+)\s+([^]+)/);
2023-09-14 14:45:22 +08:00
case "\\":
2024-04-11 10:44:25 +08:00
return str.match(/\\([\w-]+)\s+([^]+)/);
2023-09-14 14:45:22 +08:00
case "\\\\":
2024-04-11 10:44:25 +08:00
return str.match(/\\\\([\w-]+)\s+([^]+)/);
2023-09-14 14:45:22 +08:00
case ">":
2024-04-11 10:44:25 +08:00
return str.match(/>([\w-]+)\s+([^]+)/);
2023-09-14 14:45:22 +08:00
case ">>":
2024-04-11 10:44:25 +08:00
return str.match(/>>([\w-]+)\s+([^]+)/);
2023-09-14 14:45:22 +08:00
default:
}
2024-04-11 10:44:25 +08:00
return str.match(/\/([\w-]+)\s+([^]+)/);
2023-09-14 14:45:22 +08:00
};
2023-10-24 17:58:37 +08:00
/**
* 判断是否英文单词
* @param {*} str
* @returns
*/
export const isValidWord = (str) => {
const regex = /^[a-zA-Z-]+$/;
return regex.test(str);
};
2024-03-25 18:14:12 +08:00
/**
* blob转为base64
* @param {*} blob
* @returns
*/
export const blobToBase64 = (blob) => {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
};
/**
* 获取html内的文本
* @param {*} htmlStr
* @param {*} skipTag
* @returns
*/
export const getHtmlText = (htmlStr, skipTag = "") => {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlStr, "text/html");
if (skipTag) {
doc.querySelectorAll(skipTag).forEach((el) => el.remove());
}
return doc.body.innerText.trim();
};
/**
* 解析JSON字符串对象
* @param {*} str
* @returns
*/
export const parseJsonObj = (str) => {
if (!str || type(str) !== "string") {
return {};
}
try {
if (str.trim()[0] !== "{") {
str = `{${str}}`;
}
return JSON.parse(str);
} catch (err) {
//
}
return {};
};
/**
* 提取json内容
* @param {*} s
* @returns
*/
export const extractJson = (raw) => {
if (!raw) return "{}";
let s = raw.replace(/^\s*```(?:json)?\s*/i, "").replace(/\s*```\s*$/i, "");
const match = s.match(/\{[\s\S]*\}/);
return match ? match[0] : "{}";
};