-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from MarvNC:add-zhuyin
Add Zhuyin Support
- Loading branch information
Showing
8 changed files
with
125 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import pinyinNumbersToTone from 'pinyin-tone'; | ||
import zhuyin from 'zhuyin-improved'; | ||
|
||
export function normalizePinyin(pinyin: string): string { | ||
export function getPinyin(pinyin: string): string { | ||
pinyin = replaceUWithV(pinyin); | ||
return pinyinNumbersToTone(pinyin.toLowerCase()).replace(/ /g, ''); | ||
} | ||
|
||
export function replaceUWithV(pinyin: string) { | ||
pinyin = pinyin.replace(/u:/g, 'v'); | ||
return pinyinNumbersToTone(pinyin.toLowerCase()); | ||
return pinyin; | ||
} | ||
|
||
export function replacePinyinNumbers(string: string): string { | ||
export function replacePinyinNumbers(string: string, pinyin: boolean): string { | ||
// Find all pinyin within the definition and replace with tone | ||
const pinyinRegex = /\[(([a-zA-Z\:]+)([1-5]) ?)+\]/g; | ||
const pinyinMatches = string.match(pinyinRegex); | ||
if (pinyinMatches) { | ||
for (const match of pinyinMatches) { | ||
// Remove brackets | ||
const pinyinOnly = match.substring(1, match.length - 1); | ||
const pinyinTone = normalizePinyin(pinyinOnly); | ||
string = string.replace(pinyinOnly, pinyinTone); | ||
const processedText = pinyin | ||
? getPinyin(pinyinOnly) | ||
: getZhuyin(pinyinOnly); | ||
string = string.replace(pinyinOnly, processedText); | ||
} | ||
} | ||
return string; | ||
} | ||
|
||
export function getZhuyin(pinyin: string): string { | ||
pinyin = replaceUWithV(pinyin); | ||
return zhuyin(pinyin.toLowerCase()).join(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters