any-rule/src/utils.ts

96 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-01-03 00:15:54 +08:00
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;
}