Pinyin Tone

This is a practical pinyin tone converter that transforms numeric tone-marked pinyin (like "han4 yu3" or "han4yu3") into standard accented pinyin with proper tone marks (like "hàn yǔ" or "hànyǔ).

Licence: MIT

If you want to convert something like "han4 yu3 pin1 yin1" to pinyin

Your input

toPinyin (v1):

toPinyin (v2)

toUnspacedPinyin (v2)

Usage

Installation

Using npm:
npm install pinyin-tone
Then in your code:
import toPinyin from "pinyin-tone"; // v1
// /* or */
// import toPinyin from ("pinyin-tone/v2"); // v2

const input = "han4 yu3 pin1 yin1";
const result = toPinyin(input);
console.log(result); // "hàn yǔ pīn yīn"

the Tests

The tests are:
[{"id":"1","input":"test","expect":"test"},{"id":"2","input":"chu1 yin1 wei4 lai2","expect":"chū yīn wèi lái"},{"id":"3","input":"xun2 yin1 liu2 ge1","expect":"xún yīn liú gē"},{"id":"4","input":"han4 yu3 pin1 yin1 fang1 an4","expect":"hàn yǔ pīn yīn fāng àn"},{"id":"5","input":"huar1 renr2 shuir3 yuer4","expect":"huār rénr shuǐr yuèr"},{"id":"6","input":"b p m f","expect":"b p m f"},{"id":"7","input":"an1 vn2 ong3 uen4","expect":"ān ǘn ǒng uèn"},{"id":"8","input":"liou2 yuen2","expect":"liú yún","expect2":"lióu yuén"},{"id":"9","input":"iou1 uen4","expect":"iōu uèn"},{"id":"10","input":"bong1 tv2 pe3 wir4","expect":"bōng tǘ pě wìr"},{"id":"11","input":"qi3 lai2 bu2 yuan4 zuo4 nu2 li4 de ren2 men","expect":"qǐ lái bú yuàn zuò nú lì de rén men"},{"id":"12","input":"huar1 wei4 shen2 me zhe4 yang4 hong2","expect":"huār wèi shén me zhè yàng hóng"}]

toPinyin V1

Test id: 1
Input: test
Expect: test
Output: test
Output === Expect? Yes

Test id: 2
Input: chu1 yin1 wei4 lai2
Expect: chū yīn wèi lái
Output: chū yīn wèi lái
Output === Expect? Yes

Test id: 3
Input: xun2 yin1 liu2 ge1
Expect: xún yīn liú gē
Output: xún yīn liú gē
Output === Expect? Yes

Test id: 4
Input: han4 yu3 pin1 yin1 fang1 an4
Expect: hàn yǔ pīn yīn fāng àn
Output: hàn yǔ pīn yīn fāng àn
Output === Expect? Yes

Test id: 5
Input: huar1 renr2 shuir3 yuer4
Expect: huār rénr shuǐr yuèr
Output: huār rénr shuǐr yuèr
Output === Expect? Yes

Test id: 6
Input: b p m f
Expect: b p m f
Output: b p m f
Output === Expect? Yes

Test id: 7
Input: an1 vn2 ong3 uen4
Expect: ān ǘn ǒng uèn
Output: ān ǘn ǒng uèn
Output === Expect? Yes

Test id: 8
Input: liou2 yuen2
Expect: liú yún
Output: liú yún
Output === Expect? Yes

Test id: 9
Input: iou1 uen4
Expect: iōu uèn
Output: iōu uèn
Output === Expect? Yes

Test id: 10
Input: bong1 tv2 pe3 wir4
Expect: bōng tǘ pě wìr
Output: bōng tǘ pě wìr
Output === Expect? Yes

Test id: 11
Input: qi3 lai2 bu2 yuan4 zuo4 nu2 li4 de ren2 men
Expect: qǐ lái bú yuàn zuò nú lì de rén men
Output: qǐ lái bú yuàn zuò nú lì de rén men
Output === Expect? Yes

Test id: 12
Input: huar1 wei4 shen2 me zhe4 yang4 hong2
Expect: huār wèi shén me zhè yàng hóng
Output: huār wèi shén me zhè yàng hóng
Output === Expect? Yes

toPinyin V2

Test id: 1
Input: test
Expect: test
Output: test
Output === Expect?: Yes

Test id: 2
Input: chu1 yin1 wei4 lai2
Expect: chū yīn wèi lái
Output: chū yīn wèi lái
Output === Expect?: Yes

Test id: 3
Input: xun2 yin1 liu2 ge1
Expect: xún yīn liú gē
Output: xún yīn liú gē
Output === Expect?: Yes

Test id: 4
Input: han4 yu3 pin1 yin1 fang1 an4
Expect: hàn yǔ pīn yīn fāng àn
Output: hàn yǔ pīn yīn fāng àn
Output === Expect?: Yes

Test id: 5
Input: huar1 renr2 shuir3 yuer4
Expect: huār rénr shuǐr yuèr
Output: huār rénr shuǐr yuèr
Output === Expect?: Yes

Test id: 6
Input: b p m f
Expect: b p m f
Output: b p m f
Output === Expect?: Yes

Test id: 7
Input: an1 vn2 ong3 uen4
Expect: ān ǘn ǒng uèn
Output: ān ǘn ǒng uèn
Output === Expect?: Yes

Test id: 8
Input: liou2 yuen2
Expect: liú yún
Output: lióu yuén
Output === Expect?: -- NO! --
Expect2: lióu yuén
Output === Expect2? Yes

Test id: 9
Input: iou1 uen4
Expect: iōu uèn
Output: iōu uèn
Output === Expect?: Yes

Test id: 10
Input: bong1 tv2 pe3 wir4
Expect: bōng tǘ pě wìr
Output: bōng tǘ pě wìr
Output === Expect?: Yes

Test id: 11
Input: qi3 lai2 bu2 yuan4 zuo4 nu2 li4 de ren2 men
Expect: qǐ lái bú yuàn zuò nú lì de rén men
Output: qǐ lái bú yuàn zuò nú lì de rén men
Output === Expect?: Yes

Test id: 12
Input: huar1 wei4 shen2 me zhe4 yang4 hong2
Expect: huār wèi shén me zhè yàng hóng
Output: huār wèi shén me zhè yàng hóng
Output === Expect?: Yes