From 6b6a5bb799f9ae344a02e6b05302be1e4873d003 Mon Sep 17 00:00:00 2001 From: lazyloong <2950449004@qq.com> Date: Mon, 16 Dec 2024 13:36:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8B=BC=E9=9F=B3=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E6=97=B6=E5=8A=A0=E5=85=A5=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=20#72?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- src/utils/pinyin.ts | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/manifest.json b/manifest.json index 7b26d65..e77de52 100644 --- a/manifest.json +++ b/manifest.json @@ -5,5 +5,5 @@ "author": "lazyloong", "minAppVersion": "1.0.0", - "version": "2.27.12" + "version": "2.27.13" } diff --git a/src/utils/pinyin.ts b/src/utils/pinyin.ts index 0cc878a..560e1c6 100644 --- a/src/utils/pinyin.ts +++ b/src/utils/pinyin.ts @@ -35,15 +35,14 @@ export class Pinyin extends Array { return score; } match(query: string, item: T, smathCase = false): MatchData { - let range = this.match_(query, smathCase); - range = range ? toRanges(range) : false; + const range_ = this.match_(query, smathCase); + const range = range_ ? toRanges(range_) : false; if (!range) return; - let data: MatchData = { + return { item: item, score: this.getScore(range), range: range, }; - return data; } concat(pinyin: Pinyin) { let result = new Pinyin("", null); @@ -58,14 +57,19 @@ export class Pinyin extends Array { } // The following two functions are based on the work of zh-lx (https://github.com/zh-lx). // Original code: https://github.com/zh-lx/pinyin-pro/blob/main/lib/core/match/index.ts. - match_(pinyin: string, smathCase: boolean) { + match_(pinyin: string, smathCase: boolean): number[] | null { pinyin = pinyin.replace(/\s/g, ""); - let f = (str: string) => (smathCase ? str : str.toLocaleLowerCase()); - const result = this.matchAboveStart(f(this.text), f(pinyin)); + const f = (str: string) => (smathCase ? str : str.toLocaleLowerCase()); + let result: number[]; + try { + result = this.matchAboveStart(f(this.text), f(pinyin)); + } catch (e) { + console.log(this, pinyin); + } return result; } - matchAboveStart(text: string, pinyin: string) { + matchAboveStart(text: string, pinyin: string): number[] | null { const words = text.split(""); // 二维数组 dp[i][j],i 表示遍历到的 text 索引+1, j 表示遍历到的 pinyin 的索引+1