search-algoritm 1.1.3 → 1.2.0
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/CHANGELOG.md +9 -0
- package/README.md +6 -5
- package/package.json +1 -1
- package/src/searchAlgoritm.js +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
## [1.2.0] - 2026-02-25
|
|
7
|
+
### Added
|
|
8
|
+
- Search now includes **filename** in addition to title and description.
|
|
9
|
+
- Fuzzy matching and token-based scoring are applied to filenames as well.
|
|
10
|
+
- Improved relevance calculation when query matches filename, title, or description.
|
|
11
|
+
- Module normalization continues to support accent-insensitive search and stopword filtering.
|
|
12
|
+
|
|
4
13
|
---
|
|
5
14
|
## [1.1.3] - 2025-12-04
|
|
6
15
|
### Error
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://github.com/FelixLind1/SearchAlgoritm)
|
|
8
8
|
|
|
9
9
|
A lightweight Node.js library for **fuzzy searching** arrays of objects.
|
|
10
|
-
It calculates relevance scores based on how well a query matches the `title` and `
|
|
10
|
+
It calculates relevance scores based on how well a query matches the `title`, `description`, and `file` of each item.
|
|
11
11
|
|
|
12
12
|
**Features:**
|
|
13
13
|
|
|
@@ -15,7 +15,7 @@ It calculates relevance scores based on how well a query matches the `title` and
|
|
|
15
15
|
- **Accent-insensitive** searches (`é → e`)
|
|
16
16
|
- Token-based word matching for partial or multi-word queries
|
|
17
17
|
- Multi-word sequence detection (`"mac pro"` → `"MacBook Pro"`)
|
|
18
|
-
- Weighted scoring: title
|
|
18
|
+
- Weighted scoring: **title > description ≈ file**
|
|
19
19
|
- Stopword filtering for more relevant results
|
|
20
20
|
|
|
21
21
|
---
|
|
@@ -85,7 +85,7 @@ app.listen(PORT, () => {
|
|
|
85
85
|
});
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
✅ Fast searches using in-memory cache. Ideal for **medium to large datasets
|
|
88
|
+
✅ Fast searches using in-memory cache. Ideal for **medium to large datasets**.
|
|
89
89
|
|
|
90
90
|
---
|
|
91
91
|
|
|
@@ -125,7 +125,7 @@ app.listen(PORT, () => {
|
|
|
125
125
|
});
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
-
✅ Reads JSON on each request. Best for **small datasets** or
|
|
128
|
+
✅ Reads JSON on each request. Best for **small datasets** or frequently changing data.
|
|
129
129
|
|
|
130
130
|
---
|
|
131
131
|
|
|
@@ -176,7 +176,8 @@ async function initSearch() {
|
|
|
176
176
|
? results.map(item => `
|
|
177
177
|
<li class="result-item">
|
|
178
178
|
<strong>${item.title}</strong><br>
|
|
179
|
-
<span>${item.description}</span>
|
|
179
|
+
<span>${item.description}</span><br>
|
|
180
|
+
<em>File: ${item.file}</em>
|
|
180
181
|
</li>
|
|
181
182
|
`).join('')
|
|
182
183
|
: `<li class="no-result">No matches found</li>`;
|
package/package.json
CHANGED
package/src/searchAlgoritm.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// searchAlgoritm.js
|
|
2
1
|
console.log('[searchAlgoritm] 🔍 Module loaded');
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -98,7 +97,7 @@ const matchScore = (text, query) => {
|
|
|
98
97
|
|
|
99
98
|
/**
|
|
100
99
|
* Compute weighted score for an item
|
|
101
|
-
* Title is weighted
|
|
100
|
+
* Title is weighted highest, then description and file equally
|
|
102
101
|
* @param {object} item
|
|
103
102
|
* @param {string} query
|
|
104
103
|
* @returns {number}
|
|
@@ -106,7 +105,10 @@ const matchScore = (text, query) => {
|
|
|
106
105
|
const calculateItemScore = (item, query) => {
|
|
107
106
|
const title = item._lcTitle ?? normalize(item.title);
|
|
108
107
|
const desc = item._lcDesc ?? normalize(item.description);
|
|
109
|
-
|
|
108
|
+
const file = item._lcFile ?? normalize(item.file ?? '');
|
|
109
|
+
|
|
110
|
+
// Weight: title *3, description *1.5, file *1.5
|
|
111
|
+
return matchScore(title, query) * 3 + matchScore(desc, query) * 1.5 + matchScore(file, query) * 1.5;
|
|
110
112
|
};
|
|
111
113
|
|
|
112
114
|
/**
|
|
@@ -125,6 +127,7 @@ const searchAlgoritm = (query, dataList, enableLog = false) => {
|
|
|
125
127
|
dataList.forEach(item => {
|
|
126
128
|
item._lcTitle ??= normalize(item.title);
|
|
127
129
|
item._lcDesc ??= normalize(item.description);
|
|
130
|
+
item._lcFile ??= normalize(item.file ?? ''); // include file
|
|
128
131
|
});
|
|
129
132
|
|
|
130
133
|
const results = dataList
|