Add personal accounts, scoped access and editable family genealogy
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
(function(root){
|
||||
function describe(people, links, reference, target) {
|
||||
const byId = new Map(people.map(p=>[p.id,p]));
|
||||
if (!byId.has(reference) || !byId.has(target)) return '尚未关联';
|
||||
if (reference === target) return '本人';
|
||||
const edges=links.filter(e=>e.active), graph=new Map();
|
||||
function add(a,b,kind,lineage){if(!graph.has(a))graph.set(a,[]);graph.get(a).push({id:b,kind,lineage});}
|
||||
for(const e of edges){add(e.fromId,e.toId,e.kind==='parent'?'child':'spouse',e.lineage);add(e.toId,e.fromId,e.kind==='parent'?'parent':'spouse',e.lineage);}
|
||||
const queue=[{id:reference,path:[]}],seen=new Set([reference]);let path;
|
||||
for(let i=0;i<queue.length;i++){
|
||||
const node=queue[i];
|
||||
for(const next of graph.get(node.id)||[]){if(seen.has(next.id))continue;seen.add(next.id);const steps=[...node.path,next];if(next.id===target){path=steps;break;}queue.push({id:next.id,path:steps});}
|
||||
if(path)break;
|
||||
}
|
||||
if(!path)return '关系待补充';
|
||||
const gender=byId.get(target).gender, word=(m,f,u)=>gender==='male'?m:gender==='female'?f:u;
|
||||
const type=path.map(e=>e.kind),n=path.length;
|
||||
let result;
|
||||
if(type.every(k=>k==='child')){
|
||||
result=n===1?word('儿子','女儿','子女'):n===2?word('孙子','孙女','孙辈'):n===3?word('曾孙','曾孙女','曾孙辈'):n===4?word('玄孙','玄孙女','玄孙辈'):`第 ${n} 代后裔`;
|
||||
if(n===2 && byId.get(path[0].id)?.gender==='female')result=word('外孙','外孙女','外孙辈');
|
||||
}else if(type.every(k=>k==='parent')){
|
||||
result=n===1?word('爸爸','妈妈','父母'):n===2?(byId.get(path[0].id)?.gender==='female'?word('姥爷','姥姥','外祖辈'):byId.get(path[0].id)?.gender==='male'?word('爷爷','奶奶','祖辈'):'祖父母辈'):n===3?'曾祖辈':n===4?'高祖辈':`第 ${n} 代祖先`;
|
||||
}else if(n===1 && type[0]==='spouse') result='配偶';
|
||||
else if(n===2 && type.join('/')==='parent/child'){
|
||||
const a=byId.get(reference).birthDate,b=byId.get(target).birthDate;
|
||||
result=a&&b&&a!==b?(b<a?word('哥哥','姐姐','年长的手足'):word('弟弟','妹妹','年幼的手足')):word('兄弟(长幼待确认)','姐妹(长幼待确认)','手足(长幼待确认)');
|
||||
}else{
|
||||
result=path.map(e=>e.kind==='spouse'?'配偶':e.kind==='parent'?(byId.get(e.id)?.gender==='male'?'父亲':byId.get(e.id)?.gender==='female'?'母亲':'父母'):(byId.get(e.id)?.gender==='male'?'儿子':byId.get(e.id)?.gender==='female'?'女儿':'子女')).join(' → ');
|
||||
}
|
||||
return result+(path.some(e=>e.lineage==='adoptive'||e.lineage==='step')?'(含收养 / 继亲关系)':'');
|
||||
}
|
||||
const api={describe}; if(typeof module!=='undefined'&&module.exports)module.exports=api;else root.ZhaoKinship=api;
|
||||
})(typeof window!=='undefined'?window:globalThis);
|
||||
Reference in New Issue
Block a user