mirror of
https://github.com/any86/any-rule.git
synced 2025-07-14 15:38:58 +08:00
96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
![]() |
import { IRule } from './interface';
|
||
|
import * as pinyin from 'pinyin';
|
||
|
|
||
|
function preprocessText(START_IDENTIFIER: string, text: string): string[] | null {
|
||
|
const start = text.indexOf(START_IDENTIFIER);
|
||
|
if (start === -1) {
|
||
|
return null;
|
||
|
}
|
||
|
const pathString = text.substring(start, text.length);
|
||
|
const pathArray = pathString.split('.');
|
||
|
return pathArray;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 根据特定的字符串解析出当前可以用的规则列表
|
||
|
* @param text 待解析的字符串
|
||
|
*/
|
||
|
export function getRulesByText(START_IDENTIFIER: string, rules: IRule[], text: string): IRule[] {
|
||
|
const pathArray = preprocessText(START_IDENTIFIER, text);
|
||
|
if (!pathArray) {
|
||
|
return [];
|
||
|
}
|
||
|
let currentRules: IRule[] = [];
|
||
|
let targetRules = rules;
|
||
|
|
||
|
for (const path of pathArray) {
|
||
|
if (path === START_IDENTIFIER) {
|
||
|
currentRules = rules;
|
||
|
} else if (path === '') {
|
||
|
console.log('path');
|
||
|
break;
|
||
|
} else {
|
||
|
const searchRule = targetRules.find(rule => rule.title === path);
|
||
|
|
||
|
if (!searchRule) {
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
if (searchRule.regex) {
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
currentRules = searchRule.rules || [];
|
||
|
targetRules = currentRules;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return currentRules;
|
||
|
}
|
||
|
|
||
|
export function getRuleByText(START_IDENTIFIER: string, rules: IRule[], text: string): IRule | null {
|
||
|
const pathArray = preprocessText(START_IDENTIFIER, text);
|
||
|
if (!pathArray) {
|
||
|
return null;
|
||
|
}
|
||
|
let targetRules = rules;
|
||
|
let searchRule: IRule | undefined;
|
||
|
for (const path of pathArray) {
|
||
|
if (path === START_IDENTIFIER) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
searchRule = targetRules.find(rule => rule.title === path);
|
||
|
if (!searchRule) {
|
||
|
return null;
|
||
|
}
|
||
|
if (searchRule.regex) {
|
||
|
break;
|
||
|
} else {
|
||
|
targetRules = searchRule.rules || [];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return searchRule || null;
|
||
|
}
|
||
|
|
||
|
export function generateFilterString(rule: IRule) {
|
||
|
let filterString = '';
|
||
|
filterString += rule.title;
|
||
|
if (/.*[\u4e00-\u9fa5]+.*$/.test(rule.title)) {
|
||
|
filterString += ' ' + pinyin(rule.title, {
|
||
|
style: pinyin.STYLE_NORMAL
|
||
|
}).join('');
|
||
|
|
||
|
filterString += ' ' + pinyin(rule.title, {
|
||
|
style: pinyin.STYLE_FIRST_LETTER
|
||
|
}).join('');
|
||
|
}
|
||
|
|
||
|
if (rule.keywords) {
|
||
|
filterString += ' ' + rule.keywords.join(' ');
|
||
|
}
|
||
|
|
||
|
return filterString;
|
||
|
}
|