Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 护照模式 #377

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
} from "./declare";
import { ENUM_PINYIN_STYLE, ENUM_PINYIN_MODE, DEFAULT_OPTIONS } from "./constant";

export function hasKey(obj: any, key: string) {

Check warning on line 10 in src/util.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 10 in src/util.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
return Object.prototype.hasOwnProperty.call(obj, key);
}

Expand Down Expand Up @@ -35,6 +35,10 @@
[ "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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

describe("pinyin() without param", function() {
it("pinyin() => []", function() {
// @ts-ignore

Check warning on line 5 in test/test.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Do not use "@ts-ignore" because it alters compilation errors

Check warning on line 5 in test/test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Do not use "@ts-ignore" because it alters compilation errors
expect(pinyin()).toEqual([]);
});
});

const cases: any[] = [

Check warning on line 10 in test/test.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 10 in test/test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

// 单音字
[ "我", {
STYLE_NORMAL: [["wo"]],
STYLE_PASSPORT: [["WO"]],
STYLE_TONE: [["wǒ"]],
STYLE_TONE2: [["wo3"]],
STYLE_TO3NE: [["wo3"]],
Expand All @@ -21,6 +22,7 @@

[ "〇", {
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 @@
// 多音字
[ "中", {
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 @@
// 元音字
["爱", {
STYLE_NORMAL: [["ai"]],
STYLE_PASSPORT: [["AI"]],
STYLE_TONE: [["ài"]],
STYLE_TONE2: [["ai4"]],
STYLE_TO3NE: [["a4i"]],
Expand All @@ -49,16 +53,29 @@
} ],
["啊", {
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 @@
// 多音字,单音词。分词后可以准确识别读音。
[ "中国", {
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 @@
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
Loading