relationship.setMode()
设置自定义方言模式。
语法
ts
relationship.setMode(name: string, data: TitleData): void参数
name
方言模式名称,用于在查询时引用。
data
关系映射数据,键为关系链,值为称谓数组。
ts
type TitleData = Record<string, string[]>;返回值
无返回值。
示例
基础使用
ts
import relationship from 'relationship-ts';
relationship.setMode('northern', {
'm,f': ['姥爷'],
'm,m': ['姥姥'],
'm,xb,s&o': ['表哥'],
'm,xb,s&l': ['表弟'],
});
// 使用自定义模式
relationship({ text: '妈妈的妈妈', mode: 'northern' });
// => ['姥姥']覆盖默认称谓
ts
relationship.setMode('custom', {
'f': ['老爸', '爹地'],
'm': ['老妈', '妈咪'],
});
relationship({ text: '爸爸', mode: 'custom' });
// => ['老爸', '爹地']使用内置方言
ts
import relationship from 'relationship-ts';
import { guangdong } from 'relationship-ts/locale/guangdong';
import north from 'relationship-ts/locale/north';
// 设置粤语模式
relationship.setMode('guangdong', guangdong.data);
// 设置北方方言模式
relationship.setMode('north', north.data);
// 使用方言模式查询
relationship({ text: '妈妈的妈妈', mode: 'guangdong' });
// => ['阿婆']
relationship({ text: '妈妈的妈妈', mode: 'north' });
// => ['姥姥']数据格式
方言数据是一个对象,键是关系链,值是对应的称谓数组:
ts
const data: TitleData = {
'f': ['爸爸', '父亲', '老爸'],
'm': ['妈妈', '母亲', '老妈'],
'f,f': ['爷爷', '祖父', '阿公'],
'f,m': ['奶奶', '祖母', '阿嫲'],
'm,f': ['姥爷', '外公'],
'm,m': ['姥姥', '外婆'],
};关系链语法
关系链使用逗号分隔的符号表示关系:
| 符号 | 含义 | 符号 | 含义 |
|---|---|---|---|
f | 父亲 | m | 母亲 |
h | 丈夫 | w | 妻子 |
s | 儿子 | d | 女儿 |
xb | 兄弟 | xs | 姐妹 |
ob | 哥哥 | lb | 弟弟 |
os | 姐姐 | ls | 妹妹 |
修饰符
| 符号 | 含义 |
|---|---|
&o | 年长 |
&l | 年幼 |
&数字 | 排行 |
提示
详细的关系链语法请参考 关系链语法。
注意事项
- 模式切换会清除缓存:切换模式时,查询缓存会被清除
- 模式名称:建议使用有意义的名称,如
guangdong、northern等 - 数据覆盖:自定义模式会完全覆盖该模式下的原有数据
相关
- relationship() - 主函数
- 关系链语法 - 关系链语法详细说明
- 方言支持 - 内置方言列表