From 2e203f0b46775fdf4fb12466a4897efa2cfcebec Mon Sep 17 00:00:00 2001 From: zh-lx <18366276315@163.com> Date: Tue, 24 Sep 2024 22:19:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20match=20api=20=E6=94=AF=E6=8C=81=20v=20?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/match/index.ts | 10 +++ test/match.test.js | 171 +++++++++++++++++++++--------------- types/core/match/index.d.ts | 4 + 3 files changed, 112 insertions(+), 73 deletions(-) diff --git a/lib/core/match/index.ts b/lib/core/match/index.ts index 352eb57..1440d7f 100644 --- a/lib/core/match/index.ts +++ b/lib/core/match/index.ts @@ -22,6 +22,10 @@ interface MatchOptions { * @description 是否大小写不敏感 */ insensitive?: boolean; + /** + * @description 是否将 ü 替换成 v 进行匹配 + */ + v?: boolean; } const DefaultMatchOptions: MatchOptions = { @@ -30,6 +34,7 @@ const DefaultMatchOptions: MatchOptions = { space: "ignore", lastPrecision: "start", insensitive: true, + v: false, }; const MAX_PINYIN_LENGTH = 6; @@ -45,6 +50,9 @@ export const match = (text: string, pinyin: string, options?: MatchOptions) => { if (options?.precision === "any") { options.lastPrecision = "any"; } + if (options?.v) { + pinyin = pinyin.replace(/ü/g, "v"); + } const completeOptions = { ...DefaultMatchOptions, ...(options || {}), @@ -101,6 +109,7 @@ const matchAny = ( toneType: "none", multiple: true, type: "array", + v: options.v, }); let currentLength = 0; ps.forEach((p) => { @@ -179,6 +188,7 @@ const matchAboveStart = ( type: "array", toneType: "none", multiple: true, + v: options.v, }); // 非中文匹配 diff --git a/test/match.test.js b/test/match.test.js index cf3f190..6fb59dd 100644 --- a/test/match.test.js +++ b/test/match.test.js @@ -1,174 +1,199 @@ -import { match, customPinyin, clearCustomDict } from '../lib/index'; -import { expect, describe, it } from 'vitest'; +import { match, customPinyin, clearCustomDict } from "../lib/index"; +import { expect, describe, it } from "vitest"; -describe('match', () => { - it('[match]default', () => { - const result = match('欢迎使用汉语拼音', 'hy'); +describe("match", () => { + it("[match]default", () => { + const result = match("欢迎使用汉语拼音", "hy"); expect(result).to.deep.equal([0, 1]); }); - it('[match]start and continuous', () => { - const result = match('欢迎使用汉语拼音', 'yingshyon', { - precision: 'start', + it("[match]start and continuous", () => { + const result = match("欢迎使用汉语拼音", "yingshyon", { + precision: "start", continuous: true, }); expect(result).to.deep.equal([1, 2, 3]); }); - it('[match]multiple1', () => { - const result = match('会计', 'kj'); + it("[match]multiple1", () => { + const result = match("会计", "kj"); expect(result).to.deep.equal([0, 1]); }); - it('[match]multiple2', () => { - const result = match('会计', 'huij'); + it("[match]multiple2", () => { + const result = match("会计", "huij"); expect(result).to.deep.equal([0, 1]); }); - it('[match]any', () => { - const result = match('开会', 'kaiui', { precision: 'any' }); + it("[match]any", () => { + const result = match("开会", "kaiui", { precision: "any" }); expect(result).to.deep.equal([0, 1]); }); - it('[match]any&empty', () => { - const result = match('开会', '', { precision: 'any' }); + it("[match]any&empty", () => { + const result = match("开会", "", { precision: "any" }); expect(result).to.deep.equal(null); }); - it('[match]any&continuous', () => { - const result = match('开个大会', 'kaiui', { - precision: 'any', + it("[match]any&continuous", () => { + const result = match("开个大会", "kaiui", { + precision: "any", continuous: true, }); expect(result).to.deep.equal(null); }); - it('[match]any&nonZh', () => { - const result = match('开会', 'kaiuiaaaa', { precision: 'any' }); + it("[match]any&nonZh", () => { + const result = match("开会", "kaiuiaaaa", { precision: "any" }); expect(result).to.deep.equal(null); }); - it('[match]any&space', () => { - const result = match('开 会s 啊', 'kaiuisa', { precision: 'any' }); + it("[match]any&space", () => { + const result = match("开 会s 啊", "kaiuisa", { precision: "any" }); expect(result).to.deep.equal([0, 7, 8, 11]); }); - it('[match]fail with sucess', () => { - const result = match('开会', 'kaig'); + it("[match]fail with sucess", () => { + const result = match("开会", "kaig"); expect(result).to.deep.equal(null); }); - it('[match]fail', () => { - const result = match('开会', 'l'); + it("[match]fail", () => { + const result = match("开会", "l"); expect(result).to.deep.equal(null); }); - it('[match]uncontinuous', () => { - const result = match('汉语拼音', 'hanpin'); + it("[match]uncontinuous", () => { + const result = match("汉语拼音", "hanpin"); expect(result).to.deep.equal([0, 2]); }); - it('[match]basic', () => { - const result = match('汉语拼音', 'hyupy'); + it("[match]basic", () => { + const result = match("汉语拼音", "hyupy"); expect(result).to.deep.equal([0, 1, 2, 3]); }); - it('[match]first & double unicode', () => { - const result = match('𧒽测试', 'cs'); + it("[match]first & double unicode", () => { + const result = match("𧒽测试", "cs"); expect(result).to.deep.equal([2, 3]); }); - it('[match]start', () => { - const result = match('欢迎使用汉语拼音', '欢yingshy', { - precision: 'start', + it("[match]start", () => { + const result = match("欢迎使用汉语拼音", "欢yingshy", { + precision: "start", }); expect(result).to.deep.equal([0, 1, 2, 3]); }); - it('[match]first&space', () => { - const result = match('𧒽测 试', 'c s'); + it("[match]first&space", () => { + const result = match("𧒽测 试", "c s"); expect(result).to.deep.equal([2, 4]); }); - it('[match]first&space', () => { - customPinyin({ - 𧒽: 'lei' - }, { - multiple: 'replace' - }) - const result = match('𧒽测 试', 'l c s'); + it("[match]first&space", () => { + customPinyin( + { + 𧒽: "lei", + }, + { + multiple: "replace", + } + ); + const result = match("𧒽测 试", "l c s"); expect(result).to.deep.equal([0, 1, 2, 4]); - clearCustomDict(['pinyin', 'multiple', 'polyphonic']); + clearCustomDict(["pinyin", "multiple", "polyphonic"]); }); - it('[match]nonZh match', () => { - const result = match('测uuuuuuuuuu试', 'cuuuuuu'); + it("[match]nonZh match", () => { + const result = match("测uuuuuuuuuu试", "cuuuuuu"); expect(result).to.deep.equal([0, 1, 2, 3, 4, 5, 6]); }); - it('[match]lastPrecision every fail', () => { - const result = match('汉语拼音', 'hanyupinyi', { lastPrecision: 'every' }); + it("[match]lastPrecision every fail", () => { + const result = match("汉语拼音", "hanyupinyi", { lastPrecision: "every" }); expect(result).to.deep.equal(null); }); - it('[match]lastPrecision every success', () => { - const result = match('汉语拼音', 'hanyupinyin', { lastPrecision: 'every' }); + it("[match]lastPrecision every success", () => { + const result = match("汉语拼音", "hanyupinyin", { lastPrecision: "every" }); expect(result).to.deep.equal([0, 1, 2, 3]); }); - it('[match]lastPrecision first fail', () => { - const result = match('汉语拼音', 'hanyupinyi', { lastPrecision: 'first' }); + it("[match]lastPrecision first fail", () => { + const result = match("汉语拼音", "hanyupinyi", { lastPrecision: "first" }); expect(result).to.deep.equal(null); }); - it('[match]lastPrecision first success', () => { - const result = match('汉语拼音', 'hanyupiny', { lastPrecision: 'first' }); + it("[match]lastPrecision first success", () => { + const result = match("汉语拼音", "hanyupiny", { lastPrecision: "first" }); expect(result).to.deep.equal([0, 1, 2, 3]); }); - it('[match]lastPrecision any', () => { - const result = match('汉语拼音', 'hanyupini', { lastPrecision: 'any' }); + it("[match]lastPrecision any", () => { + const result = match("汉语拼音", "hanyupini", { lastPrecision: "any" }); expect(result).to.deep.equal([0, 1, 2, 3]); }); - it('[match]lastPrecision ignore space', () => { - const result = match('汉语 拼音', 'hanyu pini', { lastPrecision: 'any', space: 'ignore' }); + it("[match]lastPrecision ignore space", () => { + const result = match("汉语 拼音", "hanyu pini", { + lastPrecision: "any", + space: "ignore", + }); expect(result).to.deep.equal([0, 1, 3, 4]); }); - it('[match]lastPrecision preserve space', () => { - const result = match('汉语 拼音', 'hanyu pini', { lastPrecision: 'any', space: 'preserve' }); + it("[match]lastPrecision preserve space", () => { + const result = match("汉语 拼音", "hanyu pini", { + lastPrecision: "any", + space: "preserve", + }); expect(result).to.deep.equal([0, 1, 2, 3, 4]); }); - it('[match]precision preserve space', () => { - const result = match('汉语 拼音', 'hanyu pini', { precision: 'any', space: 'preserve' }); + it("[match]precision preserve space", () => { + const result = match("汉语 拼音", "hanyu pini", { + precision: "any", + space: "preserve", + }); expect(result).to.deep.equal([0, 1, 2, 3, 4]); }); - it('[match]precision not continuous', () => { - const result = match('汉语 拼音', 'hanyu yin', { precision: 'any', space: 'preserve', continuous: true }); + it("[match]precision not continuous", () => { + const result = match("汉语 拼音", "hanyu yin", { + precision: "any", + space: "preserve", + continuous: true, + }); expect(result).to.deep.equal(null); }); - it('[match]precision continuous', () => { - const result = match('汉语 拼音', 'hanyu pinyin', { precision: 'any', space: 'preserve', continuous: true }); + it("[match]precision continuous", () => { + const result = match("汉语 拼音", "hanyu pinyin", { + precision: "any", + space: "preserve", + continuous: true, + }); expect(result).to.deep.equal([0, 1, 2, 3, 4]); }); - it('[match]lastPrecision none', () => { + it("[match]lastPrecision none", () => { // @ts-ignore - const result = match('汉语拼音', 'hanyupini', { lastPrecision: 'none' }); + const result = match("汉语拼音", "hanyupini", { lastPrecision: "none" }); expect(result).to.deep.equal(null); }); - it('[match]insensitive', () => { - const result = match('汉语KK拼音', 'hanyukkpinyin'); + it("[match]insensitive", () => { + const result = match("汉语KK拼音", "hanyukkpinyin"); expect(result).to.deep.equal([0, 1, 2, 3, 4, 5]); - const result1 = match('汉语KK拼音', 'hanyukkpinyin', { + const result1 = match("汉语KK拼音", "hanyukkpinyin", { insensitive: false, }); expect(result1).to.deep.equal(null); }); + + it("[match]v", () => { + const result = match("我是吕布", "woshilvbu", { v: true }); + expect(result).to.deep.equal([0, 1, 2, 3]); + }); }); diff --git a/types/core/match/index.d.ts b/types/core/match/index.d.ts index 43cc442..e8f3361 100644 --- a/types/core/match/index.d.ts +++ b/types/core/match/index.d.ts @@ -19,6 +19,10 @@ interface MatchOptions { * @description 是否大小写不敏感 */ insensitive?: boolean; + /** + * @description 是否将 ü 替换成 v 进行匹配 + */ + v?: boolean; } /** * @description: 检测汉语字符串和拼音是否匹配