Skip to content

Commit

Permalink
Merge pull request #377 from hotoo/feat/passport-style
Browse files Browse the repository at this point in the history
feat: 护照模式
  • Loading branch information
hotoo committed Nov 12, 2023
2 parents d0a939e + 82ff93e commit 1a34494
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [16.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
3 changes: 3 additions & 0 deletions EXAMPLE.md
Expand Up @@ -79,6 +79,9 @@ export default function() {

<input type="radio" name="style" id="style-first-letter" value="FIRST_LETTER" checked={style==='FIRST_LETTER'} onChange={onChangeStyle} />
<label htmlFor="style-first-letter">首字母风格</label>

<input type="radio" name="style" id="style-passport" value="PASSPORT" checked={style==='PASSPORT'} onChange={onChangeStyle} />
<label htmlFor="style-first-letter">护照风格</label>
</div>
<div>
<input type="checkbox" name="segment" id="segment" value="segmentit" checked={segment==='segmentit'} onChange={onChangeSegment} />
Expand Down
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -122,8 +122,8 @@ export interface IPinyinOptions {

```typescript
export type IPinyinStyle =
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | // 方便老版本迁移
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // 方便老版本迁移
0 | 1 | 2 | 5 | 3 | 4; // 兼容老版本
```

Expand Down Expand Up @@ -204,8 +204,8 @@ wǒ xǐhuān nǐ

```typescript
IPinyinStyle =
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | // 方便老版本迁移
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // 方便老版本迁移
0 | 1 | 2 | 5 | 3 | 4; // 兼容老版本
```

Expand Down Expand Up @@ -254,6 +254,12 @@ IPinyinStyle =

如:`p y`

#### `PASSPORT`, `passport`

护照风格。转换成大写形式,并且 `ü` 会以 `YU` 形式输出。

国家移民管理局门户网站于2021年9月29日发布了《关于内地居民拼音姓名中字母“ü”在出入境证件中打印规则的公告》(以下简称公告),根据《中国人名汉语拼音字母拼写规则》和《关于机读旅行证件的相关国际通用规范》, 内地居民申办出入境证件,出入境证件上打印的持证人拼音姓名中,Lü(吕等字)、Nü(女等字)中的字母“ü”应当转换为“YU”,LüE(略等字)、NüE(虐等字)中的字母“ü”应当转换为“U”。

### <string> options.mode

拼音模式,默认 "NORMAL" 普通模式。
Expand Down
2 changes: 2 additions & 0 deletions src/PinyinBase.ts
Expand Up @@ -22,6 +22,7 @@ export default class PinyinBase {
STYLE_NORMAL = ENUM_PINYIN_STYLE.NORMAL;
STYLE_INITIALS = ENUM_PINYIN_STYLE.INITIALS;
STYLE_FIRST_LETTER = ENUM_PINYIN_STYLE.FIRST_LETTER;
STYLE_PASSPORT = ENUM_PINYIN_STYLE.PASSPORT;

// 兼容 v2.x 中的属性透出
// pinyin mode:
Expand Down Expand Up @@ -297,6 +298,7 @@ export function getPinyinInstance(py: PinyinBase) {
pinyin.STYLE_NORMAL = ENUM_PINYIN_STYLE.NORMAL;
pinyin.STYLE_INITIALS = ENUM_PINYIN_STYLE.INITIALS;
pinyin.STYLE_FIRST_LETTER = ENUM_PINYIN_STYLE.FIRST_LETTER;
pinyin.STYLE_PASSPORT = ENUM_PINYIN_STYLE.PASSPORT;

// pinyin mode: 兼容 v2.x 中的属性透出
pinyin.MODE_NORMAL = ENUM_PINYIN_MODE.NORMAL;
Expand Down
1 change: 1 addition & 0 deletions src/constant.ts
Expand Up @@ -8,6 +8,7 @@ export enum ENUM_PINYIN_STYLE {
TO3NE = 5, // 声调以数字形式在声母之后,使用数字 0~4 标识。
INITIALS = 3, // 仅需要声母部分。
FIRST_LETTER = 4, // 仅保留首字母。
PASSPORT = 6, // 护照风格,转换成大写,且 `ü` 会以 `YU` 形式输出。
};

// 拼音模式。
Expand Down
5 changes: 3 additions & 2 deletions src/declare.ts
Expand Up @@ -13,6 +13,7 @@ export interface IPinyin {
STYLE_NORMAL: ENUM_PINYIN_STYLE;
STYLE_INITIALS: ENUM_PINYIN_STYLE;
STYLE_FIRST_LETTER: ENUM_PINYIN_STYLE;
STYLE_PASSPORT: ENUM_PINYIN_STYLE;

// pinyin mode: 兼容 v2.x 中的属性透出
MODE_NORMAL: ENUM_PINYIN_MODE;
Expand All @@ -22,8 +23,8 @@ export interface IPinyin {

export type IPinyinStyle =
ENUM_PINYIN_STYLE |
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | // 方便老版本迁移
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // 方便老版本迁移
0 | 1 | 2 | 5 | 3 | 4; // 兼容老版本

// 拼音模式。
Expand Down
5 changes: 5 additions & 0 deletions src/format.ts
Expand Up @@ -45,6 +45,11 @@ export function toFixed(pinyin: string, style: ENUM_PINYIN_STYLE): string {
return PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1");
});

case ENUM_PINYIN_STYLE.PASSPORT:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1").replace("v", "YU");
}).toUpperCase();

case ENUM_PINYIN_STYLE.TO3NE:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic];
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Expand Up @@ -35,6 +35,10 @@ const pinyinStyleMap: Map<string, ENUM_PINYIN_STYLE> = new Map([
[ "normal", ENUM_PINYIN_STYLE.NORMAL ],
[ "NORMAL", ENUM_PINYIN_STYLE.NORMAL ],
[ "0", ENUM_PINYIN_STYLE.NORMAL ],

[ "passport", ENUM_PINYIN_STYLE.PASSPORT ],
[ "PASSPORT", ENUM_PINYIN_STYLE.PASSPORT ],
[ "6", ENUM_PINYIN_STYLE.PASSPORT ],
]);

// 将用户输入的拼音形式参数转换成唯一指定的强类型。
Expand Down
22 changes: 22 additions & 0 deletions test/test.ts
Expand Up @@ -12,6 +12,7 @@ const cases: any[] = [
// 单音字
[ "我", {
STYLE_NORMAL: [["wo"]],
STYLE_PASSPORT: [["WO"]],
STYLE_TONE: [["wǒ"]],
STYLE_TONE2: [["wo3"]],
STYLE_TO3NE: [["wo3"]],
Expand All @@ -21,6 +22,7 @@ const cases: any[] = [

[ "〇", {
STYLE_NORMAL: [["ling", "xing"]],
STYLE_PASSPORT: [["LING", "XING"]],
STYLE_TONE: [["líng", "xīng"]],
STYLE_TONE2: [["ling2", "xing1"]],
STYLE_TO3NE: [["li2ng", "xi1ng"]],
Expand All @@ -31,6 +33,7 @@ const cases: any[] = [
// 多音字
[ "中", {
STYLE_NORMAL: [["zhong"]],
STYLE_PASSPORT: [["ZHONG"]],
STYLE_TONE: [["zhōng", "zhòng"]],
STYLE_TONE2: [["zhong1", "zhong4"]],
STYLE_TO3NE: [["zho1ng", "zho4ng"]],
Expand All @@ -41,6 +44,7 @@ const cases: any[] = [
// 元音字
["爱", {
STYLE_NORMAL: [["ai"]],
STYLE_PASSPORT: [["AI"]],
STYLE_TONE: [["ài"]],
STYLE_TONE2: [["ai4"]],
STYLE_TO3NE: [["a4i"]],
Expand All @@ -49,16 +53,29 @@ const cases: any[] = [
} ],
["啊", {
STYLE_NORMAL: [["a"]],
STYLE_PASSPORT: [["A"]],
STYLE_TONE: [["ā", "á", "ǎ", "à", "a"]],
STYLE_TONE2: [["a1", "a2", "a3", "a4", "a"]],
STYLE_TO3NE: [["a1", "a2", "a3", "a4", "a"]],
STYLE_INITIALS: [[""]],
STYLE_FIRST_LETTER: [["a"]],
} ],

// YU
["吕", {
STYLE_NORMAL: [["lv"]],
STYLE_PASSPORT: [["LYU"]],
STYLE_TONE: [["lǚ"]],
STYLE_TONE2: [["lv3"]],
STYLE_TO3NE: [["lv3"]],
STYLE_INITIALS: [["l"]],
STYLE_FIRST_LETTER: [["l"]],
} ],

// 单音词
[ "我是谁", {
STYLE_NORMAL: [["wo"], ["shi"], ["shui"]],
STYLE_PASSPORT: [["WO"], ["SHI"], ["SHUI"]],
STYLE_TONE: [["wǒ"], ["shì"], ["shuí"]],
STYLE_TONE2: [["wo3"], ["shi4"], ["shui2"]],
STYLE_TO3NE: [["wo3"], ["shi4"], ["shui2"]],
Expand All @@ -69,6 +86,7 @@ const cases: any[] = [
// 多音字,单音词。分词后可以准确识别读音。
[ "中国", {
STYLE_NORMAL: [["zhong"], ["guo"]],
STYLE_PASSPORT: [["ZHONG"], ["GUO"]],
STYLE_TONE: {
normal: [["zhōng", "zhòng"], ["guó"]],
segment: [["zhōng"], ["guó"]],
Expand All @@ -89,6 +107,10 @@ const cases: any[] = [
normal: [["zhong", "chong"], ["xin"]],
segment: [["zhong"], ["xin"]],
},
STYLE_PASSPORT: {
normal: [["ZHONG", "CHONG"], ["XIN"]],
segment: [["ZHONG"], ["XIN"]],
},
STYLE_TONE: {
normal: [["zhòng", "chóng"], ["xīn"]],
segment: [["zhòng"], ["xīn"]],
Expand Down

0 comments on commit 1a34494

Please sign in to comment.