Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pinyin-tone

mrchenguozheng482MIT2.3.4TypeScript support: included

A library for converting pinyin tone numbers into pinyin tone marks.

pinyin, mandarin, tone, chinese, convert, kanji, hanzi, hanyu

readme

pinyin-tone

A lightweight library to algorithmically convert numerical tone notation (e.g., hao3) to standard Pinyin diacritical marks (e.g., hǎo).

Designed for developers and learners who need accurate tone mark formatting in language apps, input methods, or educational tools.

Setup

NPM

Installation via npm:

通过NPM安装:

npm install pinyin-tone

Usage (Default Behavior)

This module exports a function. You can name it as desired, such as pinyin.

// CommonJS usage (up to version 2.2.7)
var toPinyinTones = require('pinyin-tone');

// /* or ES Module usage (breaking change in version 2.3.0+) */
// import toPinyinTones from 'pinyin-tone';

// Examples
var hatsune_miku = toPinyinTones('chu1 yin1 wei4 lai2'); // -> 'chū yīn wèi lái'
var megurine_luka = toPinyinTones("xun2 yin1 liu2 ge1"); // -> 'xún yīn liú gē'
var han = toPinyinTones("han4 yu3 pin1 yin1 fang1 an4"); // -> 'hàn yǔ pīn yīn fāng àn'

// Erhua / 儿化音 (ér huà yīn)
var huar = toPinyinTones("huar1 renr2 shuir3 yuer4"); // -> 'huār rénr shuǐr yuèr'

// Initials / 声母 (shēng mǔ)
var bpmf = toPinyinTones("b p m f"); // -> 'b p m f'

// Finals / 韵母 (yùn mǔ)
var yun = toPinyinTones("an1 vn2 ong3 uen4"); // -> 'ān ǘn ǒng uèn'

// Special cases
var liuyun = toPinyinTones("liou2 yuen2"); // -> (only in v2) 'lióu yuén'
var iou_uen = toPinyinTones("iou1 uen4"); // -> 'iōu uèn' (pure final only)

// Some less common combinations
var fantastic = toPinyinTones("bong1 tv2 pe3 wir4"); // -> 'bōng tǘ pě wìr'

Usage v2 (Exprimental)

There is one key difference between v1 and v2:

v2 does not support the format pinyin-number-r (e.g., hua1r) for erhua pronunciation.

Instead, it supports the format pinyin-r-number, such as huar1.

// CommonJS usage (up to version 2.2.7)
var anotherPinyinTones = require('pinyin-tone/v2');

// ES Module usage (breaking change in version 2.3.0)
import anotherPinyinTones from 'pinyin-tone/v2';

console.log(anotherPinyinTones('qi3 lai2 bu2 yuan4 zuo4 nu2 li4 de ren2 men'));
// -> 'qǐ lái bú yuàn zuò nú lì de rén men'

console.log(anotherPinyinTones('huar1 wei4 shen2 me zhe4 yang4 hong2'));
// -> 'huār wèi shén me zhè yàng hóng'

Contributors

Contributors

Contributing

If you find any bugs, glitches, or have feature requests, please submit them here. Thank you!

Changelog

For update history, see the CHANGELOG here.

Tests

see test/*.js.

Tests have been rewritten. — 2025-03-16

License

MIT

About 汉语拼音方案(hàn yǔ pīn yīn fāng àn)

You can find the official document 汉语拼音方案.pdf at http://www.moe.gov.cn/jyb_sjzl/ziliao/A19/195802/t19580201_186000.html

changelog

Changelog

更新日志

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog | 如何维护更新日志,

and this project adheres to Semantic Versioning | 语义化版本.

[2.3.3]

  • Fixed: TypeScript type error TS2306
  • Updated: add contributors in README.md

[2.3.0]

  • BREAKING: Convert this package from a CommonJS package to an ES Module

[2.2.7]

  • revert the changes: liou -> liou, yuen -> yuen

[2.2.6] - deprecated, but the fixes was preseved in [2.2.7]

  • Fixed: strings that match the regex rule but are not present in pinyin_dict_v2 result in pinyin_dict_v2[match.groups.abc] being undefined.
  • Fixed: 修复了匹配正则表达式的字符串但不在 pinyin_dict_v2 中的问题,导致 pinyin_dict_v2[match.groups.abc] 未定义。
  • Change: liou -> liu, yuen -> yun

[2.2.2-2.2.5] 2025-02-18

  • Bumps the npm_and_yarn group with 2 updates: serialize-javascript and mocha.
  • Remove personal website link from README.MD.
  • Change username of author's account on GitHub and update links.

[2.2.1] 2024-04-04

  • Modify README.md.

[2.2.0] 2023-04-23

Patched

  • npm audit fix

Feature

  • Add a v2 version.

[2.1.2] 2022-01-26

Patched

  • bump nanoid from 3.1.25 to 3.2.0
    nanoid  <3.1.31
    Severity: moderate
    Exposure of Sensitive Information to an Unauthorized Actor in nanoid - https://github.com/advisories/GHSA-qrpm-p2h7-hrv2

[2.1.0] 2021-11-02

  • add support for tone mark 5.
  • update devDependencies: mocha ^7.2.0 ->^9.1.3

[2.0.7] 2021-08-13

Patched

  • node_modules/glob-parent 5.1.1 -> 5.1.2
glob-parent  <5.1.2
Severity: moderate
Regular expression denial of service - https://npmjs.com/advisories/1751

[2.0.6] 2021-05-21

Added

  • index.d.ts: built-in TypeScript declaration.

Patched

  • node_modules/lodash: 4.17.20 -> 4.17.21
  • repository url in package.json.

[2.0.4] 2021-03-30

Fixed

  • Upgrade y18n to v4.0.1 because the npm package y18n before version 3.2.2, 4.0.1, and 5.0.5 is vulnerable to Prototype Pollution.
  • y18n升级到v4.0.1因为3.2.2、4.0.1和5.0.5之前的版本易受原型污染。

Upgrade

  • Upgrade chai to v4.3.4.
  • chai升级到v4.3.4。

[2.0.0] 2020-11-11

Changed

  • Rewrite main code.
  • 重写了主要代码
  • Rewrite test.
  • 重写了测试。

Fixed

  • Add /ui/.
  • 补上了/ui/。

[1.2.2]

Changed

  • update dependencies to fix security alerts
  • 更新依赖,修复安全警告

[1.2.0]

Changed

  • more tests.
  • 更多的测试。

[1.1.0] - 2019-03-05

Changed

  • add vowels ü/ün/üe/üan for mocha test.
  • 为mocha测试增加了ü/ün/üe/üan
  • tone number allows 0 now.
  • 声调数字可以为0了。

Added

  • add mocha test.
  • 增加了mocha测试。

[1.0.0] - 2019-03-05

Changed

  • repository changed.
  • update README.md

[0.1.4] - 2019-02-27

Fixed

  • update README.md. correct spellings.
  • 更新README.md。修正拼写。
  • add description in Simplified Chinese.
  • 增加简体中文描述。

[0.1.3] - 2019-02-20

Fixed

  • update README.md. correct spellings and urls.
  • 更新README.md。修正拼写和链接。

[0.1.1] - 2019-02-18

Fixed

  • missing y and w.
  • 缺少yw