Skip to content

Commit bd915fe

Browse files
authored
Merge pull request #276 from zh-lx/feature-match-v
feat: match api 支持 v 转换
2 parents 2d0ac5b + 2e203f0 commit bd915fe

File tree

3 files changed

+112
-73
lines changed

3 files changed

+112
-73
lines changed

lib/core/match/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ interface MatchOptions {
2222
* @description 是否大小写不敏感
2323
*/
2424
insensitive?: boolean;
25+
/**
26+
* @description 是否将 ü 替换成 v 进行匹配
27+
*/
28+
v?: boolean;
2529
}
2630

2731
const DefaultMatchOptions: MatchOptions = {
@@ -30,6 +34,7 @@ const DefaultMatchOptions: MatchOptions = {
3034
space: "ignore",
3135
lastPrecision: "start",
3236
insensitive: true,
37+
v: false,
3338
};
3439

3540
const MAX_PINYIN_LENGTH = 6;
@@ -45,6 +50,9 @@ export const match = (text: string, pinyin: string, options?: MatchOptions) => {
4550
if (options?.precision === "any") {
4651
options.lastPrecision = "any";
4752
}
53+
if (options?.v) {
54+
pinyin = pinyin.replace(/ü/g, "v");
55+
}
4856
const completeOptions = {
4957
...DefaultMatchOptions,
5058
...(options || {}),
@@ -101,6 +109,7 @@ const matchAny = (
101109
toneType: "none",
102110
multiple: true,
103111
type: "array",
112+
v: options.v,
104113
});
105114
let currentLength = 0;
106115
ps.forEach((p) => {
@@ -179,6 +188,7 @@ const matchAboveStart = (
179188
type: "array",
180189
toneType: "none",
181190
multiple: true,
191+
v: options.v,
182192
});
183193

184194
// 非中文匹配

test/match.test.js

Lines changed: 98 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,199 @@
1-
import { match, customPinyin, clearCustomDict } from '../lib/index';
2-
import { expect, describe, it } from 'vitest';
1+
import { match, customPinyin, clearCustomDict } from "../lib/index";
2+
import { expect, describe, it } from "vitest";
33

4-
describe('match', () => {
5-
it('[match]default', () => {
6-
const result = match('欢迎使用汉语拼音', 'hy');
4+
describe("match", () => {
5+
it("[match]default", () => {
6+
const result = match("欢迎使用汉语拼音", "hy");
77
expect(result).to.deep.equal([0, 1]);
88
});
99

10-
it('[match]start and continuous', () => {
11-
const result = match('欢迎使用汉语拼音', 'yingshyon', {
12-
precision: 'start',
10+
it("[match]start and continuous", () => {
11+
const result = match("欢迎使用汉语拼音", "yingshyon", {
12+
precision: "start",
1313
continuous: true,
1414
});
1515
expect(result).to.deep.equal([1, 2, 3]);
1616
});
1717

18-
it('[match]multiple1', () => {
19-
const result = match('会计', 'kj');
18+
it("[match]multiple1", () => {
19+
const result = match("会计", "kj");
2020
expect(result).to.deep.equal([0, 1]);
2121
});
2222

23-
it('[match]multiple2', () => {
24-
const result = match('会计', 'huij');
23+
it("[match]multiple2", () => {
24+
const result = match("会计", "huij");
2525
expect(result).to.deep.equal([0, 1]);
2626
});
2727

28-
it('[match]any', () => {
29-
const result = match('开会', 'kaiui', { precision: 'any' });
28+
it("[match]any", () => {
29+
const result = match("开会", "kaiui", { precision: "any" });
3030
expect(result).to.deep.equal([0, 1]);
3131
});
3232

33-
it('[match]any&empty', () => {
34-
const result = match('开会', '', { precision: 'any' });
33+
it("[match]any&empty", () => {
34+
const result = match("开会", "", { precision: "any" });
3535
expect(result).to.deep.equal(null);
3636
});
3737

38-
it('[match]any&continuous', () => {
39-
const result = match('开个大会', 'kaiui', {
40-
precision: 'any',
38+
it("[match]any&continuous", () => {
39+
const result = match("开个大会", "kaiui", {
40+
precision: "any",
4141
continuous: true,
4242
});
4343
expect(result).to.deep.equal(null);
4444
});
4545

46-
it('[match]any&nonZh', () => {
47-
const result = match('开会', 'kaiuiaaaa', { precision: 'any' });
46+
it("[match]any&nonZh", () => {
47+
const result = match("开会", "kaiuiaaaa", { precision: "any" });
4848
expect(result).to.deep.equal(null);
4949
});
5050

51-
it('[match]any&space', () => {
52-
const result = match('开 会s 啊', 'kaiuisa', { precision: 'any' });
51+
it("[match]any&space", () => {
52+
const result = match("开 会s 啊", "kaiuisa", { precision: "any" });
5353
expect(result).to.deep.equal([0, 7, 8, 11]);
5454
});
5555

56-
it('[match]fail with sucess', () => {
57-
const result = match('开会', 'kaig');
56+
it("[match]fail with sucess", () => {
57+
const result = match("开会", "kaig");
5858
expect(result).to.deep.equal(null);
5959
});
6060

61-
it('[match]fail', () => {
62-
const result = match('开会', 'l');
61+
it("[match]fail", () => {
62+
const result = match("开会", "l");
6363
expect(result).to.deep.equal(null);
6464
});
6565

66-
it('[match]uncontinuous', () => {
67-
const result = match('汉语拼音', 'hanpin');
66+
it("[match]uncontinuous", () => {
67+
const result = match("汉语拼音", "hanpin");
6868
expect(result).to.deep.equal([0, 2]);
6969
});
7070

71-
it('[match]basic', () => {
72-
const result = match('汉语拼音', 'hyupy');
71+
it("[match]basic", () => {
72+
const result = match("汉语拼音", "hyupy");
7373
expect(result).to.deep.equal([0, 1, 2, 3]);
7474
});
7575

76-
it('[match]first & double unicode', () => {
77-
const result = match('𧒽测试', 'cs');
76+
it("[match]first & double unicode", () => {
77+
const result = match("𧒽测试", "cs");
7878
expect(result).to.deep.equal([2, 3]);
7979
});
8080

81-
it('[match]start', () => {
82-
const result = match('欢迎使用汉语拼音', '欢yingshy', {
83-
precision: 'start',
81+
it("[match]start", () => {
82+
const result = match("欢迎使用汉语拼音", "欢yingshy", {
83+
precision: "start",
8484
});
8585
expect(result).to.deep.equal([0, 1, 2, 3]);
8686
});
8787

88-
it('[match]first&space', () => {
89-
const result = match('𧒽测 试', 'c s');
88+
it("[match]first&space", () => {
89+
const result = match("𧒽测 试", "c s");
9090
expect(result).to.deep.equal([2, 4]);
9191
});
9292

93-
it('[match]first&space', () => {
94-
customPinyin({
95-
𧒽: 'lei'
96-
}, {
97-
multiple: 'replace'
98-
})
99-
const result = match('𧒽测 试', 'l c s');
93+
it("[match]first&space", () => {
94+
customPinyin(
95+
{
96+
𧒽: "lei",
97+
},
98+
{
99+
multiple: "replace",
100+
}
101+
);
102+
const result = match("𧒽测 试", "l c s");
100103
expect(result).to.deep.equal([0, 1, 2, 4]);
101-
clearCustomDict(['pinyin', 'multiple', 'polyphonic']);
104+
clearCustomDict(["pinyin", "multiple", "polyphonic"]);
102105
});
103106

104-
it('[match]nonZh match', () => {
105-
const result = match('测uuuuuuuuuu试', 'cuuuuuu');
107+
it("[match]nonZh match", () => {
108+
const result = match("测uuuuuuuuuu试", "cuuuuuu");
106109
expect(result).to.deep.equal([0, 1, 2, 3, 4, 5, 6]);
107110
});
108111

109-
it('[match]lastPrecision every fail', () => {
110-
const result = match('汉语拼音', 'hanyupinyi', { lastPrecision: 'every' });
112+
it("[match]lastPrecision every fail", () => {
113+
const result = match("汉语拼音", "hanyupinyi", { lastPrecision: "every" });
111114
expect(result).to.deep.equal(null);
112115
});
113116

114-
it('[match]lastPrecision every success', () => {
115-
const result = match('汉语拼音', 'hanyupinyin', { lastPrecision: 'every' });
117+
it("[match]lastPrecision every success", () => {
118+
const result = match("汉语拼音", "hanyupinyin", { lastPrecision: "every" });
116119
expect(result).to.deep.equal([0, 1, 2, 3]);
117120
});
118121

119-
it('[match]lastPrecision first fail', () => {
120-
const result = match('汉语拼音', 'hanyupinyi', { lastPrecision: 'first' });
122+
it("[match]lastPrecision first fail", () => {
123+
const result = match("汉语拼音", "hanyupinyi", { lastPrecision: "first" });
121124
expect(result).to.deep.equal(null);
122125
});
123126

124-
it('[match]lastPrecision first success', () => {
125-
const result = match('汉语拼音', 'hanyupiny', { lastPrecision: 'first' });
127+
it("[match]lastPrecision first success", () => {
128+
const result = match("汉语拼音", "hanyupiny", { lastPrecision: "first" });
126129
expect(result).to.deep.equal([0, 1, 2, 3]);
127130
});
128131

129-
it('[match]lastPrecision any', () => {
130-
const result = match('汉语拼音', 'hanyupini', { lastPrecision: 'any' });
132+
it("[match]lastPrecision any", () => {
133+
const result = match("汉语拼音", "hanyupini", { lastPrecision: "any" });
131134
expect(result).to.deep.equal([0, 1, 2, 3]);
132135
});
133136

134-
it('[match]lastPrecision ignore space', () => {
135-
const result = match('汉语 拼音', 'hanyu pini', { lastPrecision: 'any', space: 'ignore' });
137+
it("[match]lastPrecision ignore space", () => {
138+
const result = match("汉语 拼音", "hanyu pini", {
139+
lastPrecision: "any",
140+
space: "ignore",
141+
});
136142
expect(result).to.deep.equal([0, 1, 3, 4]);
137143
});
138144

139-
it('[match]lastPrecision preserve space', () => {
140-
const result = match('汉语 拼音', 'hanyu pini', { lastPrecision: 'any', space: 'preserve' });
145+
it("[match]lastPrecision preserve space", () => {
146+
const result = match("汉语 拼音", "hanyu pini", {
147+
lastPrecision: "any",
148+
space: "preserve",
149+
});
141150
expect(result).to.deep.equal([0, 1, 2, 3, 4]);
142151
});
143152

144-
it('[match]precision preserve space', () => {
145-
const result = match('汉语 拼音', 'hanyu pini', { precision: 'any', space: 'preserve' });
153+
it("[match]precision preserve space", () => {
154+
const result = match("汉语 拼音", "hanyu pini", {
155+
precision: "any",
156+
space: "preserve",
157+
});
146158
expect(result).to.deep.equal([0, 1, 2, 3, 4]);
147159
});
148160

149-
it('[match]precision not continuous', () => {
150-
const result = match('汉语 拼音', 'hanyu yin', { precision: 'any', space: 'preserve', continuous: true });
161+
it("[match]precision not continuous", () => {
162+
const result = match("汉语 拼音", "hanyu yin", {
163+
precision: "any",
164+
space: "preserve",
165+
continuous: true,
166+
});
151167
expect(result).to.deep.equal(null);
152168
});
153169

154-
it('[match]precision continuous', () => {
155-
const result = match('汉语 拼音', 'hanyu pinyin', { precision: 'any', space: 'preserve', continuous: true });
170+
it("[match]precision continuous", () => {
171+
const result = match("汉语 拼音", "hanyu pinyin", {
172+
precision: "any",
173+
space: "preserve",
174+
continuous: true,
175+
});
156176
expect(result).to.deep.equal([0, 1, 2, 3, 4]);
157177
});
158178

159-
it('[match]lastPrecision none', () => {
179+
it("[match]lastPrecision none", () => {
160180
// @ts-ignore
161-
const result = match('汉语拼音', 'hanyupini', { lastPrecision: 'none' });
181+
const result = match("汉语拼音", "hanyupini", { lastPrecision: "none" });
162182
expect(result).to.deep.equal(null);
163183
});
164184

165-
it('[match]insensitive', () => {
166-
const result = match('汉语KK拼音', 'hanyukkpinyin');
185+
it("[match]insensitive", () => {
186+
const result = match("汉语KK拼音", "hanyukkpinyin");
167187
expect(result).to.deep.equal([0, 1, 2, 3, 4, 5]);
168188

169-
const result1 = match('汉语KK拼音', 'hanyukkpinyin', {
189+
const result1 = match("汉语KK拼音", "hanyukkpinyin", {
170190
insensitive: false,
171191
});
172192
expect(result1).to.deep.equal(null);
173193
});
194+
195+
it("[match]v", () => {
196+
const result = match("我是吕布", "woshilvbu", { v: true });
197+
expect(result).to.deep.equal([0, 1, 2, 3]);
198+
});
174199
});

types/core/match/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ interface MatchOptions {
1919
* @description 是否大小写不敏感
2020
*/
2121
insensitive?: boolean;
22+
/**
23+
* @description 是否将 ü 替换成 v 进行匹配
24+
*/
25+
v?: boolean;
2226
}
2327
/**
2428
* @description: 检测汉语字符串和拼音是否匹配

0 commit comments

Comments
 (0)