@lppx/nlearn 1.1.14 → 1.1.19
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/demo/data-types/02-/346/225/260/347/273/204/344/270/216/345/205/203/347/273/204.js +21 -3
- package/dist/src/demo/fuse/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +34 -15
- package/dist/src/demo/operators-expressions/01-/345/237/272/347/241/200/350/277/220/347/256/227/347/254/246.js +61 -1
- package/package.json +2 -2
package/dist/src/demo/data-types/02-/346/225/260/347/273/204/344/270/216/345/205/203/347/273/204.js
CHANGED
|
@@ -54,12 +54,30 @@ function demonstrateArrayOperations() {
|
|
|
54
54
|
fruits.forEach((fruit, index) => {
|
|
55
55
|
console.log(` ${index}: ${fruit}`);
|
|
56
56
|
});
|
|
57
|
-
// map
|
|
57
|
+
// map: 传入一个转换函数 (item, index?, array?) => newItem
|
|
58
|
+
// 作用: 对数组每个元素执行转换函数,返回由转换结果组成的新数组
|
|
58
59
|
const upperFruits = fruits.map(fruit => fruit.toUpperCase());
|
|
59
60
|
console.log('\nmap 转大写:', upperFruits);
|
|
60
|
-
//
|
|
61
|
+
// reduce: 传入一个累积函数 (accumulator, currentItem, index?, array?) => newAccumulator 和初始值
|
|
62
|
+
// 作用: 将数组元素依次累积计算,最终返回一个累积结果
|
|
63
|
+
const numbers = [1, 2, 3, 4, 5];
|
|
64
|
+
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
|
|
65
|
+
console.log('\nreduce 求和:', sum);
|
|
66
|
+
// reduce 统计字符串长度总和
|
|
67
|
+
// 累积函数接收累积值和当前元素,返回新的累积值
|
|
68
|
+
const totalLength = fruits.reduce((total, fruit) => total + fruit.length, 0);
|
|
69
|
+
console.log('reduce 总长度:', totalLength);
|
|
70
|
+
// reduce 构建对象
|
|
71
|
+
// 累积函数可以构建复杂的数据结构,这里将数组转换为对象
|
|
72
|
+
const fruitLengths = fruits.reduce((obj, fruit) => {
|
|
73
|
+
obj[fruit] = fruit.length;
|
|
74
|
+
return obj;
|
|
75
|
+
}, {});
|
|
76
|
+
console.log('reduce 构建对象:', fruitLengths);
|
|
77
|
+
// filter: 传入一个判断函数 (item, index?, array?) => boolean
|
|
78
|
+
// 作用: 对数组每个元素执行判断函数,返回由判断为 true 的元素组成的新数组
|
|
61
79
|
const filtered = fruits.filter(fruit => fruit.length > 5);
|
|
62
|
-
console.log('
|
|
80
|
+
console.log('\nfilter 长度>5:', filtered);
|
|
63
81
|
}
|
|
64
82
|
// #endregion
|
|
65
83
|
// #region 示例3: 只读数组
|
|
@@ -18,6 +18,7 @@ exports.basicConfigOptions = basicConfigOptions;
|
|
|
18
18
|
exports.searchNestedObjects = searchNestedObjects;
|
|
19
19
|
exports.controlResultCount = controlResultCount;
|
|
20
20
|
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
21
|
+
const node_console_1 = require("node:console");
|
|
21
22
|
// #region 示例1: 简单字符串数组搜索
|
|
22
23
|
function searchStringArray() {
|
|
23
24
|
console.log('\n=== 示例1: 简单字符串数组搜索 ===');
|
|
@@ -25,10 +26,11 @@ function searchStringArray() {
|
|
|
25
26
|
const fruits = ['Apple', 'Banana', 'Orange', 'Pineapple', 'Grape', 'Watermelon'];
|
|
26
27
|
// 创建 Fuse 实例
|
|
27
28
|
const fuse = new fuse_js_1.default(fruits, {
|
|
28
|
-
includeScore: true
|
|
29
|
+
includeScore: true // 在结果中包含匹配分数,分数越低表示匹配度越高(0 为完美匹配)
|
|
29
30
|
});
|
|
30
31
|
// 执行搜索
|
|
31
32
|
const result = fuse.search('aple'); // 故意拼错,测试模糊搜索
|
|
33
|
+
(0, node_console_1.log)(result);
|
|
32
34
|
console.log('搜索 "aple" 的结果:');
|
|
33
35
|
result.forEach(item => {
|
|
34
36
|
console.log(` - ${item.item} (匹配度: ${item.score?.toFixed(3)})`);
|
|
@@ -48,10 +50,12 @@ function searchObjectArray() {
|
|
|
48
50
|
];
|
|
49
51
|
// 配置搜索的键
|
|
50
52
|
const fuse = new fuse_js_1.default(books, {
|
|
51
|
-
keys: ['title', 'author']
|
|
53
|
+
keys: ['title', 'author'],
|
|
54
|
+
includeScore: true
|
|
52
55
|
});
|
|
53
56
|
// 搜索标题或作者包含 "script" 的书籍
|
|
54
57
|
const result = fuse.search('script');
|
|
58
|
+
(0, node_console_1.log)(result);
|
|
55
59
|
console.log('搜索 "script" 的结果:');
|
|
56
60
|
result.forEach(item => {
|
|
57
61
|
console.log(` - ${item.item.title} by ${item.item.author}`);
|
|
@@ -77,11 +81,6 @@ function basicConfigOptions() {
|
|
|
77
81
|
includeScore: true, // 包含匹配分数
|
|
78
82
|
minMatchCharLength: 2 // 最小匹配字符长度
|
|
79
83
|
});
|
|
80
|
-
console.log('配置说明:');
|
|
81
|
-
console.log(' - threshold: 0.3 (较严格的匹配)');
|
|
82
|
-
console.log(' - distance: 100');
|
|
83
|
-
console.log(' - includeScore: true');
|
|
84
|
-
console.log(' - minMatchCharLength: 2\n');
|
|
85
84
|
const result = fuse.search('java');
|
|
86
85
|
console.log('搜索 "java" 的结果:');
|
|
87
86
|
result.forEach(item => {
|
|
@@ -121,6 +120,7 @@ function searchNestedObjects() {
|
|
|
121
120
|
keys: ['name', 'profile.email', 'profile.city']
|
|
122
121
|
});
|
|
123
122
|
const result = fuse.search('上海');
|
|
123
|
+
(0, node_console_1.log)(result);
|
|
124
124
|
console.log('搜索 "上海" 的结果:');
|
|
125
125
|
result.forEach(item => {
|
|
126
126
|
console.log(` - ${item.item.name}`);
|
|
@@ -134,17 +134,36 @@ function controlResultCount() {
|
|
|
134
134
|
console.log('\n=== 示例5: 结果数量控制 ===');
|
|
135
135
|
const countries = [
|
|
136
136
|
'China', 'Canada', 'Chile', 'Colombia', 'Croatia',
|
|
137
|
-
'Cuba', 'Cyprus', 'Czech Republic', 'Cambodia', 'Cameroon'
|
|
137
|
+
'Cuba', 'Cyprus', 'Czech Republic', 'Cambodia', 'Cameroon',
|
|
138
|
+
'Costa Rica', 'Comoros', 'Congo', 'Cape Verde'
|
|
138
139
|
];
|
|
139
|
-
const fuse = new fuse_js_1.default(countries
|
|
140
|
+
const fuse = new fuse_js_1.default(countries, {
|
|
141
|
+
includeScore: true,
|
|
142
|
+
threshold: 0.4
|
|
143
|
+
});
|
|
140
144
|
// 搜索以 C 开头的国家
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
+
const searchTerm = 'ca';
|
|
146
|
+
const allResults = fuse.search(searchTerm);
|
|
147
|
+
console.log(`\n1. 获取所有结果 (共 ${allResults.length} 个):`);
|
|
148
|
+
allResults.forEach((item, index) => {
|
|
149
|
+
console.log(` ${index + 1}. ${item.item} (分数: ${item.score?.toFixed(3)})`);
|
|
145
150
|
});
|
|
146
|
-
|
|
147
|
-
|
|
151
|
+
// 使用 limit 选项限制结果数量
|
|
152
|
+
console.log(`\n2. 使用 limit 限制为前 3 个结果:`);
|
|
153
|
+
const limitedResults = fuse.search(searchTerm, { limit: 3 });
|
|
154
|
+
limitedResults.forEach((item, index) => {
|
|
155
|
+
console.log(` ${index + 1}. ${item.item} (分数: ${item.score?.toFixed(3)})`);
|
|
156
|
+
});
|
|
157
|
+
// 根据分数过滤结果
|
|
158
|
+
console.log(`\n4. 只显示分数低于 0.3 的高质量匹配:`);
|
|
159
|
+
const highQualityResults = allResults.filter(item => (item.score ?? 1) < 0.3);
|
|
160
|
+
if (highQualityResults.length > 0) {
|
|
161
|
+
highQualityResults.forEach((item, index) => {
|
|
162
|
+
console.log(` ${index + 1}. ${item.item} (分数: ${item.score?.toFixed(3)})`);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
console.log(' 没有找到高质量匹配结果');
|
|
148
167
|
}
|
|
149
168
|
}
|
|
150
169
|
// #endregion
|
|
@@ -11,6 +11,7 @@ exports.assignmentOperators = assignmentOperators;
|
|
|
11
11
|
exports.comparisonOperators = comparisonOperators;
|
|
12
12
|
exports.logicalOperators = logicalOperators;
|
|
13
13
|
exports.stringOperators = stringOperators;
|
|
14
|
+
exports.spreadOperator = spreadOperator;
|
|
14
15
|
// #region 示例1: 算术运算符
|
|
15
16
|
function arithmeticOperators() {
|
|
16
17
|
console.log('=== 算术运算符 ===');
|
|
@@ -119,13 +120,72 @@ function stringOperators() {
|
|
|
119
120
|
console.log(`字符串 - 数字: '${str}' - ${num} = ${str - num}`); // 数字运算(自动转换)
|
|
120
121
|
}
|
|
121
122
|
// #endregion
|
|
123
|
+
// #region 示例6: 展开运算符 (...)
|
|
124
|
+
function spreadOperator() {
|
|
125
|
+
console.log('\n=== 展开运算符 (...) ===');
|
|
126
|
+
// 数组展开
|
|
127
|
+
const arr1 = [1, 2, 3];
|
|
128
|
+
const arr2 = [4, 5, 6];
|
|
129
|
+
const combined = [...arr1, ...arr2];
|
|
130
|
+
console.log(`合并数组: [${combined}]`);
|
|
131
|
+
// 数组复制(浅拷贝)
|
|
132
|
+
const original = [1, 2, 3];
|
|
133
|
+
const copy = [...original];
|
|
134
|
+
copy.push(4);
|
|
135
|
+
console.log(`\n原数组: [${original}]`);
|
|
136
|
+
console.log(`复制数组: [${copy}]`);
|
|
137
|
+
// 在数组中插入元素
|
|
138
|
+
const numbers = [1, 2, 5, 6];
|
|
139
|
+
const inserted = [1, 2, ...[3, 4], 5, 6];
|
|
140
|
+
console.log(`\n插入元素: [${inserted}]`);
|
|
141
|
+
// 对象展开
|
|
142
|
+
const person = { name: '张三', age: 25 };
|
|
143
|
+
const employee = { ...person, job: '工程师', salary: 10000 };
|
|
144
|
+
console.log(`\n员工信息:`, employee);
|
|
145
|
+
// 对象合并(后面的属性会覆盖前面的)
|
|
146
|
+
const defaults = { theme: 'light', fontSize: 14, showSidebar: true };
|
|
147
|
+
const userSettings = { fontSize: 16, showSidebar: false };
|
|
148
|
+
const finalSettings = { ...defaults, ...userSettings };
|
|
149
|
+
console.log(`\n最终设置:`, finalSettings);
|
|
150
|
+
// 对象复制(浅拷贝)
|
|
151
|
+
const originalObj = { x: 1, y: 2 };
|
|
152
|
+
const copiedObj = { ...originalObj };
|
|
153
|
+
copiedObj.x = 10;
|
|
154
|
+
console.log(`\n原对象:`, originalObj);
|
|
155
|
+
console.log(`复制对象:`, copiedObj);
|
|
156
|
+
// 函数参数展开
|
|
157
|
+
function sum(a, b, c) {
|
|
158
|
+
return a + b + c;
|
|
159
|
+
}
|
|
160
|
+
const nums = [1, 2, 3];
|
|
161
|
+
const result = sum(...nums);
|
|
162
|
+
console.log(`\nsum(...[1, 2, 3]) = ${result}`);
|
|
163
|
+
// Math 函数中使用
|
|
164
|
+
const values = [5, 2, 8, 1, 9, 3];
|
|
165
|
+
const max = Math.max(...values);
|
|
166
|
+
const min = Math.min(...values);
|
|
167
|
+
console.log(`\n数组 [${values}]`);
|
|
168
|
+
console.log(`最大值: ${max}`);
|
|
169
|
+
console.log(`最小值: ${min}`);
|
|
170
|
+
// 字符串展开为字符数组
|
|
171
|
+
const str = 'Hello';
|
|
172
|
+
const chars = [...str];
|
|
173
|
+
console.log(`\n字符串展开: '${str}' => [${chars.map(c => `'${c}'`).join(', ')}]`);
|
|
174
|
+
// 去重(结合 Set)
|
|
175
|
+
const duplicates = [1, 2, 2, 3, 3, 3, 4, 5, 5];
|
|
176
|
+
const unique = [...new Set(duplicates)];
|
|
177
|
+
console.log(`\n去重前: [${duplicates}]`);
|
|
178
|
+
console.log(`去重后: [${unique}]`);
|
|
179
|
+
}
|
|
180
|
+
// #endregion
|
|
122
181
|
if (require.main === module) {
|
|
123
182
|
const examples = [
|
|
124
183
|
arithmeticOperators,
|
|
125
184
|
assignmentOperators,
|
|
126
185
|
comparisonOperators,
|
|
127
186
|
logicalOperators,
|
|
128
|
-
stringOperators
|
|
187
|
+
stringOperators,
|
|
188
|
+
spreadOperator
|
|
129
189
|
];
|
|
130
190
|
const exampleNumber = parseInt(process.argv[2]);
|
|
131
191
|
if (exampleNumber && exampleNumber > 0 && exampleNumber <= examples.length) {
|
package/package.json
CHANGED