Skip to content

Commit

Permalink
Merge pull request #276 from zh-lx/feature-match-v
Browse files Browse the repository at this point in the history
feat: match api 支持 v 转换
  • Loading branch information
zh-lx authored Sep 24, 2024
2 parents 2d0ac5b + 2e203f0 commit bd915fe
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 73 deletions.
10 changes: 10 additions & 0 deletions lib/core/match/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface MatchOptions {
* @description 是否大小写不敏感
*/
insensitive?: boolean;
/**
* @description 是否将 ü 替换成 v 进行匹配
*/
v?: boolean;
}

const DefaultMatchOptions: MatchOptions = {
Expand All @@ -30,6 +34,7 @@ const DefaultMatchOptions: MatchOptions = {
space: "ignore",
lastPrecision: "start",
insensitive: true,
v: false,
};

const MAX_PINYIN_LENGTH = 6;
Expand All @@ -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 || {}),
Expand Down Expand Up @@ -101,6 +109,7 @@ const matchAny = (
toneType: "none",
multiple: true,
type: "array",
v: options.v,
});
let currentLength = 0;
ps.forEach((p) => {
Expand Down Expand Up @@ -179,6 +188,7 @@ const matchAboveStart = (
type: "array",
toneType: "none",
multiple: true,
v: options.v,
});

// 非中文匹配
Expand Down
171 changes: 98 additions & 73 deletions test/match.test.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});
4 changes: 4 additions & 0 deletions types/core/match/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface MatchOptions {
* @description 是否大小写不敏感
*/
insensitive?: boolean;
/**
* @description 是否将 ü 替换成 v 进行匹配
*/
v?: boolean;
}
/**
* @description: 检测汉语字符串和拼音是否匹配
Expand Down

0 comments on commit bd915fe

Please sign in to comment.