rapid-fuzzy 0.4.0 → 0.6.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 CHANGED
@@ -8,18 +8,36 @@
8
8
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
9
9
  [![Node.js](https://img.shields.io/badge/node-%3E%3D20.0.0-brightgreen)](https://nodejs.org/)
10
10
 
11
- Rust-powered fuzzy search and string distance for JavaScript/TypeScript.
11
+ Blazing-fast fuzzy search for JavaScript powered by Rust, works everywhere.
12
12
 
13
- > **Status**: Early release (v0.x). API may change between minor versions.
13
+ <img src=".github/assets/demo.svg" alt="rapid-fuzzy demo fuzzy search, query syntax, FuzzyIndex, and string distance" width="580" />
14
14
 
15
15
  ## Features
16
16
 
17
- - **Fast**: Up to 40x faster than fuse.js for large datasets (Rust + napi-rs)
17
+ - **Fast**: Up to 7,000x faster than fuse.js with FuzzyIndex (Rust + napi-rs)
18
18
  - **Universal**: Works in Node.js (native), browsers (WASM), Deno, and Bun
19
19
  - **Zero JS dependencies**: Pure Rust core with napi-rs bindings
20
20
  - **Type-safe**: Full TypeScript support with auto-generated type definitions
21
21
  - **Drop-in**: API compatible with popular fuzzy search libraries
22
22
 
23
+ ## Quick Start
24
+
25
+ ```typescript
26
+ import { search } from 'rapid-fuzzy';
27
+
28
+ const results = search('typscript', ['TypeScript', 'JavaScript', 'Python']);
29
+ // → [{ item: 'TypeScript', score: 0.85, index: 0 }, ...]
30
+ ```
31
+
32
+ For repeated searches, use `FuzzyIndex` for up to 182x faster lookups:
33
+
34
+ ```typescript
35
+ import { FuzzyIndex } from 'rapid-fuzzy';
36
+
37
+ const index = new FuzzyIndex(['TypeScript', 'JavaScript', 'Python', ...]);
38
+ index.search('typscript'); // sub-millisecond with incremental cache
39
+ ```
40
+
23
41
  ## Installation
24
42
 
25
43
  ```bash
@@ -33,17 +51,7 @@ pnpm add rapid-fuzzy
33
51
  - **Node.js** (>=20): Uses native bindings via napi-rs for best performance.
34
52
  - **Browser / Deno / Bun**: Falls back to a WASM build automatically.
35
53
 
36
- ## Usage
37
-
38
- ### String Distance
39
-
40
- ```typescript
41
- import { levenshtein, jaroWinkler, sorensenDice } from 'rapid-fuzzy';
42
-
43
- levenshtein('kitten', 'sitting'); // 3
44
- jaroWinkler('MARTHA', 'MARHTA'); // 0.961
45
- sorensenDice('night', 'nacht'); // 0.25
46
- ```
54
+ > **Note**: rapid-fuzzy is pre-1.0 — the API is stable but minor versions may include additions.
47
55
 
48
56
  ### Fuzzy Search
49
57
 
@@ -81,6 +89,32 @@ closest('xyz', items, 0.5);
81
89
  // → null
82
90
  ```
83
91
 
92
+ ### String Distance
93
+
94
+ ```typescript
95
+ import { levenshtein, jaroWinkler, sorensenDice } from 'rapid-fuzzy';
96
+
97
+ levenshtein('kitten', 'sitting'); // 3
98
+ jaroWinkler('MARTHA', 'MARHTA'); // 0.961
99
+ sorensenDice('night', 'nacht'); // 0.25
100
+ ```
101
+
102
+ ### Query Syntax
103
+
104
+ Queries support extended syntax powered by the [nucleo](https://github.com/helix-editor/nucleo) pattern parser:
105
+
106
+ | Pattern | Match type | Example |
107
+ |---|---|---|
108
+ | `foo bar` | AND (order-independent) | `john smith` matches "Smith, John" |
109
+ | `!term` | Exclude | `apple !pie` excludes "apple pie" |
110
+ | `^term` | Starts with | `^app` matches "apple" but not "pineapple" |
111
+ | `term$` | Ends with | `pie$` matches "apple pie" |
112
+ | `'term` | Exact substring | `'pie` matches "pie" literally |
113
+
114
+ Diacritics are handled automatically — `cafe` matches `café`, `uber` matches `über`, and `naive` matches `naïve` with no configuration needed.
115
+
116
+ > **Note**: These patterns apply to all search functions: `search()`, `closest()`, `FuzzyIndex.search()`, `FuzzyObjectIndex.search()`, and `searchObjects()`. They do **not** apply to distance functions (`levenshtein`, `jaro`, etc.).
117
+
84
118
  ### Object Search
85
119
 
86
120
  Search across object properties with weighted keys — a drop-in replacement for fuse.js's `keys` option:
@@ -112,6 +146,38 @@ searchObjects('john', users, {
112
146
  searchObjects('new york', items, { keys: ['address.city'] });
113
147
  ```
114
148
 
149
+ ### Persistent Index
150
+
151
+ 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:
152
+
153
+ ```typescript
154
+ import { FuzzyIndex, FuzzyObjectIndex } from 'rapid-fuzzy';
155
+
156
+ // String search index — up to 182x faster than standalone search()
157
+ const index = new FuzzyIndex(['TypeScript', 'JavaScript', 'Python', ...]);
158
+
159
+ index.search('typscript', { maxResults: 5 });
160
+ index.closest('tsc');
161
+
162
+ // Mutate the index without rebuilding
163
+ index.add('Rust');
164
+ index.remove(2); // swap-remove by index
165
+
166
+ // Object search index — keeps objects on the JS side, keys on the Rust side
167
+ const userIndex = new FuzzyObjectIndex(users, {
168
+ keys: [
169
+ { name: 'name', weight: 2.0 },
170
+ { name: 'email', weight: 1.0 },
171
+ ],
172
+ });
173
+
174
+ userIndex.search('john', { maxResults: 10 });
175
+
176
+ // Free Rust-side memory when done
177
+ index.destroy();
178
+ userIndex.destroy();
179
+ ```
180
+
115
181
  ### Match Highlighting
116
182
 
117
183
  Convert matched positions into highlighted markup for UI rendering:
@@ -183,44 +249,84 @@ levenshteinMany('kitten', ['sitting', 'kittens', 'kitchen']);
183
249
 
184
250
  > **Tip**: Prefer batch/many variants over calling single-pair functions in a loop — they are significantly faster for multiple comparisons.
185
251
 
252
+ ## Choosing an Algorithm
253
+
254
+ | Use case | Recommended | Why |
255
+ |---|---|---|
256
+ | Typo detection / spell check | `levenshtein`, `damerauLevenshtein` | Counts edits; Damerau adds transposition support |
257
+ | Name / address matching | `jaroWinkler`, `tokenSortRatio` | Prefix-weighted or order-independent matching |
258
+ | Document / text similarity | `sorensenDice` | Bigram-based; handles longer text well |
259
+ | Normalized comparison (0–1) | `normalizedLevenshtein` | Length-independent similarity score |
260
+ | Reordered words / messy data | `tokenSortRatio`, `tokenSetRatio` | Handles word order differences and extra tokens |
261
+ | Substring / abbreviation matching | `partialRatio` | Finds best partial match within longer strings |
262
+ | Best-effort similarity | `weightedRatio` | Picks the best score across all methods automatically |
263
+ | Interactive fuzzy search | `search`, `closest` | Nucleo algorithm (same as Helix editor) |
264
+ | Repeated search on same data | `FuzzyIndex`, `FuzzyObjectIndex` | Persistent Rust-side index with incremental cache, up to 182x faster |
265
+
266
+ **Return types:**
267
+
268
+ - `levenshtein`, `damerauLevenshtein` → integer (edit count)
269
+ - `jaro`, `jaroWinkler`, `sorensenDice`, `normalizedLevenshtein` → float between 0.0 (no match) and 1.0 (identical)
270
+ - `tokenSortRatio`, `tokenSetRatio`, `partialRatio`, `weightedRatio` → float between 0.0 and 1.0
271
+ - `search` → array of `{ item, score, index, positions }` sorted by relevance (score: 0.0–1.0)
272
+
186
273
  ## Benchmarks
187
274
 
188
275
  Measured on Apple M-series with Node.js v22 using [Vitest bench](https://vitest.dev/guide/features.html#benchmarking). Each benchmark processes 6 realistic string pairs of varying length and similarity.
189
276
 
190
277
  ### Distance Functions
191
278
 
279
+ <img src=".github/assets/bench-distance.svg" alt="Distance function performance chart" width="680" />
280
+
281
+ <details>
282
+ <summary>Raw numbers</summary>
283
+
192
284
  | Function | rapid-fuzzy | fastest-levenshtein | leven | string-similarity |
193
285
  |---|---:|---:|---:|---:|
194
- | Levenshtein | 193,593 ops/s | **774,820 ops/s** | 204,047 ops/s | — |
195
- | Normalized Levenshtein | **136,854 ops/s** | — | — | — |
196
- | Sorensen-Dice | **144,698 ops/s** | — | — | 84,108 ops/s |
197
- | Jaro-Winkler | **291,673 ops/s** | — | — | — |
198
- | Damerau-Levenshtein | **72,238 ops/s** | — | — | — |
286
+ | Levenshtein | 562,063 ops/s | **794,298 ops/s** | 228,688 ops/s | — |
287
+ | Normalized Levenshtein | **546,107 ops/s** | — | — | — |
288
+ | Sorensen-Dice | **147,850 ops/s** | — | — | 84,308 ops/s |
289
+ | Jaro-Winkler | **293,403 ops/s** | — | — | — |
290
+ | Damerau-Levenshtein | **116,153 ops/s** | — | — | — |
291
+
292
+ </details>
199
293
 
200
- > **Note**: For single-pair Levenshtein distance, fastest-levenshtein is faster due to its highly optimized pure-JS implementation that avoids FFI overhead. rapid-fuzzy provides broader algorithm coverage and excels in batch / search scenarios.
294
+ > **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.5x faster** than leven, and provides broader algorithm coverage plus batch / search scenarios.
201
295
 
202
296
  ### Search Performance
203
297
 
204
- | Dataset size | rapid-fuzzy | fuse.js | fuzzysort |
205
- |---|---:|---:|---:|
206
- | Small (20 items) | 179,222 ops/s | 109,059 ops/s | **2,501,773 ops/s** |
207
- | Medium (1K items) | 6,614 ops/s | 381 ops/s | **63,032 ops/s** |
208
- | Large (10K items) | 794 ops/s | 20 ops/s | **28,616 ops/s** |
298
+ <img src=".github/assets/bench-search.svg" alt="Search performance chart — rapid-fuzzy vs fuse.js vs fuzzysort vs uFuzzy" width="680" />
299
+
300
+ > Both `rapid-fuzzy` columns below show the same library: standalone `search()` vs `FuzzyIndex` (indexed mode for repeated searches).
301
+
302
+ <details>
303
+ <summary>Raw numbers</summary>
304
+
305
+ | Dataset size | rapid-fuzzy | rapid-fuzzy (indexed) | fuse.js | fuzzysort | uFuzzy |
306
+ |---|---:|---:|---:|---:|---:|
307
+ | Small (20 items) | 303,982 ops/s | 405,604 ops/s | 105,568 ops/s | **2,606,394 ops/s** | 923,069 ops/s |
308
+ | Medium (1K items) | 6,787 ops/s | **80,579 ops/s** | 367 ops/s | 64,372 ops/s | 28,953 ops/s |
309
+ | Large (10K items) | 751 ops/s | **136,528 ops/s** | 19 ops/s | 26,112 ops/s | 6,393 ops/s |
310
+ | XL (50K items) | — | **31,903 ops/s** | — | 5,916 ops/s | 1,292 ops/s |
311
+
312
+ </details>
209
313
 
210
314
  ### Closest Match (Levenshtein-based)
211
315
 
212
- | Dataset size | rapid-fuzzy | fastest-levenshtein |
213
- |---|---:|---:|
214
- | Medium (1K items) | 8,416 ops/s | **8,762 ops/s** |
215
- | Large (10K items) | **905 ops/s** | 662 ops/s |
316
+ | Dataset size | rapid-fuzzy | rapid-fuzzy (indexed) | fastest-levenshtein |
317
+ |---|---:|---:|---:|
318
+ | Medium (1K items) | 8,611 ops/s | **989,095 ops/s** | 6,797 ops/s |
319
+ | Large (10K items) | 924 ops/s | **156,014 ops/s** | 658 ops/s |
216
320
 
217
- > rapid-fuzzy is up to **1.4x faster** than fastest-levenshtein for closest-match lookups on large datasets.
321
+ > In indexed mode (`FuzzyIndex`), rapid-fuzzy is up to **237x faster** than fastest-levenshtein for closest-match lookups.
218
322
 
219
323
  ### Why these numbers matter
220
324
 
221
- - **vs fuse.js**: rapid-fuzzy is **17x faster** on medium datasets and **40x faster** on large datasets for fuzzy search.
222
- - **vs fastest-levenshtein**: rapid-fuzzy wins on closest-match at scale where batch FFI overhead is amortized.
223
- - **fuzzysort** uses a different (substring-based) matching algorithm that is extremely fast but produces different ranking results. Choose based on your matching needs.
325
+ - **vs fuse.js**: `FuzzyIndex` is **219x faster** on medium datasets and **6,869x faster** on large datasets. Even standalone `search()` is 18x / 40x faster.
326
+ - **Indexed mode**: `FuzzyIndex` keeps data on the Rust side with an incremental search cache, delivering sub-millisecond autocomplete. On large datasets this is **182x faster** than standalone `search()`.
327
+ - **vs fuzzysort**: `FuzzyIndex` now **outperforms fuzzysort** on medium-and-above datasets 1.25x faster at 1K, 5.2x at 10K, and 5.4x at 50K.
328
+ - **vs uFuzzy**: `FuzzyIndex` is **2.8x faster** at medium and **21x faster** at large datasets.
329
+ - **vs fastest-levenshtein**: With `FuzzyIndex`, closest-match is **145x faster** at 1K and **237x faster** at 10K.
224
330
 
225
331
  Run benchmarks yourself:
226
332
 
@@ -229,48 +335,34 @@ pnpm run bench # JavaScript benchmarks
229
335
  cargo bench # Rust internal benchmarks
230
336
  ```
231
337
 
232
- ## Choosing an Algorithm
233
-
234
- | Use case | Recommended | Why |
235
- |---|---|---|
236
- | Typo detection / spell check | `levenshtein`, `damerauLevenshtein` | Counts edits; Damerau adds transposition support |
237
- | Name / address matching | `jaroWinkler`, `tokenSortRatio` | Prefix-weighted or order-independent matching |
238
- | Document / text similarity | `sorensenDice` | Bigram-based; handles longer text well |
239
- | Normalized comparison (0–1) | `normalizedLevenshtein` | Length-independent similarity score |
240
- | Reordered words / messy data | `tokenSortRatio`, `tokenSetRatio` | Handles word order differences and extra tokens |
241
- | Substring / abbreviation matching | `partialRatio` | Finds best partial match within longer strings |
242
- | Best-effort similarity | `weightedRatio` | Picks the best score across all methods automatically |
243
- | Interactive fuzzy search | `search`, `closest` | Nucleo algorithm (same as Helix editor) |
244
-
245
- **Return types:**
246
-
247
- - `levenshtein`, `damerauLevenshtein` → integer (edit count)
248
- - `jaro`, `jaroWinkler`, `sorensenDice`, `normalizedLevenshtein` → float between 0.0 (no match) and 1.0 (identical)
249
- - `tokenSortRatio`, `tokenSetRatio`, `partialRatio`, `weightedRatio` → float between 0.0 and 1.0
250
- - `search` → array of `{ item, score, index, positions }` sorted by relevance (score: 0.0–1.0)
251
-
252
338
  ## Why rapid-fuzzy?
253
339
 
254
- | | rapid-fuzzy | fuse.js | fastest-levenshtein | fuzzysort |
255
- |---|---|---|---|---|
256
- | **Algorithms** | Levenshtein, Jaro-Winkler, Sorensen-Dice, Damerau-Levenshtein, token sort/set, partial ratio, fuzzy search | Bitap-based fuzzy | Levenshtein only | Substring fuzzy |
257
- | **Runtime** | Rust (native + WASM) | Pure JS | Pure JS | Pure JS |
258
- | **Object search** | Yes (searchObjects with weighted keys) | Yes (keys option) | No | Yes (keys) |
259
- | **Score threshold** | Yes (minScore) | Yes (threshold) | No | Yes (threshold) |
260
- | **Match positions** | Yes (includePositions) | Yes | No | Yes |
261
- | **Highlight utility** | Yes (highlight, highlightRanges) | No (manual) | No | Yes (highlight) |
262
- | **Batch API** | Yes | No | No | No |
263
- | **Node.js native** | Yes (napi-rs) | No | No | No |
264
- | **Browser support** | Yes (WASM) | Yes | Yes | Yes |
265
- | **TypeScript** | Full (auto-generated) | Full | Yes | Yes |
340
+ | | rapid-fuzzy | fuse.js | fastest-levenshtein | fuzzysort | uFuzzy |
341
+ |---|:---:|:---:|:---:|:---:|:---:|
342
+ | **Algorithms** | 9 (Levenshtein, Jaro, Dice, …) | Bitap | Levenshtein | Substring | Regex-based |
343
+ | **Runtime** | Rust native + WASM | Pure JS | Pure JS | Pure JS | Pure JS |
344
+ | **Object search** | weighted keys | | | | |
345
+ | **Persistent index** | FuzzyIndex / FuzzyObjectIndex | | | prepared targets | — |
346
+ | **Query syntax** | exclude, prefix, suffix, exact | extended search | | | partial (`-` only) |
347
+ | **Out-of-order matching** | automatic | | | | with option |
348
+ | **Diacritics** | automatic | option | | auto | ✅ `latinize()` |
349
+ | **Score threshold** | | | | | |
350
+ | **Match positions** | | | | | |
351
+ | **Highlight utility** | | | | | |
352
+ | **Batch API** | ✅ | — | — | — | — |
353
+ | **Node.js native** | ✅ napi-rs | — | — | — | — |
354
+ | **Browser** | ✅ WASM | ✅ | ✅ | ✅ | ✅ |
355
+ | **TypeScript** | ✅ full | ✅ full | ✅ | ✅ | ✅ |
266
356
 
267
357
  ## Migration Guides
268
358
 
269
359
  Switching from another library? These guides provide API mapping tables, code examples, and performance comparisons:
270
360
 
271
361
  - [**From string-similarity**](docs/migration/from-string-similarity.md) — Same Dice coefficient algorithm, now maintained and faster
272
- - [**From fuse.js**](docs/migration/from-fuse-js.md) — 17–40x faster fuzzy search with a simpler API
362
+ - [**From fuse.js**](docs/migration/from-fuse-js.md) — Up to 7,000x faster fuzzy search with FuzzyIndex
273
363
  - [**From leven / fastest-levenshtein**](docs/migration/from-leven.md) — Multi-algorithm upgrade with batch APIs
364
+ - [**From fuzzysort**](docs/migration/from-fuzzysort.md) — Richer matching with query syntax and 9 distance algorithms
365
+ - [**From uFuzzy**](docs/migration/from-ufuzzy.md) — Weighted object search, batch APIs, and persistent indexes
274
366
 
275
367
  ## License
276
368
 
package/highlight.mjs CHANGED
@@ -1,57 +1,4 @@
1
- // ESM version of highlight utilities for browser bundlers and ESM-only environments.
2
- // Keep in sync with highlight.js (CJS version).
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-exportsingle 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 { KeyConfig, ObjectSearchOptions, ObjectSearchResult } from './objects';
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.3.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.3.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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
@@ -42,4 +42,4 @@ export const {
42
42
  highlightRanges,
43
43
  } = { ...binding, ...require('./highlight.js') };
44
44
 
45
- export const { searchObjects } = require('./objects.js');
45
+ export const { searchObjects, FuzzyObjectIndex } = require('./objects.js');
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
- module.exports = { searchObjects };
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.4.0",
3
+ "version": "0.6.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",
@@ -106,6 +107,7 @@
106
107
  "@commitlint/config-conventional": "^20.4.4",
107
108
  "@emnapi/core": "^1.9.0",
108
109
  "@emnapi/runtime": "^1.9.0",
110
+ "@leeoniya/ufuzzy": "^1.0.19",
109
111
  "@napi-rs/cli": "^3.5.1",
110
112
  "@napi-rs/wasm-runtime": "^1.1.1",
111
113
  "@playwright/test": "^1.58.2",
@@ -130,14 +132,14 @@
130
132
  ]
131
133
  },
132
134
  "optionalDependencies": {
133
- "rapid-fuzzy-darwin-x64": "0.4.0",
134
- "rapid-fuzzy-darwin-arm64": "0.4.0",
135
- "rapid-fuzzy-linux-x64-gnu": "0.4.0",
136
- "rapid-fuzzy-linux-x64-musl": "0.4.0",
137
- "rapid-fuzzy-linux-arm64-gnu": "0.4.0",
138
- "rapid-fuzzy-linux-arm64-musl": "0.4.0",
139
- "rapid-fuzzy-win32-x64-msvc": "0.4.0",
140
- "rapid-fuzzy-win32-arm64-msvc": "0.4.0",
141
- "rapid-fuzzy-wasm32-wasi": "0.4.0"
135
+ "rapid-fuzzy-darwin-x64": "0.6.0",
136
+ "rapid-fuzzy-darwin-arm64": "0.6.0",
137
+ "rapid-fuzzy-linux-x64-gnu": "0.6.0",
138
+ "rapid-fuzzy-linux-x64-musl": "0.6.0",
139
+ "rapid-fuzzy-linux-arm64-gnu": "0.6.0",
140
+ "rapid-fuzzy-linux-arm64-musl": "0.6.0",
141
+ "rapid-fuzzy-win32-x64-msvc": "0.6.0",
142
+ "rapid-fuzzy-win32-arm64-msvc": "0.6.0",
143
+ "rapid-fuzzy-wasm32-wasi": "0.6.0"
142
144
  }
143
145
  }