updated 代码优化

This commit is contained in:
PASSER-BY 2024-04-19 23:50:42 +08:00
parent 230b0a8d23
commit 970094a88f
7 changed files with 68 additions and 79 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,10 @@ import {modeData} from './mode.js';
// 逆转ID
export function reverseId(id,sex){
if(!id){
return [''];
}
// 映射关系
let hash = {
f:['d','s'],
m:['d','s'],
@ -23,42 +27,35 @@ export function reverseId(id,sex){
os:['ls','lb'],
xs:['xs','xb']
};
// 年纪判断
let age = '';
if(id.match(/&o$/)){
age = '&l';
}else if(id.match(/&l$/)){
age = '&o';
}
if(id){
id = id.replace(/&[ol\d+]/g,'');
//性别判断
if(sex<0){
if(id.match(/^w/)){
sex = 1;
}else if(id.match(/^h/)){
sex = 0;
}
id = id.replace(/&[ol\d+]/g,'');
// 性别判断
if(sex<0){
if(id.match(/^w/)){
sex = 1;
}else if(id.match(/^h/)){
sex = 0;
}
let result = [];
let doing = function(sex){
let sid = (','+sex+','+id).replace(/,[fhs]|,[olx]b/g,',1').replace(/,[mwd]|,[olx]s/g,',0');
sid = sid.substring(0,sid.length-2);
let id_arr = id.split(',').reverse();
let sid_arr = sid.split(',').reverse();
let arr = id_arr.map((id,i)=>hash[id][sid_arr[i]]);
let r_id = arr.join(',');
let gen = getGenById(r_id);
return r_id +(gen?'':age);
};
if(sex<0){
result.push(doing(1));
result.push(doing(0));
}else{
result.push(doing(sex));
}
return result;
}
return [''];
let doing = function(sex){
let sid = (','+sex+','+id).replace(/,[fhs]|,[olx]b/g,',1').replace(/,[mwd]|,[olx]s/g,',0');
sid = sid.substring(0,sid.length-2);
let sid_arr = sid.split(',').reverse();
let r_id = id.split(',').reverse().map((id,i)=>hash[id][sid_arr[i]]).join(',');
let gen = getGenById(r_id);
return r_id + (gen?'':age);
};
if(sex<0){
return [doing(1),doing(0)];
}else{
return [doing(sex)];
}
};
// ID列表去重
@ -112,18 +109,18 @@ export function getItemsById(id){
}
});
if(!item){
item = modeData[id][0].match(/^[大小]/)?modeData[id][0].replace(/^[大小]/,zh):zh+modeData[id][0];
item = modeData[id][0];
item = item.match(/^[大小]/)?item.replace(/^[大小]/,zh):zh+item;
}
}else{
item = modeData[id][0]
}
items.push(item);
}
}else{
id = id.replace(/&\d+/g,'');
}
// 直接匹配称呼
if(!items.length){
id = id.replace(/&\d+/g,'');
items = getData(id);
}
// 忽略年龄条件查找
@ -150,19 +147,19 @@ let data = Object.assign({},modeData,{
'xb':['兄弟'],
'xs':['姐妹']
});
export function getChainById(id,sex){
export function getChainById(id,sex=-1){
let item = id.split(',').map(function(sign){
let key = sign.replace(/&[ol\d]+/,'');
return data[key][0];
}).join('的');
if(sex&&sex>-1&&data[sex+','+id]){
if(sex>-1&&data[sex+','+id]){
if(sex==0){
item = '(女性)'+item;
}else if(sex==1){
item = '(男性)'+item;
}
}
return item
return item;
};
// 通过ID获取关系合称

View File

@ -9,6 +9,25 @@ import _multipie from './data/multiple.js';
import {expandSelector} from './selector.js';
let _map = Object.assign({},_multipie);
// 分支 - 前缀处理
let prefixMap = {};
for(let key in _prefix){
prefixMap[key] = {};
for(let selector in _prefix[key]){
expandSelector(selector).forEach(function(s){
prefixMap[key][s] = _prefix[key][selector];
});
}
}
// 分支 - 节点处理
let branchMap = {};
for(let selector in _branch){
expandSelector(selector).forEach(function(s){
branchMap[s] = _branch[selector];
});
}
// 分支 - 合并
let getMap = function(prefixMap,branchMap){
let map = {};
for(let key in branchMap){
@ -38,25 +57,8 @@ let getMap = function(prefixMap,branchMap){
}
return map;
};
// 分支前缀处理
let prefixMap = {};
for(let key in _prefix){
prefixMap[key] = {};
for(let selector in _prefix[key]){
expandSelector(selector).forEach(function(s){
prefixMap[key][s] = _prefix[key][selector];
});
}
}
// 分支节点处理
let branchMap = {};
for(let selector in _branch){
expandSelector(selector).forEach(function(s){
branchMap[s] = _branch[selector];
});
}
_map = Object.assign({},_map,getMap(prefixMap,branchMap));
// 主要关系
for(let key in _main){
_map[key] = [].concat(_main[key],_map[key]||[]);

View File

@ -16,8 +16,8 @@ let getOptimal = function(options){
to,
sex
} = options;
let from_chain = options['from'].split(',');
let to_chain = options['to'].split(',');
let from_chain = from.split(',');
let to_chain = to.split(',');
for(let i=0;i<from_chain.length&&i<to_chain.length;i++){
if(from_chain[i]==to_chain[i]){
from = from_chain.slice(i+1).join(',');
@ -31,16 +31,8 @@ let getOptimal = function(options){
if(form_type!=to_type){
break;
}
let from_match = from_chain[i].match(/&([ol\d]+)/);
let to_match = to_chain[i].match(/&([ol\d]+)/);
if(!from_match){
from_match = from_chain[i].match(/([ol])[bs]/);
}
if(!to_match){
to_match = to_chain[i].match(/([ol])[bs]/);
}
let from_attr = from_match?from_match[1]:'';
let to_attr = to_match?to_match[1]:'';
let from_attr = from_chain[i].match(/&([ol\d]+)/)?.[1]||from_chain[i].match(/([ol])[bs]/)?.[1]||'';
let to_attr = to_chain[i].match(/&([ol\d]+)/)?.[1]||to_chain[i].match(/([ol])[bs]/)?.[1]||'';
if(from_attr&&to_attr){
if(!isNaN(from_attr)&&!isNaN(to_attr)){
if(+from_attr>+to_attr){
@ -57,10 +49,8 @@ let getOptimal = function(options){
to = to_chain.slice(i+1).join(',');
sex = to_chain[i].match(/^([fhs1](&[ol\d]+)?|[olx]b)(&[ol\d]+)?/)?1:0;
}else if(options['optimal']){
from_match = from_chain[i].match(/([xol])[bs]/);
to_match = to_chain[i].match(/([xol])[bs]/);
from_attr = from_match?from_match[1]:'';
to_attr = to_match?to_match[1]:'';
from_attr = from_chain[i].match(/([xol])[bs]/)?.[1]||'';
to_attr = to_chain[i].match(/([xol])[bs]/)?.[1]||'';
if(from_attr=='x'||to_attr=='x'){
from = from_chain.slice(i+1).join(',');
to = to_chain.slice(i+1).join(',');
@ -73,9 +63,9 @@ let getOptimal = function(options){
}
}
return {
'from':from,
'to':to,
'sex':sex
from,
to,
sex
};
};