@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.
@@ -0,0 +1,354 @@
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 users = [
19
+ {
20
+ id: 1,
21
+ username: 'john_doe',
22
+ email: 'john@example.com',
23
+ fullName: 'John Doe',
24
+ bio: 'Full-stack developer specializing in React and Node.js',
25
+ tags: ['javascript', 'react', 'nodejs']
26
+ },
27
+ {
28
+ id: 2,
29
+ username: 'jane_smith',
30
+ email: 'jane@example.com',
31
+ fullName: 'Jane Smith',
32
+ bio: 'Frontend developer passionate about TypeScript and Vue',
33
+ tags: ['typescript', 'vue', 'css']
34
+ },
35
+ {
36
+ id: 3,
37
+ username: 'bob_wilson',
38
+ email: 'bob@example.com',
39
+ fullName: 'Bob Wilson',
40
+ bio: 'Backend engineer working with Python and Django',
41
+ tags: ['python', 'django', 'postgresql']
42
+ }
43
+ ];
44
+ const fuse = new fuse_js_1.default(users, {
45
+ keys: [
46
+ { name: 'username', weight: 0.3 },
47
+ { name: 'fullName', weight: 0.3 },
48
+ { name: 'email', weight: 0.1 },
49
+ { name: 'bio', weight: 0.2 },
50
+ { name: 'tags', weight: 0.1 }
51
+ ],
52
+ threshold: 0.4,
53
+ includeScore: true
54
+ });
55
+ const searchQuery = 'typescript';
56
+ console.log(`搜索用户: "${searchQuery}"\n`);
57
+ const results = fuse.search(searchQuery);
58
+ if (results.length === 0) {
59
+ console.log('未找到匹配的用户');
60
+ }
61
+ else {
62
+ results.forEach((result, index) => {
63
+ const user = result.item;
64
+ console.log(`${index + 1}. ${user.fullName} (@${user.username})`);
65
+ console.log(` 邮箱: ${user.email}`);
66
+ console.log(` 简介: ${user.bio}`);
67
+ console.log(` 标签: ${user.tags.join(', ')}`);
68
+ console.log(` 匹配度: ${((1 - (result.score || 0)) * 100).toFixed(1)}%\n`);
69
+ });
70
+ }
71
+ }
72
+ // #endregion
73
+ // #region 示例2: 商品搜索与过滤
74
+ function 商品搜索与过滤() {
75
+ console.log('\n=== 示例2: 商品搜索与过滤 ===');
76
+ const products = [
77
+ {
78
+ id: 1,
79
+ name: 'iPhone 15 Pro',
80
+ category: 'smartphone',
81
+ brand: 'Apple',
82
+ price: 999,
83
+ description: '最新款 iPhone,配备 A17 Pro 芯片',
84
+ inStock: true
85
+ },
86
+ {
87
+ id: 2,
88
+ name: 'Samsung Galaxy S24',
89
+ category: 'smartphone',
90
+ brand: 'Samsung',
91
+ price: 899,
92
+ description: '旗舰安卓手机,强大的拍照功能',
93
+ inStock: true
94
+ },
95
+ {
96
+ id: 3,
97
+ name: 'MacBook Pro 16',
98
+ category: 'laptop',
99
+ brand: 'Apple',
100
+ price: 2499,
101
+ description: '专业级笔记本电脑,M3 Max 芯片',
102
+ inStock: false
103
+ },
104
+ {
105
+ id: 4,
106
+ name: 'iPad Pro 12.9',
107
+ category: 'tablet',
108
+ brand: 'Apple',
109
+ price: 1099,
110
+ description: '大屏平板电脑,支持 Apple Pencil',
111
+ inStock: true
112
+ }
113
+ ];
114
+ // 创建搜索实例
115
+ const fuse = new fuse_js_1.default(products, {
116
+ keys: [
117
+ { name: 'name', weight: 0.4 },
118
+ { name: 'brand', weight: 0.2 },
119
+ { name: 'category', weight: 0.2 },
120
+ { name: 'description', weight: 0.2 }
121
+ ],
122
+ threshold: 0.3,
123
+ includeScore: true
124
+ });
125
+ // 搜索功能
126
+ function searchProducts(query, filters) {
127
+ let results = fuse.search(query);
128
+ // 应用过滤器
129
+ if (filters) {
130
+ results = results.filter(result => {
131
+ const product = result.item;
132
+ if (filters.inStock !== undefined && product.inStock !== filters.inStock) {
133
+ return false;
134
+ }
135
+ if (filters.maxPrice !== undefined && product.price > filters.maxPrice) {
136
+ return false;
137
+ }
138
+ return true;
139
+ });
140
+ }
141
+ return results;
142
+ }
143
+ // 示例搜索
144
+ console.log('搜索 "pro" 且价格 <= 1500 的商品:\n');
145
+ const results = searchProducts('pro', { maxPrice: 1500 });
146
+ results.forEach((result, index) => {
147
+ const product = result.item;
148
+ console.log(`${index + 1}. ${product.name} - $${product.price}`);
149
+ console.log(` 品牌: ${product.brand} | 类别: ${product.category}`);
150
+ console.log(` 库存: ${product.inStock ? '有货' : '缺货'}`);
151
+ console.log(` ${product.description}\n`);
152
+ });
153
+ }
154
+ // #endregion
155
+ // #region 示例3: 文档搜索引擎
156
+ function 文档搜索引擎() {
157
+ console.log('\n=== 示例3: 文档搜索引擎 ===');
158
+ const documents = [
159
+ {
160
+ id: 'doc1',
161
+ title: 'TypeScript 入门指南',
162
+ content: 'TypeScript 是 JavaScript 的超集,添加了静态类型系统。它可以帮助开发者在编译时发现错误。',
163
+ category: '教程',
164
+ tags: ['typescript', 'javascript', '入门'],
165
+ lastUpdated: '2024-01-15'
166
+ },
167
+ {
168
+ id: 'doc2',
169
+ title: 'React Hooks 最佳实践',
170
+ content: 'React Hooks 是 React 16.8 引入的新特性,让你在不编写 class 的情况下使用 state 和其他 React 特性。',
171
+ category: '最佳实践',
172
+ tags: ['react', 'hooks', 'javascript'],
173
+ lastUpdated: '2024-02-20'
174
+ },
175
+ {
176
+ id: 'doc3',
177
+ title: 'Node.js 性能优化',
178
+ content: '本文介绍 Node.js 应用的性能优化技巧,包括异步编程、缓存策略和数据库优化。',
179
+ category: '性能优化',
180
+ tags: ['nodejs', 'performance', 'optimization'],
181
+ lastUpdated: '2024-03-10'
182
+ }
183
+ ];
184
+ const fuse = new fuse_js_1.default(documents, {
185
+ keys: [
186
+ { name: 'title', weight: 0.5 },
187
+ { name: 'content', weight: 0.3 },
188
+ { name: 'tags', weight: 0.2 }
189
+ ],
190
+ threshold: 0.4,
191
+ includeScore: true,
192
+ includeMatches: true,
193
+ minMatchCharLength: 2
194
+ });
195
+ function searchDocuments(query) {
196
+ const results = fuse.search(query);
197
+ console.log(`搜索 "${query}" 找到 ${results.length} 个结果:\n`);
198
+ results.forEach((result, index) => {
199
+ const doc = result.item;
200
+ console.log(`${index + 1}. ${doc.title}`);
201
+ console.log(` 类别: ${doc.category} | 更新: ${doc.lastUpdated}`);
202
+ console.log(` 标签: ${doc.tags.join(', ')}`);
203
+ console.log(` 相关度: ${((1 - (result.score || 0)) * 100).toFixed(1)}%`);
204
+ // 显示内容摘要
205
+ const preview = doc.content.substring(0, 60) + '...';
206
+ console.log(` ${preview}\n`);
207
+ });
208
+ }
209
+ searchDocuments('react');
210
+ }
211
+ // #endregion
212
+ // #region 示例4: 命令行工具搜索
213
+ function 命令行工具搜索() {
214
+ console.log('\n=== 示例4: 命令行工具搜索 ===');
215
+ const commands = [
216
+ {
217
+ name: 'git commit',
218
+ description: '记录更改到仓库',
219
+ usage: 'git commit -m "message"',
220
+ aliases: ['commit', 'ci'],
221
+ category: 'git'
222
+ },
223
+ {
224
+ name: 'git push',
225
+ description: '推送本地更改到远程仓库',
226
+ usage: 'git push origin main',
227
+ aliases: ['push'],
228
+ category: 'git'
229
+ },
230
+ {
231
+ name: 'npm install',
232
+ description: '安装项目依赖',
233
+ usage: 'npm install [package]',
234
+ aliases: ['npm i', 'install'],
235
+ category: 'npm'
236
+ },
237
+ {
238
+ name: 'npm run',
239
+ description: '运行 package.json 中定义的脚本',
240
+ usage: 'npm run [script]',
241
+ aliases: ['run'],
242
+ category: 'npm'
243
+ }
244
+ ];
245
+ const fuse = new fuse_js_1.default(commands, {
246
+ keys: [
247
+ { name: 'name', weight: 0.4 },
248
+ { name: 'description', weight: 0.3 },
249
+ { name: 'aliases', weight: 0.3 }
250
+ ],
251
+ threshold: 0.3,
252
+ useExtendedSearch: true
253
+ });
254
+ function findCommand(query) {
255
+ const results = fuse.search(query);
256
+ if (results.length === 0) {
257
+ console.log(`未找到命令: "${query}"`);
258
+ return;
259
+ }
260
+ console.log(`搜索 "${query}" 的命令:\n`);
261
+ results.forEach((result, index) => {
262
+ const cmd = result.item;
263
+ console.log(`${index + 1}. ${cmd.name}`);
264
+ console.log(` ${cmd.description}`);
265
+ console.log(` 用法: ${cmd.usage}`);
266
+ if (cmd.aliases.length > 0) {
267
+ console.log(` 别名: ${cmd.aliases.join(', ')}`);
268
+ }
269
+ console.log();
270
+ });
271
+ }
272
+ findCommand('install');
273
+ }
274
+ // #endregion
275
+ // #region 示例5: 智能联系人搜索
276
+ function 智能联系人搜索() {
277
+ console.log('\n=== 示例5: 智能联系人搜索 ===');
278
+ const contacts = [
279
+ {
280
+ id: 1,
281
+ name: '张伟',
282
+ phone: '13800138000',
283
+ email: 'zhangwei@company.com',
284
+ company: '阿里巴巴',
285
+ notes: '技术总监,负责云计算业务'
286
+ },
287
+ {
288
+ id: 2,
289
+ name: '李娜',
290
+ phone: '13900139000',
291
+ email: 'lina@tech.com',
292
+ company: '腾讯',
293
+ notes: '产品经理,微信团队'
294
+ },
295
+ {
296
+ id: 3,
297
+ name: '王强',
298
+ phone: '13700137000',
299
+ email: 'wangqiang@startup.com',
300
+ company: '字节跳动',
301
+ notes: '前端工程师,抖音部门'
302
+ }
303
+ ];
304
+ const fuse = new fuse_js_1.default(contacts, {
305
+ keys: [
306
+ { name: 'name', weight: 0.4 },
307
+ { name: 'company', weight: 0.3 },
308
+ { name: 'notes', weight: 0.2 },
309
+ { name: 'email', weight: 0.1 }
310
+ ],
311
+ threshold: 0.4,
312
+ includeScore: true
313
+ });
314
+ function searchContacts(query) {
315
+ const results = fuse.search(query);
316
+ console.log(`搜索联系人: "${query}"\n`);
317
+ if (results.length === 0) {
318
+ console.log('未找到匹配的联系人');
319
+ return;
320
+ }
321
+ results.forEach((result, index) => {
322
+ const contact = result.item;
323
+ console.log(`${index + 1}. ${contact.name} - ${contact.company}`);
324
+ console.log(` 电话: ${contact.phone}`);
325
+ console.log(` 邮箱: ${contact.email}`);
326
+ console.log(` 备注: ${contact.notes}\n`);
327
+ });
328
+ }
329
+ searchContacts('腾讯');
330
+ }
331
+ // #endregion
332
+ // #region 主程序入口
333
+ if (require.main === module) {
334
+ const args = process.argv.slice(2);
335
+ const exampleNumber = args[0] ? parseInt(args[0]) : 0;
336
+ const examples = [
337
+ 用户搜索系统,
338
+ 商品搜索与过滤,
339
+ 文档搜索引擎,
340
+ 命令行工具搜索,
341
+ 智能联系人搜索
342
+ ];
343
+ if (exampleNumber > 0 && exampleNumber <= examples.length) {
344
+ console.log(`\n运行示例 ${exampleNumber}:`);
345
+ examples[exampleNumber - 1]();
346
+ }
347
+ else {
348
+ console.log('Fuse.js 实战案例示例\n');
349
+ console.log('运行所有示例...\n');
350
+ examples.forEach(example => example());
351
+ console.log('\n\n提示: 使用 ts-node src/demo/fuse/04-实战案例.ts [1-5] 运行指定示例');
352
+ }
353
+ }
354
+ // #endregion
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.1.4",
6
+ "version": "1.1.7",
7
7
  "description": "一个nodejs学习工具",
8
8
  "bin": {
9
9
  "nlearn": "dist/src/cli/index.js",