This commit is contained in:
tackchen 2020-12-10 18:12:34 +08:00
parent d444edc987
commit 0d8a147308
6 changed files with 189 additions and 1 deletions

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

@ -43,7 +43,7 @@ export declare interface CnCharStatic {
transformTone(spell: string, tone?: boolean, type?: 'low' | 'up'): {
spell: string;
tone: toneType;
index: number;
index: number;
isTrans: boolean;
};
isCnChar(word: string): boolean;
@ -59,6 +59,10 @@ export declare interface CnCharStatic {
setStrokeCount(word: string, count: number): void;
setStrokeCount(json: {[key: string]: number}): void;
shapeSpell(spell: string): string;
_: {
}
}
declare const cnchar: CnCharStatic;

87
src/main/index.ts Normal file
View File

@ -0,0 +1,87 @@
import version from './version';
let version = require('./version');
let {spell, tones, stroke, arg,
dealUpLowFirst, removeTone, sumStroke,
checkArgs, initCnchar, transformTone,
shapeSpell} = require('./tool');
let {has, _throw, _warn, isCnChar, isPolyWord, mapJson} = require('./util');
let dict = require('./dict');
let {setSpellDefault, setIntoJson, setSpell, setStrokeCount} = require('./config');
let {initSpellToWord} = require('./spellToWord');
let initStrokeToWord = require('./strokeToWord');
let {compareSpell, sortSpell, compareStroke, sortStroke, initSort} = require('./sort');
function _spell (...args) {
return spell(dict.spell, args);
}
function _stroke (...args) {
return stroke(dict.stroke, args);
}
function initStrProto () {
String.prototype.spell = function (...args) {
return _spell(this, ...args);
};
String.prototype.stroke = function (...args) {
return _stroke(this, ...args);
};
}
function use (...plugins) {
plugins.forEach(f => {
if (typeof f === 'function') {
if (typeof f.init === 'function') {
f.init(cnchar);
} else {
f(cnchar);
}
}
});
}
let cnchar = {
version,
spell: _spell,
stroke: _stroke,
check: true,
_origin: {
spell: _spell,
stroke: _stroke,
},
plugins: [],
use,
_: {arg, has, _throw, tones, setIntoJson, _warn, dealUpLowFirst, removeTone,
sumStroke, isCnChar, checkArgs, transformTone, dict: {}, mapJson},
type: {
spell: arg,
stroke: {
array: arg.array
}
},
// 工具方法
transformTone,
isCnChar,
compareSpell,
compareStroke,
sortSpell,
sortStroke,
setSpellDefault,
setSpell,
setStrokeCount,
isPolyWord,
shapeSpell,
};
function init () {
initStrProto();
initCnchar(cnchar);
initSpellToWord(cnchar);
initStrokeToWord(cnchar);
initSort(cnchar);
if (typeof window !== 'undefined') {
window.cnchar = window.CnChar = cnchar;
}
}
init();
module.exports = cnchar;

96
src/main/spellToWord.ts Normal file
View File

@ -0,0 +1,96 @@
import dict from './spell-dict-jian.json';
import {initial as initialDict} from './info-dict.json';
import {CnCharStatic} from './index.d';
// let dict = require('./spell-dict-jian.json');
// let initialDict = require('./info-dict.json').initial;
let arg = {simple: 'simple', trad: 'trad', poly: 'poly', alltone: 'alltone', array: 'array'};
let _ = {};// 工具方法
declare interface
function initSpellToWord (cnchar: CnCharStatic) {
_ = cnchar._;
cnchar.spellToWord = spellToWord;
spellInfo.tones = _.tones.split('');
spellInfo.initials = initialDict;
cnchar.spellInfo = spellInfo;
cnchar.type.spellToWord = arg;
}
// 获取拼音信息 spell,tone,index,initial,final
function spellInfo (spell) {
spell = spell.toLowerCase();
let info = _.removeTone(spell, false);
if (info.index === -1) {
if (!dict[info.spell]) {
throw new Error('【spellInfo】错误的拼音: ' + spell);
}
info.index = parseInt(dict[info.spell][0]) + 1;
}
let initial = '';
let final = info.spell;
for (let i = 0; i < initialDict.length; i++) {
if (info.spell.indexOf(initialDict[i]) === 0) {
initial = initialDict[i];
final = info.spell.substr(initial.length);
break;
}
}
info.initial = initial;
info.final = final;
return info;
}
spellInfo.tones:Array;
function spellToWord (...args) {
let spell = args[0].toLowerCase();
if (typeof spell !== 'string') {
throw new Error('spellToWord: 输入必须是字符串');
}
let info = _.transformTone(spell, false);
args = args.splice(1);
_.checkArgs('spellToWord', args);
let argRes = {
simple: _.has(args, arg.simple),
trad: _.has(args, arg.trad),
poly: _.has(args, arg.poly),
alltone: _.has(args, arg.alltone),
};
if (!argRes.simple && !argRes.trad) {
argRes.simple = argRes.trad = true;
}
let res = '';
let str = dict[info.spell].substr(2);
for (let i = 0; i < str.length; i += 2) {
let word = str[i];
let tone = parseInt(str[i + 1]);
let isPoly = tone > 4;
if (isPoly) {tone = tone - 5;}
if (res.indexOf(word) === -1 && (argRes.alltone || tone === info.tone)) {
if (!argRes.poly || isPoly) {
res += word;
}
}
}
if (argRes.trad && _.convert) {
let tradRes = '';
for (let i = 0; i < res.length; i++) {
let trad = _.convert.simpleToTrad(res[i]);
if (trad !== res[i]) {
tradRes += trad;
}
}
if (!argRes.simple) {
res = tradRes;
} else {
res += tradRes;
}
}
if (_.has(args, arg.array)) {
return res.split('');
}
return res;
}
module.exports = {initSpellToWord, spellInfo};

0
src/main/tool.ts Normal file
View File

0
src/main/types/tool.d.ts vendored Normal file
View File

1
src/main/version.ts Normal file
View File

@ -0,0 +1 @@
export default '2.2.8';