{"id":16485836,"url":"https://github.com/creeperyang/pinyin","last_synced_at":"2025-04-08T04:14:25.828Z","repository":{"id":20774727,"uuid":"90880206","full_name":"creeperyang/pinyin","owner":"creeperyang","description":"Pure JavaScript library for converting Hanzi to Pinyin.","archived":false,"fork":false,"pushed_at":"2023-03-03T23:40:56.000Z","size":1902,"stargazers_count":649,"open_issues_count":15,"forks_count":48,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-10-29T22:52:16.694Z","etag":null,"topics":["chinese-characters","hanzi","hanzi-pinyin","pinyin","tiny-pinyin"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/creeperyang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-10T15:32:00.000Z","updated_at":"2024-10-26T06:09:00.000Z","dependencies_parsed_at":"2024-06-18T12:35:51.326Z","dependency_job_id":"f1aecd72-8cb8-43c1-85f6-ad22355d2f6b","html_url":"https://github.com/creeperyang/pinyin","commit_stats":{"total_commits":100,"total_committers":4,"mean_commits":25.0,"dds":"0.050000000000000044","last_synced_commit":"b1b274fad82d6a52f4d5eacd8d94a61c4eb55800"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creeperyang%2Fpinyin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creeperyang%2Fpinyin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creeperyang%2Fpinyin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creeperyang%2Fpinyin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/creeperyang","download_url":"https://codeload.github.com/creeperyang/pinyin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773719,"owners_count":20993639,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chinese-characters","hanzi","hanzi-pinyin","pinyin","tiny-pinyin"],"created_at":"2024-10-11T13:27:26.949Z","updated_at":"2025-04-08T04:14:25.810Z","avatar_url":"https://github.com/creeperyang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tiny-pinyin [![Build Status](https://travis-ci.org/creeperyang/pinyin.svg?branch=master)](https://travis-ci.org/creeperyang/pinyin) [![npm version](https://badge.fury.io/js/tiny-pinyin.svg)](https://badge.fury.io/js/tiny-pinyin) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fcreeperyang%2Fpinyin.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fcreeperyang%2Fpinyin?ref=badge_shield)\n\n[English Doc](./README_EN.md)\n\n[![Build Status](https://saucelabs.com/browser-matrix/creeperyang.svg)](https://saucelabs.com/beta/builds/8f2adabb0c47479fbcf50d1bbcdf8ecb)\n\n轻量的 **汉字转拼音** JavaScript库。可以轻松获取汉字的拼音。有以下特性：\n\n1. 300行左右代码，内置一个很小的字典。\n2. 可以轻松处理 **6763** 个的常用汉字，其它汉字未测试，但应该有相当正确率，欢迎测试。\n3. 同时支持 **`node.js (4-latest)` 和 浏览器 (safari/chrome/firefox/android 6+/ios)** 。\n\n**注意：不支持多音字；`ie/edge` 测试未通过。**\n\n## 安装和使用\n\n[![NPM](https://nodei.co/npm/tiny-pinyin.png?compact=true)](https://nodei.co/npm/tiny-pinyin/)\n\n使用（浏览器端使用可通过`webpack`等打包，或直接引入demo中[已打包好的JS](https://creeperyang.github.io/pinyin/browser.js)）:\n\n```js\n// test.js\nconst pinyin = require('tiny-pinyin')\n\nif (pinyin.isSupported()) {\n  pinyin.convertToPinyin('我') // WO\n}\n```\n\n注意在浏览器中使用时页面的charset 必须为utf-8 ，见[issue#21](https://github.com/creeperyang/pinyin/issues/21)\n\n一般情况下，我们的`node.js`只支持英文，所以，我们需要让`node.js`支持中文（`zh-Hans-CN`），即`pinyin.isSupported()`为`true`：\n\n```bash\nnpm i --save full-icu\n```\n\n通过安装`full-icu`，我们可以安装缺失的`ICU`数据文件，使`node.js`支持中文。通过`node --icu-data-dir=node_modules/full-icu test.js`即可使`node.js`支持全语言，正确把汉字转为拼音。\n\n更多相关信息可以参考 [full-icu-npm](https://github.com/unicode-org/full-icu-npm)，或者 [Node Intl](https://github.com/nodejs/node/wiki/Intl)。\n\n### Important: node 13+ support full icu by default\n\nnode 13 开始，node 默认开始完整的ICU支持，意味着我们不必再安装 `full-icu`。\n\n详情看\u003chttps://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V13.md#notable-changes-16\u003e。\n\n## Demo\n\n\u003ca href=\"https://creeperyang.github.io/pinyin/\"\u003e\n\u003cimg src=\"https://cloud.githubusercontent.com/assets/8046480/25986097/a86896c2-3720-11e7-9bfe-17285f8005f0.png\" width=\"295\" height=\"421\" alt=\"Demo\" /\u003e\n\u003c/a\u003e\n\n可点击上面的图片体验[线上版本](https://creeperyang.github.io/pinyin/)。\n\n## API\n\n*已通过 [c6b3ba9](https://github.com/creeperyang/pinyin/commit/c6b3ba9fcd66e0d1225ddbc95fc84c6fa75e664e)@zhanba 支持 typescript typings。*\n\n### 1. `pinyin.isSupported([forceRedetect])`\n\n- `forceRedetect`，`bool`类型，是否强制重新检测。\n\n测试环境是否支持`Intl.Collator`以及`zh-CN`。本库所有功能依赖此支持。默认只检测一次，可以置`forceRedetect`为`true`强制重新检测。\n\n### 2. `pinyin.parse(string)`\n\n- `string`，待转成拼音的字符串。\n\n返回指定字符串转成的token数组。典型的token格式如下：\n\n```js\n{\n  type: Number, // 1-拉丁, 2-拼音, 3-未知\n  source: String, // 源字符\n  target: String // 目标字符\n}\n```\n\n**请注意，当字符串为：**\n\n1. 拉丁字母，即ascii码`0-255`，不处理，原样输出，即`source/target`一致，`type`为`1`。\n2. 中文，即unicode `\\u4e00-\\u9FFF` ，转成拼音，`type`为`2`。\n3. 其它，即以上两者以外的字符，不处理，原样输出，`type`为`3`。\n\n### 3. `pinyin.convertToPinyin(string[, separator[, lowerCase]])`\n\n- `string`，待转成拼音的字符串。\n- `separator`，拼音的分隔符，默认`''`。比如设置`-`，则`我们`转成`WO-MEN`。\n- `lowerCase`，转成的拼音是否小写，默认`false`。仅对中文转成的拼音起效（对拉丁文字和其它文字无效）。\n\n返回指定字符串转成的拼音字符串。\n\n```js\npinyin.convertToPinyin('我们和他们', '-', true) // wo-men-he-ta-men\n```\n\n### 4. `pinyin.patchDict(fn|[fn])`\n\n- `fn`，`function`类型，接受参数为当前使用的字典对象`DICT`，可以修改`DICT.UNIHANS/DICT.PINYINS/DICT.EXCEPTIONS`来修改字典。另外，`fn`可以是数组，数组的每个元素为函数类型。\n\n其中：`DICT.UNIHANS/DICT.PINYINS`两者相对应，记录边界汉字和其对应拼音。`DICT.EXCEPTIONS`是 **例外** 字典，为`汉字-拼音`的键值对，拥有更高优先级。\n\n## 致谢\n\n感谢博客[利用Android源码，轻松实现汉字转拼音功能](http://blog.coderclock.com/2017/04/04/android/2017-04-04/)，由这篇博客才知道Android库的相关代码和汉字转拼音的原理。\n\n非常感谢 [Android Contacts Source Code](https://android.googlesource.com/platform/packages/providers/ContactsProvider/+/0c49720fb3d58e346739c2ccd56ed2b739249e07/src/com/android/providers/contacts/HanziToPinyin.java)。本库由它启发，可以算作Java到JavaScript的一次转译。\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fcreeperyang%2Fpinyin.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fcreeperyang%2Fpinyin?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreeperyang%2Fpinyin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcreeperyang%2Fpinyin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreeperyang%2Fpinyin/lists"}