diff --git a/lib/core/pinyin/handle.ts b/lib/core/pinyin/handle.ts index 01a352b..3150aec 100644 --- a/lib/core/pinyin/handle.ts +++ b/lib/core/pinyin/handle.ts @@ -13,6 +13,7 @@ import { SingleWordResult } from "../../common/type"; import type { SurnameMode } from "../../common/type"; import { acTree, MatchPattern, TokenizationAlgorithm } from "../../common/segmentit"; import { + DoubleUnicodeCharReg, Priority, } from "@/common/constant"; import { splitString } from "@/common/utils"; @@ -275,12 +276,12 @@ const getPinyinWithNum: GetPinyinWithNum = (pinyin, originPinyin) => { * @param {string} pinyin * @return {string} */ -type GetFirstLetter = (pinyin: string) => string; -const getFirstLetter: GetFirstLetter = (pinyin) => { +type GetFirstLetter = (pinyin: string, isZh: boolean) => string; +const getFirstLetter: GetFirstLetter = (pinyin: string, isZh: boolean) => { const first_letter_arr: string[] = []; const pinyin_arr = pinyin.split(" "); pinyin_arr.forEach((pinyin) => { - first_letter_arr.push(pinyin[0]); + first_letter_arr.push(isZh ? pinyin[0] : pinyin); }); return first_letter_arr.join(" "); }; diff --git a/lib/core/pinyin/middlewares.ts b/lib/core/pinyin/middlewares.ts index 6adc745..97bbcd5 100644 --- a/lib/core/pinyin/middlewares.ts +++ b/lib/core/pinyin/middlewares.ts @@ -95,7 +95,7 @@ export const middlewarePattern = ( break; case "first": list.forEach((item) => { - item.result = getFirstLetter(item.result); + item.result = getFirstLetter(item.result, item.isZh); }); break; case "finalHead": @@ -198,7 +198,7 @@ export const middlewareType = ( pinyin, initial, final, - first: getFirstLetter(item.result), + first: getFirstLetter(item.result, item.isZh), finalHead: head, finalBody: body, finalTail: tail, diff --git a/lib/core/polyphonic/index.ts b/lib/core/polyphonic/index.ts index d0fe881..1dcc245 100644 --- a/lib/core/polyphonic/index.ts +++ b/lib/core/polyphonic/index.ts @@ -249,7 +249,7 @@ export const handleType = ( pinyin, initial, final, - first: getFirstLetter(item.result), + first: getFirstLetter(item.result, item.isZh), finalHead: head, finalBody: body, finalTail: tail, diff --git a/test/all.test.js b/test/all.test.js index 688c540..eb74bed 100644 --- a/test/all.test.js +++ b/test/all.test.js @@ -99,7 +99,7 @@ describe('all', () => { pinyin: '', initial: '', final: '', - first: '', + first: 'a', finalHead: '', finalBody: '', finalTail: '', @@ -114,7 +114,7 @@ describe('all', () => { pinyin: '', initial: '', final: '', - first: '', + first: '𧒽', finalHead: '', finalBody: '', finalTail: '', @@ -207,7 +207,7 @@ describe('all', () => { pinyin: '', initial: '', final: '', - first: '', + first: 'a𧒽', finalHead: '', finalBody: '', finalTail: '',