feat: docs
This commit is contained in:
parent
b5385e20a6
commit
90832540f4
303
README.md
303
README.md
|
@ -22,20 +22,24 @@ cnchar 由于重建过一次仓库,所以之前的 star 清零了 (〒︿〒)
|
|||
|
||||
---
|
||||
|
||||
|
||||
<details>
|
||||
<summary>展开目录</summary>
|
||||
|
||||
<!-- toc -->
|
||||
|
||||
- [前言](#前言)
|
||||
- [0.快速使用](#0快速使用)
|
||||
- [1.功能](#1功能)
|
||||
- [2.概览](#2概览)
|
||||
- [3 安装](#3-安装)
|
||||
- [3.安装](#3安装)
|
||||
- [3.1 使用 npm 安装](#31-使用-npm-安装)
|
||||
- [3.2 使用 script 引入](#32-使用-script-引入)
|
||||
- [4 使用](#4-使用)
|
||||
- [4.使用](#4使用)
|
||||
- [4.1 webpack浏览器环境(有window对象)](#41-webpack浏览器环境有window对象)
|
||||
- [4.2 nodejs 等非浏览器环境](#42-nodejs-等非浏览器环境)
|
||||
- [4.3 原生浏览器环境](#43-原生浏览器环境)
|
||||
- [5 API](#5-api)
|
||||
- [5.API](#5api)
|
||||
- [5.1 拼音笔画基础 API: spell & stroke](#51-拼音笔画基础-api-spell--stroke)
|
||||
- [5.2 可视化绘制汉字: draw](#52-可视化绘制汉字-draw)
|
||||
- [5.2.1 使用](#521-使用)
|
||||
|
@ -44,14 +48,23 @@ cnchar 由于重建过一次仓库,所以之前的 star 清零了 (〒︿〒)
|
|||
- [5.4 笔画序列推出原汉字: orderToWord](#54-笔画序列推出原汉字-ordertoword)
|
||||
- [5.5 通过拼音查询原汉字: spellToWord](#55-通过拼音查询原汉字-spelltoword)
|
||||
- [5.6 通过笔画数查询原汉字: strokeToWord](#56-通过笔画数查询原汉字-stroketoword)
|
||||
- [5.7 查询拼音详细信息: spellInfo](#57-查询拼音详细信息-spellinfo)
|
||||
- [5.8 其他 api](#58-其他-api)
|
||||
- [5.8.1 .use()](#581-use)
|
||||
- [5.8.2 .type](#582-type)
|
||||
- [5.8.3 .check](#583-check)
|
||||
- [5.8.4 .version](#584-version)
|
||||
- [5.8.5 .plugins](#585-plugins)
|
||||
- [6 spell stroke 参数](#6-spell-stroke-参数)
|
||||
- [5.7 成语功能](#57-成语功能)
|
||||
- [5.8 歇后语功能](#58-歇后语功能)
|
||||
- [5.9 汉字、拼音工具方法](#59-汉字拼音工具方法)
|
||||
- [5.9.1 查询拼音详细信息: spellInfo](#591-查询拼音详细信息-spellinfo)
|
||||
- [5.9.2 拼音音调操作: transformTone](#592-拼音音调操作-transformtone)
|
||||
- [5.9.3 是否是汉字: isCnChar](#593-是否是汉字-iscnchar)
|
||||
- [5.9.4 比较拼音(汉字)大小: compareSpell](#594-比较拼音汉字大小-comparespell)
|
||||
- [5.9.5 比较汉字笔画数大小: compareStroke](#595-比较汉字笔画数大小-comparestroke)
|
||||
- [5.9.6 根据拼音排序: sortSpell](#596-根据拼音排序-sortspell)
|
||||
- [5.9.7 根据笔画数排序: sortStroke](#597-根据笔画数排序-sortstroke)
|
||||
- [5.10 其他 api](#510-其他-api)
|
||||
- [5.10.1 .use()](#5101-use)
|
||||
- [5.10.2 .type](#5102-type)
|
||||
- [5.10.3 .check](#5103-check)
|
||||
- [5.10.4 .version](#5104-version)
|
||||
- [5.10.5 .plugins](#5105-plugins)
|
||||
- [6.spell stroke 参数](#6spell-stroke-参数)
|
||||
- [6.1 spell 参数](#61-spell-参数)
|
||||
- [6.2 stroke 参数](#62-stroke-参数)
|
||||
- [6.3 orderToWord 参数](#63-ordertoword-参数)
|
||||
|
@ -69,10 +82,21 @@ cnchar 由于重建过一次仓库,所以之前的 star 清零了 (〒︿〒)
|
|||
- [6.8.4.2 spell 和 stroke 方法](#6842-spell-和-stroke-方法)
|
||||
- [6.8.5 cnchar-idiom 库功能](#685-cnchar-idiom-库功能)
|
||||
- [6.8.6 cnchar-xhy 库功能](#686-cnchar-xhy-库功能)
|
||||
- [7 应用例子](#7-应用例子)
|
||||
- [6.8.7 工具方法](#687-工具方法)
|
||||
- [6.8.7.1 spellInfo](#6871-spellinfo)
|
||||
- [6.8.7.2 isCnChar](#6872-iscnchar)
|
||||
- [6.8.7.3 transformTone](#6873-transformtone)
|
||||
- [6.8.7.4 compareSpell](#6874-comparespell)
|
||||
- [6.8.7.5 compareStroke](#6875-comparestroke)
|
||||
- [6.8.7.6 sortSpell](#6876-sortspell)
|
||||
- [6.8.7.7 sortStroke](#6877-sortstroke)
|
||||
- [7.应用例子](#7应用例子)
|
||||
|
||||
<!-- tocstop -->
|
||||
|
||||
|
||||
</details>
|
||||
|
||||
---
|
||||
|
||||
### 前言
|
||||
|
@ -81,6 +105,18 @@ cnchar 由于重建过一次仓库,所以之前的 star 清零了 (〒︿〒)
|
|||
|
||||
[我要反馈错误或缺漏](https://github.com/theajack/cnchar/issues/new)
|
||||
|
||||
关于该文档
|
||||
|
||||
由于文档较长,做一下简介,请按照需要阅读
|
||||
|
||||
- 第零章可以帮助开发者快速接入cnchar
|
||||
- 第一、二章介绍了cnchar的功能及其功能库
|
||||
- 第三章介绍了cnchar的安装和使用
|
||||
- 第四章介绍了各种不同环境下cnchar的使用差异
|
||||
- 第五章详细介绍了cnchar及其功能库的API使用
|
||||
- 第六章列举了各个方法的参数和大量的cnchar使用实例
|
||||
- 第七章介绍了一些cnchar使用案例
|
||||
|
||||
### 0.快速使用
|
||||
|
||||
使用 npm 安装:
|
||||
|
@ -143,7 +179,7 @@ import cnchar from 'cnchar';
|
|||
| cnchar-idiom | 成语库 | 支持成语查询等功能 | 2.2+ |
|
||||
| cnchar-xhy | 歇后语库 | 支持歇后语查询等功能 | 2.2+ |
|
||||
|
||||
### 3 安装
|
||||
### 3.安装
|
||||
|
||||
#### 3.1 使用 npm 安装
|
||||
|
||||
|
@ -184,8 +220,7 @@ npm i cnchar-all
|
|||
<script src="https://cdn.jsdelivr.net/npm/cnchar-all/cnchar.all.min.js"></script>
|
||||
```
|
||||
|
||||
|
||||
### 4 使用
|
||||
### 4.使用
|
||||
|
||||
#### 4.1 webpack浏览器环境(有window对象)
|
||||
|
||||
|
@ -248,10 +283,11 @@ console.log(cnchar.spell('汉字')); // cnchar api 调用
|
|||
</script>
|
||||
```
|
||||
|
||||
### 5 API
|
||||
### 5.API
|
||||
|
||||
类型声明:[cnchar.d.ts](https://github.com/theajack/cnchar/blob/master/src/main/index.d.ts) | [cnchar-order.d.ts](https://github.com/theajack/cnchar/blob/master/src/plugin/order/index.d.ts) | [cnchar-trad.d.ts](https://github.com/theajack/cnchar/blob/master/src/plugin/trad/index.d.ts)
|
||||
|
||||
注:该章节仅介绍API用法,更多使用实例请参考第六章
|
||||
|
||||
#### 5.1 拼音笔画基础 API: spell & stroke
|
||||
|
||||
|
@ -498,7 +534,15 @@ cnchar.strokeToWord(25, 'simple'); // 返回 '鬣馕囔戆攮纛'
|
|||
cnchar.strokeToWord(1, 'array'); // 返回 ['一', '乙']
|
||||
```
|
||||
|
||||
#### 5.7 查询拼音详细信息: spellInfo
|
||||
#### 5.7 成语功能
|
||||
|
||||
cnchar在2.2.0加入了成语功能,安装 `cnchar-draw`
|
||||
|
||||
#### 5.8 歇后语功能
|
||||
|
||||
#### 5.9 汉字、拼音工具方法
|
||||
|
||||
##### 5.9.1 查询拼音详细信息: spellInfo
|
||||
|
||||
`spellInfo` 方法用于查询拼音的详细信息,用法如下:
|
||||
|
||||
|
@ -532,9 +576,126 @@ cnchar.spellInfo.tones;
|
|||
// n 的一声使用 * 代替
|
||||
```
|
||||
|
||||
#### 5.8 其他 api
|
||||
##### 5.9.2 拼音音调操作: transformTone
|
||||
|
||||
##### 5.8.1 .use()
|
||||
`transformTone` 方法用于将有音调拼音转换为无音调拼音,且可以获取音调位置和声调
|
||||
|
||||
使用方式如下:
|
||||
|
||||
```ts
|
||||
cnchar.transformTone(spell: string, tone?: boolean, type?: 'low' | 'up');
|
||||
/* 返回值
|
||||
{
|
||||
spell: string; // 转换后的拼音
|
||||
tone: toneType; // 声调
|
||||
index: number; // 音调位置
|
||||
isTrans: boolean; // 是否是经过转换的比如 lv2 -> lǘ
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
tone 为可选参数,表示返回值spell是否需要带上声调,默认为 false
|
||||
|
||||
type 为可选参数,表示返回值spell设置大小写,默认为 'low'
|
||||
|
||||
transformTone spell参数 支持使用 v 代替 ü,支持使用末尾带数字表示声调,比如 `lv 等价于 lü` `shang4 等价于 shàng`
|
||||
|
||||
##### 5.9.3 是否是汉字: isCnChar
|
||||
|
||||
`isCnChar` 方法用于判断一个字符是否是汉字
|
||||
|
||||
```ts
|
||||
cnchar.isCnChar(word: string): boolean;
|
||||
```
|
||||
|
||||
##### 5.9.4 比较拼音(汉字)大小: compareSpell
|
||||
|
||||
`compareSpell` 方法用于按照拼音比较拼音或汉字的大小,可用于通讯录姓名拼音排序等场景
|
||||
|
||||
该方法支持按照拼音和声调比较,如需排序可以参考 `sortSpell` 方法
|
||||
|
||||
```ts
|
||||
cnchar.compareSpell(spell1: string, spell2: string, tone?: boolean);
|
||||
```
|
||||
|
||||
tone参数表示是否需要按照音调比较,默认为false
|
||||
|
||||
该方法返回一个字符串,'more', 'less', 'even' 分别表示 spell1 大于、小于、等于 spell2
|
||||
|
||||
例
|
||||
|
||||
```js
|
||||
cnchar.compareSpell('ao', 'ai') // 返回 'more' 因为 o 排在 i 之后
|
||||
cnchar.compareSpell('奥', 'ai') // 返回 'more'
|
||||
```
|
||||
|
||||
##### 5.9.5 比较汉字笔画数大小: compareStroke
|
||||
|
||||
`compareStroke` 方法用于按照笔画数比较汉字大小,可用于按照姓名首个汉字笔画排序等场景,排序可以参考 `sortStroke` 方法
|
||||
|
||||
```ts
|
||||
cnchar.compareStroke(stroke1: string, stroke2: string);
|
||||
```
|
||||
|
||||
该方法支持输入汉字或数字,汉字可以输入多个
|
||||
|
||||
该方法返回一个字符串,'more', 'less', 'even' 分别表示 stroke1 大于、小于、等于 stroke2
|
||||
|
||||
例子:
|
||||
|
||||
```js
|
||||
cnchar.compareStroke('你', '好') // 返回 'more'
|
||||
cnchar.compareStroke(20, '好') // 返回 'more'
|
||||
cnchar.compareStroke('一个', '好') // 返回 'less'
|
||||
```
|
||||
|
||||
##### 5.9.6 根据拼音排序: sortSpell
|
||||
|
||||
`sortSpell` 方法用于按照拼音排序汉字或拼音,支持输入数组或字符串,支持按照声调排序、支持倒序
|
||||
|
||||
```ts
|
||||
cnchar.sortSpell(spells:Array<string> | string, ...args?: Array<'tone'|'desc'>): Array<string> | string;
|
||||
```
|
||||
|
||||
spells参数可以是数组或字符串
|
||||
|
||||
当为数组时,数组元素可以时汉字或拼音,返回的是数组
|
||||
|
||||
当为字符串时,字符串必须全部是汉字,返回的是字符串
|
||||
|
||||
该方法可选参数有两个,'tone' 表示按照音调排序,'desc' 表示倒序,默认不区分声调且升序。请看一些例子
|
||||
|
||||
```js
|
||||
cnchar.sortSpell(['你', '好', '吗']) // ['好', '吗', '你']
|
||||
cnchar.sortSpell('你好吗') // '好吗你'
|
||||
cnchar.sortSpell('拼品频爱', 'tone', 'desc') // '品频拼爱'
|
||||
```
|
||||
|
||||
##### 5.9.7 根据笔画数排序: sortStroke
|
||||
|
||||
`sortStroke` 方法用于按照笔画数排序汉字
|
||||
|
||||
```ts
|
||||
cnchar.sortStroke(strokes:Array<string|number> | string, desc?: 'desc'): Array<string> | string;
|
||||
```
|
||||
|
||||
strokes参数可以是数组或字符串
|
||||
|
||||
当为数组时,数组元素可以时汉字或数字,返回的是数组
|
||||
|
||||
当为字符串时,字符串必须全部是汉字,返回的是字符串
|
||||
|
||||
该方法有一个可选参数,'desc' 表示倒序,默认升序。请看一些例子
|
||||
|
||||
```js
|
||||
cnchar.sortStroke(['一', '三', '二']) // ['一', '二', '三']
|
||||
cnchar.sortStroke(['一', '三', 2]) // ['一', 2, '三'],
|
||||
cnchar.sortStroke('一三二', 'desc') // '三二一'
|
||||
```
|
||||
|
||||
#### 5.10 其他 api
|
||||
|
||||
##### 5.10.1 .use()
|
||||
|
||||
这个 api 的功能是显式启用 `poly`、`order`、`trad` 三个功能库
|
||||
|
||||
|
@ -563,7 +724,7 @@ import 'cnchar-order';
|
|||
import 'cnchar-trad';
|
||||
```
|
||||
|
||||
##### 5.8.2 .type
|
||||
##### 5.10.2 .type
|
||||
|
||||
type 对象用户获取当前可用的 `spell` 、 `stroke` 、 `orderToWord` 、`spellToWord`、`strokeToWord` 参数类型:
|
||||
|
||||
|
@ -587,7 +748,7 @@ strokeToWordArg 最多可用值: `['simple','trad','array']`
|
|||
|
||||
具体用法<a href="#user-content-6-spell-stroke-参数">第六章</a>讲到
|
||||
|
||||
##### 5.8.3 .check
|
||||
##### 5.10.3 .check
|
||||
|
||||
该值是一个 布尔类型,用于控制是否开启参数校验,默认值为 true
|
||||
|
||||
|
@ -597,7 +758,7 @@ strokeToWordArg 最多可用值: `['simple','trad','array']`
|
|||
cnchar.check = false; // 关闭参数校验
|
||||
```
|
||||
|
||||
##### 5.8.4 .version
|
||||
##### 5.10.4 .version
|
||||
|
||||
获取当前版本:
|
||||
|
||||
|
@ -605,7 +766,7 @@ cnchar.check = false; // 关闭参数校验
|
|||
var version = cnchar.version; // string 类型
|
||||
```
|
||||
|
||||
##### 5.8.5 .plugins
|
||||
##### 5.10.5 .plugins
|
||||
|
||||
当前使用的功能库列表,最多的情况为 `["order", "trad", "poly"]`
|
||||
|
||||
|
@ -613,7 +774,7 @@ var version = cnchar.version; // string 类型
|
|||
var plugins = cnchar.plugins; // array 类型
|
||||
```
|
||||
|
||||
### 6 spell stroke 参数
|
||||
### 6.spell stroke 参数
|
||||
|
||||
#### 6.1 spell 参数
|
||||
|
||||
|
@ -803,10 +964,6 @@ cnchar.spellToWord('lv2'); // 返回 "驴闾榈"
|
|||
|
||||
//strokeToWord 功能
|
||||
cnchar.strokeToWord(2); // 返回 "丁七乃乜九了二人亻儿入八冂几凵刀刁力勹"
|
||||
|
||||
//spellInfo 功能
|
||||
cnchar.spellInfo('shàng');
|
||||
// 返回 {spell: "shang", tone: 4, index: 3, initial: "sh", final: "ang"}
|
||||
```
|
||||
|
||||
备注:
|
||||
|
@ -927,11 +1084,99 @@ cnchar.convert.sparkToTrad('①个亾');
|
|||
|
||||
##### 6.8.5 cnchar-idiom 库功能
|
||||
|
||||
该库为cnchar扩展了成语功能
|
||||
|
||||
```js
|
||||
cnchar.idiom(['五', '', '十', '']) // ['五风十雨', '五光十色']
|
||||
cnchar.idiom([4, 6, 2, 6], 'stroke') // ['五光十色']
|
||||
cnchar.idiom('shang', 'spell') // ['伤风败化', '伤风败俗', ... ]
|
||||
cnchar.idiom('shang4', 'spell', 'tone') // ['伤风败化', '伤风败俗', ... ]
|
||||
```
|
||||
|
||||
##### 6.8.6 cnchar-xhy 库功能
|
||||
|
||||
该库为cnchar扩展了歇后语功能
|
||||
|
||||
### 7 应用例子
|
||||
```js
|
||||
cnchar.xhy('大水冲了龙王庙') // ['大水冲了龙王庙-自家人不识自家人', '大水冲了龙王庙-一家人不认一家人']
|
||||
cnchar.xhy('大水', 'fuzzy') // ['江河里长大水-泥沙俱下', '江河发大水-后浪推前浪', ... ]
|
||||
cnchar.xhy('大水', 'fuzzy', 'answer') // ['泥沙俱下', '后浪推前浪', ... ]
|
||||
cnchar.xhy('上晃下摇', 'fuzzy', 'answer', 'second') // ['醉汉过铁索桥', '扶着醉汉过破桥']
|
||||
```
|
||||
|
||||
##### 6.8.7 工具方法
|
||||
|
||||
cnchar提供了一些汉字工具方法,方便开发者更便捷高效地操作拼音和汉字
|
||||
|
||||
###### 6.8.7.1 spellInfo
|
||||
|
||||
```js
|
||||
cnchar.spellInfo('shàng');
|
||||
// 返回 {spell: "shang", tone: 4, index: 3, initial: "sh", final: "ang"}
|
||||
```
|
||||
|
||||
###### 6.8.7.2 isCnChar
|
||||
|
||||
```js
|
||||
cnchar.isCnChar('a') // false
|
||||
cnchar.isCnChar('1') // false
|
||||
cnchar.isCnChar('?') // false
|
||||
cnchar.isCnChar('国') // true
|
||||
cnchar.isCnChar('國') // true
|
||||
```
|
||||
|
||||
###### 6.8.7.3 transformTone
|
||||
|
||||
```js
|
||||
cnchar.transformTone('lv2') // {spell: 'lü', tone: 2, index: 2, isTrans: true}
|
||||
cnchar.transformTone('lv2', true) // {spell: 'lǘ', tone: 2, index: 2, isTrans: true}
|
||||
cnchar.transformTone('lv2', true, 'up') // {spell: 'LǗ', tone: 2, index: 2, isTrans: true}
|
||||
cnchar.transformTone('lǘ') // {spell: 'lü', tone: 2, index: 2, isTrans: false}
|
||||
```
|
||||
|
||||
###### 6.8.7.4 compareSpell
|
||||
|
||||
```js
|
||||
cnchar.compareSpell('ao', 'ai') // 'more'
|
||||
cnchar.compareSpell('ai', 'ai') // 'even'
|
||||
cnchar.compareSpell('pín', 'pǐn', 'tone') // 'less'
|
||||
cnchar.compareSpell('pin2', 'pǐn', 'tone') // 'less'
|
||||
cnchar.compareSpell('频', 'pǐn', 'tone') // 'less'
|
||||
cnchar.compareSpell('品', '频', 'tone') // 'more'
|
||||
cnchar.compareSpell('贫', '频', 'tone') // 'even'
|
||||
```
|
||||
|
||||
###### 6.8.7.5 compareStroke
|
||||
|
||||
```js
|
||||
cnchar.compareStroke('你', '好') // 'more'
|
||||
cnchar.compareStroke('你', '苏') // 'even'
|
||||
cnchar.compareStroke('好', '苏') // 'less'
|
||||
cnchar.compareStroke('一个', '好') // 'less'
|
||||
cnchar.compareStroke('你', 14) // 'less'
|
||||
```
|
||||
|
||||
###### 6.8.7.6 sortSpell
|
||||
|
||||
```js
|
||||
cnchar.sortSpell(['你', '好', '吗']) // ['好', '吗', '你']
|
||||
cnchar.sortSpell('你好吗') // '好吗你'
|
||||
cnchar.sortSpell(['拼', '品', '频', '爱'], 'tone') // ['爱', '拼', '频', '品']
|
||||
cnchar.sortSpell(['拼', '品', 'pin2', 'ai'], 'tone') // ['ai', '拼', 'pin2', '品']
|
||||
cnchar.sortSpell(['拼', '品', '频', '爱'], 'tone', 'desc') // ['品', '频', '拼', '爱']
|
||||
cnchar.sortSpell('拼品频爱', 'tone', 'desc') // '品频拼爱'
|
||||
```
|
||||
|
||||
###### 6.8.7.7 sortStroke
|
||||
|
||||
```js
|
||||
cnchar.sortStroke(['一', '三', '二']) // ['一', '二', '三']
|
||||
cnchar.sortStroke('一三二') // '一二三'
|
||||
cnchar.sortStroke(['一', '三', 2]) // ['一', 2, '三']
|
||||
cnchar.sortStroke(['一', '三', '二'], 'desc') // ['三', '二', '一']
|
||||
```
|
||||
|
||||
### 7.应用例子
|
||||
|
||||
[汉字打字游戏](https://www.theajack.com/type/)
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import 'easy-icon';
|
|||
import '../src/style/element.css';
|
||||
import '../src/style/main.css';
|
||||
import '../../public/config';
|
||||
import '../src/object-assign.polyfill';
|
||||
|
||||
// import leancloud from 'leancloud-storage';
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
J.id('spark').txt('火星文:' + str.convertSimpleToSpark('spark'));
|
||||
str = pickCnChar(str);
|
||||
if (str !== '') {
|
||||
window.cnchar.draw(str, {el: 'draw'});
|
||||
window.cnchar.draw(str, {el: '#draw'});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
if (typeof Object.assign === 'undefined') {
|
||||
Object.defineProperty(Object, 'assign', {
|
||||
value: function (target) {
|
||||
if (target == null) {
|
||||
throw TypeError('Cannot convert undefined or null to Object');
|
||||
}
|
||||
|
||||
target = new Object(target);
|
||||
var args = [].slice.call(arguments, 1);
|
||||
|
||||
for (var index = 0; index < args.length; index++) {
|
||||
var source = args[index];
|
||||
if (source == null) continue;
|
||||
for (var key in source) {
|
||||
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
||||
target[key] = source[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
},
|
||||
writable: true,
|
||||
configurable: true
|
||||
});
|
||||
}
|
|
@ -32,6 +32,18 @@ Thank you for your support for cnchar. Since the cnchar lexicon comes from the I
|
|||
|
||||
[I want to report errors or omissions](https://github.com/theajack/cnchar/issues/new)
|
||||
|
||||
About this document
|
||||
|
||||
Since the document is long, please do a brief introduction, please read as needed
|
||||
|
||||
-Chapter zero can help developers quickly access cnchar
|
||||
-Chapters 1 and 2 introduce the functions of cnchar and its function library
|
||||
-Chapter 3 introduces the installation and use of cnchar
|
||||
-Chapter 4 introduces the differences in the use of cnchar in various environments
|
||||
-Chapter 5 introduces the API usage of cnchar and its function library in detail
|
||||
-Chapter 6 lists the parameters of each method and a large number of cnchar use examples
|
||||
-Chapter 7 introduces some cnchar use cases
|
||||
|
||||
### 0. Quick use
|
||||
|
||||
Use npm to install:
|
||||
|
@ -94,7 +106,7 @@ cnchar|The main js library, the other three libraries depend on this library|Con
|
|||
| cnchar-idiom | idiom library | support idiom query and other functions | 2.2+ |
|
||||
| cnchar-xhy | Xiehou language library | Support Xiehouyu query and other functions | 2.2+ |
|
||||
|
||||
### 3 Installation
|
||||
### 3. Installation
|
||||
|
||||
#### 3.1 Install with npm
|
||||
|
||||
|
@ -137,7 +149,7 @@ Or use the following cdn, which contains the above seven libraries
|
|||
```
|
||||
|
||||
|
||||
### 4 Use
|
||||
### 4. Use
|
||||
|
||||
#### 4.1 webpack browser environment(with window object)
|
||||
|
||||
|
@ -200,10 +212,12 @@ The native browser environment requires the use of script tags to import js file
|
|||
</script>
|
||||
```
|
||||
|
||||
### 5 API
|
||||
### 5. API
|
||||
|
||||
Type declaration: [cnchar.d.ts](https://github.com/theajack/cnchar/blob/master/src/main/index.d.ts) | [cnchar-order.d.ts](https://github.com/theajack/cnchar/blob/master/src/plugin/order/index.d.ts) | [cnchar-trad.d.ts](https://github.com/theajack/cnchar/blob/master/src/plugin/trad/index.d.ts)
|
||||
|
||||
Note: This chapter only introduces API usage, please refer to Chapter 6 for more usage examples
|
||||
|
||||
#### 5.1 Basic Pinyin Stroke API: spell & stroke
|
||||
|
||||
In order to make the API use as simple as possible, the library has designed two main and very simple APIs, and to ensure that the calling method is consistent:
|
||||
|
@ -449,7 +463,15 @@ cnchar.strokeToWord(25,'simple'); // returns'鬣馕囔戆攮纛'
|
|||
cnchar.strokeToWord(1,'array'); // returns ['一','乙']
|
||||
```
|
||||
|
||||
#### 5.7 Query pinyin details: spellInfo
|
||||
#### 5.7 Idiom Function
|
||||
|
||||
cnchar added idiom function in 2.2.0, install `cnchar-draw`
|
||||
|
||||
#### 5.8 Xiehouyu function
|
||||
|
||||
#### 5.9 Chinese Characters and Pinyin Tools
|
||||
|
||||
##### 5.9.1 Query pinyin details: spellInfo
|
||||
|
||||
The `spellInfo` method is used to query the detailed information of Pinyin, the usage is as follows:
|
||||
|
||||
|
@ -483,9 +505,126 @@ cnchar.spellInfo.tones;
|
|||
// Use * instead of n
|
||||
```
|
||||
|
||||
#### 5.8 Other APIs
|
||||
##### 5.9.2 Pinyin tone operation: transformTone
|
||||
|
||||
##### 5.8.1 .use()
|
||||
The `transformTone` method is used to convert toned pinyin to toneless pinyin, and the tone position and tone
|
||||
|
||||
The usage is as follows:
|
||||
|
||||
```ts
|
||||
cnchar.transformTone(spell: string, tone?: boolean, type?: 'low' | 'up');
|
||||
/* return value
|
||||
{
|
||||
spell: string; // converted pinyin
|
||||
tone: toneType; // tone
|
||||
index: number; // tone position
|
||||
isTrans: boolean; // Whether it is converted such as lv2 -> lǘ
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
tone is an optional parameter, indicating whether the return value spell needs to bring a tone, the default is false
|
||||
|
||||
type is an optional parameter, indicating that the return value spell sets the case, the default is'low'
|
||||
|
||||
The transformTone spell parameter supports the use of v instead of ü, and the use of numbers at the end to represent the tone, for example, `lv is equivalent to lü`shang4 is equivalent to shàng`
|
||||
|
||||
##### 5.9.3 Is it Chinese characters: isCnChar
|
||||
|
||||
The `isCnChar` method is used to determine whether a character is a Chinese character
|
||||
|
||||
```ts
|
||||
cnchar.isCnChar(word: string): boolean;
|
||||
```
|
||||
|
||||
##### 5.9.4 Compare Pinyin (Chinese character) size: compareSpell
|
||||
|
||||
`CompareSpell` method is used to compare the size of Pinyin or Chinese characters according to Pinyin
|
||||
|
||||
This method supports comparison according to Pinyin and tones. For sorting, please refer to the `sortSpell` method
|
||||
|
||||
```ts
|
||||
cnchar.compareSpell(spell1: string, spell2: string, tone?: boolean);
|
||||
```
|
||||
|
||||
The tone parameter indicates whether to compare according to the tone, the default is false
|
||||
|
||||
This method returns a string,'more','less','even' means that spell1 is greater than, less than, equal to spell2
|
||||
|
||||
example:
|
||||
|
||||
```js
|
||||
cnchar.compareSpell('ao', 'ai') // Returns 'more' because o comes after i
|
||||
cnchar.compareSpell('奥', 'ai') // Returns 'more'
|
||||
```
|
||||
|
||||
##### 5.9.5 Compare the number of strokes of Chinese characters: compareStroke
|
||||
|
||||
The `compareStroke` method is used to compare the size of Chinese characters according to the number of strokes. It can be used to sort the first Chinese character strokes according to the name. For sorting, please refer to the `sortStroke` method
|
||||
|
||||
```ts
|
||||
cnchar.compareStroke(stroke1: string, stroke2: string);
|
||||
```
|
||||
|
||||
This method supports the input of Chinese characters or numbers, Chinese characters can be input multiple
|
||||
|
||||
This method returns a string,'more','less','even' means stroke1 is greater than, less than, equal to stroke2
|
||||
|
||||
example:
|
||||
|
||||
```js
|
||||
cnchar.compareStroke('你', '好') // Returns 'more'
|
||||
cnchar.compareStroke(20, '好') // Returns 'more'
|
||||
cnchar.compareStroke('一个', '好') // Returns 'less'
|
||||
```
|
||||
|
||||
##### 5.9.6 Sort by pinyin: sortSpell
|
||||
|
||||
The `sortSpell` method is used to sort Chinese characters or Pinyin according to Pinyin, supports input arrays or strings, supports sorting by tone, and supports reverse order
|
||||
|
||||
```ts
|
||||
cnchar.sortSpell(spells:Array<string> | string, ...args?: Array<'tone'|'desc'>): Array<string> | string;
|
||||
```
|
||||
|
||||
The spells parameter can be an array or a string
|
||||
|
||||
When it is an array, the array elements can be Chinese characters or Pinyin, and the returned array is
|
||||
|
||||
When it is a string, the string must be all Chinese characters, and the returned string
|
||||
|
||||
There are two optional parameters for this method,'tone' means sorting by tone, and'desc' means reverse order, by default, no distinction is made between tone and ascending order. Please see some examples
|
||||
|
||||
```js
|
||||
cnchar.sortSpell(['你', '好', '吗']) // ['好', '吗', '你']
|
||||
cnchar.sortSpell('你好吗') // '好吗你'
|
||||
cnchar.sortSpell('拼品频爱', 'tone', 'desc') // '品频拼爱'
|
||||
```
|
||||
|
||||
##### 5.9.7 Sort by number of strokes: sortStroke
|
||||
|
||||
The `sortStroke` method is used to sort Chinese characters according to the number of strokes
|
||||
|
||||
```ts
|
||||
cnchar.sortStroke(strokes:Array<string|number> | string, desc?:'desc'): Array<string> | string;
|
||||
```
|
||||
|
||||
The strokes parameter can be an array or a string
|
||||
|
||||
When it is an array, the array elements can be Chinese characters or numbers, and the returned array
|
||||
|
||||
When it is a string, the string must be all Chinese characters, and the returned string
|
||||
|
||||
This method has an optional parameter,'desc' means reverse order, default ascending order. Please see some examples
|
||||
|
||||
```js
|
||||
cnchar.sortStroke(['一', '三', '二']) // ['一', '二', '三']
|
||||
cnchar.sortStroke(['一', '三', 2]) // ['一', 2, '三'],
|
||||
cnchar.sortStroke('一三二', 'desc') // '三二一'
|
||||
```
|
||||
|
||||
#### 5.10 Other APIs
|
||||
|
||||
##### 5.10.1 .use()
|
||||
|
||||
The function of this API is to explicitly enable the three function libraries `poly`,`order`, and `trad`
|
||||
|
||||
|
@ -514,7 +653,7 @@ import 'cnchar-order';
|
|||
import 'cnchar-trad';
|
||||
```
|
||||
|
||||
##### 5.8.2 .type
|
||||
##### 5.10.2 .type
|
||||
|
||||
The type object user gets the currently available `spell`,` stroke`, `orderToWord`,` spellToWord`, `strokeToWord` parameter types:
|
||||
|
||||
|
@ -538,7 +677,7 @@ strokeToWordArg Maximum available values: `['simple','trad','array']`
|
|||
|
||||
Specific usage <a href="#user-content-6-spell-stroke-parameters"> Chapter 6 </a>
|
||||
|
||||
##### 5.8.3 .check
|
||||
##### 5.10.3 .check
|
||||
|
||||
This value is a Boolean type, used to control whether to enable parameter verification, the default value is true
|
||||
|
||||
|
@ -548,7 +687,7 @@ Parameter verification can check the incoming parameters of `spell` and` stroke`
|
|||
cnchar.check = false; // Turn off parameter check
|
||||
```
|
||||
|
||||
##### 5.8.4 .version
|
||||
##### 5.10.4 .version
|
||||
|
||||
Get the current version:
|
||||
|
||||
|
@ -556,7 +695,7 @@ Get the current version:
|
|||
var version = cnchar.version; // string type
|
||||
```
|
||||
|
||||
##### 5.8.5 .plugins
|
||||
##### 5.10.5 .plugins
|
||||
|
||||
List of currently used function libraries, the most common case is `["order","trad","poly"]`
|
||||
|
||||
|
@ -754,10 +893,6 @@ cnchar.spellToWord('lv2'); // returns"驴闾榈"
|
|||
|
||||
// strokeToWord function
|
||||
cnchar.strokeToWord(2); // returns"丁七乃乜九了二人亻儿入八冂几凵刀刁力勹"
|
||||
|
||||
// spellInfo function
|
||||
cnchar.spellInfo('shàng');
|
||||
// returns {spell:"shang", tone: 4, index: 3, initial:"sh", final:"ang"}
|
||||
```
|
||||
|
||||
Remarks:
|
||||
|
@ -878,9 +1013,97 @@ The library adds pinyin stroke function extension for traditional characters, an
|
|||
|
||||
##### 6.8.5 cnchar-idiom library function
|
||||
|
||||
This library extends the idiom function for cnchar
|
||||
|
||||
```js
|
||||
cnchar.idiom(['五', '', '十', '']) // ['五风十雨', '五光十色']
|
||||
cnchar.idiom([4, 6, 2, 6], 'stroke') // ['五光十色']
|
||||
cnchar.idiom('shang', 'spell') // ['伤风败化', '伤风败俗', ... ]
|
||||
cnchar.idiom('shang4', 'spell', 'tone') // ['伤风败化', '伤风败俗', ... ]
|
||||
```
|
||||
|
||||
##### 6.8.6 cnchar-xhy library function
|
||||
|
||||
This library extends the function of Xiehouyu for cnchar
|
||||
|
||||
```js
|
||||
cnchar.xhy('大水冲了龙王庙') // ['大水冲了龙王庙-自家人不识自家人', '大水冲了龙王庙-一家人不认一家人']
|
||||
cnchar.xhy('大水', 'fuzzy') // ['江河里长大水-泥沙俱下', '江河发大水-后浪推前浪', ... ]
|
||||
cnchar.xhy('大水', 'fuzzy', 'answer') // ['泥沙俱下', '后浪推前浪', ... ]
|
||||
cnchar.xhy('上晃下摇', 'fuzzy', 'answer', 'second') // ['醉汉过铁索桥', '扶着醉汉过破桥']
|
||||
```
|
||||
|
||||
##### 6.8.7 Tools and methods
|
||||
|
||||
cnchar provides some Chinese character tool methods to facilitate developers to operate pinyin and Chinese characters more conveniently and efficiently
|
||||
|
||||
###### 6.8.7.1 spellInfo
|
||||
|
||||
```js
|
||||
cnchar.spellInfo('shàng');
|
||||
// Returns {spell: "shang", tone: 4, index: 3, initial: "sh", final: "ang"}
|
||||
```
|
||||
|
||||
###### 6.8.7.2 isCnChar
|
||||
|
||||
```js
|
||||
cnchar.isCnChar('a') // false
|
||||
cnchar.isCnChar('1') // false
|
||||
cnchar.isCnChar('?') // false
|
||||
cnchar.isCnChar('国') // true
|
||||
cnchar.isCnChar('國') // true
|
||||
```
|
||||
|
||||
###### 6.8.7.3 transformTone
|
||||
|
||||
```js
|
||||
cnchar.transformTone('lv2') // {spell: 'lü', tone: 2, index: 2, isTrans: true}
|
||||
cnchar.transformTone('lv2', true) // {spell: 'lǘ', tone: 2, index: 2, isTrans: true}
|
||||
cnchar.transformTone('lv2', true, 'up') // {spell: 'LǗ', tone: 2, index: 2, isTrans: true}
|
||||
cnchar.transformTone('lǘ') // {spell: 'lü', tone: 2, index: 2, isTrans: false}
|
||||
```
|
||||
|
||||
###### 6.8.7.4 compareSpell
|
||||
|
||||
```js
|
||||
cnchar.compareSpell('ao', 'ai') // 'more'
|
||||
cnchar.compareSpell('ai', 'ai') // 'even'
|
||||
cnchar.compareSpell('pín', 'pǐn', 'tone') // 'less'
|
||||
cnchar.compareSpell('pin2', 'pǐn', 'tone') // 'less'
|
||||
cnchar.compareSpell('频', 'pǐn', 'tone') // 'less'
|
||||
cnchar.compareSpell('品', '频', 'tone') // 'more'
|
||||
cnchar.compareSpell('贫', '频', 'tone') // 'even'
|
||||
```
|
||||
|
||||
###### 6.8.7.5 compareStroke
|
||||
|
||||
```js
|
||||
cnchar.compareStroke('你', '好') // 'more'
|
||||
cnchar.compareStroke('你', '苏') // 'even'
|
||||
cnchar.compareStroke('好', '苏') // 'less'
|
||||
cnchar.compareStroke('一个', '好') // 'less'
|
||||
cnchar.compareStroke('你', 14) // 'less'
|
||||
```
|
||||
|
||||
###### 6.8.7.6 sortSpell
|
||||
|
||||
```js
|
||||
cnchar.sortSpell(['你', '好', '吗']) // ['好', '吗', '你']
|
||||
cnchar.sortSpell('你好吗') // '好吗你'
|
||||
cnchar.sortSpell(['拼', '品', '频', '爱'], 'tone') // ['爱', '拼', '频', '品']
|
||||
cnchar.sortSpell(['拼', '品', 'pin2', 'ai'], 'tone') // ['ai', '拼', 'pin2', '品']
|
||||
cnchar.sortSpell(['拼', '品', '频', '爱'], 'tone', 'desc') // ['品', '频', '拼', '爱']
|
||||
cnchar.sortSpell('拼品频爱', 'tone', 'desc') // '品频拼爱'
|
||||
```
|
||||
|
||||
###### 6.8.7.7 sortStroke
|
||||
|
||||
```js
|
||||
cnchar.sortStroke(['一', '三', '二']) // ['一', '二', '三']
|
||||
cnchar.sortStroke('一三二') // '一二三'
|
||||
cnchar.sortStroke(['一', '三', 2]) // ['一', 2, '三']
|
||||
cnchar.sortStroke(['一', '三', '二'], 'desc') // ['三', '二', '一']
|
||||
```
|
||||
|
||||
### 7 Application examples
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
// powerd by hanzi-writer v2.2.2
|
||||
require('./promise-polyfill');
|
||||
const draw = require('./writer');
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
/**
|
||||
* Promise-pollfill 兼容ie低版本
|
||||
*/
|
||||
!function (e, n) {'object' == typeof exports && 'undefined' != typeof module ? n() : 'function' == typeof define && define.amd ? define(n) : n();}(0, function () {'use strict'; function e (e) {var n = this.constructor; return this.then(function (t) {return n.resolve(e()).then(function () {return t;});}, function (t) {return n.resolve(e()).then(function () {return n.reject(t);});});} function n (e) {return !(!e || 'undefined' == typeof e.length);} function t () {} function o (e) {if (!(this instanceof o)) throw new TypeError('Promises must be constructed via new'); if ('function' != typeof e) throw new TypeError('not a function'); this._state = 0, this._handled = !1, this._value = undefined, this._deferreds = [], c(e, this);} function r (e, n) {for (;3 === e._state;)e = e._value; 0 !== e._state ? (e._handled = !0, o._immediateFn(function () {var t = 1 === e._state ? n.onFulfilled : n.onRejected; if (null !== t) {var o; try {o = t(e._value);} catch (r) {return void f(n.promise, r);}i(n.promise, o);} else (1 === e._state ? i : f)(n.promise, e._value);})) : e._deferreds.push(n);} function i (e, n) {try {if (n === e) throw new TypeError('A promise cannot be resolved with itself.'); if (n && ('object' == typeof n || 'function' == typeof n)) {var t = n.then; if (n instanceof o) return e._state = 3, e._value = n, void u(e); if ('function' == typeof t) return void c(function (e, n) {return function () {e.apply(n, arguments);};}(t, n), e);}e._state = 1, e._value = n, u(e);} catch (r) {f(e, r);}} function f (e, n) {e._state = 2, e._value = n, u(e);} function u (e) {2 === e._state && 0 === e._deferreds.length && o._immediateFn(function () {e._handled || o._unhandledRejectionFn(e._value);}); for (var n = 0, t = e._deferreds.length; t > n; n++)r(e, e._deferreds[n]); e._deferreds = null;} function c (e, n) {var t = !1; try {e(function (e) {t || (t = !0, i(n, e));}, function (e) {t || (t = !0, f(n, e));});} catch (o) {if (t) return; t = !0, f(n, o);}} var a = setTimeout; o.prototype['catch'] = function (e) {return this.then(null, e);}, o.prototype.then = function (e, n) {var o = new this.constructor(t); return r(this, new function (e, n, t) {this.onFulfilled = 'function' == typeof e ? e : null, this.onRejected = 'function' == typeof n ? n : null, this.promise = t;}(e, n, o)), o;}, o.prototype['finally'] = e, o.all = function (e) {return new o(function (t, o) {function r (e, n) {try {if (n && ('object' == typeof n || 'function' == typeof n)) {var u = n.then; if ('function' == typeof u) return void u.call(n, function (n) {r(e, n);}, o);}i[e] = n, 0 == --f && t(i);} catch (c) {o(c);}} if (!n(e)) return o(new TypeError('Promise.all accepts an array')); var i = Array.prototype.slice.call(e); if (0 === i.length) return t([]); for (var f = i.length, u = 0; i.length > u; u++)r(u, i[u]);});}, o.resolve = function (e) {return e && 'object' == typeof e && e.constructor === o ? e : new o(function (n) {n(e);});}, o.reject = function (e) {return new o(function (n, t) {t(e);});}, o.race = function (e) {return new o(function (t, r) {if (!n(e)) return r(new TypeError('Promise.race accepts an array')); for (var i = 0, f = e.length; f > i; i++)o.resolve(e[i]).then(t, r);});}, o._immediateFn = 'function' == typeof setImmediate && function (e) {setImmediate(e);} || function (e) {a(e, 0);}, o._unhandledRejectionFn = function (e) {void 0 !== console && console && console.warn('Possible Unhandled Promise Rejection:', e);}; var l = function () {if ('undefined' != typeof self) return self; if ('undefined' != typeof window) return window; if ('undefined' != typeof global) return global; throw Error('unable to locate global object');}(); 'Promise' in l ? l.Promise.prototype['finally'] || (l.Promise.prototype['finally'] = e) : l.Promise = o;});
|
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id='draw'></div>
|
||||
<script src="../npm/draw/cnchar.draw.min.js"></script>
|
||||
<script>
|
||||
CncharDraw('你好',{el:'#draw'})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue