@lppx/nlearn 1.1.4 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cli/cli.js +103 -3
- package/dist/src/cli/index.js +15 -1
- package/dist/src/demo/class/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +169 -0
- package/dist/src/demo/class/02-/347/273/247/346/211/277/344/270/216/345/244/232/346/200/201.js +232 -0
- package/dist/src/demo/class/03-/351/235/231/346/200/201/346/210/220/345/221/230/344/270/216/345/215/225/344/276/213.js +221 -0
- package/dist/src/demo/class/04-/346/216/245/345/217/243/344/270/216/345/256/236/347/216/260.js +163 -0
- package/dist/src/demo/fuse/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +168 -0
- package/dist/src/demo/fuse/02-/351/253/230/347/272/247/346/220/234/347/264/242.js +212 -0
- package/dist/src/demo/fuse/03-/351/205/215/347/275/256/350/257/246/350/247/243.js +261 -0
- package/dist/src/demo/fuse/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +354 -0
- package/package.json +1 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Fuse.js 高级搜索
|
|
4
|
+
* ================
|
|
5
|
+
* 介绍 Fuse.js 的高级搜索功能,包括扩展搜索、权重搜索等
|
|
6
|
+
*
|
|
7
|
+
* 官方文档: https://www.fusejs.io/
|
|
8
|
+
* 适用版本: fuse.js 7.x
|
|
9
|
+
*/
|
|
10
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
11
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
15
|
+
// #region 示例1: 扩展搜索 - 精确匹配
|
|
16
|
+
function 扩展搜索精确匹配() {
|
|
17
|
+
console.log('\n=== 示例1: 扩展搜索 - 精确匹配 ===');
|
|
18
|
+
const languages = [
|
|
19
|
+
{ name: 'JavaScript' },
|
|
20
|
+
{ name: 'TypeScript' },
|
|
21
|
+
{ name: 'Java' },
|
|
22
|
+
{ name: 'Python' },
|
|
23
|
+
{ name: 'C++' }
|
|
24
|
+
];
|
|
25
|
+
// 启用扩展搜索
|
|
26
|
+
const fuse = new fuse_js_1.default(languages, {
|
|
27
|
+
keys: ['name'],
|
|
28
|
+
useExtendedSearch: true
|
|
29
|
+
});
|
|
30
|
+
// 使用 = 前缀进行精确匹配
|
|
31
|
+
const exactResult = fuse.search("=Java");
|
|
32
|
+
console.log('精确搜索 "=Java" (只匹配 Java,不匹配 JavaScript):');
|
|
33
|
+
exactResult.forEach(item => {
|
|
34
|
+
console.log(` - ${item.item.name}`);
|
|
35
|
+
});
|
|
36
|
+
// 对比:模糊搜索
|
|
37
|
+
const fuzzyResult = fuse.search("Java");
|
|
38
|
+
console.log('\n模糊搜索 "Java" (会匹配 Java 和 JavaScript):');
|
|
39
|
+
fuzzyResult.forEach(item => {
|
|
40
|
+
console.log(` - ${item.item.name}`);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// #endregion
|
|
44
|
+
// #region 示例2: 扩展搜索 - 前缀和后缀匹配
|
|
45
|
+
function 前缀后缀匹配() {
|
|
46
|
+
console.log('\n=== 示例2: 扩展搜索 - 前缀和后缀匹配 ===');
|
|
47
|
+
const files = [
|
|
48
|
+
{ name: 'index.js' },
|
|
49
|
+
{ name: 'app.ts' },
|
|
50
|
+
{ name: 'config.json' },
|
|
51
|
+
{ name: 'test.js' },
|
|
52
|
+
{ name: 'main.ts' }
|
|
53
|
+
];
|
|
54
|
+
const fuse = new fuse_js_1.default(files, {
|
|
55
|
+
keys: ['name'],
|
|
56
|
+
useExtendedSearch: true
|
|
57
|
+
});
|
|
58
|
+
// 前缀匹配:以 ^ 开头
|
|
59
|
+
console.log('前缀搜索 "^index" (以 index 开头):');
|
|
60
|
+
const prefixResult = fuse.search("^index");
|
|
61
|
+
prefixResult.forEach(item => {
|
|
62
|
+
console.log(` - ${item.item.name}`);
|
|
63
|
+
});
|
|
64
|
+
// 后缀匹配:以 $ 结尾
|
|
65
|
+
console.log('\n后缀搜索 ".js$" (以 .js 结尾):');
|
|
66
|
+
const suffixResult = fuse.search(".js$");
|
|
67
|
+
suffixResult.forEach(item => {
|
|
68
|
+
console.log(` - ${item.item.name}`);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// #endregion
|
|
72
|
+
// #region 示例3: 扩展搜索 - 包含和排除
|
|
73
|
+
function 包含排除搜索() {
|
|
74
|
+
console.log('\n=== 示例3: 扩展搜索 - 包含和排除 ===');
|
|
75
|
+
const frameworks = [
|
|
76
|
+
{ name: 'React', type: 'frontend' },
|
|
77
|
+
{ name: 'Vue', type: 'frontend' },
|
|
78
|
+
{ name: 'Angular', type: 'frontend' },
|
|
79
|
+
{ name: 'Express', type: 'backend' },
|
|
80
|
+
{ name: 'Koa', type: 'backend' }
|
|
81
|
+
];
|
|
82
|
+
const fuse = new fuse_js_1.default(frameworks, {
|
|
83
|
+
keys: ['name', 'type'],
|
|
84
|
+
useExtendedSearch: true
|
|
85
|
+
});
|
|
86
|
+
// 包含匹配:使用单引号
|
|
87
|
+
console.log("包含搜索 \"'react\" (包含 react):");
|
|
88
|
+
const includeResult = fuse.search("'react");
|
|
89
|
+
includeResult.forEach(item => {
|
|
90
|
+
console.log(` - ${item.item.name} (${item.item.type})`);
|
|
91
|
+
});
|
|
92
|
+
// 排除匹配:使用 ! 前缀
|
|
93
|
+
console.log("\n排除搜索 \"!backend\" (排除 backend):");
|
|
94
|
+
const excludeResult = fuse.search("!backend");
|
|
95
|
+
excludeResult.forEach(item => {
|
|
96
|
+
console.log(` - ${item.item.name} (${item.item.type})`);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
// #endregion
|
|
100
|
+
// #region 示例4: 扩展搜索 - 逻辑运算符
|
|
101
|
+
function 逻辑运算符搜索() {
|
|
102
|
+
console.log('\n=== 示例4: 扩展搜索 - 逻辑运算符 ===');
|
|
103
|
+
const products = [
|
|
104
|
+
{ name: 'iPhone 15 Pro', brand: 'Apple', price: 999 },
|
|
105
|
+
{ name: 'MacBook Pro', brand: 'Apple', price: 1999 },
|
|
106
|
+
{ name: 'Galaxy S24', brand: 'Samsung', price: 899 },
|
|
107
|
+
{ name: 'iPad Air', brand: 'Apple', price: 599 },
|
|
108
|
+
{ name: 'Surface Pro', brand: 'Microsoft', price: 1299 }
|
|
109
|
+
];
|
|
110
|
+
const fuse = new fuse_js_1.default(products, {
|
|
111
|
+
keys: ['name', 'brand'],
|
|
112
|
+
useExtendedSearch: true
|
|
113
|
+
});
|
|
114
|
+
// AND 运算符:空格分隔
|
|
115
|
+
console.log('AND 搜索 "Apple Pro" (同时包含 Apple 和 Pro):');
|
|
116
|
+
const andResult = fuse.search("Apple Pro");
|
|
117
|
+
andResult.forEach(item => {
|
|
118
|
+
console.log(` - ${item.item.name} (${item.item.brand})`);
|
|
119
|
+
});
|
|
120
|
+
// OR 运算符:使用 |
|
|
121
|
+
console.log('\nOR 搜索 "iPhone | Galaxy" (包含 iPhone 或 Galaxy):');
|
|
122
|
+
const orResult = fuse.search("iPhone | Galaxy");
|
|
123
|
+
orResult.forEach(item => {
|
|
124
|
+
console.log(` - ${item.item.name} (${item.item.brand})`);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// #endregion
|
|
128
|
+
// #region 示例5: 权重搜索
|
|
129
|
+
function 权重搜索() {
|
|
130
|
+
console.log('\n=== 示例5: 权重搜索 ===');
|
|
131
|
+
const articles = [
|
|
132
|
+
{
|
|
133
|
+
title: 'TypeScript 入门教程',
|
|
134
|
+
content: '这是一篇关于 TypeScript 基础的文章',
|
|
135
|
+
tags: ['typescript', 'tutorial']
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
title: 'JavaScript 高级技巧',
|
|
139
|
+
content: '深入探讨 JavaScript 的高级特性,包括 TypeScript',
|
|
140
|
+
tags: ['javascript', 'advanced']
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
title: 'Node.js 实战',
|
|
144
|
+
content: '使用 TypeScript 开发 Node.js 应用',
|
|
145
|
+
tags: ['nodejs', 'typescript']
|
|
146
|
+
}
|
|
147
|
+
];
|
|
148
|
+
// 为不同字段设置权重
|
|
149
|
+
const fuse = new fuse_js_1.default(articles, {
|
|
150
|
+
keys: [
|
|
151
|
+
{ name: 'title', weight: 0.7 }, // 标题权重最高
|
|
152
|
+
{ name: 'tags', weight: 0.2 }, // 标签次之
|
|
153
|
+
{ name: 'content', weight: 0.1 } // 内容权重最低
|
|
154
|
+
],
|
|
155
|
+
includeScore: true
|
|
156
|
+
});
|
|
157
|
+
const result = fuse.search('TypeScript');
|
|
158
|
+
console.log('搜索 "TypeScript" (标题权重 0.7, 标签 0.2, 内容 0.1):');
|
|
159
|
+
result.forEach(item => {
|
|
160
|
+
console.log(` - ${item.item.title}`);
|
|
161
|
+
console.log(` 分数: ${item.score?.toFixed(4)}`);
|
|
162
|
+
console.log(` 标签: ${item.item.tags.join(', ')}`);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
// #endregion
|
|
166
|
+
// #region 示例6: 复杂查询组合
|
|
167
|
+
function 复杂查询组合() {
|
|
168
|
+
console.log('\n=== 示例6: 复杂查询组合 ===');
|
|
169
|
+
const employees = [
|
|
170
|
+
{ name: 'John Smith', department: 'Engineering', level: 'Senior' },
|
|
171
|
+
{ name: 'Jane Doe', department: 'Engineering', level: 'Junior' },
|
|
172
|
+
{ name: 'Bob Johnson', department: 'Marketing', level: 'Senior' },
|
|
173
|
+
{ name: 'Alice Williams', department: 'Sales', level: 'Mid' },
|
|
174
|
+
{ name: 'Charlie Brown', department: 'Engineering', level: 'Mid' }
|
|
175
|
+
];
|
|
176
|
+
const fuse = new fuse_js_1.default(employees, {
|
|
177
|
+
keys: ['name', 'department', 'level'],
|
|
178
|
+
useExtendedSearch: true
|
|
179
|
+
});
|
|
180
|
+
// 复杂查询:工程部门的高级员工
|
|
181
|
+
console.log('复杂查询 "Engineering Senior" (工程部门且高级):');
|
|
182
|
+
const result = fuse.search("Engineering Senior");
|
|
183
|
+
result.forEach(item => {
|
|
184
|
+
console.log(` - ${item.item.name}`);
|
|
185
|
+
console.log(` 部门: ${item.item.department}, 级别: ${item.item.level}`);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
// #endregion
|
|
189
|
+
// #region 主程序入口
|
|
190
|
+
if (require.main === module) {
|
|
191
|
+
const args = process.argv.slice(2);
|
|
192
|
+
const exampleNumber = args[0] ? parseInt(args[0]) : 0;
|
|
193
|
+
const examples = [
|
|
194
|
+
扩展搜索精确匹配,
|
|
195
|
+
前缀后缀匹配,
|
|
196
|
+
包含排除搜索,
|
|
197
|
+
逻辑运算符搜索,
|
|
198
|
+
权重搜索,
|
|
199
|
+
复杂查询组合
|
|
200
|
+
];
|
|
201
|
+
if (exampleNumber > 0 && exampleNumber <= examples.length) {
|
|
202
|
+
console.log(`\n运行示例 ${exampleNumber}:`);
|
|
203
|
+
examples[exampleNumber - 1]();
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
console.log('Fuse.js 高级搜索示例\n');
|
|
207
|
+
console.log('运行所有示例...\n');
|
|
208
|
+
examples.forEach(example => example());
|
|
209
|
+
console.log('\n\n提示: 使用 ts-node src/demo/fuse/02-高级搜索.ts [1-6] 运行指定示例');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// #endregion
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Fuse.js 配置详解
|
|
4
|
+
* ================
|
|
5
|
+
* 深入了解 Fuse.js 的各种配置选项及其影响
|
|
6
|
+
*
|
|
7
|
+
* 官方文档: https://www.fusejs.io/api/options.html
|
|
8
|
+
* 适用版本: fuse.js 7.x
|
|
9
|
+
*/
|
|
10
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
11
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
15
|
+
// #region 示例1: threshold 阈值配置
|
|
16
|
+
function 阈值配置对比() {
|
|
17
|
+
console.log('\n=== 示例1: threshold 阈值配置 ===');
|
|
18
|
+
const words = ['apple', 'application', 'apply', 'appreciate'];
|
|
19
|
+
// 严格匹配 (threshold = 0.0)
|
|
20
|
+
const strictFuse = new fuse_js_1.default(words, { threshold: 0.0 });
|
|
21
|
+
console.log('严格匹配 (threshold: 0.0) 搜索 "apple":');
|
|
22
|
+
strictFuse.search('apple').forEach(item => {
|
|
23
|
+
console.log(` - ${item.item}`);
|
|
24
|
+
});
|
|
25
|
+
// 中等匹配 (threshold = 0.3)
|
|
26
|
+
const mediumFuse = new fuse_js_1.default(words, { threshold: 0.3, includeScore: true });
|
|
27
|
+
console.log('\n中等匹配 (threshold: 0.3) 搜索 "aple":');
|
|
28
|
+
mediumFuse.search('aple').forEach(item => {
|
|
29
|
+
console.log(` - ${item.item} (分数: ${item.score?.toFixed(3)})`);
|
|
30
|
+
});
|
|
31
|
+
// 宽松匹配 (threshold = 0.6)
|
|
32
|
+
const looseFuse = new fuse_js_1.default(words, { threshold: 0.6, includeScore: true });
|
|
33
|
+
console.log('\n宽松匹配 (threshold: 0.6) 搜索 "apl":');
|
|
34
|
+
looseFuse.search('apl').forEach(item => {
|
|
35
|
+
console.log(` - ${item.item} (分数: ${item.score?.toFixed(3)})`);
|
|
36
|
+
});
|
|
37
|
+
console.log('\n说明: threshold 越小,匹配越严格;越大,匹配越宽松');
|
|
38
|
+
}
|
|
39
|
+
// #endregion
|
|
40
|
+
// #region 示例2: location 和 distance 配置
|
|
41
|
+
function 位置距离配置() {
|
|
42
|
+
console.log('\n=== 示例2: location 和 distance 配置 ===');
|
|
43
|
+
const texts = [
|
|
44
|
+
'The quick brown fox jumps over the lazy dog',
|
|
45
|
+
'A quick brown fox',
|
|
46
|
+
'The lazy dog sleeps'
|
|
47
|
+
];
|
|
48
|
+
// 默认配置:在整个字符串中搜索
|
|
49
|
+
const defaultFuse = new fuse_js_1.default(texts, {
|
|
50
|
+
includeScore: true,
|
|
51
|
+
threshold: 0.3
|
|
52
|
+
});
|
|
53
|
+
console.log('默认配置搜索 "quick":');
|
|
54
|
+
defaultFuse.search('quick').forEach(item => {
|
|
55
|
+
console.log(` - "${item.item.substring(0, 30)}..." (分数: ${item.score?.toFixed(3)})`);
|
|
56
|
+
});
|
|
57
|
+
// 限制搜索位置和距离
|
|
58
|
+
const restrictedFuse = new fuse_js_1.default(texts, {
|
|
59
|
+
includeScore: true,
|
|
60
|
+
threshold: 0.3,
|
|
61
|
+
location: 0, // 从字符串开头开始
|
|
62
|
+
distance: 10 // 只在前 10 个字符内搜索
|
|
63
|
+
});
|
|
64
|
+
console.log('\n限制位置配置 (location: 0, distance: 10) 搜索 "quick":');
|
|
65
|
+
const result = restrictedFuse.search('quick');
|
|
66
|
+
if (result.length > 0) {
|
|
67
|
+
result.forEach(item => {
|
|
68
|
+
console.log(` - "${item.item.substring(0, 30)}..." (分数: ${item.score?.toFixed(3)})`);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
console.log(' - 无结果(因为 "quick" 不在前 10 个字符内)');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// #endregion
|
|
76
|
+
// #region 示例3: minMatchCharLength 最小匹配长度
|
|
77
|
+
function 最小匹配长度() {
|
|
78
|
+
console.log('\n=== 示例3: minMatchCharLength 最小匹配长度 ===');
|
|
79
|
+
const codes = [
|
|
80
|
+
'JavaScript',
|
|
81
|
+
'Java',
|
|
82
|
+
'TypeScript',
|
|
83
|
+
'Python',
|
|
84
|
+
'Go'
|
|
85
|
+
];
|
|
86
|
+
// 不限制最小匹配长度
|
|
87
|
+
const noMinFuse = new fuse_js_1.default(codes, {
|
|
88
|
+
minMatchCharLength: 1
|
|
89
|
+
});
|
|
90
|
+
console.log('最小匹配长度 1,搜索 "ja":');
|
|
91
|
+
noMinFuse.search('ja').forEach(item => {
|
|
92
|
+
console.log(` - ${item.item}`);
|
|
93
|
+
});
|
|
94
|
+
// 要求至少匹配 3 个字符
|
|
95
|
+
const minFuse = new fuse_js_1.default(codes, {
|
|
96
|
+
minMatchCharLength: 3
|
|
97
|
+
});
|
|
98
|
+
console.log('\n最小匹配长度 3,搜索 "ja":');
|
|
99
|
+
const result = minFuse.search('ja');
|
|
100
|
+
if (result.length > 0) {
|
|
101
|
+
result.forEach(item => {
|
|
102
|
+
console.log(` - ${item.item}`);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
console.log(' - 无结果(搜索词少于 3 个字符)');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// #endregion
|
|
110
|
+
// #region 示例4: shouldSort 和 sortFn 排序配置
|
|
111
|
+
function 排序配置() {
|
|
112
|
+
console.log('\n=== 示例4: shouldSort 和排序配置 ===');
|
|
113
|
+
const items = [
|
|
114
|
+
{ name: 'React', stars: 200000 },
|
|
115
|
+
{ name: 'Vue', stars: 180000 },
|
|
116
|
+
{ name: 'Angular', stars: 85000 },
|
|
117
|
+
{ name: 'Svelte', stars: 65000 },
|
|
118
|
+
{ name: 'Preact', stars: 35000 }
|
|
119
|
+
];
|
|
120
|
+
// 默认按匹配分数排序
|
|
121
|
+
const scoreSortFuse = new fuse_js_1.default(items, {
|
|
122
|
+
keys: ['name'],
|
|
123
|
+
includeScore: true,
|
|
124
|
+
shouldSort: true
|
|
125
|
+
});
|
|
126
|
+
console.log('按匹配分数排序,搜索 "react":');
|
|
127
|
+
scoreSortFuse.search('react').forEach(item => {
|
|
128
|
+
console.log(` - ${item.item.name} (分数: ${item.score?.toFixed(3)}, stars: ${item.item.stars})`);
|
|
129
|
+
});
|
|
130
|
+
// 禁用排序
|
|
131
|
+
const noSortFuse = new fuse_js_1.default(items, {
|
|
132
|
+
keys: ['name'],
|
|
133
|
+
includeScore: true,
|
|
134
|
+
shouldSort: false
|
|
135
|
+
});
|
|
136
|
+
console.log('\n禁用排序,搜索 "react":');
|
|
137
|
+
noSortFuse.search('react').forEach(item => {
|
|
138
|
+
console.log(` - ${item.item.name} (分数: ${item.score?.toFixed(3)}, stars: ${item.item.stars})`);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
// #endregion
|
|
142
|
+
// #region 示例5: includeMatches 匹配详情
|
|
143
|
+
function 匹配详情配置() {
|
|
144
|
+
console.log('\n=== 示例5: includeMatches 匹配详情 ===');
|
|
145
|
+
const books = [
|
|
146
|
+
{
|
|
147
|
+
title: 'JavaScript 权威指南',
|
|
148
|
+
author: 'David Flanagan'
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
title: 'TypeScript 编程',
|
|
152
|
+
author: 'Boris Cherny'
|
|
153
|
+
}
|
|
154
|
+
];
|
|
155
|
+
const fuse = new fuse_js_1.default(books, {
|
|
156
|
+
keys: ['title', 'author'],
|
|
157
|
+
includeMatches: true, // 包含匹配详情
|
|
158
|
+
includeScore: true
|
|
159
|
+
});
|
|
160
|
+
const result = fuse.search('script');
|
|
161
|
+
console.log('搜索 "script" 的匹配详情:');
|
|
162
|
+
result.forEach(item => {
|
|
163
|
+
console.log(`\n 书名: ${item.item.title}`);
|
|
164
|
+
console.log(` 作者: ${item.item.author}`);
|
|
165
|
+
console.log(` 分数: ${item.score?.toFixed(3)}`);
|
|
166
|
+
if (item.matches) {
|
|
167
|
+
console.log(' 匹配位置:');
|
|
168
|
+
item.matches.forEach(match => {
|
|
169
|
+
console.log(` - 字段: ${match.key}`);
|
|
170
|
+
console.log(` 值: ${match.value}`);
|
|
171
|
+
if (match.indices) {
|
|
172
|
+
console.log(` 索引: ${JSON.stringify(match.indices)}`);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// #endregion
|
|
179
|
+
// #region 示例6: 完整配置示例
|
|
180
|
+
function 完整配置示例() {
|
|
181
|
+
console.log('\n=== 示例6: 完整配置示例 ===');
|
|
182
|
+
const products = [
|
|
183
|
+
{
|
|
184
|
+
id: 1,
|
|
185
|
+
name: 'iPhone 15 Pro Max',
|
|
186
|
+
category: 'smartphone',
|
|
187
|
+
brand: 'Apple',
|
|
188
|
+
price: 1199
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
id: 2,
|
|
192
|
+
name: 'MacBook Pro 16',
|
|
193
|
+
category: 'laptop',
|
|
194
|
+
brand: 'Apple',
|
|
195
|
+
price: 2499
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
id: 3,
|
|
199
|
+
name: 'iPad Pro',
|
|
200
|
+
category: 'tablet',
|
|
201
|
+
brand: 'Apple',
|
|
202
|
+
price: 799
|
|
203
|
+
}
|
|
204
|
+
];
|
|
205
|
+
const options = {
|
|
206
|
+
// 基础配置
|
|
207
|
+
isCaseSensitive: false, // 不区分大小写
|
|
208
|
+
includeScore: true, // 包含分数
|
|
209
|
+
includeMatches: false, // 不包含匹配详情
|
|
210
|
+
// 匹配配置
|
|
211
|
+
minMatchCharLength: 2, // 最小匹配 2 个字符
|
|
212
|
+
shouldSort: true, // 按分数排序
|
|
213
|
+
// 模糊匹配配置
|
|
214
|
+
threshold: 0.4, // 中等严格度
|
|
215
|
+
location: 0, // 从开头开始
|
|
216
|
+
distance: 100, // 搜索距离
|
|
217
|
+
// 搜索字段配置
|
|
218
|
+
keys: [
|
|
219
|
+
{ name: 'name', weight: 0.5 },
|
|
220
|
+
{ name: 'category', weight: 0.3 },
|
|
221
|
+
{ name: 'brand', weight: 0.2 }
|
|
222
|
+
]
|
|
223
|
+
};
|
|
224
|
+
const fuse = new fuse_js_1.default(products, options);
|
|
225
|
+
console.log('配置说明:');
|
|
226
|
+
console.log(' - 不区分大小写');
|
|
227
|
+
console.log(' - 中等匹配严格度 (threshold: 0.4)');
|
|
228
|
+
console.log(' - 权重: name(50%), category(30%), brand(20%)');
|
|
229
|
+
const result = fuse.search('pro');
|
|
230
|
+
console.log('\n搜索 "pro" 的结果:');
|
|
231
|
+
result.forEach(item => {
|
|
232
|
+
console.log(` - ${item.item.name}`);
|
|
233
|
+
console.log(` 类别: ${item.item.category}, 品牌: ${item.item.brand}`);
|
|
234
|
+
console.log(` 价格: $${item.item.price}, 分数: ${item.score?.toFixed(3)}`);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
// #endregion
|
|
238
|
+
// #region 主程序入口
|
|
239
|
+
if (require.main === module) {
|
|
240
|
+
const args = process.argv.slice(2);
|
|
241
|
+
const exampleNumber = args[0] ? parseInt(args[0]) : 0;
|
|
242
|
+
const examples = [
|
|
243
|
+
阈值配置对比,
|
|
244
|
+
位置距离配置,
|
|
245
|
+
最小匹配长度,
|
|
246
|
+
排序配置,
|
|
247
|
+
匹配详情配置,
|
|
248
|
+
完整配置示例
|
|
249
|
+
];
|
|
250
|
+
if (exampleNumber > 0 && exampleNumber <= examples.length) {
|
|
251
|
+
console.log(`\n运行示例 ${exampleNumber}:`);
|
|
252
|
+
examples[exampleNumber - 1]();
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
console.log('Fuse.js 配置详解示例\n');
|
|
256
|
+
console.log('运行所有示例...\n');
|
|
257
|
+
examples.forEach(example => example());
|
|
258
|
+
console.log('\n\n提示: 使用 ts-node src/demo/fuse/03-配置详解.ts [1-6] 运行指定示例');
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// #endregion
|