Pinyin Tone

This is a simple pinyin tone converter. It converts a string to pinyin tone.

Licence: MIT

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

Your input

toPinyin (v1):

toPinyin (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
result: test
result === expect? yes

test id: 2
input: chu1 yin1 wei4 lai2
expect: chū yīn wèi lái
result: chū yīn wèi lái
result === expect? yes

test id: 3
input: xun2 yin1 liu2 ge1
expect: xún yīn liú gē
result: xún yīn liú gē
result === expect? yes

test id: 4
input: han4 yu3 pin1 yin1 fang1 an4
expect: hàn yǔ pīn yīn fāng àn
result: hàn yǔ pīn yīn fāng àn
result === expect? yes

test id: 5
input: huar1 renr2 shuir3 yuer4
expect: huār rénr shuǐr yuèr
result: huār rénr shuǐr yuèr
result === expect? yes

test id: 6
input: b p m f
expect: b p m f
result: b p m f
result === expect? yes

test id: 7
input: an1 vn2 ong3 uen4
expect: ān ǘn ǒng uèn
result: ān ǘn ǒng uèn
result === expect? yes

test id: 8
input: liou2 yuen2
expect: liú yún
result: liú yún
result === expect? yes

test id: 9
input: iou1 uen4
expect: iōu uèn
result: iōu uèn
result === expect? yes

test id: 10
input: bong1 tv2 pe3 wir4
expect: bōng tǘ pě wìr
result: bōng tǘ pě wìr
result === 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
result: qǐ lái bú yuàn zuò nú lì de rén men
result === 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
result: huār wèi shén me zhè yàng hóng
result === expect? yes

toPinyin V2

test id: 1
input: test
expect: test
result: test
result === expect?: yes

test id: 2
input: chu1 yin1 wei4 lai2
expect: chū yīn wèi lái
result: chū yīn wèi lái
result === expect?: yes

test id: 3
input: xun2 yin1 liu2 ge1
expect: xún yīn liú gē
result: xún yīn liú gē
result === expect?: yes

test id: 4
input: han4 yu3 pin1 yin1 fang1 an4
expect: hàn yǔ pīn yīn fāng àn
result: hàn yǔ pīn yīn fāng àn
result === expect?: yes

test id: 5
input: huar1 renr2 shuir3 yuer4
expect: huār rénr shuǐr yuèr
result: huār rénr shuǐr yuèr
result === expect?: yes

test id: 6
input: b p m f
expect: b p m f
result: b p m f
result === expect?: yes

test id: 7
input: an1 vn2 ong3 uen4
expect: ān ǘn ǒng uèn
result: ān ǘn ǒng uèn
result === expect?: yes

test id: 8
input: liou2 yuen2
expect: liú yún
result: lióu yuén
result === expect?: NO!
expect2: lióu yuén
result === expect2? yes

test id: 9
input: iou1 uen4
expect: iōu uèn
result: iōu uèn
result === expect?: yes

test id: 10
input: bong1 tv2 pe3 wir4
expect: bōng tǘ pě wìr
result: bōng tǘ pě wìr
result === 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
result: qǐ lái bú yuàn zuò nú lì de rén men
result === 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
result: huār wèi shén me zhè yàng hóng
result === expect?: yes