Files
EasytierGame/utils/index.ts
T
黑先森 b94d5de463 优化多个界面的数据同步问题,增加功能,修复bug
高级选项-增加默认协议配置
高级选项-增加自定义监听相关配置
修复创建子窗口多次触发监听的问题
config.json增加对自定义协议配置
优化高级选项文本说明
2024-12-03 18:07:42 +08:00

120 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { open } from "@tauri-apps/plugin-shell";
export const ENV = import.meta.env;
//防抖
export const bounce = (time = 3000) => {
let bounceTimer: NodeJS.Timeout | null = null;
return (cb: Function) => {
bounceTimer && clearTimeout(bounceTimer);
bounceTimer = setTimeout(() => {
cb();
}, time);
};
};
/**
*
* @param {number} size byte number
* @param {number} fixed 小数点后几位
*/
export const SizeFormat = (size: number, fixed: number = 1) => {
const _unit = ["B", "KB", "MB", "GB", "TB"];
const max = _unit.length - 1;
const endAdd = 8; //取小数点后 fixed + endAdd 位小数,目的是尽量保证不发生进位,endAdd越大,越可以保证不发生进位
const c = 1024; // 1000或者1024
let n = 0;
for (; n < max; n++) {
if (size < c) {
break;
}
size = size / c;
}
const result = Number(size).toFixed(fixed + endAdd);
return `${result.slice(0, result.length - endAdd)}${_unit[n]}`;
};
export const numAddZero = (num: Number) => {
const newNum = Number(num);
return String(newNum > 9 ? newNum : `0${newNum}`);
};
export const numRemoveZero = (num: Number) => {
if (String(num).startsWith("0")) {
const numArr = [].slice.call(num);
numArr.splice(0, 1);
return Number(numArr.join(""));
}
return Number(num);
};
// await-to-js
export const ATJ = (promise: Promise<any>, errorExt: string | undefined = undefined) => {
return promise
.then((data) => [null, data])
.catch((err) => {
if (errorExt) {
if (typeof errorExt !== "object") {
return [errorExt, undefined];
} else {
const parsedError = Object.assign({}, err, errorExt);
return [parsedError, undefined];
}
}
return [err, undefined];
});
};
const _supportProtocols = ["tcp", "udp", "ws", "wss", "quic"];
export const supportProtocols = () => {
return _supportProtocols.slice();
}
export const parsePeerInfo = (content: string) => {
if(!content) return [];
// 将表格字符串分割成行
const lines = content.split("\n");
// 提取表头(keys
const headers = lines[1]
.split("│")
.slice(1, -1)
.map((h) => h.trim());
// 初始化结果数组
const result: any[] = [];
// 遍历数据行
for (let i = 3; i < lines.length - 1; i += 2) {
if (lines[i].trim() === "") continue; // 跳过空行
// 分割每一行的数据
const values = lines[i]
.split("│")
.slice(1, -1)
.map((v) => v.trim());
// 创建对象并添加到结果数组
const obj: any = {};
headers.forEach((header, index) => {
obj[header] = values[index] === "-" || values[index] === "" ? null : values[index];
});
// 每行数据都作为一个新对象添加到结果数组中
result.push(obj);
}
return result;
};
export const addQQGroup = () => {
open("https://qm.qq.com/q/Yo3HmaEIWC");
};
export function isValidIP(ip: string) {
const ipv4Pattern =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const ipv6Pattern = /^(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}$/;
return ipv4Pattern.test(ip) || ipv6Pattern.test(ip);
}