updated 代码优化

This commit is contained in:
PASSER-BY 2022-11-11 23:38:23 +08:00
parent 07c6d08e0d
commit 1fd73283f7
4 changed files with 19 additions and 41 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -55,7 +55,7 @@ export default [
}) })
}, },
{ {
exp:/^(\S+?)是(什么|啥)(关系|人)?$/, exp:/^(\S+?)(指的)?是(什么|啥)(意思|关系|辈分|人)?$/,
opt:match=>({ opt:match=>({
text:match[1], text:match[1],
type:'chain' type:'chain'

View File

@ -15,17 +15,8 @@ var zh2number = function(text){
if(map[text]){ if(map[text]){
num = map[text]; num = map[text];
}else{ }else{
if(text.includes('十')){ var [unit,dec=0] = text.replace(/^十/,'一十').split('十').map(word=> textAttr.indexOf(word)).reverse();
var numAttr = text.split('十'); num = dec*10+unit;
if(!numAttr[0]){
num = 10;
}else{
num = textAttr.indexOf(numAttr[0])*10;
}
num += textAttr.indexOf(numAttr[1]);
}else{
num += textAttr.includes(text)?textAttr.indexOf(text):0;
}
} }
return num; return num;
}; };
@ -40,15 +31,7 @@ var number2zh = function(num){
}else{ }else{
var dec = ~~(num/10); var dec = ~~(num/10);
var unit = num%10; var unit = num%10;
if(dec){ text = (dec?(textAttr[dec]+'十').replace('一十','十'):'')+textAttr[unit];
if(dec>1){
text = dec<textAttr.length?textAttr[dec]:'';
}
text += '十';
text += unit<textAttr.length?textAttr[unit]:'';
}else{
text += num<textAttr.length?textAttr[num]:'';
}
} }
return text; return text;
}; };
@ -67,9 +50,7 @@ var getGen = function(id){
// 获得最简 // 获得最简
var getOptimal = function(options){ var getOptimal = function(options){
var from = options['from']; var {from,to,sex} = options;
var to = options['to']
var sex = options['sex'];
var from_chain = options['from'].split(','); var from_chain = options['from'].split(',');
var to_chain = options['to'].split(','); var to_chain = options['to'].split(',');
for(var i=0;i<from_chain.length&&i<to_chain.length;i++){ for(var i=0;i<from_chain.length&&i<to_chain.length;i++){
@ -218,10 +199,10 @@ export function getSelectors(str){
getList(name); getList(name);
// 通过关键词找关系 // 通过关键词找关系
keywords.forEach(function(name){ keywords.forEach(function(name){
var x_name = name.replace(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/,'几');
var r_name = name.replace(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/,'');
var match = name.match(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/); var match = name.match(/^[大|小]|^[一|二|三|四|五|六|七|八|九|十]+/);
if(match){ if(match){
var x_name = name.replace(match[0],'几');
var r_name = name.replace(match[0],'');
var num = zh2number(match[0]); var num = zh2number(match[0]);
for(var i in _data){ for(var i in _data){
var r_i = i.replace(/(,[hw])$/,'&'+num+'$1').replace(/([^hw]+)$/,'$1&'+num); var r_i = i.replace(/(,[hw])$/,'&'+num+'$1').replace(/([^hw]+)$/,'$1&'+num);
@ -273,9 +254,7 @@ export function getSelectors(str){
// 合并选择器,查找两个对象之间的关系 // 合并选择器,查找两个对象之间的关系
export function mergeSelector(param){ export function mergeSelector(param){
var from_selector = param['from']; var {from:from_selector,to:to_selector,sex:my_sex} = param;
var to_selector = param['to'];
var my_sex = param['sex'];
if(my_sex<0){ if(my_sex<0){
var to_sex = -1; var to_sex = -1;
var from_sex = -1; var from_sex = -1;
@ -457,19 +436,18 @@ export function reverseId(id,sex){
// 通过ID获取关系称呼 // 通过ID获取关系称呼
export function getItemsById(id){ export function getItemsById(id){
var items = []; var items = [];
var getData = function(d){ var getData = function(key){
var res = []; var res = [];
if(_data[d]){ if(_data[key]){
res.push(_data[d][0]); res.push(_data[key][0]);
}else{ }else{
for(var i in _data){ for(var i in _data){
if(i.replace(/&[ol]/g,'')==d){ if(i.replace(/&[ol]/g,'')==key){
res.push(_data[i][0]); res.push(_data[i][0]);
}else{ }else{
var expr = d; while (key.match(/[ol](b|s)/)){
while (expr.match(/[ol](b|s)/)){ key = key.replace(/[ol](b|s)/,'x$1');
expr = expr.replace(/[ol](b|s)/,'x$1'); if(key==i){
if(expr==i){
res.push(_data[i][0]); res.push(_data[i][0]);
break; break;
} }