vite-uni-dev-tool
Version:
vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试
134 lines (120 loc) • 3.14 kB
text/typescript
/**
* 将匹配到的字符串高亮显示
*
* @export
* @param {string} [str='']
* @param {string} [search='']
* @return {*}
*/
export function hightLight(str: string = '', search: string = '') {
if (!str) return '';
if (!search) return str;
// 转义特殊字符,确保正则表达式安全
const escapedSearch = search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(escapedSearch, 'g');
const html = str.replace(
regex,
`<span style="color:#fff; background-color: var(--dev-tool-main-color);">${search}</span>`,
);
return html;
}
export function escapeHTML(unsafe: string) {
if (!unsafe) return '';
const escapeMap: Record<string, string> = {
'&': '&',
'"': '"',
"'": ''',
'>': '>',
'<': '<',
'=': '=',
'+': '+',
'-': '-',
'(': '(',
')': ')',
'[': '[',
']': ']',
'{': '{',
'}': '}',
'.': '.',
'?': '?',
'/': '/',
':': ':',
};
return unsafe.replace(
/[&"'><=+\-().?\/:[\]{}]/g,
(char) => escapeMap[char] || char,
);
}
const urlPattern = /https?:\/\/[^\s/$.?#](?:(?!\s|\)).)*/g;
/**
* 在字符串中获取url
*
* @export
* @param {string} text
* @return {*}
*/
export function extractUrl(text: string) {
const match = text.match(urlPattern);
return match ? match[0] : '';
}
const rowAndColPattern = /:(\d+)(?::(\d+))?\)?$/;
/**
* 获取url 中末尾的行号和列号
*
* @export
* @param {string} url
* @return {*}
*/
export function extractRowAndCol(url: string) {
const match = url.match(rowAndColPattern);
if (match) {
const lineNumber = match[1];
const columnNumber = match[2];
return {
row: parseInt(lineNumber) - 1,
col: parseInt(columnNumber) - 1,
};
}
return {
row: -1,
col: -1,
};
}
// at pages/index/index.js:46
// at pages/index/index.js:46:10
// at handleClick (http://localhost:8080/pages/main.js:123:4)
// at http://example.com/app.bundle.js:5:2
// at init (lib/utils.js:10)
const stockReg =
/at\s+(?:([^(]+)\s+\()?((?:[^:\/\s]+(?:\/[^:\/\s]+)*\.[^:\/\s]+)|(?:[^:]+:\/\/[^:\/\s]+(?::\d+)?(?:\/[^:\s]*)*)):(\d+)(?::(\d+))?(?:\s*\)?)?$/;
/**
* 解析栈信息
*
* @export
* @param {string} stack
* @return {*}
*/
export function parseStock(stack: string) {
const match = stack.match(stockReg);
return {
info: match?.[1] ?? '',
path: match?.[2] ?? '',
row: match?.[3] ? parseInt(match?.[3]) - 1 : -1,
col: match?.[4] ? parseInt(match?.[4]) - 1 : -1,
};
}
const mockWXReg = /https?:\/\/usr/;
export function isMockWX(stack: string) {
return mockWXReg.test(stack);
}
export function uniqueId(
pre: string = '',
length = 16,
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
): string {
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return pre + result;
}