feat: 2.2.0 on the way

This commit is contained in:
tackchen 2020-06-03 18:11:37 +08:00
parent 21f71f0800
commit 7a3565e555
33 changed files with 796 additions and 250 deletions

178
README.md
View File

@ -24,48 +24,52 @@ cnchar 由于重建过一次仓库,所以之前的 star 清零了 (〒︿〒)
<!-- toc -->
- [前言](#%e5%89%8d%e8%a8%80)
- [0.快速使用](#0%e5%bf%ab%e9%80%9f%e4%bd%bf%e7%94%a8)
- [1.功能](#1%e5%8a%9f%e8%83%bd)
- [2.概览](#2%e6%a6%82%e8%a7%88)
- [3 安装](#3-%e5%ae%89%e8%a3%85)
- [3.1 使用 npm 安装](#31-%e4%bd%bf%e7%94%a8-npm-%e5%ae%89%e8%a3%85)
- [3.2 使用 script 引入](#32-%e4%bd%bf%e7%94%a8-script-%e5%bc%95%e5%85%a5)
- [4 使用](#4-%e4%bd%bf%e7%94%a8)
- [4.1 webpack浏览器环境(有window对象)](#41-webpack%e6%b5%8f%e8%a7%88%e5%99%a8%e7%8e%af%e5%a2%83%e6%9c%89window%e5%af%b9%e8%b1%a1)
- [4.2 nodejs 等非浏览器环境](#42-nodejs-%e7%ad%89%e9%9d%9e%e6%b5%8f%e8%a7%88%e5%99%a8%e7%8e%af%e5%a2%83)
- [4.3 原生浏览器环境](#43-%e5%8e%9f%e7%94%9f%e6%b5%8f%e8%a7%88%e5%99%a8%e7%8e%af%e5%a2%83)
- [前言](#前言)
- [0.快速使用](#0快速使用)
- [1.功能](#1功能)
- [2.概览](#2概览)
- [3 安装](#3-安装)
- [3.1 使用 npm 安装](#31-使用-npm-安装)
- [3.2 使用 script 引入](#32-使用-script-引入)
- [4 使用](#4-使用)
- [4.1 webpack浏览器环境(有window对象)](#41-webpack浏览器环境有window对象)
- [4.2 nodejs 等非浏览器环境](#42-nodejs-等非浏览器环境)
- [4.3 原生浏览器环境](#43-原生浏览器环境)
- [5 API](#5-api)
- [5.1 拼音笔画基础 API: spell & stroke](#51-%e6%8b%bc%e9%9f%b3%e7%ac%94%e7%94%bb%e5%9f%ba%e7%a1%80-api-spell--stroke)
- [5.2 可视化绘制汉字: draw](#52-%e5%8f%af%e8%a7%86%e5%8c%96%e7%bb%98%e5%88%b6%e6%b1%89%e5%ad%97-draw)
- [5.2.1 使用](#521-%e4%bd%bf%e7%94%a8)
- [5.2.2 参数](#522-%e5%8f%82%e6%95%b0)
- [5.3 繁体、简体、火星文互转: convert](#53-%e7%b9%81%e4%bd%93%e7%ae%80%e4%bd%93%e7%81%ab%e6%98%9f%e6%96%87%e4%ba%92%e8%bd%ac-convert)
- [5.4 笔画序列推出原汉字: orderToWord](#54-%e7%ac%94%e7%94%bb%e5%ba%8f%e5%88%97%e6%8e%a8%e5%87%ba%e5%8e%9f%e6%b1%89%e5%ad%97-ordertoword)
- [5.5 通过拼音查询原汉字: spellToWord](#55-%e9%80%9a%e8%bf%87%e6%8b%bc%e9%9f%b3%e6%9f%a5%e8%af%a2%e5%8e%9f%e6%b1%89%e5%ad%97-spelltoword)
- [5.6 通过笔画数查询原汉字: strokeToWord](#56-%e9%80%9a%e8%bf%87%e7%ac%94%e7%94%bb%e6%95%b0%e6%9f%a5%e8%af%a2%e5%8e%9f%e6%b1%89%e5%ad%97-stroketoword)
- [5.7 查询拼音详细信息: spellInfo](#57-%e6%9f%a5%e8%af%a2%e6%8b%bc%e9%9f%b3%e8%af%a6%e7%bb%86%e4%bf%a1%e6%81%af-spellinfo)
- [5.8 其他 api](#58-%e5%85%b6%e4%bb%96-api)
- [5.1 拼音笔画基础 API: spell & stroke](#51-拼音笔画基础-api-spell--stroke)
- [5.2 可视化绘制汉字: draw](#52-可视化绘制汉字-draw)
- [5.2.1 使用](#521-使用)
- [5.2.2 参数](#522-参数)
- [5.3 繁体、简体、火星文互转: convert](#53-繁体简体火星文互转-convert)
- [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-%e5%8f%82%e6%95%b0)
- [6.1 spell 参数](#61-spell-%e5%8f%82%e6%95%b0)
- [6.2 stroke 参数](#62-stroke-%e5%8f%82%e6%95%b0)
- [6.3 orderToWord 参数](#63-ordertoword-%e5%8f%82%e6%95%b0)
- [6.4 spellToWord 参数](#64-spelltoword-%e5%8f%82%e6%95%b0)
- [6.5 strokeToWord 参数](#65-stroketoword-%e5%8f%82%e6%95%b0)
- [6.6 使用实例大全:](#66-%e4%bd%bf%e7%94%a8%e5%ae%9e%e4%be%8b%e5%a4%a7%e5%85%a8)
- [6.6.0 安装使用](#660-%e5%ae%89%e8%a3%85%e4%bd%bf%e7%94%a8)
- [6.6.1 cnchar 基础库功能](#661-cnchar-%e5%9f%ba%e7%a1%80%e5%ba%93%e5%8a%9f%e8%83%bd)
- [6.6.2 cnchar-poly 库功能](#662-cnchar-poly-%e5%ba%93%e5%8a%9f%e8%83%bd)
- [6.6.3 cnchar-order 库功能](#663-cnchar-order-%e5%ba%93%e5%8a%9f%e8%83%bd)
- [6.6.4 cnchar-trad 库功能](#664-cnchar-trad-%e5%ba%93%e5%8a%9f%e8%83%bd)
- [6.6.4.1 convert 字体转换](#6641-convert-%e5%ad%97%e4%bd%93%e8%bd%ac%e6%8d%a2)
- [6.6.4.2 spell 和 stroke 方法](#6642-spell-%e5%92%8c-stroke-%e6%96%b9%e6%b3%95)
- [7 应用例子](#7-%e5%ba%94%e7%94%a8%e4%be%8b%e5%ad%90)
- [6 spell stroke 参数](#6-spell-stroke-参数)
- [6.1 spell 参数](#61-spell-参数)
- [6.2 stroke 参数](#62-stroke-参数)
- [6.3 orderToWord 参数](#63-ordertoword-参数)
- [6.4 spellToWord 参数](#64-spelltoword-参数)
- [6.5 strokeToWord 参数](#65-stroketoword-参数)
- [6.6 idiom 参数](#66-idiom-参数)
- [6.7 xhy 参数](#67-xhy-参数)
- [6.8 使用实例大全:](#68-使用实例大全)
- [6.8.0 安装使用](#680-安装使用)
- [6.8.1 cnchar 基础库功能](#681-cnchar-基础库功能)
- [6.8.2 cnchar-poly 库功能](#682-cnchar-poly-库功能)
- [6.8.3 cnchar-order 库功能](#683-cnchar-order-库功能)
- [6.8.4 cnchar-trad 库功能](#684-cnchar-trad-库功能)
- [6.8.4.1 convert 字体转换](#6841-convert-字体转换)
- [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-应用例子)
<!-- tocstop -->
@ -117,22 +121,27 @@ import cnchar from 'cnchar';
10. 支持 **根据笔画顺序查询** 汉字
11. 支持 **查询拼音的信息**,包含声母、韵母、音调、音调位置的等
12. 支持 **繁体字** 拼音、笔画数及以上所有功能,实现和简体字一样的功能
13. **体积小**min 版本仅 46 kbzip 版本 34 kb (含有大量汉字拼音字典)
14. **多端可用**,可用于 **浏览器、nodejs、小程序/小游戏、ReactNative/Weex/Uniapp/Electron、webpack**...,支持所有 js 能运行的环境
15. **typescript支持**支持在typescript中调用
16. 丰富的配置,按需取用
13. 支持 **成语** 查询功能,可以按照汉字、拼音(声调)、笔画数查询成语
14. 支持 **歇后语** 查询功能,支持模糊查询
15. 提供汉字工具方法,方便开发者更便捷高效地 **操作拼音和汉字**
16. **体积小**min 版本仅 46 kbzip 版本 34 kb (含有大量汉字拼音字典)
17. **多端可用**,可用于 **浏览器、nodejs、小程序/小游戏、ReactNative/Weex/Uniapp/Electron、webpack**...,支持所有 js 能运行的环境
18. **typescript支持**支持在typescript中调用
19. 丰富的配置按功能拆分成7个库按需取用
### 2.概览
考虑到不同的需求cnchar 的功能被拆分到以下五个库中
考虑到不同的需求cnchar 的功能被拆分到以下七个库中,方便开发者按需取用
| 名称 | 描述 | 功能 |
| :----------: | :------------------------------: | :--------------------: |
| cnchar | 主 js 库,其他三个库依赖于这个库 | 含有简体字拼音、多音字、音调、笔画数等功能 |
| cnchar-poly | 多音词库 | 含有识别多音词功能 |
| cnchar-order | 笔画顺序库 | 含有识别笔画顺序、笔画名称、笔画形状等功能 |
| cnchar-trad | 繁体字库 | 支持繁体、火星、简体互转,支持繁体拼音笔画多音字全功能 |
| cnchar-draw | 绘制笔画库 | 指出可视化绘制汉字,有 normal,animation,stroke,test 四种模式可选,该库仅在浏览器环境下可用 |
| 名称 | 描述 | 功能 | 支持版本 |
| :----------: | :------------------------------: | :--------------------: | :--------------------: |
| cnchar | 主 js 库,其他三个库依赖于这个库 | 含有简体字拼音、多音字、音调、笔画数等功能 | -- |
| cnchar-poly | 多音词库 | 含有识别多音词功能 | -- |
| cnchar-order | 笔画顺序库 | 含有识别笔画顺序、笔画名称、笔画形状等功能 | -- |
| cnchar-trad | 繁体字库 | 支持繁体、火星、简体互转,支持繁体拼音笔画多音字全功能 | -- |
| cnchar-draw | 绘制笔画库 | 支持可视化绘制汉字该库可脱离cnchar使用该库仅在浏览器环境下可用 | 2.1+ |
| cnchar-idiom | 成语库 | 支持成语查询等功能 | 2.2+ |
| cnchar-xhy | 歇后语库 | 支持歇后语查询等功能 | 2.2+ |
### 3 安装
@ -165,10 +174,11 @@ npm i cnchar-all
<script src="https://cdn.jsdelivr.net/npm/cnchar-poly/cnchar.poly.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-order/cnchar.order.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-trad/cnchar.trad.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-draw/cnchar.draw.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-idiom/cnchar.idiom.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-xhy/cnchar.xhy.min.js"></script>
```
或使用以下cdn包含了以上个库
或使用以下cdn包含了以上个库
```html
<script src="https://cdn.jsdelivr.net/npm/cnchar-all/cnchar.all.min.js"></script>
@ -188,6 +198,8 @@ import 'cnchar-poly';
import 'cnchar-order';
import 'cnchar-trad';
import 'cnchar-draw';
import 'cnchar-idiom';
import 'cnchar-xhy';
// 插件请按需取用
console.log('汉字'.spell()); // prototype 方式调用
@ -206,9 +218,11 @@ var cnchar = require('cnchar');
var poly = require('cnchar-poly');
var order = require('cnchar-order');
var trad = require('cnchar-trad');
var idiom = require('cnchar-idiom');
var xhy = require('cnchar-xhy');
// 插件请按需取用
// cnchar-draw 在非浏览器环境下不可使用
cnchar.use(poly, order, trad);
// 注:cnchar-draw 在非浏览器环境下不可使用
cnchar.use(poly, order, trad, idiom, xhy);
console.log('汉字'.spell()); // prototype 方式调用
console.log(cnchar.spell('汉字')); // cnchar api 调用
@ -226,6 +240,8 @@ console.log(cnchar.spell('汉字')); // cnchar api 调用
<script src="https://cdn.jsdelivr.net/npm/cnchar-order/cnchar.order.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-trad/cnchar.trad.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-draw/cnchar.draw.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-idiom/cnchar.idiom.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cnchar-xhy/cnchar.xhy.min.js"></script>
<script>
console.log('汉字'.spell()); // prototype 方式调用
console.log(cnchar.spell('汉字')); // cnchar api 调用
@ -234,7 +250,8 @@ console.log(cnchar.spell('汉字')); // cnchar api 调用
### 5 API
类型声明:[cnchar.d.ts](https://github.com/theajack/cnchar/blob/master/src/main/index.d.ts)
类型声明:[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)
#### 5.1 拼音笔画基础 API: spell & stroke
@ -260,7 +277,7 @@ string.stroke([...args])
类型声明:[cnchar.draw.d.ts](https://github.com/theajack/cnchar/blob/master/src/plugin/draw/index.d.ts)
`cnchar-draw` 库用于支持在浏览器环境下可视化绘制汉字,所以该库仅在浏览器环境下可用。
`cnchar-draw` 库用于支持在浏览器环境下可视化绘制汉字,所以该库仅在浏览器环境下可用。绘制模式有 normal,animation,stroke,test 四种模式可选。
##### 5.2.1 使用
@ -706,9 +723,40 @@ cnchar.strokeToWord(count,arg1,arg2,...);
注:`simple`与`trad`参数若是都不存在,则当引入`cnchar-trad`时会同时匹配繁简体,没有引入`cnchar-trad`时则只匹配简体
#### 6.6 使用实例大全:
#### 6.6 idiom 参数
##### 6.6.0 安装使用
参数调用如下value表示查询对象可以试拼音汉字笔画数所有 arg 参数都是可选的
```js
cnchar.idiom(value,arg1,arg2,...);
```
| 参数 | 作用 | 是否默认 | 依赖库 | 备注 |
| :-----: | :----: | :------: | :---: | :---: |
| char | 根据汉字查询成语 | 是 | -- | 默认值无需调用 |
| stroke | 根据笔画数查询成语 | 否 | -- | 优先级高于char |
| spell | 根据拼音查询成语 | 否 | -- | 优先级高于stroke |
| tone | 启用拼音音调查询 | 否 | -- | 仅在spell模式下生效 |
注:优先级 `spell` > `stroke` > `char`
#### 6.7 xhy 参数
参数调用如下value表示歇后语查询对象可以是歇后语的第一句或第二句所有 arg 参数都是可选的
```js
cnchar.xhy(value,arg1,arg2,...);
```
| 参数 | 作用 | 是否默认 | 依赖库 | 备注 |
| :-----: | :----: | :------: | :---: | :---: |
| fuzzy | 是否支持模糊查询 | 否 | -- | 是否包含输入的字符串 |
| answer | 是否只输出答案 | 否 | -- | 默认是输出整句歇后语 |
| second | 是否是根据歇后语后一句查询 | 否 | -- | -- |
#### 6.8 使用实例大全:
##### 6.8.0 安装使用
npm 方式
@ -730,7 +778,7 @@ script 标签引用 方式
</script>
```
##### 6.6.1 cnchar 基础库功能
##### 6.8.1 cnchar 基础库功能
```js
//spell 功能
@ -769,7 +817,7 @@ cnchar.spellInfo('shàng');
4. stroke 方法 非中文字符会笔画数会计为 0
5. stroke 方法 order 模式 非中文字符会返回 undefined
##### 6.6.2 cnchar-poly 库功能
##### 6.8.2 cnchar-poly 库功能
该库用于准确识别多音词,同样支持 6.3.1 中的其他参数功能
@ -779,7 +827,7 @@ cnchar.spellInfo('shàng');
'长大了'.spell('poly'); // 返回 '(Zhang|Chang)(Da|Dai)(Le|Liao)'
```
##### 6.6.3 cnchar-order 库功能
##### 6.8.3 cnchar-order 库功能
该库用于查询汉字笔画顺序、笔画名称等,返回值为 数组
@ -835,11 +883,11 @@ cnchar.orderToWord(['横', '撇', '捺'], 'contain');
// 返回 "丈大天太夫夭尺攵文犬仗叭史央夯失疋矢乔..." // 省略后面的
```
##### 6.6.4 cnchar-trad 库功能
##### 6.8.4 cnchar-trad 库功能
该库用于支持繁体字火星文转换及为拼音笔画数等基本功能提供繁体字支持
###### 6.6.4.1 convert 字体转换
###### 6.8.4.1 convert 字体转换
```js
'一个人'.convertSimpleToTrad(); // 返回 "壹個人" 等价于 cnchar.convert.simpleToTrad
@ -861,7 +909,7 @@ cnchar.convert.sparkToSimple('①个亾');
cnchar.convert.sparkToTrad('①个亾');
```
###### 6.6.4.2 spell 和 stroke 方法
###### 6.8.4.2 spell 和 stroke 方法
该库增加了对于繁体字的拼音笔画功能扩展,其他基础用法与 基础库一致:
@ -877,6 +925,12 @@ cnchar.convert.sparkToTrad('①个亾');
'長大'.stroke('order', 'shape', 'simple'); // 返回 [undefined, ["㇐","","㇏"]]
```
##### 6.8.5 cnchar-idiom 库功能
##### 6.8.6 cnchar-xhy 库功能
### 7 应用例子
[汉字打字游戏](https://www.theajack.com/type/)

View File

@ -9,6 +9,8 @@ let files = [
'../npm/poly/package.json',
'../npm/trad/package.json',
'../npm/draw/package.json',
'../npm/idiom/package.json',
'../npm/xhy/package.json',
'../npm/cnchar/package.json',
'../npm/all/package.json',
'../npm/hanzi-util/package.json',
@ -61,7 +63,9 @@ function copyToNPM () {
.pipe(gulp.dest('npm/order'))
.pipe(gulp.dest('npm/poly'))
.pipe(gulp.dest('npm/trad'))
.pipe(gulp.dest('npm/draw'));
.pipe(gulp.dest('npm/draw'))
.pipe(gulp.dest('npm/idiom'))
.pipe(gulp.dest('npm/xhy'));
gulp.src(['src/main/*.json', 'src/main/index.d.ts', 'LICENSE'])
.pipe(gulp.dest('npm/cnchar'));
@ -78,6 +82,12 @@ function copyToNPM () {
gulp.src(['src/plugin/draw/*.json', 'src/plugin/draw/index.d.ts', 'LICENSE'])
.pipe(gulp.dest('npm/draw'));
gulp.src(['src/plugin/idiom/*.json', 'src/plugin/idiom/index.d.ts', 'LICENSE'])
.pipe(gulp.dest('npm/idiom'));
gulp.src(['src/plugin/xhy/*.json', 'src/plugin/xhy/index.d.ts', 'LICENSE'])
.pipe(gulp.dest('npm/xhy'));
gulp.src(['src/main/index.d.ts', 'LICENSE'])
.pipe(gulp.dest('npm/all'))
.pipe(gulp.dest('npm/hanzi-util'))
@ -132,6 +142,13 @@ function transEs6ByBabel () {
.pipe(babel({presets: ['@babel/env']}))
.pipe(gulp.dest('npm/draw'));
gulp.src('src/plugin/idiom/*.js')
.pipe(babel({presets: ['@babel/env']}))
.pipe(gulp.dest('npm/idiom'));
gulp.src('src/plugin/xhy/*.js')
.pipe(babel({presets: ['@babel/env']}))
.pipe(gulp.dest('npm/xhy'));
// gulp.src('src/plugin/all/*.js')
// .pipe(babel({presets: ['@babel/env']}))
// .pipe(gulp.dest('npm/all'));

View File

@ -44,4 +44,6 @@ for (let i = uselessIndexesNoTone.length - 1; i >= 0; i--) {
spellDictNoTone.splice(uselessIndexesNoTone[i], 1);
}
fs.writeFileSync('./helper/idiom/spell2.json', JSON.stringify(spellDict, null, 4), 'utf8');
fs.writeFileSync('./helper/idiom/spell2.notone.json', JSON.stringify(spellDictNoTone, null, 4), 'utf8');
fs.writeFileSync('./helper/idiom/spell2.notone.json', JSON.stringify(spellDictNoTone, null, 4), 'utf8');
fs.writeFileSync('./helper/idiom/spell2.min.json', JSON.stringify(spellDict), 'utf8');
fs.writeFileSync('./helper/idiom/spell2.min.notone.json', JSON.stringify(spellDictNoTone), 'utf8');

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -80,6 +80,10 @@
1. 修复cnchar-all及cnchar-draw库在非浏览器环境会报错的bug
2. 修复 draw库和main库在ts环境中引用的异常
## 2.1.5
1. 鸟字增加默认读音
2. 彷徨读音修正
## 2.2.0
1. 增加cnchar-idiom 成语库
2. 增加cnchar-xhy 歇后语库
3. 增加transformTone,isCnchar,compareSpell,compareStroke,sortSpell,sortStroke工具方法方便开发者更便捷的操作拼音和汉字
4. 对ts声明文件进行修改对ts的支持更加友好
5. 鸟字增加默认读音
6. 彷徨读音修正

View File

@ -31,6 +31,8 @@
"cnchar-order": "^2.1.4",
"cnchar-poly": "^2.1.4",
"cnchar-trad": "^2.1.4",
"cnchar-draw": "^2.1.4"
"cnchar-draw": "^2.1.4",
"cnchar-idiom": "^2.1.4",
"cnchar-xhy": "^2.1.4"
}
}

23
npm/idiom/LICENSE Normal file
View File

@ -0,0 +1,23 @@
The MIT License (MIT)
Copyright (c) 2017 - present theajack <contact@theajack.com>
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

29
npm/idiom/package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "cnchar-idiom",
"version": "2.1.4",
"description": "功能全面、多端支持的汉字拼音笔画js库支持多音字、繁体字、火星文",
"main": "index.js",
"unpkg": "cnchar.idiom.min.js",
"jsdelivr": "cnchar.idiom.min.js",
"typings": "index.d.ts",
"scripts": {},
"repository": {
"type": "git",
"url": "git+https://github.com/theajack/cnchar.git"
},
"keywords": [
"汉字拼音",
"汉字笔画数",
"汉字笔画顺序",
"繁体字",
"火星文",
"js库",
"theajack"
],
"author": "theajack",
"license": "MIT",
"bugs": {
"url": "https://github.com/theajack/cnchar/issues"
},
"homepage": "https://www.theajack.com/cnchar/"
}

23
npm/xhy/LICENSE Normal file
View File

@ -0,0 +1,23 @@
The MIT License (MIT)
Copyright (c) 2017 - present theajack <contact@theajack.com>
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

29
npm/xhy/package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "cnchar-xhy",
"version": "2.1.4",
"description": "功能全面、多端支持的汉字拼音笔画js库支持多音字、繁体字、火星文",
"main": "index.js",
"unpkg": "cnchar.xhy.min.js",
"jsdelivr": "cnchar.xhy.min.js",
"typings": "index.d.ts",
"scripts": {},
"repository": {
"type": "git",
"url": "git+https://github.com/theajack/cnchar.git"
},
"keywords": [
"汉字拼音",
"汉字笔画数",
"汉字笔画顺序",
"繁体字",
"火星文",
"js库",
"theajack"
],
"author": "theajack",
"license": "MIT",
"bugs": {
"url": "https://github.com/theajack/cnchar/issues"
},
"homepage": "https://www.theajack.com/cnchar/"
}

View File

@ -1,6 +1,6 @@
{
"name": "cnchar",
"version": "2.1.4",
"version": "2.1.5",
"description": "功能全面、多端支持的汉字拼音笔画js库支持多音字、繁体字、火星文",
"main": "index.html",
"author": "theajack <contact@theajack.com>",
@ -26,12 +26,14 @@
"build:order": "webpack --config webpack-config/build.plugin.js --env.pluginname=order",
"build:trad": "webpack --config webpack-config/build.plugin.js --env.pluginname=trad",
"build:draw": "webpack --config webpack-config/build.plugin.js --env.pluginname=draw",
"build:idiom": "webpack --config webpack-config/build.plugin.js --env.pluginname=idiom",
"build:xhy": "webpack --config webpack-config/build.plugin.js --env.pluginname=xhy",
"build:all": "webpack --config webpack-config/build.plugin.js --env.pluginname=all",
"build:plugin": "npm run build:poly && npm run build:order && npm run build:trad && npm run build:draw && npm run build:all",
"build:npm": "node ./helper/build-npm.js",
"start": "webpack-dev-server --open --config webpack-config/dev.js",
"dev": "webpack-dev-server --open --config webpack-config/dev.js",
"publish": "npm publish npm/cnchar && npm publish npm/order && npm publish npm/poly && npm publish npm/trad && npm publish npm/draw && npm publish npm/all && npm publish npm/hanzi-util-base && npm publish npm/hanzi-util",
"publish": "npm publish npm/cnchar && npm publish npm/order && npm publish npm/poly && npm publish npm/trad && npm publish npm/draw && npm publish npm/idiom && npm publish npm/xhy && npm publish npm/all && npm publish npm/hanzi-util-base && npm publish npm/hanzi-util",
"lint": "eslint src --ext js",
"test": "node test",
"dev:docs": "vuepress dev docs",

View File

@ -2,29 +2,31 @@ import cnchar from '../src/main/index';
import '../src/plugin/order';
import '../src/plugin/trad';
import '../src/plugin/poly';
import '../src/plugin/draw';
import '../src/plugin/idiom';
import '../src/plugin/xhy';
import spell from '../src/main/spell-dict-jian.json';
// import cncharDraw from '../src/plugin/draw';
// import spell from '../src/main/spell-dict-jian.json';
// // import cncharDraw from '../src/plugin/draw';
// import cnchar from '../npm/cnchar';
// import '../npm/order';
// import '../npm/trad';
// import '../npm/poly';
// import cncharDraw from '../npm/draw';
// // import cnchar from '../npm/cnchar';
// // import '../npm/order';
// // import '../npm/trad';
// // import '../npm/poly';
// // import cncharDraw from '../npm/draw';
// import './plugin/draw'
// console.log('1',cnchar)
console.log(cnchar.stroke('一个', 'order'));
console.log(cnchar.stroke('長城', 'count', 'order', 'name'));
// console.log(cnchar.orderToWord(['横', '撇', '捺']));
// console.log(cnchar.orderToWord(['横', '撇', '捺'], 'start'));
console.log(cnchar.spellToWord('lv2'));
console.log(cnchar.spellInfo('lǘ'));
console.log(cnchar.strokeToWord(1));
console.log('美好的地方'.spell('tone'));
// // import './plugin/draw'
// // console.log('1',cnchar)
// console.log(cnchar.stroke('一个', 'order'));
// console.log(cnchar.stroke('長城', 'count', 'order', 'name'));
// // console.log(cnchar.orderToWord(['横', '撇', '捺']));
// // console.log(cnchar.orderToWord(['横', '撇', '捺'], 'start'));
// console.log(cnchar.spellToWord('lv2'));
// console.log(cnchar.spellInfo('lǘ'));
// console.log(cnchar.strokeToWord(1));
// console.log('美好的地方'.spell('tone'));
window.keys = Object.keys(spell);
// window.keys = Object.keys(spell);
// cncharDraw('你好', {
// type: cncharDraw.TYPE.NORMAL,

View File

@ -32,4 +32,6 @@ function getCharCode (char, tone = false) {
return code;
}
// 比较拼音大小的方法考虑移到cnchar工具方法中
module.exports = {initToneCodes, getCharCode};

48
src/main/index.d.ts vendored
View File

@ -1,14 +1,10 @@
import {Draw} from 'cnchar-draw';
// import {Draw} from 'cnchar-draw';
declare type spellArg = 'array' | 'low' | 'up' | 'first' | 'poly' | 'tone' | 'simple' | 'trad';
declare type strokeArg = 'letter' | 'shape' | 'count' | 'name' | 'detail' | 'array' | 'order' | 'simple' | 'trad';
declare type orderToWordArg = 'match' | 'matchorder' | 'contain' | 'start' | 'array' | 'simple' | 'trad';
declare type spellToWordArg = 'poly' | 'alltone' | 'array' | 'simple' | 'trad';
declare type strokeToWordArg = 'array' | 'simple' | 'trad';
declare type pluginArg = 'order' | 'trad' | 'poly' | 'draw' | 'idiom' | 'xhy';
declare type orderName = '横折折撇' | '竖弯' | '横折' | '撇点' | '横斜钩' | '横' | '捺' | '横折钩' | '竖' | '竖钩' | '点' | '撇' | '撇折' | '竖折撇' | '横折折折钩' | '竖折折钩' | '提' | '弯钩' | '斜钩' | '横折折' | '横撇' | '横折提' | '横折折折' | '竖提' | '竖弯钩'
| '竖折折' | '横撇弯钩' | '卧钩' | '横折弯' | '横钩';
declare type toneType = 0 | 1 | 2 | 3 | 4;
declare type compareType = 'more' | 'less' | 'even';
declare interface spellInfoReturnStatic {
spell: string;
@ -17,40 +13,42 @@ declare interface spellInfoReturnStatic {
tone: number;
index: number;
}
declare interface CnCharStatic {
spell(sentence: string, ...args: Array<spellArg>): string | Array<any>;
stroke(sentence: string, ...args: Array<strokeArg>): number | Array<any>;
draw: Draw;
export declare interface CnCharStatic {
spell(sentence: string, ...args?: Array<spellArg>): string | Array<any>;
stroke(sentence: string, ...args?: Array<strokeArg>): number | Array<any>;
use(...plugins: Array<Function>): void;
orderToWord: {
(orders: string | Array<orderName>, ...args: Array<orderToWordArg>): string | Array<string>;
orders: object;
};
spellToWord(spell: string, ...args: Array<spellToWordArg>): string | Array<string>;
strokeToWord(stroke: number, ...args: Array<strokeToWordArg>): string | Array<string>;
spellToWord(spell: string, ...args?: Array<spellToWordArg>): string | Array<string>;
strokeToWord(stroke: number, ...args?: Array<strokeToWordArg>): string | Array<string>;
spellInfo: {
(spell: string): spellInfoReturnStatic;
tones: Array<string>;
initials: Array<string>;
};
convert: {
simpleToSpark(sentence: string): string;
simpleToTrad(sentence: string): string;
sparkToSimple(sentence: string): string;
sparkToTrad(sentence: string): string;
tradToSimple(sentence: string): string;
tradToSpark(sentence: string): string;
};
plugins: Array<pluginArg>;
type: {
spell: object;
stroke: object;
orderToWord: object;
spellToWord: object;
strokeToWord: object;
orderToWord?: object;
idiom?: object;
xhy?: object;
};
check: boolean;
readonly version: string;
transformTone(spell: string, tone?: boolean, type?: 'low' | 'up'): {
spell: string;
tone: toneType;
index: number;
isTrans: boolean;
};
isCnChar(word: string): boolean;
compareSpell(spell1: string, spell2: string, tone?: boolean): compareType;
compareStroke(stroke1: string, stroke2: string): compareType;
sortSpell(spells:Array<string> | string, ...args?: Array<'tone'|'desc'>): Array<string> | string;
sortStroke(strokes:Array<string|number> | string, desc?: 'desc'): Array<string> | string;
}
declare const cnchar: CnCharStatic;

View File

@ -5,6 +5,7 @@ let {spell, tones, stroke, arg, has, _throw, _wran,
let dict = require('./dict');
let {initSpellToWord} = require('./spellToWord');
let initStrokeToWord = require('./strokeToWord');
let {compareSpell, sortSpell, compareStroke, sortStroke, initSort} = require('./sort');
function _spell (...args) {
return spell(dict.spell, args);
@ -51,7 +52,14 @@ let cnchar = {
stroke: {
array: arg.array
}
}
},
// 工具方法
transformTone,
isCnChar,
compareSpell,
compareStroke,
sortSpell,
sortStroke
};
function init () {
@ -59,6 +67,7 @@ function init () {
initCnchar(cnchar);
initSpellToWord(cnchar);
initStrokeToWord(cnchar);
initSort(cnchar);
if (typeof window !== 'undefined') {
window.cnchar = window.CnChar = cnchar;
}

121
src/main/sort.js Normal file
View File

@ -0,0 +1,121 @@
let {initToneCodes, getCharCode} = require('./compareSpell');
let _cnchar = null;
let arg = {
'tone': 'tone',
'desc': 'desc',
'array': 'array'
};
function initSort (__cnchar) {
_cnchar = __cnchar;
initToneCodes(__cnchar);
}
const TYPE = {
MORE: 'more', // 大于
LESS: 'less', // 小于
EVEN: 'even', // 等于
ERROR: 'error'
};
function pretreat (spell, tone) {
let arr = ['low'];
if (tone) {arr.push('tone');}
if (_cnchar.isCnChar(spell)) {
return _cnchar.spell(spell, ...arr);
}
return _cnchar._.transformTone(spell, tone).spell;
}
function compareSpell (spell1, spell2, tone = false) {
spell1 = pretreat(spell1, tone);
spell2 = pretreat(spell2, tone);
for (let i = 0; i < spell1.length; i++) {
if (!spell2[i]) { // spell1与spell2值前面拼音一样但是spell1长度大于spell2 说明 spell1 > spell2
return TYPE.MORE;
}
let code = getCharCode(spell1[i], tone);
let code2 = getCharCode(spell2[i], tone);
if (code > code2) { // spell1 > spell2
return TYPE.MORE;
} else if (code < code2) { // spell1 < spell2
return TYPE.LESS;
}
}
if (spell1.length === spell2.length) {
return TYPE.EVEN;
}
return TYPE.LESS; // spell1与spell2值前面拼音一样但是spell1长度小于spell2 说明 中 < spell2
}
function sortSpell (spells, ...args) {
let isString = false;
if (typeof spells === 'string') {
spells = spells.split('');
isString = true;
}
let tone = args.indexOf(arg.tone) !== -1;
let desc = args.indexOf(arg.desc) !== -1;
let more = desc ? -1 : 1;
let less = more * -1;
let result = spells.sort((a, b) => {
let res = compareSpell(a, b, tone);
if (res === TYPE.MORE) {
return more;
}
if (res === TYPE.LESS) {
return less;
}
return 0;
});
if (isString) {
return result.join('');
}
return result;
}
function compareStroke (stroke1, stroke2) {
if (typeof stroke1 === 'string') {
stroke1 = _cnchar.stroke(stroke1);
}
if (typeof stroke2 === 'string') {
stroke2 = _cnchar.stroke(stroke2);
}
if (stroke1 > stroke2) {
return TYPE.MORE;
}
if (stroke1 < stroke2) {
return TYPE.LESS;
}
return TYPE.EVEN;
}
function sortStroke (strokes, ...args) {
let isString = false;
if (typeof strokes === 'string') {
strokes = strokes.split('');
isString = true;
}
let desc = args.indexOf(arg.desc) !== -1;
let more = desc ? -1 : 1;
let less = more * -1;
let result = strokes.sort((a, b) => {
let res = compareStroke(a, b);
if (res === TYPE.MORE) {
return more;
}
if (res === TYPE.LESS) {
return less;
}
return 0;
});
if (isString) {
return result.join('');
}
return result;
}
module.exports = {sortSpell, sortStroke, initSort, compareSpell, compareStroke};

View File

@ -3,5 +3,7 @@ var order = require('../order');
var trad = require('../trad');
var poly = require('../poly');
var draw = require('../draw');
cnchar.use(order, trad, poly, draw);
var idiom = require('../idiom');
var xhy = require('../xhy');
cnchar.use(order, trad, poly, draw, idiom, xhy);
module.exports = cnchar;

View File

@ -91,4 +91,10 @@ export declare interface Draw {
declare const draw: Draw;
declare module 'cnchar' {
interface CnCharStatic {
draw: Draw;
}
}
export default draw;

View File

@ -4,11 +4,13 @@
* 拼音 ['shi', '']
*/
const dict = require('./idiom.json');
const {initToneCodes, getCharCode} = require('./tone');
window.getCharCode = getCharCode;
// const {initToneCodes, getCharCode} = require('./tone');
const spellDict = require('./spell.json');
const spellNoToneDict = require('./spell.notone.json');
let _cnchar = null;
let total = dict.length;
let arg = {
char: 'char',
@ -59,108 +61,106 @@ function idiomWithChar (input) {
}
// needTone是否需要匹配音调
function idiomWithSpell (input, tone) {
let low = 0;
let high = total;
input = input.toLowerCase();
// console.log(input, tone);
const total = dict.length;
let _dict = tone ? spellDict : spellNoToneDict;
if (tone) {
input = _cnchar._.transformTone(input, 'tone').spell;
input = _cnchar._.transformTone(input, true).spell;
}
let target = input.split('').map(c => getCharCode(c, tone));
let area = step1FindArea(low, high, target, tone);
if (area === TYPE.ERROR) {
let filter = _dict.filter((item) => {
return input === item.split(':')[0];
});
if (filter.length === 0) {
return [];
}
let floor = step2FindFloor(area.low, area.mid, target, tone).mid;
let ceil = step3FindCeil(area.mid, area.high, target, tone).mid;
return dict.slice(floor, ceil + 1);
}
const TYPE = {
MORE: 1, // 大于
LESS: 2, // 小于
EVEN: 3, // 等于
ERROR: -1
};
function binarySearch (low, high, condition) {
if (low > high) {
return TYPE.ERROR;
}
let mid = Math.floor((low + high) / 2);
let res = condition(mid);
if (res === TYPE.MORE) {
return binarySearch(low, mid - 1, condition);
} else if (res === TYPE.LESS) {
return binarySearch(mid + 1, high, condition);
} else {
return {low, high, mid};
}
}
function step1FindArea (low, high, target, tone) { // 找到一个区间,该区间包含所有拼音,且中值为该拼音
return binarySearch(low, high, (mid) => {
return compareSpell(mid, target, tone);
let res = [];
let n = _dict.length - 1;
filter.forEach(item => {
let index = _dict.indexOf(item);
let curDIndex = item.split(':')[1];
let nextDIndex = index === n ? total : _dict[index + 1].split(':')[1];
res = res.concat(dict.slice(curDIndex, nextDIndex));
});
}
function step2FindFloor (low, high, target, tone) { // 查找下界
return binarySearch(low, high, (mid) => {
return compareBoundary(mid, target, TYPE.MORE, TYPE.LESS, -1, tone);
});
}
function step3FindCeil (low, high, target, tone) { // 查找上界
return binarySearch(low, high, (mid) => {
return compareBoundary(mid, target, TYPE.LESS, TYPE.MORE, 1, tone);
});
}
// typeCenter 朝区域中心的大小type typeSide 朝区域两端的大小type
function compareBoundary (mid, target, typeCenter, typeSide, neighborPlus, tone) {
let res = compareSpell(mid, target, tone);
if (res === typeSide) {
return res;
}
if (res === TYPE.EVEN) {
let neighborIndex = mid + neighborPlus;
if (neighborIndex < 0 || neighbor >= total) {
return TYPE.EVEN;
}
let neighbor = compareSpell(neighborIndex, target, tone); // + 1
if (neighbor === TYPE.EVEN) {
return typeCenter;
} else if (neighbor === typeSide) {
return TYPE.EVEN;
} else {
console.error(neighbor);
throw new Error('idoim Error');
}
}
}
window.dict = dict;
window.compareSpell = compareSpell;
function compareSpell (mid, target, tone) {
let arr = ['low'];
if (tone) {arr.push('tone');}
let _spell = _cnchar.spell(dict[mid][0], ...arr); // 获取成语第一个字的拼音
return res;
for (let i = 0; i < _spell.length; i++) {
if (!target[i]) { // 中值与目标值前面拼音一样,但是中长度大于目标 说明 中 > 目标
return TYPE.MORE;
}
let code = getCharCode(_spell[i], tone);
if (code > target[i]) { // 中值 > 目标
return TYPE.MORE;
} else if (code < target[i]) { // 中值 < 目标
return TYPE.LESS;
}
}
if (_spell.length === target.length) {
return TYPE.EVEN;
}
return TYPE.LESS; // 中值与目标值前面拼音一样,但是中长度小于目标 说明 中 < 目标
// let low = 0;
// let high = total;
// input = input.toLowerCase();
// if (tone) {
// input = _cnchar._.transformTone(input, 'tone').spell;
// }
// let target = input.split('').map(c => getCharCode(c, tone));
// let area = step1FindArea(low, high, target, tone);
// if (area === TYPE.ERROR) {
// return [];
// }
// let floor = step2FindFloor(area.low, area.mid, target, tone).mid;
// let ceil = step3FindCeil(area.mid, area.high, target, tone).mid;
// return dict.slice(floor, ceil + 1);
}
// 二分查找拼音依赖拼音准确率,可能会不准确,故放弃
// function binarySearch (low, high, condition) {
// if (low > high) {
// return TYPE.ERROR;
// }
// let mid = Math.floor((low + high) / 2);
// let res = condition(mid);
// if (res === TYPE.MORE) {
// return binarySearch(low, mid - 1, condition);
// } else if (res === TYPE.LESS) {
// return binarySearch(mid + 1, high, condition);
// } else {
// return {low, high, mid};
// }
// }
// function step1FindArea (low, high, target, tone) { // 找到一个区间,该区间包含所有拼音,且中值为该拼音
// return binarySearch(low, high, (mid) => {
// return _cnchar._.compareSpell(mid, target, tone);
// });
// }
// function step2FindFloor (low, high, target, tone) { // 查找下界
// return binarySearch(low, high, (mid) => {
// return compareBoundary(mid, target, TYPE.MORE, TYPE.LESS, -1, tone);
// });
// }
// function step3FindCeil (low, high, target, tone) { // 查找上界
// return binarySearch(low, high, (mid) => {
// return compareBoundary(mid, target, TYPE.LESS, TYPE.MORE, 1, tone);
// });
// }
// typeCenter 朝区域中心的大小type typeSide 朝区域两端的大小type
// function compareBoundary (mid, target, typeCenter, typeSide, neighborPlus, tone) {
// let res = _cnchar._.compareSpell(mid, target, tone);
// if (res === typeSide) {
// return res;
// }
// if (res === TYPE.EVEN) {
// let neighborIndex = mid + neighborPlus;
// if (neighborIndex < 0 || neighbor >= total) {
// return TYPE.EVEN;
// }
// let neighbor = _cnchar._.compareSpell(neighborIndex, target, tone); // + 1
// if (neighbor === TYPE.EVEN) {
// return typeCenter;
// } else if (neighbor === typeSide) {
// return TYPE.EVEN;
// } else {
// console.error(neighbor);
// throw new Error('idoim Error');
// }
// }
// }
function idiomWithStroke (input) {
if (typeof input === 'number') { // 总笔画数为多少
return dict.filter((item) => {
@ -196,7 +196,7 @@ function checkArg (args, name) {
function setCnchar (cnchar) {
_cnchar = cnchar;
initToneCodes(cnchar);
// initToneCodes(cnchar);
}
module.exports = {idiom, arg, setCnchar};

View File

@ -1,21 +1,15 @@
declare type DrawType = 'normal' | 'animation' | 'stroke' | 'test';
declare type TestStatusType = 'mistake' | 'correct' | 'complete';
declare type idiomArg = 'char' | 'stroke' | 'spell' | 'tone';
export declare interface Draw {
(text:string, option?:DrawOption):Writer;
TYPE: {
ANIMATION: 'animation',
NORMAL: 'normal',
STROKE: 'stroke',
TEST: 'test'
},
TEST_STATUS: {
MISTAKE: 'mistake',
CORRECT: 'correct',
COMPLETE: 'complete'
export declare interface Idiom {
(text:string, ...idiomArgs: Array<idiomArg>):Array<string>;
}
declare const idiom: Idiom;
declare module 'cnchar' {
interface CnCharStatic {
idiom: Idiom;
}
}
declare const draw: Draw;
export default draw;
export default idiom;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,17 @@
declare const order: Function;
declare type orderToWordArg = 'match' | 'matchorder' | 'contain' | 'start' | 'array' | 'simple' | 'trad';
declare type orderName = '横折折撇' | '竖弯' | '横折' | '撇点' | '横斜钩' | '横' | '捺' | '横折钩' | '竖' | '竖钩' | '点' | '撇' | '撇折' | '竖折撇' | '横折折折钩' | '竖折折钩' | '提' | '弯钩' | '斜钩' | '横折折' | '横撇' | '横折提' | '横折折折' | '竖提' | '竖弯钩'
| '竖折折' | '横撇弯钩' | '卧钩' | '横折弯' | '横钩';
declare module 'cnchar' {
interface CnCharStatic {
orderToWord: {
(orders: string | Array<orderName>, ...args: Array<orderToWordArg>): string | Array<string>;
orders: object;
};
}
}
export default order;

View File

@ -1,3 +1,16 @@
declare const trad: Function;
declare module 'cnchar' {
interface CnCharStatic {
convert: {
simpleToSpark(sentence: string): string;
simpleToTrad(sentence: string): string;
sparkToSimple(sentence: string): string;
sparkToTrad(sentence: string): string;
tradToSimple(sentence: string): string;
tradToSpark(sentence: string): string;
};
}
}
export default trad;

View File

@ -163,4 +163,5 @@ function reinitStroke (proto, cnchar) {
}
init();
module.exports = init;

View File

@ -1,21 +1,15 @@
declare type DrawType = 'normal' | 'animation' | 'stroke' | 'test';
declare type TestStatusType = 'mistake' | 'correct' | 'complete';
declare type xhyArg = 'fuzzy' | 'answer' | 'second';
export declare interface Draw {
(text:string, option?:DrawOption):Writer;
TYPE: {
ANIMATION: 'animation',
NORMAL: 'normal',
STROKE: 'stroke',
TEST: 'test'
},
TEST_STATUS: {
MISTAKE: 'mistake',
CORRECT: 'correct',
COMPLETE: 'complete'
export declare interface XHY {
(text:string, ...xhyArgs: Array<xhyArg>):Array<string>;
}
declare const xhy: XHY;
declare module 'cnchar' {
interface CnCharStatic {
xhy: XHY;
}
}
declare const draw: Draw;
export default draw;
export default xhy;

View File

@ -1,6 +1,4 @@
// powerd by hanzi-writer v2.2.2
const xhy = require('./writer');
const {xhy, arg} = require('./xhy');
function main (cnchar) {
if (cnchar.plugins.indexOf('xhy') !== -1) {
@ -8,6 +6,7 @@ function main (cnchar) {
}
cnchar.plugins.push('xhy');
cnchar.xhy = xhy;
cnchar.type.xhy = arg;
}
function init (cnchar) {

View File

@ -0,0 +1,53 @@
const dict = require('./xhy.json');
let arg = {
'fuzzy': 'fuzzy',
'answer': 'answer',
'second': 'second'
};
function xhy (...args) {
let str = args.shift();
let isFuzzy = args.indexOf(arg.fuzzy) !== -1;
let onlyAnswer = args.indexOf(arg.answer) !== -1;
let second = args.indexOf(arg.second) !== -1;
let quesIndex = second ? 1 : 0;
let answerIndex = 1 - quesIndex;
if (isFuzzy) {
let res = [];
for (let i = 0; i < dict.length; i++) {
if (dict[i][quesIndex].indexOf(str) !== -1) {
let answer = shapeAnswer(dict[i], onlyAnswer, answerIndex);
res = res.concat(answer);
}
}
return res;
} else {
for (let i = 0; i < dict.length; i++) {
if (dict[i][quesIndex] === str) {
return shapeAnswer(dict[i], onlyAnswer, answerIndex);
}
}
}
}
function shapeAnswer (item, onlyAnswer, answerIndex) {
let answer = null;
if (item[answerIndex].indexOf('') !== -1) {
answer = item[answerIndex].split('');
} else {
answer = [item[answerIndex]];
}
if (!onlyAnswer) {
let fn = answerIndex === 1 ? (a, index) => {
answer[index] = `${item[1 - answerIndex]}-${a}`;
} : (a, index) => {
answer[index] = `${a}-${item[1 - answerIndex]}`;
};
answer.forEach(fn);
}
return answer;
}
module.exports = {xhy, arg}; ;

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,9 @@ const cnchar = require('../src/main/index');
const order = require('../src/plugin/order');
const trad = require('../src/plugin/trad');
const poly = require('../src/plugin/poly');
cnchar.use(order, trad, poly);
const idiom = require('../src/plugin/idiom');
const xhy = require('../src/plugin/xhy');
cnchar.use(order, trad, poly, idiom, xhy);
let config = require('./test.config');

View File

@ -161,5 +161,141 @@ module.exports = [
'①个亾',
'壹个人'
]
},
{
name: '测试成语',
test (cnchar) {
return [
cnchar.idiom(['五', '', '十', '']),
cnchar.idiom([4, 6, 2, 6], 'stroke'),
cnchar.idiom('shang', 'spell').slice(0, 2),
cnchar.idiom('shang4', 'spell', 'tone').slice(0, 2)
];
},
expect: [
['五风十雨', '五光十色'],
['五光十色'],
['伤风败化', '伤风败俗'],
['上兵伐谋', '上不着天,下不着地']
]
},
{
name: '测试歇后语',
test (cnchar) {
return [
cnchar.xhy('大水冲了龙王庙'),
cnchar.xhy('大水', 'fuzzy').slice(0, 2),
cnchar.xhy('大水', 'fuzzy', 'answer').slice(0, 2),
cnchar.xhy('上晃下摇', 'fuzzy', 'answer', 'second')
];
},
expect: [
['大水冲了龙王庙-自家人不识自家人', '大水冲了龙王庙-一家人不认一家人'],
['江河里长大水-泥沙俱下', '江河发大水-后浪推前浪'],
['泥沙俱下', '后浪推前浪'],
['醉汉过铁索桥', '扶着醉汉过破桥']
]
},
{
name: '测试 transformTone',
test (cnchar) {
return [
cnchar.transformTone('lv2'),
cnchar.transformTone('lv2', true),
cnchar.transformTone('lv2', true, 'up'),
cnchar.transformTone('lǘ')
];
},
expect: [
{spell: 'lü', tone: 2, index: 2, isTrans: true},
{spell: 'lǘ', tone: 2, index: 2, isTrans: true},
{spell: 'LǗ', tone: 2, index: 2, isTrans: true},
{spell: 'lü', tone: 2, index: 2, isTrans: false}
]
},
{
name: '测试 isCnChar',
test (cnchar) {
return [
cnchar.isCnChar('a'),
cnchar.isCnChar('1'),
cnchar.isCnChar(''),
cnchar.isCnChar('国'),
cnchar.isCnChar('國'),
];
},
expect: [
false, false, false, true, true
]
},
{
name: '测试 compareSpell',
test (cnchar) {
return [
cnchar.compareSpell('ao', 'ai'),
cnchar.compareSpell('ai', 'ai'),
cnchar.compareSpell('pín', 'pǐn', 'tone'),
cnchar.compareSpell('pin2', 'pǐn', 'tone'),
cnchar.compareSpell('频', 'pǐn', 'tone'),
cnchar.compareSpell('品', '频', 'tone'),
cnchar.compareSpell('贫', '频', 'tone'),
];
},
expect: [
'more', 'even', 'less', 'less', 'less', 'more', 'even'
]
},
{
name: '测试 compareStroke',
test (cnchar) {
return [
cnchar.compareStroke('你', '好'),
cnchar.compareStroke('你', '苏'),
cnchar.compareStroke('好', '苏'),
cnchar.compareStroke('一个', '好'),
cnchar.compareStroke('你', 14),
];
},
expect: [
'more', 'even', 'less', 'less', 'less'
]
},
{
name: '测试 sortSpell',
test (cnchar) {
return [
cnchar.sortSpell(['你', '好', '吗']),
cnchar.sortSpell('你好吗'),
cnchar.sortSpell(['拼', '品', '频', '爱'], 'tone'),
cnchar.sortSpell(['拼', '品', 'pin2', 'ai'], 'tone'),
cnchar.sortSpell(['拼', '品', '频', '爱'], 'tone', 'desc'),
cnchar.sortSpell('拼品频爱', 'tone', 'desc'),
];
},
expect: [
['好', '吗', '你'],
'好吗你',
['爱', '拼', '频', '品'],
['ai', '拼', 'pin2', '品'],
['品', '频', '拼', '爱'],
'品频拼爱'
]
},
{
name: '测试 sortStroke',
test (cnchar) {
return [
cnchar.sortStroke(['一', '三', '二']),
cnchar.sortStroke('一三二'),
cnchar.sortStroke(['一', '三', 2]),
cnchar.sortStroke(['一', '三', '二'], 'desc'),
];
},
expect: [
['一', '二', '三'],
'一二三',
['一', 2, '三'],
['三', '二', '一']
]
}
];

View File

@ -39,16 +39,27 @@ function testSingle ({
};
function main (config, argsConfig) {
let d = new Date();
blue('Test start:');
console.log();
let success = true;
config.forEach((item) => {
let result = testSingle(item, argsConfig);
if (result.pass) {
green(`PASS: [${result.name}]; result:${JSON.stringify(result.result)}`);
} else {
if (success) {success = false;}
red(`FAIL: [${result.name}]; result:${JSON.stringify(result.result)}; but expect: ${JSON.stringify(result.expect)}`);
}
console.log();
});
let text = `****** TEST ${success ? 'successed' : 'failed'} in ${(new Date()) - d}ms! ******`;
if (success) {
green(text);
} else {
red(text);
}
console.log();
}
function objectEqual (o1, o2) {
return objectEqualBase(o1, o2) && objectEqualBase(o2, o1);