Skip to content

Commit

Permalink
refact: code lint and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Mar 19, 2022
1 parent a498e87 commit bbd58a5
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"prettier"
],
"rules": {
"@typescript-eslint/ban-ts-comment": "warn",

"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-explicit-any": "warn",
"quotes": ["error", "double"],
"comma-dangle": ["error", "always-multiline"],
"semi": ["error", "always"]
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@
"benchmark": "~1.0.0",
"dumi": "^1.1.30",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-prettier": "^4.0.0",
"gh-pages": "^3.2.3",
"jest": "^27.2.3",
"mock-aws-s3": "^4.0.2",
"nock": "^13.1.4",
"npx": "^10.2.2",
"prettier": "^2.5.1",
"prettier": "^2.6.0",
"react-json-view": "^1.21.3",
"request": "~2.68.0",
"ts-node": "^10.5.0",
Expand Down
12 changes: 6 additions & 6 deletions src/data/dict-zi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26150,7 +26150,7 @@ dict[0x20007] = "qiě"; /*  */
dict[0x20008] = "qiū"; /*  */
dict[0x20009] = "qiū"; /* */
dict[0x2000a] = "cāo"; /* */
dict[0x2000c] = "qiū"; /* */
dict[0x2000c] = "qiū"; /* */
dict[0x2000d] = "shì"; /* */
dict[0x20013] = "sī"; /*  */
dict[0x20014] = "jué"; /*  */
Expand Down Expand Up @@ -28767,10 +28767,10 @@ dict[0x21ffa] = "nǎo"; /* Ὼ */
dict[0x21ffc] = "xùn"; /* ῼ */
dict[0x21ffe] = "jú"; /* ῾ */
dict[0x21fff] = "huò"; /* ῿ */
dict[0x22001] = "yì"; /* */
dict[0x2200a] = "xī"; /* */
dict[0x2200b] = "sè"; /* */
dict[0x2200c] = "jiǎo"; /* */
dict[0x22001] = "yì"; /* */
dict[0x2200a] = "xī"; /* */
dict[0x2200b] = "sè"; /* */
dict[0x2200c] = "jiǎo"; /* */
dict[0x2200d] = "yōng"; /* ‍ */
dict[0x22013] = "jù"; /* – */
dict[0x22015] = "shī"; /* ― */
Expand Down Expand Up @@ -30112,7 +30112,7 @@ dict[0x22ff1] = "sàn"; /* ⿱ */
dict[0x22ff2] = "qiāo"; /* ⿲ */
dict[0x22ff3] = "luàn"; /* ⿳ */
dict[0x22ff8] = "cè"; /* ⿸ */
dict[0x23000] = "léi"; /*   */
dict[0x23000] = "léi"; /* */
dict[0x23001] = "zhǎn"; /* 、 */
dict[0x23002] = "lǐ"; /* 。 */
dict[0x23003] = "lián"; /* 〃 */
Expand Down
7 changes: 4 additions & 3 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ENUM_PINYIN_STYLE, PHONETIC_SYMBOL, INITIALS } from "./constant";
import { hasKey } from "./util";

/*
* 格式化拼音为声母(Initials)形式。
Expand All @@ -14,8 +15,8 @@ function initials(py: string): string {
return "";
}

const RE_PHONETIC_SYMBOL: RegExp = new RegExp("([" + Object.keys(PHONETIC_SYMBOL).join("") + "])", "g");
const RE_TONE2: RegExp = /([aeoiuvnm])([0-4])$/;
const RE_PHONETIC_SYMBOL = new RegExp("([" + Object.keys(PHONETIC_SYMBOL).join("") + "])", "g");
const RE_TONE2 = /([aeoiuvnm])([0-4])$/;

/**
* 格式化拼音风格。
Expand All @@ -34,7 +35,7 @@ export function toFixed(pinyin: string, style: ENUM_PINYIN_STYLE): string {

case ENUM_PINYIN_STYLE.FIRST_LETTER:
first_letter = pinyin.charAt(0);
if (PHONETIC_SYMBOL.hasOwnProperty(first_letter)) {
if (hasKey(PHONETIC_SYMBOL, first_letter)) {
first_letter = PHONETIC_SYMBOL[first_letter].charAt(0);
}
return first_letter;
Expand Down
2 changes: 1 addition & 1 deletion src/phonetic-symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ const phonetic_symbol: Record<string,string> = {
"ń": "n2",
"ň": "n3",
"": "m2",
}
};

export default phonetic_symbol;
26 changes: 13 additions & 13 deletions src/pinyin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { segment } from "./segment";
import { toFixed } from "./format";
import SurnamePinyinData from "./data/surname";
import CompoundSurnamePinyinData from "./data/compound_surname";
import { convertUserOptions, combo, compact } from "./util";
import { hasKey, convertUserOptions, combo, compact } from "./util";
import { ENUM_PINYIN_MODE } from "./constant";
import type {
IPinyinAllOptions,
Expand Down Expand Up @@ -45,7 +45,7 @@ export default pinyin;
* 不使用分词算法的拼音转换。
*/
function normal_pinyin(hans: string, options: IPinyinAllOptions): string[][] {
let pys: string[][] = [];
const pys: string[][] = [];
let nohans = "";

for(let i = 0, l = hans.length; i < l; i++) {
Expand Down Expand Up @@ -85,23 +85,23 @@ function single_pinyin(han: string, options: IPinyinAllOptions): string[] {
return single_pinyin(han.charAt(0), options);
}

let hanCode = han.charCodeAt(0);
const hanCode = han.charCodeAt(0);

if (!DICT_ZI[hanCode]) {
return [han];
}

let pys = DICT_ZI[hanCode].split(",");
const pys = DICT_ZI[hanCode].split(",");
if(!options.heteronym){
return [toFixed(pys[0], options.style)];
}

// 临时存储已存在的拼音,避免多音字拼音转换为非注音风格出现重复。
let py_cached: Record<string,string> = {};
let pinyins = [];
const py_cached: Record<string,string> = {};
const pinyins = [];
for(let i = 0, l = pys.length; i < l; i++){
const py = toFixed(pys[i], options.style);
if(py_cached.hasOwnProperty(py)){
if(hasKey(py_cached, py)){
continue;
}
py_cached[py] = py;
Expand Down Expand Up @@ -159,8 +159,8 @@ function segment_pinyin(hans: string, options: IPinyinAllOptions): string[][] {
* @return {Array}
*/
function phrases_pinyin(phrases: string, options: IPinyinAllOptions): string[][] {
let py: string[][] = [];
if (DICT_PHRASES.hasOwnProperty(phrases)) {
const py: string[][] = [];
if (hasKey(DICT_PHRASES, phrases)) {
//! copy pinyin result.
DICT_PHRASES[phrases].forEach(function(item: string[], idx: number) {
py[idx] = [];
Expand Down Expand Up @@ -197,12 +197,12 @@ function surname_pinyin(hans: string, options: IPinyinAllOptions): string[][] {

// 复姓处理
function compound_surname(hans: string, options: IPinyinAllOptions): string[][] {
let len = hans.length;
const len = hans.length;
let prefixIndex = 0;
let result: string[][] = [];
for (let i = 0; i < len; i++) {
const twowords = hans.substring(i, i + 2);
if (CompoundSurnamePinyinData.hasOwnProperty(twowords)) {
if (hasKey(CompoundSurnamePinyinData, twowords)) {
if (prefixIndex <= i - 1) {
result = result.concat(
single_surname(
Expand Down Expand Up @@ -232,7 +232,7 @@ function single_surname(hans: string, options: IPinyinAllOptions) {
let result: string[][] = [];
for (let i = 0, l = hans.length; i < l; i++) {
const word = hans.charAt(i);
if (SurnamePinyinData.hasOwnProperty(word)) {
if (hasKey(SurnamePinyinData, word)) {
result = result.concat(SurnamePinyinData[word]);
} else {
result.push(single_pinyin(word, options));
Expand All @@ -254,4 +254,4 @@ export function compare(hanA: string, hanB: string): number {
return String(pinyinA).localeCompare(String(pinyinB));
}

export { compact } from './util';
export { compact } from "./util";
6 changes: 3 additions & 3 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import nodejieba from "nodejieba";
import { load, cut /*, tag */ } from '@node-rs/jieba';
import { load, cut /*, tag */ } from "@node-rs/jieba";
// @ts-ignore
import { Segment, useDefault } from 'segmentit';
import { Segment, useDefault } from "segmentit";
import type { IPinyinSegment } from "./declare";

let nodeRsJiebaLoaded = false; // @node-rs/jieba 加载词典。
const nodeRsJiebaLoaded = false; // @node-rs/jieba 加载词典。
let segmentit: any; // segmentit 加载词典。

/**
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type {
} from "./declare";
import { ENUM_PINYIN_STYLE, ENUM_PINYIN_MODE, DEFAULT_OPTIONS } from "./constant";

export function hasKey(obj: any, key: string) {
return Object.prototype.hasOwnProperty.call(obj, key);
}

const pinyinStyleMap: Map<string, ENUM_PINYIN_STYLE> = new Map([
[ "tone", ENUM_PINYIN_STYLE.TONE ],
[ "TONE", ENUM_PINYIN_STYLE.TONE ],
Expand Down
2 changes: 1 addition & 1 deletion test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exec } from "child_process";

var testcases = [
const testcases = [
{
command: "pinyin 中国",
stdout: "zhōng guó",
Expand Down
20 changes: 10 additions & 10 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("pinyin() without param", function() {
});
});

const cases: any = [
const cases: any[] = [

// 单音字
[ "我", {
Expand Down Expand Up @@ -241,36 +241,36 @@ function getPinyinStyle(styleName: string): IPinyinStyle {
return styleName.replace(/^STYLE_/, "") as IPinyinStyle;
}
function makeTest(han: string, opt: any, style: string){
var py = opt[style];
var pys = py;
let py = opt[style];
let pys = py;
// 有多音字的词组。
if (py.normal && py.segment) {
pys = py.segment;
py = py.normal;
}
var single_pinyin = [];
for(var i = 0, l = py.length; i < l; i++){
const single_pinyin = [];
for(let i = 0, l = py.length; i < l; i++){
single_pinyin[i] = [py[i][0]];
}

// 非多音字模式。
var _py = pinyin(han, {style: getPinyinStyle(style)});
const _py = pinyin(han, {style: getPinyinStyle(style)});
it("pinyin(\"" + han + "\", " + style + ") : " +
JSON.stringify(_py) + " === " + JSON.stringify(single_pinyin), function() {

expect(_py).toEqual(single_pinyin);
});

// 普通多音字模式。
var _py2 = pinyin(han, {style: getPinyinStyle(style), heteronym:true});
const _py2 = pinyin(han, {style: getPinyinStyle(style), heteronym:true});
it("pinyin(\"" + han + "\", " + style + ",heteronym) : " +
JSON.stringify(_py2) + " === " + JSON.stringify(py), function() {

expect(_py2).toEqual(py);
});

// 分词多音字模式。
var _py2s = pinyin(han, {
const _py2s = pinyin(han, {
style: getPinyinStyle(style),
heteronym: true,
segment: true,
Expand All @@ -284,10 +284,10 @@ function makeTest(han: string, opt: any, style: string){

describe("pinyin", function() {

for (var i = 0, l = cases.length; i < l; i++) {
for (let i = 0, l = cases.length; i < l; i++) {
const han = cases[i][0];
const opt: Record<string, string[][]> = cases[i][1];
for(var style in opt) {
for(const style in opt) {
makeTest(han, opt, style);
}
}
Expand Down

0 comments on commit bbd58a5

Please sign in to comment.