@lppx/nlearn 1.1.3 → 1.1.5
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 +43 -0
- package/dist/src/cli/index.js +33 -0
- 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
package/dist/src/demo/class/04-/346/216/245/345/217/243/344/270/216/345/256/236/347/216/260.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TypeScript 类与接口
|
|
4
|
+
* ====================
|
|
5
|
+
* 介绍接口定义、类实现接口、接口继承
|
|
6
|
+
* 适用于 TypeScript 3.0+
|
|
7
|
+
*/
|
|
8
|
+
class Document {
|
|
9
|
+
constructor(content) {
|
|
10
|
+
this.content = content;
|
|
11
|
+
}
|
|
12
|
+
print() {
|
|
13
|
+
console.log('打印文档:');
|
|
14
|
+
console.log(this.content);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class Image {
|
|
18
|
+
constructor(url, width, height) {
|
|
19
|
+
this.url = url;
|
|
20
|
+
this.width = width;
|
|
21
|
+
this.height = height;
|
|
22
|
+
}
|
|
23
|
+
print() {
|
|
24
|
+
console.log(`打印图片: ${this.url}`);
|
|
25
|
+
console.log(`尺寸: ${this.width}x${this.height}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function singleInterfaceDemo() {
|
|
29
|
+
const doc = new Document('这是一份重要文档');
|
|
30
|
+
doc.print();
|
|
31
|
+
console.log();
|
|
32
|
+
const img = new Image('photo.jpg', 1920, 1080);
|
|
33
|
+
img.print();
|
|
34
|
+
}
|
|
35
|
+
class Duck {
|
|
36
|
+
constructor(name) {
|
|
37
|
+
this.name = name;
|
|
38
|
+
}
|
|
39
|
+
fly() {
|
|
40
|
+
console.log(`${this.name} 在飞翔`);
|
|
41
|
+
}
|
|
42
|
+
swim() {
|
|
43
|
+
console.log(`${this.name} 在游泳`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
class Penguin {
|
|
47
|
+
constructor(name) {
|
|
48
|
+
this.name = name;
|
|
49
|
+
}
|
|
50
|
+
swim() {
|
|
51
|
+
console.log(`${this.name} 在游泳`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function multipleInterfacesDemo() {
|
|
55
|
+
const duck = new Duck('唐老鸭');
|
|
56
|
+
duck.fly();
|
|
57
|
+
duck.swim();
|
|
58
|
+
console.log();
|
|
59
|
+
const penguin = new Penguin('企鹅');
|
|
60
|
+
penguin.swim();
|
|
61
|
+
}
|
|
62
|
+
class Student {
|
|
63
|
+
constructor(name, age, email, studentId) {
|
|
64
|
+
this.name = name;
|
|
65
|
+
this.age = age;
|
|
66
|
+
this.email = email;
|
|
67
|
+
this.studentId = studentId;
|
|
68
|
+
}
|
|
69
|
+
greet() {
|
|
70
|
+
console.log(`你好,我是学生 ${this.name}`);
|
|
71
|
+
}
|
|
72
|
+
displayInfo() {
|
|
73
|
+
console.log(`学生: ${this.name}`);
|
|
74
|
+
console.log(`年龄: ${this.age}`);
|
|
75
|
+
console.log(`邮箱: ${this.email}`);
|
|
76
|
+
console.log(`学号: ${this.studentId}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function interfaceInheritanceDemo() {
|
|
80
|
+
const student = new Student('张三', 20, 'zhangsan@example.com', 'S2024001');
|
|
81
|
+
student.displayInfo();
|
|
82
|
+
}
|
|
83
|
+
class BasicCalculator {
|
|
84
|
+
add(a, b) {
|
|
85
|
+
return a + b;
|
|
86
|
+
}
|
|
87
|
+
subtract(a, b) {
|
|
88
|
+
return a - b;
|
|
89
|
+
}
|
|
90
|
+
multiply(a, b) {
|
|
91
|
+
return a * b;
|
|
92
|
+
}
|
|
93
|
+
divide(a, b) {
|
|
94
|
+
if (b === 0) {
|
|
95
|
+
throw new Error('除数不能为零');
|
|
96
|
+
}
|
|
97
|
+
return a / b;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function methodSignatureDemo() {
|
|
101
|
+
const calc = new BasicCalculator();
|
|
102
|
+
console.log('10 + 5 =', calc.add(10, 5));
|
|
103
|
+
console.log('10 - 5 =', calc.subtract(10, 5));
|
|
104
|
+
console.log('10 × 5 =', calc.multiply(10, 5));
|
|
105
|
+
console.log('10 ÷ 5 =', calc.divide(10, 5));
|
|
106
|
+
}
|
|
107
|
+
class Version {
|
|
108
|
+
constructor(major, minor, patch) {
|
|
109
|
+
this.major = major;
|
|
110
|
+
this.minor = minor;
|
|
111
|
+
this.patch = patch;
|
|
112
|
+
}
|
|
113
|
+
compareTo(other) {
|
|
114
|
+
if (this.major !== other.major) {
|
|
115
|
+
return this.major - other.major;
|
|
116
|
+
}
|
|
117
|
+
if (this.minor !== other.minor) {
|
|
118
|
+
return this.minor - other.minor;
|
|
119
|
+
}
|
|
120
|
+
return this.patch - other.patch;
|
|
121
|
+
}
|
|
122
|
+
toString() {
|
|
123
|
+
return `${this.major}.${this.minor}.${this.patch}`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function sortVersions(versions) {
|
|
127
|
+
return versions.sort((a, b) => a.compareTo(b));
|
|
128
|
+
}
|
|
129
|
+
function genericInterfaceDemo() {
|
|
130
|
+
const versions = [
|
|
131
|
+
new Version(2, 1, 0),
|
|
132
|
+
new Version(1, 5, 3),
|
|
133
|
+
new Version(2, 0, 1),
|
|
134
|
+
new Version(1, 5, 10)
|
|
135
|
+
];
|
|
136
|
+
console.log('排序前:');
|
|
137
|
+
versions.forEach(v => console.log(v.toString()));
|
|
138
|
+
const sorted = sortVersions(versions);
|
|
139
|
+
console.log('\n排序后:');
|
|
140
|
+
sorted.forEach(v => console.log(v.toString()));
|
|
141
|
+
}
|
|
142
|
+
// #endregion
|
|
143
|
+
if (require.main === module) {
|
|
144
|
+
const demos = [
|
|
145
|
+
singleInterfaceDemo,
|
|
146
|
+
multipleInterfacesDemo,
|
|
147
|
+
interfaceInheritanceDemo,
|
|
148
|
+
methodSignatureDemo,
|
|
149
|
+
genericInterfaceDemo
|
|
150
|
+
];
|
|
151
|
+
const index = parseInt(process.argv[2]) || 0;
|
|
152
|
+
if (index > 0 && index <= demos.length) {
|
|
153
|
+
console.log(`\n=== 运行示例 ${index} ===\n`);
|
|
154
|
+
demos[index - 1]();
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
console.log('=== 运行所有示例 ===\n');
|
|
158
|
+
demos.forEach((demo, i) => {
|
|
159
|
+
console.log(`\n--- 示例 ${i + 1}: ${demo.name} ---\n`);
|
|
160
|
+
demo();
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Fuse.js 基础概念
|
|
4
|
+
* ================
|
|
5
|
+
* Fuse.js 是一个轻量级的模糊搜索库,零依赖
|
|
6
|
+
* 适用于客户端对小到中等规模数据集进行模糊搜索
|
|
7
|
+
*
|
|
8
|
+
* 官方文档: https://www.fusejs.io/
|
|
9
|
+
* 适用版本: fuse.js 7.x
|
|
10
|
+
*/
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
16
|
+
// #region 示例1: 简单字符串数组搜索
|
|
17
|
+
function 搜索字符串数组() {
|
|
18
|
+
console.log('\n=== 示例1: 简单字符串数组搜索 ===');
|
|
19
|
+
// 准备数据
|
|
20
|
+
const fruits = ['Apple', 'Banana', 'Orange', 'Pineapple', 'Grape', 'Watermelon'];
|
|
21
|
+
// 创建 Fuse 实例
|
|
22
|
+
const fuse = new fuse_js_1.default(fruits, {
|
|
23
|
+
includeScore: true
|
|
24
|
+
});
|
|
25
|
+
// 执行搜索
|
|
26
|
+
const result = fuse.search('aple'); // 故意拼错,测试模糊搜索
|
|
27
|
+
console.log('搜索 "aple" 的结果:');
|
|
28
|
+
result.forEach(item => {
|
|
29
|
+
console.log(` - ${item.item} (匹配度: ${item.score?.toFixed(3)})`);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// #endregion
|
|
33
|
+
// #region 示例2: 搜索对象数组
|
|
34
|
+
function 搜索对象数组() {
|
|
35
|
+
console.log('\n=== 示例2: 搜索对象数组 ===');
|
|
36
|
+
// 准备书籍数据
|
|
37
|
+
const books = [
|
|
38
|
+
{ title: 'JavaScript 高级程序设计', author: 'Nicholas C. Zakas' },
|
|
39
|
+
{ title: 'TypeScript 编程', author: 'Boris Cherny' },
|
|
40
|
+
{ title: 'Node.js 设计模式', author: 'Mario Casciaro' },
|
|
41
|
+
{ title: 'Vue.js 实战', author: '梁灏' },
|
|
42
|
+
{ title: 'React 进阶之路', author: '徐超' }
|
|
43
|
+
];
|
|
44
|
+
// 配置搜索的键
|
|
45
|
+
const fuse = new fuse_js_1.default(books, {
|
|
46
|
+
keys: ['title', 'author']
|
|
47
|
+
});
|
|
48
|
+
// 搜索标题或作者包含 "script" 的书籍
|
|
49
|
+
const result = fuse.search('script');
|
|
50
|
+
console.log('搜索 "script" 的结果:');
|
|
51
|
+
result.forEach(item => {
|
|
52
|
+
console.log(` - ${item.item.title} by ${item.item.author}`);
|
|
53
|
+
console.log(` 匹配度: ${item.score?.toFixed(3)}`);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
// #endregion
|
|
57
|
+
// #region 示例3: 基础配置选项
|
|
58
|
+
function 基础配置选项() {
|
|
59
|
+
console.log('\n=== 示例3: 基础配置选项 ===');
|
|
60
|
+
const languages = [
|
|
61
|
+
'JavaScript',
|
|
62
|
+
'TypeScript',
|
|
63
|
+
'Python',
|
|
64
|
+
'Java',
|
|
65
|
+
'C++',
|
|
66
|
+
'Ruby'
|
|
67
|
+
];
|
|
68
|
+
// 配置 Fuse 选项
|
|
69
|
+
const fuse = new fuse_js_1.default(languages, {
|
|
70
|
+
threshold: 0.3, // 匹配阈值,0.0 完全匹配,1.0 匹配任何内容
|
|
71
|
+
distance: 100, // 搜索距离
|
|
72
|
+
includeScore: true, // 包含匹配分数
|
|
73
|
+
minMatchCharLength: 2 // 最小匹配字符长度
|
|
74
|
+
});
|
|
75
|
+
console.log('配置说明:');
|
|
76
|
+
console.log(' - threshold: 0.3 (较严格的匹配)');
|
|
77
|
+
console.log(' - distance: 100');
|
|
78
|
+
console.log(' - includeScore: true');
|
|
79
|
+
console.log(' - minMatchCharLength: 2\n');
|
|
80
|
+
const result = fuse.search('java');
|
|
81
|
+
console.log('搜索 "java" 的结果:');
|
|
82
|
+
result.forEach(item => {
|
|
83
|
+
console.log(` - ${item.item} (分数: ${item.score?.toFixed(3)})`);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// #endregion
|
|
87
|
+
// #region 示例4: 嵌套对象搜索
|
|
88
|
+
function 嵌套对象搜索() {
|
|
89
|
+
console.log('\n=== 示例4: 嵌套对象搜索 ===');
|
|
90
|
+
// 准备嵌套结构的数据
|
|
91
|
+
const users = [
|
|
92
|
+
{
|
|
93
|
+
name: '张三',
|
|
94
|
+
profile: {
|
|
95
|
+
email: 'zhangsan@example.com',
|
|
96
|
+
city: '北京'
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: '李四',
|
|
101
|
+
profile: {
|
|
102
|
+
email: 'lisi@example.com',
|
|
103
|
+
city: '上海'
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: '王五',
|
|
108
|
+
profile: {
|
|
109
|
+
email: 'wangwu@example.com',
|
|
110
|
+
city: '深圳'
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
];
|
|
114
|
+
// 使用点号访问嵌套属性
|
|
115
|
+
const fuse = new fuse_js_1.default(users, {
|
|
116
|
+
keys: ['name', 'profile.email', 'profile.city']
|
|
117
|
+
});
|
|
118
|
+
const result = fuse.search('上海');
|
|
119
|
+
console.log('搜索 "上海" 的结果:');
|
|
120
|
+
result.forEach(item => {
|
|
121
|
+
console.log(` - ${item.item.name}`);
|
|
122
|
+
console.log(` 邮箱: ${item.item.profile.email}`);
|
|
123
|
+
console.log(` 城市: ${item.item.profile.city}`);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
// #endregion
|
|
127
|
+
// #region 示例5: 获取所有结果与限制结果数量
|
|
128
|
+
function 结果数量控制() {
|
|
129
|
+
console.log('\n=== 示例5: 结果数量控制 ===');
|
|
130
|
+
const countries = [
|
|
131
|
+
'China', 'Canada', 'Chile', 'Colombia', 'Croatia',
|
|
132
|
+
'Cuba', 'Cyprus', 'Czech Republic', 'Cambodia', 'Cameroon'
|
|
133
|
+
];
|
|
134
|
+
const fuse = new fuse_js_1.default(countries);
|
|
135
|
+
// 搜索以 C 开头的国家
|
|
136
|
+
const allResults = fuse.search('c');
|
|
137
|
+
console.log(`搜索 "c" 找到 ${allResults.length} 个结果:`);
|
|
138
|
+
allResults.slice(0, 5).forEach((item, index) => {
|
|
139
|
+
console.log(` ${index + 1}. ${item.item}`);
|
|
140
|
+
});
|
|
141
|
+
if (allResults.length > 5) {
|
|
142
|
+
console.log(` ... 还有 ${allResults.length - 5} 个结果`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// #endregion
|
|
146
|
+
// #region 主程序入口
|
|
147
|
+
if (require.main === module) {
|
|
148
|
+
const args = process.argv.slice(2);
|
|
149
|
+
const exampleNumber = args[0] ? parseInt(args[0]) : 0;
|
|
150
|
+
const examples = [
|
|
151
|
+
搜索字符串数组,
|
|
152
|
+
搜索对象数组,
|
|
153
|
+
基础配置选项,
|
|
154
|
+
嵌套对象搜索,
|
|
155
|
+
结果数量控制
|
|
156
|
+
];
|
|
157
|
+
if (exampleNumber > 0 && exampleNumber <= examples.length) {
|
|
158
|
+
console.log(`\n运行示例 ${exampleNumber}:`);
|
|
159
|
+
examples[exampleNumber - 1]();
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
console.log('Fuse.js 基础概念示例\n');
|
|
163
|
+
console.log('运行所有示例...\n');
|
|
164
|
+
examples.forEach(example => example());
|
|
165
|
+
console.log('\n\n提示: 使用 ts-node src/demo/fuse/01-基础概念.ts [1-5] 运行指定示例');
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// #endregion
|
|
@@ -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
|