rapid-fuzzy 0.4.0 → 0.5.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/README.md +77 -28
- package/highlight.mjs +4 -57
- package/index.d.mts +8 -2
- package/index.d.ts +52 -0
- package/index.js +53 -52
- package/index.mjs +1 -1
- package/objects.d.ts +62 -0
- package/objects.js +115 -3
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -112,6 +112,38 @@ searchObjects('john', users, {
|
|
|
112
112
|
searchObjects('new york', items, { keys: ['address.city'] });
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
+
### Persistent Index
|
|
116
|
+
|
|
117
|
+
For applications that search the same dataset repeatedly (autocomplete, file finders, etc.), use `FuzzyIndex` or `FuzzyObjectIndex` to keep data on the Rust side and eliminate per-search FFI overhead:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { FuzzyIndex, FuzzyObjectIndex } from 'rapid-fuzzy';
|
|
121
|
+
|
|
122
|
+
// String search index — up to 5x faster than standalone search()
|
|
123
|
+
const index = new FuzzyIndex(['TypeScript', 'JavaScript', 'Python', ...]);
|
|
124
|
+
|
|
125
|
+
index.search('typscript', { maxResults: 5 });
|
|
126
|
+
index.closest('tsc');
|
|
127
|
+
|
|
128
|
+
// Mutate the index without rebuilding
|
|
129
|
+
index.add('Rust');
|
|
130
|
+
index.remove(2); // swap-remove by index
|
|
131
|
+
|
|
132
|
+
// Object search index — keeps objects on the JS side, keys on the Rust side
|
|
133
|
+
const userIndex = new FuzzyObjectIndex(users, {
|
|
134
|
+
keys: [
|
|
135
|
+
{ name: 'name', weight: 2.0 },
|
|
136
|
+
{ name: 'email', weight: 1.0 },
|
|
137
|
+
],
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
userIndex.search('john', { maxResults: 10 });
|
|
141
|
+
|
|
142
|
+
// Free Rust-side memory when done
|
|
143
|
+
index.destroy();
|
|
144
|
+
userIndex.destroy();
|
|
145
|
+
```
|
|
146
|
+
|
|
115
147
|
### Match Highlighting
|
|
116
148
|
|
|
117
149
|
Convert matched positions into highlighted markup for UI rendering:
|
|
@@ -189,37 +221,52 @@ Measured on Apple M-series with Node.js v22 using [Vitest bench](https://vitest.
|
|
|
189
221
|
|
|
190
222
|
### Distance Functions
|
|
191
223
|
|
|
224
|
+
<img src=".github/assets/bench-distance.svg" alt="Distance function performance chart" width="680" />
|
|
225
|
+
|
|
226
|
+
<details>
|
|
227
|
+
<summary>Raw numbers</summary>
|
|
228
|
+
|
|
192
229
|
| Function | rapid-fuzzy | fastest-levenshtein | leven | string-similarity |
|
|
193
230
|
|---|---:|---:|---:|---:|
|
|
194
|
-
| Levenshtein |
|
|
195
|
-
| Normalized Levenshtein | **
|
|
196
|
-
| Sorensen-Dice | **
|
|
197
|
-
| Jaro-Winkler | **
|
|
198
|
-
| Damerau-Levenshtein | **
|
|
231
|
+
| Levenshtein | 528,195 ops/s | **739,107 ops/s** | 221,817 ops/s | — |
|
|
232
|
+
| Normalized Levenshtein | **534,231 ops/s** | — | — | — |
|
|
233
|
+
| Sorensen-Dice | **149,567 ops/s** | — | — | 82,908 ops/s |
|
|
234
|
+
| Jaro-Winkler | **278,554 ops/s** | — | — | — |
|
|
235
|
+
| Damerau-Levenshtein | **112,370 ops/s** | — | — | — |
|
|
236
|
+
|
|
237
|
+
</details>
|
|
199
238
|
|
|
200
|
-
> **Note**: For single-pair Levenshtein
|
|
239
|
+
> **Note**: For single-pair Levenshtein, fastest-levenshtein is ~1.4x faster due to its optimized pure-JS implementation that avoids FFI overhead. rapid-fuzzy is **2.4x faster** than leven, and provides broader algorithm coverage plus batch / search scenarios.
|
|
201
240
|
|
|
202
241
|
### Search Performance
|
|
203
242
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
243
|
+
<img src=".github/assets/bench-search.svg" alt="Search performance chart — rapid-fuzzy vs fuse.js vs fuzzysort" width="680" />
|
|
244
|
+
|
|
245
|
+
<details>
|
|
246
|
+
<summary>Raw numbers</summary>
|
|
247
|
+
|
|
248
|
+
| Dataset size | rapid-fuzzy | FuzzyIndex | fuse.js | fuzzysort |
|
|
249
|
+
|---|---:|---:|---:|---:|
|
|
250
|
+
| Small (20 items) | 174,812 ops/s | 406,269 ops/s | 127,167 ops/s | **2,502,299 ops/s** |
|
|
251
|
+
| Medium (1K items) | 6,531 ops/s | 22,014 ops/s | 395 ops/s | **62,845 ops/s** |
|
|
252
|
+
| Large (10K items) | 794 ops/s | 3,985 ops/s | 20 ops/s | **28,846 ops/s** |
|
|
253
|
+
|
|
254
|
+
</details>
|
|
209
255
|
|
|
210
256
|
### Closest Match (Levenshtein-based)
|
|
211
257
|
|
|
212
|
-
| Dataset size | rapid-fuzzy | fastest-levenshtein |
|
|
213
|
-
|
|
214
|
-
| Medium (1K items) | 8,
|
|
215
|
-
| Large (10K items) | **
|
|
258
|
+
| Dataset size | rapid-fuzzy | FuzzyIndex | fastest-levenshtein |
|
|
259
|
+
|---|---:|---:|---:|
|
|
260
|
+
| Medium (1K items) | 8,304 ops/s | **60,469 ops/s** | 8,946 ops/s |
|
|
261
|
+
| Large (10K items) | 757 ops/s | **4,103 ops/s** | 604 ops/s |
|
|
216
262
|
|
|
217
|
-
> rapid-fuzzy is up to **
|
|
263
|
+
> With `FuzzyIndex`, rapid-fuzzy is up to **6.8x faster** than fastest-levenshtein for closest-match lookups.
|
|
218
264
|
|
|
219
265
|
### Why these numbers matter
|
|
220
266
|
|
|
221
267
|
- **vs fuse.js**: rapid-fuzzy is **17x faster** on medium datasets and **40x faster** on large datasets for fuzzy search.
|
|
222
|
-
- **
|
|
268
|
+
- **FuzzyIndex**: Pre-computing string data on the Rust side gives an additional **3–5x speedup** over standalone `search()`, making it the recommended approach for repeated searches.
|
|
269
|
+
- **vs fastest-levenshtein**: With `FuzzyIndex`, closest-match is **6.8x faster** at scale. Even standalone `closest()` wins on large datasets.
|
|
223
270
|
- **fuzzysort** uses a different (substring-based) matching algorithm that is extremely fast but produces different ranking results. Choose based on your matching needs.
|
|
224
271
|
|
|
225
272
|
Run benchmarks yourself:
|
|
@@ -241,6 +288,7 @@ cargo bench # Rust internal benchmarks
|
|
|
241
288
|
| Substring / abbreviation matching | `partialRatio` | Finds best partial match within longer strings |
|
|
242
289
|
| Best-effort similarity | `weightedRatio` | Picks the best score across all methods automatically |
|
|
243
290
|
| Interactive fuzzy search | `search`, `closest` | Nucleo algorithm (same as Helix editor) |
|
|
291
|
+
| Repeated search on same data | `FuzzyIndex`, `FuzzyObjectIndex` | Persistent Rust-side index, 3–5x faster than standalone |
|
|
244
292
|
|
|
245
293
|
**Return types:**
|
|
246
294
|
|
|
@@ -252,17 +300,18 @@ cargo bench # Rust internal benchmarks
|
|
|
252
300
|
## Why rapid-fuzzy?
|
|
253
301
|
|
|
254
302
|
| | rapid-fuzzy | fuse.js | fastest-levenshtein | fuzzysort |
|
|
255
|
-
|
|
256
|
-
| **Algorithms** | Levenshtein, Jaro
|
|
257
|
-
| **Runtime** | Rust
|
|
258
|
-
| **Object search** |
|
|
259
|
-
| **
|
|
260
|
-
| **
|
|
261
|
-
| **
|
|
262
|
-
| **
|
|
263
|
-
| **
|
|
264
|
-
| **
|
|
265
|
-
| **
|
|
303
|
+
|---|:---:|:---:|:---:|:---:|
|
|
304
|
+
| **Algorithms** | 9 (Levenshtein, Jaro, Dice, …) | Bitap | Levenshtein | Substring |
|
|
305
|
+
| **Runtime** | Rust native + WASM | Pure JS | Pure JS | Pure JS |
|
|
306
|
+
| **Object search** | ✅ weighted keys | ✅ | — | ✅ |
|
|
307
|
+
| **Persistent index** | ✅ FuzzyIndex / FuzzyObjectIndex | — | — | ✅ prepared targets |
|
|
308
|
+
| **Score threshold** | ✅ | ✅ | — | ✅ |
|
|
309
|
+
| **Match positions** | ✅ | ✅ | — | ✅ |
|
|
310
|
+
| **Highlight utility** | ✅ | — | — | ✅ |
|
|
311
|
+
| **Batch API** | ✅ | — | — | — |
|
|
312
|
+
| **Node.js native** | ✅ napi-rs | — | — | — |
|
|
313
|
+
| **Browser** | ✅ WASM | ✅ | ✅ | ✅ |
|
|
314
|
+
| **TypeScript** | ✅ full | ✅ full | ✅ | ✅ |
|
|
266
315
|
|
|
267
316
|
## Migration Guides
|
|
268
317
|
|
package/highlight.mjs
CHANGED
|
@@ -1,57 +1,4 @@
|
|
|
1
|
-
// ESM
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Convert matched positions into an array of ranges for custom rendering.
|
|
6
|
-
*
|
|
7
|
-
* @param {string} item - The original string from the search result.
|
|
8
|
-
* @param {number[]} positions - Array of matched character indices.
|
|
9
|
-
* @returns {Array<{start: number, end: number, matched: boolean}>} Array of ranges.
|
|
10
|
-
*/
|
|
11
|
-
export function highlightRanges(item, positions) {
|
|
12
|
-
if (!item) return [];
|
|
13
|
-
if (!positions || positions.length === 0) {
|
|
14
|
-
return [{ start: 0, end: item.length, matched: false }];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const set = new Set(positions);
|
|
18
|
-
const ranges = [];
|
|
19
|
-
let i = 0;
|
|
20
|
-
|
|
21
|
-
while (i < item.length) {
|
|
22
|
-
const matched = set.has(i);
|
|
23
|
-
const start = i;
|
|
24
|
-
while (i < item.length && set.has(i) === matched) i++;
|
|
25
|
-
ranges.push({ start, end: i, matched });
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return ranges;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Highlight matched characters in a search result string.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} item - The original string from the search result.
|
|
35
|
-
* @param {number[]} positions - Array of matched character indices.
|
|
36
|
-
* @param {string | ((substring: string) => string)} openOrCallback - Opening tag or callback.
|
|
37
|
-
* @param {string} [close] - Closing tag (required when openOrCallback is a string).
|
|
38
|
-
* @returns {string} The highlighted string.
|
|
39
|
-
*/
|
|
40
|
-
export function highlight(item, positions, openOrCallback, close) {
|
|
41
|
-
if (!positions || positions.length === 0) return item;
|
|
42
|
-
|
|
43
|
-
const ranges = highlightRanges(item, positions);
|
|
44
|
-
const useCallback = typeof openOrCallback === 'function';
|
|
45
|
-
|
|
46
|
-
const parts = [];
|
|
47
|
-
for (const range of ranges) {
|
|
48
|
-
const segment = item.slice(range.start, range.end);
|
|
49
|
-
if (range.matched) {
|
|
50
|
-
parts.push(useCallback ? openOrCallback(segment) : openOrCallback + segment + (close ?? ''));
|
|
51
|
-
} else {
|
|
52
|
-
parts.push(segment);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return parts.join('');
|
|
57
|
-
}
|
|
1
|
+
// ESM re-export — single source of truth is highlight.js (CJS).
|
|
2
|
+
// Node.js detects named exports from CJS via static analysis.
|
|
3
|
+
// Bundlers (webpack, vite, rollup) handle CJS interop natively.
|
|
4
|
+
export { highlight, highlightRanges } from './highlight.js';
|
package/index.d.mts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export * from './index.d.ts';
|
|
2
2
|
export { highlight, highlightRanges } from './highlight.d.ts';
|
|
3
3
|
export type { HighlightRange } from './highlight.d.ts';
|
|
4
|
-
export { searchObjects } from './objects';
|
|
5
|
-
export type {
|
|
4
|
+
export { searchObjects, FuzzyObjectIndex } from './objects';
|
|
5
|
+
export type {
|
|
6
|
+
KeyConfig,
|
|
7
|
+
ObjectSearchOptions,
|
|
8
|
+
ObjectSearchResult,
|
|
9
|
+
ObjectIndexOptions,
|
|
10
|
+
ObjectIndexSearchOptions,
|
|
11
|
+
} from './objects';
|
package/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Holds items in memory on the Rust side, avoiding repeated FFI overhead
|
|
7
7
|
* for applications that search the same dataset multiple times.
|
|
8
|
+
* Pre-computes Utf32String representations for each item, eliminating
|
|
9
|
+
* per-search string conversion overhead.
|
|
8
10
|
* Memory is freed when the JavaScript garbage collector collects the instance
|
|
9
11
|
* or when `destroy()` is called explicitly.
|
|
10
12
|
*/
|
|
@@ -41,6 +43,56 @@ export declare class FuzzyIndex {
|
|
|
41
43
|
destroy(): void
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
/**
|
|
47
|
+
* A persistent multi-key fuzzy search index backed by Rust-side data.
|
|
48
|
+
*
|
|
49
|
+
* Holds key text arrays and weights in memory on the Rust side,
|
|
50
|
+
* avoiding repeated FFI overhead for applications that search the
|
|
51
|
+
* same dataset multiple times with multiple keys.
|
|
52
|
+
* Pre-computes Utf32String representations and reuses the Matcher
|
|
53
|
+
* instance for optimal repeated-search performance.
|
|
54
|
+
*
|
|
55
|
+
* Typically wrapped by a JS-side `FuzzyObjectIndex` class that maps
|
|
56
|
+
* results back to original objects.
|
|
57
|
+
*/
|
|
58
|
+
export declare class KeyedFuzzyIndex {
|
|
59
|
+
/**
|
|
60
|
+
* Create a new KeyedFuzzyIndex.
|
|
61
|
+
*
|
|
62
|
+
* `key_texts[k]` is an array of strings for key `k`, one per item.
|
|
63
|
+
* All inner arrays must have the same length (the number of items).
|
|
64
|
+
*/
|
|
65
|
+
constructor(keyTexts: Array<Array<string>>, weights: Array<number>)
|
|
66
|
+
/** Return the number of items in the index. */
|
|
67
|
+
get size(): number
|
|
68
|
+
/**
|
|
69
|
+
* Search the index for items matching the query.
|
|
70
|
+
*
|
|
71
|
+
* Returns results sorted by combined weighted score (best match first).
|
|
72
|
+
*/
|
|
73
|
+
search(query: string, options?: SearchOptions | undefined | null): Array<KeySearchResult>
|
|
74
|
+
/**
|
|
75
|
+
* Add a single item to the index.
|
|
76
|
+
*
|
|
77
|
+
* `key_values` must have the same length as the number of keys.
|
|
78
|
+
*/
|
|
79
|
+
add(keyValues: Array<string>): void
|
|
80
|
+
/**
|
|
81
|
+
* Add multiple items to the index at once.
|
|
82
|
+
*
|
|
83
|
+
* Each element of `items_key_values` is an array of key values for one item.
|
|
84
|
+
*/
|
|
85
|
+
addMany(itemsKeyValues: Array<Array<string>>): void
|
|
86
|
+
/**
|
|
87
|
+
* Remove the item at the given index.
|
|
88
|
+
*
|
|
89
|
+
* Uses swap-remove for O(1) performance. Returns false if out of bounds.
|
|
90
|
+
*/
|
|
91
|
+
remove(index: number): boolean
|
|
92
|
+
/** Free the internal data. After calling this, the index is empty. */
|
|
93
|
+
destroy(): void
|
|
94
|
+
}
|
|
95
|
+
|
|
44
96
|
/**
|
|
45
97
|
* Find the closest matching string from a list.
|
|
46
98
|
*
|
package/index.js
CHANGED
|
@@ -77,8 +77,8 @@ function requireNative() {
|
|
|
77
77
|
try {
|
|
78
78
|
const binding = require('rapid-fuzzy-android-arm64')
|
|
79
79
|
const bindingPackageVersion = require('rapid-fuzzy-android-arm64/package.json').version
|
|
80
|
-
if (bindingPackageVersion !== '0.
|
|
81
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
80
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
81
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
82
82
|
}
|
|
83
83
|
return binding
|
|
84
84
|
} catch (e) {
|
|
@@ -93,8 +93,8 @@ function requireNative() {
|
|
|
93
93
|
try {
|
|
94
94
|
const binding = require('rapid-fuzzy-android-arm-eabi')
|
|
95
95
|
const bindingPackageVersion = require('rapid-fuzzy-android-arm-eabi/package.json').version
|
|
96
|
-
if (bindingPackageVersion !== '0.
|
|
97
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
96
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
97
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
98
98
|
}
|
|
99
99
|
return binding
|
|
100
100
|
} catch (e) {
|
|
@@ -114,8 +114,8 @@ function requireNative() {
|
|
|
114
114
|
try {
|
|
115
115
|
const binding = require('rapid-fuzzy-win32-x64-gnu')
|
|
116
116
|
const bindingPackageVersion = require('rapid-fuzzy-win32-x64-gnu/package.json').version
|
|
117
|
-
if (bindingPackageVersion !== '0.
|
|
118
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
117
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
118
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
119
119
|
}
|
|
120
120
|
return binding
|
|
121
121
|
} catch (e) {
|
|
@@ -130,8 +130,8 @@ function requireNative() {
|
|
|
130
130
|
try {
|
|
131
131
|
const binding = require('rapid-fuzzy-win32-x64-msvc')
|
|
132
132
|
const bindingPackageVersion = require('rapid-fuzzy-win32-x64-msvc/package.json').version
|
|
133
|
-
if (bindingPackageVersion !== '0.
|
|
134
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
133
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
134
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
135
135
|
}
|
|
136
136
|
return binding
|
|
137
137
|
} catch (e) {
|
|
@@ -147,8 +147,8 @@ function requireNative() {
|
|
|
147
147
|
try {
|
|
148
148
|
const binding = require('rapid-fuzzy-win32-ia32-msvc')
|
|
149
149
|
const bindingPackageVersion = require('rapid-fuzzy-win32-ia32-msvc/package.json').version
|
|
150
|
-
if (bindingPackageVersion !== '0.
|
|
151
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
150
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
151
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
152
152
|
}
|
|
153
153
|
return binding
|
|
154
154
|
} catch (e) {
|
|
@@ -163,8 +163,8 @@ function requireNative() {
|
|
|
163
163
|
try {
|
|
164
164
|
const binding = require('rapid-fuzzy-win32-arm64-msvc')
|
|
165
165
|
const bindingPackageVersion = require('rapid-fuzzy-win32-arm64-msvc/package.json').version
|
|
166
|
-
if (bindingPackageVersion !== '0.
|
|
167
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
166
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
167
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
168
168
|
}
|
|
169
169
|
return binding
|
|
170
170
|
} catch (e) {
|
|
@@ -182,8 +182,8 @@ function requireNative() {
|
|
|
182
182
|
try {
|
|
183
183
|
const binding = require('rapid-fuzzy-darwin-universal')
|
|
184
184
|
const bindingPackageVersion = require('rapid-fuzzy-darwin-universal/package.json').version
|
|
185
|
-
if (bindingPackageVersion !== '0.
|
|
186
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
185
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
186
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
187
187
|
}
|
|
188
188
|
return binding
|
|
189
189
|
} catch (e) {
|
|
@@ -198,8 +198,8 @@ function requireNative() {
|
|
|
198
198
|
try {
|
|
199
199
|
const binding = require('rapid-fuzzy-darwin-x64')
|
|
200
200
|
const bindingPackageVersion = require('rapid-fuzzy-darwin-x64/package.json').version
|
|
201
|
-
if (bindingPackageVersion !== '0.
|
|
202
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
201
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
202
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
203
203
|
}
|
|
204
204
|
return binding
|
|
205
205
|
} catch (e) {
|
|
@@ -214,8 +214,8 @@ function requireNative() {
|
|
|
214
214
|
try {
|
|
215
215
|
const binding = require('rapid-fuzzy-darwin-arm64')
|
|
216
216
|
const bindingPackageVersion = require('rapid-fuzzy-darwin-arm64/package.json').version
|
|
217
|
-
if (bindingPackageVersion !== '0.
|
|
218
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
217
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
218
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
219
219
|
}
|
|
220
220
|
return binding
|
|
221
221
|
} catch (e) {
|
|
@@ -234,8 +234,8 @@ function requireNative() {
|
|
|
234
234
|
try {
|
|
235
235
|
const binding = require('rapid-fuzzy-freebsd-x64')
|
|
236
236
|
const bindingPackageVersion = require('rapid-fuzzy-freebsd-x64/package.json').version
|
|
237
|
-
if (bindingPackageVersion !== '0.
|
|
238
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
237
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
238
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
239
239
|
}
|
|
240
240
|
return binding
|
|
241
241
|
} catch (e) {
|
|
@@ -250,8 +250,8 @@ function requireNative() {
|
|
|
250
250
|
try {
|
|
251
251
|
const binding = require('rapid-fuzzy-freebsd-arm64')
|
|
252
252
|
const bindingPackageVersion = require('rapid-fuzzy-freebsd-arm64/package.json').version
|
|
253
|
-
if (bindingPackageVersion !== '0.
|
|
254
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
253
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
254
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
255
255
|
}
|
|
256
256
|
return binding
|
|
257
257
|
} catch (e) {
|
|
@@ -271,8 +271,8 @@ function requireNative() {
|
|
|
271
271
|
try {
|
|
272
272
|
const binding = require('rapid-fuzzy-linux-x64-musl')
|
|
273
273
|
const bindingPackageVersion = require('rapid-fuzzy-linux-x64-musl/package.json').version
|
|
274
|
-
if (bindingPackageVersion !== '0.
|
|
275
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
274
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
275
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
276
276
|
}
|
|
277
277
|
return binding
|
|
278
278
|
} catch (e) {
|
|
@@ -287,8 +287,8 @@ function requireNative() {
|
|
|
287
287
|
try {
|
|
288
288
|
const binding = require('rapid-fuzzy-linux-x64-gnu')
|
|
289
289
|
const bindingPackageVersion = require('rapid-fuzzy-linux-x64-gnu/package.json').version
|
|
290
|
-
if (bindingPackageVersion !== '0.
|
|
291
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
290
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
291
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
292
292
|
}
|
|
293
293
|
return binding
|
|
294
294
|
} catch (e) {
|
|
@@ -305,8 +305,8 @@ function requireNative() {
|
|
|
305
305
|
try {
|
|
306
306
|
const binding = require('rapid-fuzzy-linux-arm64-musl')
|
|
307
307
|
const bindingPackageVersion = require('rapid-fuzzy-linux-arm64-musl/package.json').version
|
|
308
|
-
if (bindingPackageVersion !== '0.
|
|
309
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
308
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
309
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
310
310
|
}
|
|
311
311
|
return binding
|
|
312
312
|
} catch (e) {
|
|
@@ -321,8 +321,8 @@ function requireNative() {
|
|
|
321
321
|
try {
|
|
322
322
|
const binding = require('rapid-fuzzy-linux-arm64-gnu')
|
|
323
323
|
const bindingPackageVersion = require('rapid-fuzzy-linux-arm64-gnu/package.json').version
|
|
324
|
-
if (bindingPackageVersion !== '0.
|
|
325
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
324
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
325
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
326
326
|
}
|
|
327
327
|
return binding
|
|
328
328
|
} catch (e) {
|
|
@@ -339,8 +339,8 @@ function requireNative() {
|
|
|
339
339
|
try {
|
|
340
340
|
const binding = require('rapid-fuzzy-linux-arm-musleabihf')
|
|
341
341
|
const bindingPackageVersion = require('rapid-fuzzy-linux-arm-musleabihf/package.json').version
|
|
342
|
-
if (bindingPackageVersion !== '0.
|
|
343
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
342
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
343
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
344
344
|
}
|
|
345
345
|
return binding
|
|
346
346
|
} catch (e) {
|
|
@@ -355,8 +355,8 @@ function requireNative() {
|
|
|
355
355
|
try {
|
|
356
356
|
const binding = require('rapid-fuzzy-linux-arm-gnueabihf')
|
|
357
357
|
const bindingPackageVersion = require('rapid-fuzzy-linux-arm-gnueabihf/package.json').version
|
|
358
|
-
if (bindingPackageVersion !== '0.
|
|
359
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
358
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
359
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
360
360
|
}
|
|
361
361
|
return binding
|
|
362
362
|
} catch (e) {
|
|
@@ -373,8 +373,8 @@ function requireNative() {
|
|
|
373
373
|
try {
|
|
374
374
|
const binding = require('rapid-fuzzy-linux-loong64-musl')
|
|
375
375
|
const bindingPackageVersion = require('rapid-fuzzy-linux-loong64-musl/package.json').version
|
|
376
|
-
if (bindingPackageVersion !== '0.
|
|
377
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
376
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
377
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
378
378
|
}
|
|
379
379
|
return binding
|
|
380
380
|
} catch (e) {
|
|
@@ -389,8 +389,8 @@ function requireNative() {
|
|
|
389
389
|
try {
|
|
390
390
|
const binding = require('rapid-fuzzy-linux-loong64-gnu')
|
|
391
391
|
const bindingPackageVersion = require('rapid-fuzzy-linux-loong64-gnu/package.json').version
|
|
392
|
-
if (bindingPackageVersion !== '0.
|
|
393
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
392
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
393
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
394
394
|
}
|
|
395
395
|
return binding
|
|
396
396
|
} catch (e) {
|
|
@@ -407,8 +407,8 @@ function requireNative() {
|
|
|
407
407
|
try {
|
|
408
408
|
const binding = require('rapid-fuzzy-linux-riscv64-musl')
|
|
409
409
|
const bindingPackageVersion = require('rapid-fuzzy-linux-riscv64-musl/package.json').version
|
|
410
|
-
if (bindingPackageVersion !== '0.
|
|
411
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
410
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
411
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
412
412
|
}
|
|
413
413
|
return binding
|
|
414
414
|
} catch (e) {
|
|
@@ -423,8 +423,8 @@ function requireNative() {
|
|
|
423
423
|
try {
|
|
424
424
|
const binding = require('rapid-fuzzy-linux-riscv64-gnu')
|
|
425
425
|
const bindingPackageVersion = require('rapid-fuzzy-linux-riscv64-gnu/package.json').version
|
|
426
|
-
if (bindingPackageVersion !== '0.
|
|
427
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
426
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
427
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
428
428
|
}
|
|
429
429
|
return binding
|
|
430
430
|
} catch (e) {
|
|
@@ -440,8 +440,8 @@ function requireNative() {
|
|
|
440
440
|
try {
|
|
441
441
|
const binding = require('rapid-fuzzy-linux-ppc64-gnu')
|
|
442
442
|
const bindingPackageVersion = require('rapid-fuzzy-linux-ppc64-gnu/package.json').version
|
|
443
|
-
if (bindingPackageVersion !== '0.
|
|
444
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
443
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
444
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
445
445
|
}
|
|
446
446
|
return binding
|
|
447
447
|
} catch (e) {
|
|
@@ -456,8 +456,8 @@ function requireNative() {
|
|
|
456
456
|
try {
|
|
457
457
|
const binding = require('rapid-fuzzy-linux-s390x-gnu')
|
|
458
458
|
const bindingPackageVersion = require('rapid-fuzzy-linux-s390x-gnu/package.json').version
|
|
459
|
-
if (bindingPackageVersion !== '0.
|
|
460
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
459
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
460
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
461
461
|
}
|
|
462
462
|
return binding
|
|
463
463
|
} catch (e) {
|
|
@@ -476,8 +476,8 @@ function requireNative() {
|
|
|
476
476
|
try {
|
|
477
477
|
const binding = require('rapid-fuzzy-openharmony-arm64')
|
|
478
478
|
const bindingPackageVersion = require('rapid-fuzzy-openharmony-arm64/package.json').version
|
|
479
|
-
if (bindingPackageVersion !== '0.
|
|
480
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
479
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
480
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
481
481
|
}
|
|
482
482
|
return binding
|
|
483
483
|
} catch (e) {
|
|
@@ -492,8 +492,8 @@ function requireNative() {
|
|
|
492
492
|
try {
|
|
493
493
|
const binding = require('rapid-fuzzy-openharmony-x64')
|
|
494
494
|
const bindingPackageVersion = require('rapid-fuzzy-openharmony-x64/package.json').version
|
|
495
|
-
if (bindingPackageVersion !== '0.
|
|
496
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
495
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
496
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
497
497
|
}
|
|
498
498
|
return binding
|
|
499
499
|
} catch (e) {
|
|
@@ -508,8 +508,8 @@ function requireNative() {
|
|
|
508
508
|
try {
|
|
509
509
|
const binding = require('rapid-fuzzy-openharmony-arm')
|
|
510
510
|
const bindingPackageVersion = require('rapid-fuzzy-openharmony-arm/package.json').version
|
|
511
|
-
if (bindingPackageVersion !== '0.
|
|
512
|
-
throw new Error(`Native binding package version mismatch, expected 0.
|
|
511
|
+
if (bindingPackageVersion !== '0.4.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
512
|
+
throw new Error(`Native binding package version mismatch, expected 0.4.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
513
513
|
}
|
|
514
514
|
return binding
|
|
515
515
|
} catch (e) {
|
|
@@ -577,6 +577,7 @@ if (!nativeBinding) {
|
|
|
577
577
|
|
|
578
578
|
module.exports = nativeBinding
|
|
579
579
|
module.exports.FuzzyIndex = nativeBinding.FuzzyIndex
|
|
580
|
+
module.exports.KeyedFuzzyIndex = nativeBinding.KeyedFuzzyIndex
|
|
580
581
|
module.exports.closest = nativeBinding.closest
|
|
581
582
|
module.exports.damerauLevenshtein = nativeBinding.damerauLevenshtein
|
|
582
583
|
module.exports.damerauLevenshteinBatch = nativeBinding.damerauLevenshteinBatch
|
package/index.mjs
CHANGED
package/objects.d.ts
CHANGED
|
@@ -14,6 +14,11 @@ export interface ObjectSearchResult<T> {
|
|
|
14
14
|
index: number;
|
|
15
15
|
score: number;
|
|
16
16
|
keyScores: Array<number>;
|
|
17
|
+
/**
|
|
18
|
+
* Indices of matched characters in the best-matching key string.
|
|
19
|
+
* Empty unless `includePositions` is set to true in ObjectSearchOptions.
|
|
20
|
+
*/
|
|
21
|
+
positions: Array<number>;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
/**
|
|
@@ -40,3 +45,60 @@ export declare function searchObjects<T>(
|
|
|
40
45
|
items: Array<T>,
|
|
41
46
|
options: ObjectSearchOptions,
|
|
42
47
|
): Array<ObjectSearchResult<T>>;
|
|
48
|
+
|
|
49
|
+
export interface ObjectIndexOptions {
|
|
50
|
+
keys: Array<string | KeyConfig>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ObjectIndexSearchOptions {
|
|
54
|
+
maxResults?: number;
|
|
55
|
+
minScore?: number;
|
|
56
|
+
isCaseSensitive?: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A persistent fuzzy search index for object collections with weighted keys.
|
|
61
|
+
*
|
|
62
|
+
* Pre-computes key texts and stores them on the Rust side for fast repeated
|
|
63
|
+
* searches. Use this when searching the same collection multiple times.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const index = new FuzzyObjectIndex(users, {
|
|
68
|
+
* keys: [{ name: 'name', weight: 2.0 }, 'email'],
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* const results = index.search('john');
|
|
72
|
+
* // results[0].item → { name: 'John Smith', ... }
|
|
73
|
+
*
|
|
74
|
+
* index.add({ name: 'New User', email: 'new@example.com' });
|
|
75
|
+
* index.destroy(); // free Rust-side memory
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare class FuzzyObjectIndex<T> {
|
|
79
|
+
constructor(items: Array<T>, options: ObjectIndexOptions);
|
|
80
|
+
|
|
81
|
+
/** Number of items in the index. */
|
|
82
|
+
get size(): number;
|
|
83
|
+
|
|
84
|
+
/** Search for objects matching the query. */
|
|
85
|
+
search(
|
|
86
|
+
query: string,
|
|
87
|
+
options?: ObjectIndexSearchOptions,
|
|
88
|
+
): Array<Omit<ObjectSearchResult<T>, 'positions'>>;
|
|
89
|
+
|
|
90
|
+
/** Find the closest matching object, or null if no match. */
|
|
91
|
+
closest(query: string, minScore?: number): T | null;
|
|
92
|
+
|
|
93
|
+
/** Add a single item to the index. */
|
|
94
|
+
add(item: T): void;
|
|
95
|
+
|
|
96
|
+
/** Add multiple items at once. */
|
|
97
|
+
addMany(items: Array<T>): void;
|
|
98
|
+
|
|
99
|
+
/** Remove the item at the given index (swap-remove semantics). */
|
|
100
|
+
remove(index: number): boolean;
|
|
101
|
+
|
|
102
|
+
/** Free all internal data. */
|
|
103
|
+
destroy(): void;
|
|
104
|
+
}
|
package/objects.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { searchKeys } = require('./index.js');
|
|
3
|
+
const { searchKeys, KeyedFuzzyIndex } = require('./index.js');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Get a nested property value from an object using a dot-separated path.
|
|
@@ -31,7 +31,7 @@ function getNestedValue(obj, path) {
|
|
|
31
31
|
* @param {number} [options.maxResults] - Maximum results to return.
|
|
32
32
|
* @param {number} [options.minScore] - Minimum score threshold.
|
|
33
33
|
* @param {boolean} [options.isCaseSensitive] - Enable case-sensitive matching.
|
|
34
|
-
* @returns {Array<{ item: T; index: number; score: number; keyScores: number[] }>}
|
|
34
|
+
* @returns {Array<{ item: T; index: number; score: number; keyScores: number[]; positions: number[] }>}
|
|
35
35
|
*/
|
|
36
36
|
function searchObjects(query, items, options) {
|
|
37
37
|
const { keys, ...searchOpts } = options;
|
|
@@ -52,7 +52,119 @@ function searchObjects(query, items, options) {
|
|
|
52
52
|
index: r.index,
|
|
53
53
|
score: r.score,
|
|
54
54
|
keyScores: r.keyScores,
|
|
55
|
+
positions: r.positions ?? [],
|
|
55
56
|
}));
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
/**
|
|
60
|
+
* A persistent fuzzy search index for object collections with weighted keys.
|
|
61
|
+
*
|
|
62
|
+
* Pre-computes key texts and stores them on the Rust side for fast repeated
|
|
63
|
+
* searches. Use this when searching the same collection multiple times.
|
|
64
|
+
*
|
|
65
|
+
* @template T
|
|
66
|
+
*/
|
|
67
|
+
class FuzzyObjectIndex {
|
|
68
|
+
/** @type {T[]} */
|
|
69
|
+
#items;
|
|
70
|
+
/** @type {KeyedFuzzyIndex} */
|
|
71
|
+
#index;
|
|
72
|
+
/** @type {Array<{ name: string; weight: number }>} */
|
|
73
|
+
#keys;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {T[]} items - Array of objects to index.
|
|
77
|
+
* @param {object} options - Index configuration.
|
|
78
|
+
* @param {Array<string | { name: string; weight?: number }>} options.keys - Keys to search.
|
|
79
|
+
*/
|
|
80
|
+
constructor(items, options) {
|
|
81
|
+
this.#keys = options.keys.map((k) =>
|
|
82
|
+
typeof k === 'string' ? { name: k, weight: 1.0 } : { name: k.name, weight: k.weight ?? 1.0 },
|
|
83
|
+
);
|
|
84
|
+
this.#items = [...items];
|
|
85
|
+
|
|
86
|
+
const keyTexts = this.#keys.map((k) => items.map((item) => getNestedValue(item, k.name)));
|
|
87
|
+
const weights = this.#keys.map((k) => k.weight);
|
|
88
|
+
this.#index = new KeyedFuzzyIndex(keyTexts, weights);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Return the number of items in the index. */
|
|
92
|
+
get size() {
|
|
93
|
+
return this.#index.size;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Search the index for objects matching the query.
|
|
98
|
+
* @param {string} query
|
|
99
|
+
* @param {object} [options]
|
|
100
|
+
* @param {number} [options.maxResults]
|
|
101
|
+
* @param {number} [options.minScore]
|
|
102
|
+
* @param {boolean} [options.isCaseSensitive]
|
|
103
|
+
* @returns {Array<{ item: T; index: number; score: number; keyScores: number[] }>}
|
|
104
|
+
*/
|
|
105
|
+
search(query, options) {
|
|
106
|
+
const results = this.#index.search(query, options);
|
|
107
|
+
return results.map((r) => ({
|
|
108
|
+
item: this.#items[r.index],
|
|
109
|
+
index: r.index,
|
|
110
|
+
score: r.score,
|
|
111
|
+
keyScores: r.keyScores,
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Find the closest matching object.
|
|
117
|
+
* @param {string} query
|
|
118
|
+
* @param {number} [minScore]
|
|
119
|
+
* @returns {T | null}
|
|
120
|
+
*/
|
|
121
|
+
closest(query, minScore) {
|
|
122
|
+
const results = this.#index.search(query, { maxResults: 1, minScore });
|
|
123
|
+
return results.length > 0 ? this.#items[results[0].index] : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Add a single item to the index.
|
|
128
|
+
* @param {T} item
|
|
129
|
+
*/
|
|
130
|
+
add(item) {
|
|
131
|
+
this.#items.push(item);
|
|
132
|
+
this.#index.add(this.#keys.map((k) => getNestedValue(item, k.name)));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Add multiple items to the index at once.
|
|
137
|
+
* @param {T[]} items
|
|
138
|
+
*/
|
|
139
|
+
addMany(items) {
|
|
140
|
+
for (const item of items) {
|
|
141
|
+
this.#items.push(item);
|
|
142
|
+
}
|
|
143
|
+
this.#index.addMany(items.map((item) => this.#keys.map((k) => getNestedValue(item, k.name))));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Remove the item at the given index.
|
|
148
|
+
* Uses swap-remove semantics for O(1) performance.
|
|
149
|
+
* @param {number} index
|
|
150
|
+
* @returns {boolean}
|
|
151
|
+
*/
|
|
152
|
+
remove(index) {
|
|
153
|
+
if (index < 0 || index >= this.#items.length) return false;
|
|
154
|
+
// Swap-remove to match Rust-side behavior
|
|
155
|
+
const lastIdx = this.#items.length - 1;
|
|
156
|
+
if (index !== lastIdx) {
|
|
157
|
+
this.#items[index] = this.#items[lastIdx];
|
|
158
|
+
}
|
|
159
|
+
this.#items.pop();
|
|
160
|
+
return this.#index.remove(index);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Free all internal data. */
|
|
164
|
+
destroy() {
|
|
165
|
+
this.#items = [];
|
|
166
|
+
this.#index.destroy();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = { searchObjects, FuzzyObjectIndex };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rapid-fuzzy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Rust-powered fuzzy search and string distance for JavaScript/TypeScript. 10-50x faster than fuse.js/leven.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "derodero24",
|
|
@@ -92,6 +92,7 @@
|
|
|
92
92
|
"test:watch": "vitest watch",
|
|
93
93
|
"bench": "vitest bench",
|
|
94
94
|
"bench:readme": "npx tsx scripts/update-bench-readme.ts",
|
|
95
|
+
"bench:charts": "npx tsx scripts/generate-bench-charts.ts",
|
|
95
96
|
"publint": "publint",
|
|
96
97
|
"bench:rust": "cargo bench -p rapid-fuzzy-bench",
|
|
97
98
|
"verify": "pnpm run check && cargo clippy -- -W clippy::all && cargo test && pnpm run build && pnpm run typecheck && pnpm test",
|
|
@@ -130,14 +131,14 @@
|
|
|
130
131
|
]
|
|
131
132
|
},
|
|
132
133
|
"optionalDependencies": {
|
|
133
|
-
"rapid-fuzzy-darwin-x64": "0.
|
|
134
|
-
"rapid-fuzzy-darwin-arm64": "0.
|
|
135
|
-
"rapid-fuzzy-linux-x64-gnu": "0.
|
|
136
|
-
"rapid-fuzzy-linux-x64-musl": "0.
|
|
137
|
-
"rapid-fuzzy-linux-arm64-gnu": "0.
|
|
138
|
-
"rapid-fuzzy-linux-arm64-musl": "0.
|
|
139
|
-
"rapid-fuzzy-win32-x64-msvc": "0.
|
|
140
|
-
"rapid-fuzzy-win32-arm64-msvc": "0.
|
|
141
|
-
"rapid-fuzzy-wasm32-wasi": "0.
|
|
134
|
+
"rapid-fuzzy-darwin-x64": "0.5.0",
|
|
135
|
+
"rapid-fuzzy-darwin-arm64": "0.5.0",
|
|
136
|
+
"rapid-fuzzy-linux-x64-gnu": "0.5.0",
|
|
137
|
+
"rapid-fuzzy-linux-x64-musl": "0.5.0",
|
|
138
|
+
"rapid-fuzzy-linux-arm64-gnu": "0.5.0",
|
|
139
|
+
"rapid-fuzzy-linux-arm64-musl": "0.5.0",
|
|
140
|
+
"rapid-fuzzy-win32-x64-msvc": "0.5.0",
|
|
141
|
+
"rapid-fuzzy-win32-arm64-msvc": "0.5.0",
|
|
142
|
+
"rapid-fuzzy-wasm32-wasi": "0.5.0"
|
|
142
143
|
}
|
|
143
144
|
}
|