rapid-fuzzy 0.2.0 → 0.4.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
@@ -1,6 +1,7 @@
1
1
  # rapid-fuzzy
2
2
 
3
3
  [![CI](https://github.com/derodero24/rapid-fuzzy/actions/workflows/ci.yml/badge.svg)](https://github.com/derodero24/rapid-fuzzy/actions/workflows/ci.yml)
4
+ [![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/derodero24/rapid-fuzzy)
4
5
  [![codecov](https://codecov.io/gh/derodero24/rapid-fuzzy/branch/develop/graph/badge.svg)](https://codecov.io/gh/derodero24/rapid-fuzzy)
5
6
  [![npm version](https://img.shields.io/npm/v/rapid-fuzzy)](https://www.npmjs.com/package/rapid-fuzzy)
6
7
  [![npm downloads](https://img.shields.io/npm/dm/rapid-fuzzy)](https://www.npmjs.com/package/rapid-fuzzy)
@@ -56,13 +57,110 @@ const results = search('typscript', [
56
57
  'Python',
57
58
  'TypeSpec',
58
59
  ]);
59
- // → [{ item: 'TypeScript', score: 0.85, index: 0 }, ...]
60
+ // → [{ item: 'TypeScript', score: 0.85, index: 0, positions: [] }, ...]
61
+
62
+ // With options: filter by minimum score and limit results
63
+ search('app', items, { maxResults: 5, minScore: 0.3 });
64
+
65
+ // Backward compatible: pass a number for maxResults
66
+ search('app', items, 5);
67
+
68
+ // Get matched character positions for highlighting
69
+ const [match] = search('hlo', ['hello world'], { includePositions: true });
70
+ // → { item: 'hello world', score: 0.75, index: 0, positions: [0, 2, 4] }
71
+
72
+ // Case-sensitive matching (default: smart case)
73
+ search('Type', items, { isCaseSensitive: true });
60
74
 
61
75
  // Find the single best match
62
76
  closest('tsc', ['TypeScript', 'JavaScript', 'Python']);
63
77
  // → 'TypeScript'
78
+
79
+ // With minimum score threshold (returns null if no match is good enough)
80
+ closest('xyz', items, 0.5);
81
+ // → null
82
+ ```
83
+
84
+ ### Object Search
85
+
86
+ Search across object properties with weighted keys — a drop-in replacement for fuse.js's `keys` option:
87
+
88
+ ```typescript
89
+ import { searchObjects } from 'rapid-fuzzy';
90
+
91
+ const users = [
92
+ { name: 'John Smith', email: 'john@example.com' },
93
+ { name: 'Jane Doe', email: 'jane@example.com' },
94
+ { name: 'Bob Johnson', email: 'bob@test.com' },
95
+ ];
96
+
97
+ // Search across multiple keys
98
+ const results = searchObjects('john', users, {
99
+ keys: ['name', 'email'],
100
+ });
101
+ // → [{ item: { name: 'John Smith', ... }, score: 0.95, keyScores: [0.98, 0.85], index: 0 }]
102
+
103
+ // Weighted keys — prioritize name matches over email
104
+ searchObjects('john', users, {
105
+ keys: [
106
+ { name: 'name', weight: 2.0 },
107
+ { name: 'email', weight: 1.0 },
108
+ ],
109
+ });
110
+
111
+ // Nested key paths
112
+ searchObjects('new york', items, { keys: ['address.city'] });
113
+ ```
114
+
115
+ ### Match Highlighting
116
+
117
+ Convert matched positions into highlighted markup for UI rendering:
118
+
119
+ ```typescript
120
+ import { search, highlight, highlightRanges } from 'rapid-fuzzy';
121
+
122
+ const results = search('fzy', ['fuzzy'], { includePositions: true });
123
+ const { item, positions } = results[0];
124
+
125
+ // String markers
126
+ highlight(item, positions, '<b>', '</b>');
127
+ // → '<b>f</b>u<b>zy</b>'
128
+
129
+ // Callback (React, JSX, custom DOM)
130
+ highlight(item, positions, (matched) => `<mark>${matched}</mark>`);
131
+
132
+ // Raw ranges for custom rendering
133
+ highlightRanges(item, positions);
134
+ // → [{ start: 0, end: 1, matched: true }, { start: 1, end: 2, matched: false }, ...]
135
+ ```
136
+
137
+ ### Token-Based Matching
138
+
139
+ Order-independent and partial string matching, inspired by Python's [RapidFuzz](https://github.com/rapidfuzz/RapidFuzz):
140
+
141
+ ```typescript
142
+ import {
143
+ tokenSortRatio,
144
+ tokenSetRatio,
145
+ partialRatio,
146
+ weightedRatio,
147
+ } from 'rapid-fuzzy';
148
+
149
+ // Token Sort: order-independent comparison
150
+ tokenSortRatio('New York Mets', 'Mets New York'); // 1.0
151
+
152
+ // Token Set: handles extra/missing tokens
153
+ tokenSetRatio('Great Gatsby', 'The Great Gatsby by Fitzgerald'); // ~0.85
154
+
155
+ // Partial: best substring match
156
+ partialRatio('hello', 'hello world'); // 1.0
157
+
158
+ // Weighted: best score across all methods
159
+ weightedRatio('John Smith', 'Smith, John'); // 1.0
64
160
  ```
65
161
 
162
+ All token-based functions include `Batch` and `Many` variants (e.g., `tokenSortRatioBatch`, `tokenSortRatioMany`).
163
+
66
164
  ### Batch Operations
67
165
 
68
166
  All distance functions have `Batch` and `Many` variants that amortize FFI overhead:
@@ -93,11 +191,11 @@ Measured on Apple M-series with Node.js v22 using [Vitest bench](https://vitest.
93
191
 
94
192
  | Function | rapid-fuzzy | fastest-levenshtein | leven | string-similarity |
95
193
  |---|---:|---:|---:|---:|
96
- | Levenshtein | 67,346 ops/s | **243,026 ops/s** | 51,789 ops/s | — |
97
- | Normalized Levenshtein | **64,592 ops/s** | — | — | — |
98
- | Sorensen-Dice | **61,050 ops/s** | — | — | 40,241 ops/s |
99
- | Jaro-Winkler | **198,140 ops/s** | — | — | — |
100
- | Damerau-Levenshtein | **58,888 ops/s** | — | — | — |
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** | — | — | — |
101
199
 
102
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.
103
201
 
@@ -105,23 +203,23 @@ Measured on Apple M-series with Node.js v22 using [Vitest bench](https://vitest.
105
203
 
106
204
  | Dataset size | rapid-fuzzy | fuse.js | fuzzysort |
107
205
  |---|---:|---:|---:|
108
- | 20 items | 171,967 ops/s | 121,978 ops/s | **2,537,323 ops/s** |
109
- | 1,000 items | 4,941 ops/s | 376 ops/s | **55,388 ops/s** |
110
- | 10,000 items | 588 ops/s | 14 ops/s | **15,005 ops/s** |
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** |
111
209
 
112
210
  ### Closest Match (Levenshtein-based)
113
211
 
114
212
  | Dataset size | rapid-fuzzy | fastest-levenshtein |
115
213
  |---|---:|---:|
116
- | 1,000 items | **5,912 ops/s** | 3,974 ops/s |
117
- | 10,000 items | **387 ops/s** | 126 ops/s |
214
+ | Medium (1K items) | 8,416 ops/s | **8,762 ops/s** |
215
+ | Large (10K items) | **905 ops/s** | 662 ops/s |
118
216
 
119
- > rapid-fuzzy is up to **3x faster** than fastest-levenshtein for closest-match lookups on large datasets.
217
+ > rapid-fuzzy is up to **1.4x faster** than fastest-levenshtein for closest-match lookups on large datasets.
120
218
 
121
219
  ### Why these numbers matter
122
220
 
123
- - **vs fuse.js**: rapid-fuzzy is **13x faster** on medium datasets and **41x faster** on large datasets for fuzzy search.
124
- - **vs fastest-levenshtein**: rapid-fuzzy wins on closest-match (1.5–3x faster) where batch FFI overhead is amortized.
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.
125
223
  - **fuzzysort** uses a different (substring-based) matching algorithm that is extremely fast but produces different ranking results. Choose based on your matching needs.
126
224
 
127
225
  Run benchmarks yourself:
@@ -136,28 +234,44 @@ cargo bench # Rust internal benchmarks
136
234
  | Use case | Recommended | Why |
137
235
  |---|---|---|
138
236
  | Typo detection / spell check | `levenshtein`, `damerauLevenshtein` | Counts edits; Damerau adds transposition support |
139
- | Name / address matching | `jaroWinkler` | Prefix-weighted similarity for short strings |
237
+ | Name / address matching | `jaroWinkler`, `tokenSortRatio` | Prefix-weighted or order-independent matching |
140
238
  | Document / text similarity | `sorensenDice` | Bigram-based; handles longer text well |
141
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 |
142
243
  | Interactive fuzzy search | `search`, `closest` | Nucleo algorithm (same as Helix editor) |
143
244
 
144
245
  **Return types:**
145
246
 
146
247
  - `levenshtein`, `damerauLevenshtein` → integer (edit count)
147
248
  - `jaro`, `jaroWinkler`, `sorensenDice`, `normalizedLevenshtein` → float between 0.0 (no match) and 1.0 (identical)
148
- - `search` → array of `{ item, score, index }` sorted by relevance (score: 0.01.0)
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)
149
251
 
150
252
  ## Why rapid-fuzzy?
151
253
 
152
254
  | | rapid-fuzzy | fuse.js | fastest-levenshtein | fuzzysort |
153
255
  |---|---|---|---|---|
154
- | **Algorithms** | Levenshtein, Jaro-Winkler, Sorensen-Dice, Damerau-Levenshtein, fuzzy search | Bitap-based fuzzy | Levenshtein only | Substring fuzzy |
256
+ | **Algorithms** | Levenshtein, Jaro-Winkler, Sorensen-Dice, Damerau-Levenshtein, token sort/set, partial ratio, fuzzy search | Bitap-based fuzzy | Levenshtein only | Substring fuzzy |
155
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) |
156
262
  | **Batch API** | Yes | No | No | No |
157
263
  | **Node.js native** | Yes (napi-rs) | No | No | No |
158
264
  | **Browser support** | Yes (WASM) | Yes | Yes | Yes |
159
265
  | **TypeScript** | Full (auto-generated) | Full | Yes | Yes |
160
266
 
267
+ ## Migration Guides
268
+
269
+ Switching from another library? These guides provide API mapping tables, code examples, and performance comparisons:
270
+
271
+ - [**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
273
+ - [**From leven / fastest-levenshtein**](docs/migration/from-leven.md) — Multi-algorithm upgrade with batch APIs
274
+
161
275
  ## License
162
276
 
163
277
  MIT
package/browser.js CHANGED
@@ -1 +1,4 @@
1
1
  export * from 'rapid-fuzzy-wasm32-wasi'
2
+
3
+ // --- JS utilities (appended by scripts/patch-binding.js) ---
4
+ export { highlight, highlightRanges } from './highlight.mjs';
package/highlight.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ /** A range within a string, indicating whether it was matched. */
2
+ export interface HighlightRange {
3
+ /** Start index (inclusive). */
4
+ start: number;
5
+ /** End index (exclusive). */
6
+ end: number;
7
+ /** Whether this range was part of the match. */
8
+ matched: boolean;
9
+ }
10
+
11
+ /**
12
+ * Highlight matched characters in a search result string.
13
+ *
14
+ * Use with `SearchResult.positions` from a search with `includePositions: true`.
15
+ *
16
+ * @example String markers
17
+ * ```typescript
18
+ * const results = search('fzy', ['fuzzy'], { includePositions: true });
19
+ * highlight(results[0].item, results[0].positions, '<b>', '</b>');
20
+ * // → '<b>f</b>u<b>zy</b>'
21
+ * ```
22
+ *
23
+ * @example Callback (React, custom DOM, etc.)
24
+ * ```typescript
25
+ * highlight(result.item, result.positions, (matched) => `<mark>${matched}</mark>`);
26
+ * ```
27
+ */
28
+ export declare function highlight(
29
+ item: string,
30
+ positions: Array<number>,
31
+ open: string,
32
+ close: string,
33
+ ): string;
34
+ export declare function highlight(
35
+ item: string,
36
+ positions: Array<number>,
37
+ callback: (matched: string) => string,
38
+ ): string;
39
+
40
+ /**
41
+ * Convert matched positions into an array of ranges for custom rendering.
42
+ *
43
+ * Each range indicates a contiguous segment of the string and whether it was
44
+ * part of the match. Useful for building custom highlight components.
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const ranges = highlightRanges(result.item, result.positions);
49
+ * // → [{ start: 0, end: 1, matched: true }, { start: 1, end: 2, matched: false }, ...]
50
+ * ```
51
+ */
52
+ export declare function highlightRanges(
53
+ item: string,
54
+ positions: Array<number>,
55
+ ): Array<HighlightRange>;
package/highlight.js ADDED
@@ -0,0 +1,58 @@
1
+ // Pure JS highlight utilities — works in both Node.js and browser environments.
2
+ // This file is manually maintained (not auto-generated by napi-rs).
3
+
4
+ 'use strict';
5
+
6
+ /**
7
+ * @param {string} item
8
+ * @param {number[]} positions
9
+ * @returns {Array<{start: number, end: number, matched: boolean}>}
10
+ */
11
+ 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
+ * @param {string} item
33
+ * @param {number[]} positions
34
+ * @param {string | ((substring: string) => string)} openOrCallback
35
+ * @param {string} [close]
36
+ * @returns {string}
37
+ */
38
+ function highlight(item, positions, openOrCallback, close) {
39
+ if (!positions || positions.length === 0) return item;
40
+
41
+ const ranges = highlightRanges(item, positions);
42
+ const useCallback = typeof openOrCallback === 'function';
43
+
44
+ const parts = [];
45
+ for (const range of ranges) {
46
+ const segment = item.slice(range.start, range.end);
47
+ if (range.matched) {
48
+ parts.push(useCallback ? openOrCallback(segment) : openOrCallback + segment + (close ?? ''));
49
+ } else {
50
+ parts.push(segment);
51
+ }
52
+ }
53
+
54
+ return parts.join('');
55
+ }
56
+
57
+ module.exports.highlight = highlight;
58
+ module.exports.highlightRanges = highlightRanges;
package/highlight.mjs ADDED
@@ -0,0 +1,57 @@
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
+ }
package/index.d.mts CHANGED
@@ -1 +1,5 @@
1
1
  export * from './index.d.ts';
2
+ export { highlight, highlightRanges } from './highlight.d.ts';
3
+ export type { HighlightRange } from './highlight.d.ts';
4
+ export { searchObjects } from './objects';
5
+ export type { KeyConfig, ObjectSearchOptions, ObjectSearchResult } from './objects';
package/index.d.ts CHANGED
@@ -1,11 +1,53 @@
1
1
  /* auto-generated by NAPI-RS */
2
2
  /* eslint-disable */
3
+ /**
4
+ * A persistent fuzzy search index backed by Rust-side data.
5
+ *
6
+ * Holds items in memory on the Rust side, avoiding repeated FFI overhead
7
+ * for applications that search the same dataset multiple times.
8
+ * Memory is freed when the JavaScript garbage collector collects the instance
9
+ * or when `destroy()` is called explicitly.
10
+ */
11
+ export declare class FuzzyIndex {
12
+ /** Create a new FuzzyIndex from an array of strings. */
13
+ constructor(items: Array<string>)
14
+ /** Return the number of items in the index. */
15
+ get size(): number
16
+ /**
17
+ * Search the index for items matching the query.
18
+ *
19
+ * Returns matches sorted by score (best match first).
20
+ * Scores are normalized to a 0.0-1.0 range where 1.0 is a perfect match.
21
+ */
22
+ search(query: string, options?: number | SearchOptions | undefined | null): Array<SearchResult>
23
+ /**
24
+ * Find the closest matching string in the index.
25
+ *
26
+ * Returns the best match, or null if no match is found.
27
+ * If minScore is provided, returns null when the best match scores below the threshold.
28
+ */
29
+ closest(query: string, minScore?: number | undefined | null): string | null
30
+ /** Add a single item to the index. */
31
+ add(item: string): void
32
+ /** Add multiple items to the index at once. */
33
+ addMany(items: Array<string>): void
34
+ /**
35
+ * Remove the item at the given index.
36
+ *
37
+ * Returns false if the index is out of bounds.
38
+ */
39
+ remove(index: number): boolean
40
+ /** Free the internal data. After calling this, the index is empty. */
41
+ destroy(): void
42
+ }
43
+
3
44
  /**
4
45
  * Find the closest matching string from a list.
5
46
  *
6
47
  * Returns the best match, or null if no match is found.
48
+ * If minScore is provided, returns null when the best match scores below the threshold.
7
49
  */
8
- export declare function closest(query: string, items: Array<string>): string | null
50
+ export declare function closest(query: string, items: Array<string>, minScore?: number | undefined | null): string | null
9
51
 
10
52
  /**
11
53
  * Compute the Damerau-Levenshtein distance between two strings.
@@ -72,6 +114,19 @@ export declare function jaroWinklerBatch(pairs: Array<Array<string>>): Array<num
72
114
  */
73
115
  export declare function jaroWinklerMany(reference: string, candidates: Array<string>): Array<number>
74
116
 
117
+ /** A single result from multi-key fuzzy search. */
118
+ export interface KeySearchResult {
119
+ /** The index of the item in the original input array. */
120
+ index: number
121
+ /** The combined weighted score normalized to 0.0-1.0 range. */
122
+ score: number
123
+ /**
124
+ * Per-key scores in the same order as the input keys.
125
+ * A score of 0.0 means the item did not match on that key.
126
+ */
127
+ keyScores: Array<number>
128
+ }
129
+
75
130
  /**
76
131
  * Compute the Levenshtein distance between two strings.
77
132
  *
@@ -117,6 +172,30 @@ export declare function normalizedLevenshteinBatch(pairs: Array<Array<string>>):
117
172
  */
118
173
  export declare function normalizedLevenshteinMany(reference: string, candidates: Array<string>): Array<number>
119
174
 
175
+ /**
176
+ * Compute the partial ratio between two strings.
177
+ *
178
+ * Finds the best matching substring of the shorter string within the longer string
179
+ * using a sliding window approach. Returns the highest normalized Levenshtein
180
+ * similarity across all windows. Useful for matching when one string is a
181
+ * substring or abbreviation of the other. Returns a value between 0.0 and 1.0.
182
+ */
183
+ export declare function partialRatio(a: string, b: string): number
184
+
185
+ /**
186
+ * Compute the partial ratio for multiple pairs of strings in a single call.
187
+ *
188
+ * Returns an array of similarity scores in the same order as the input pairs.
189
+ */
190
+ export declare function partialRatioBatch(pairs: Array<Array<string>>): Array<number>
191
+
192
+ /**
193
+ * Compute the partial ratio from one reference string to many candidates.
194
+ *
195
+ * Returns an array of similarity scores, one per candidate, in the same order as the input.
196
+ */
197
+ export declare function partialRatioMany(reference: string, candidates: Array<string>): Array<number>
198
+
120
199
  /**
121
200
  * Perform fuzzy search over a list of strings.
122
201
  *
@@ -124,8 +203,37 @@ export declare function normalizedLevenshteinMany(reference: string, candidates:
124
203
  * Scores are normalized to a 0.0-1.0 range where 1.0 is a perfect match.
125
204
  * Uses the nucleo algorithm (same as Helix editor), which is
126
205
  * significantly faster than fzf/skim for large datasets.
206
+ *
207
+ * The third argument accepts either a number (maxResults for backward
208
+ * compatibility) or a SearchOptions object with maxResults and minScore.
127
209
  */
128
- export declare function search(query: string, items: Array<string>, maxResults?: number | undefined | null): Array<SearchResult>
210
+ export declare function search(query: string, items: Array<string>, options?: number | SearchOptions | undefined | null): Array<SearchResult>
211
+
212
+ /**
213
+ * Perform fuzzy search across multiple text keys with weights.
214
+ *
215
+ * `key_texts[k]` is an array of strings for key `k`, one per item.
216
+ * All inner arrays must have the same length (the number of items).
217
+ * `weights` specifies the relative importance of each key.
218
+ *
219
+ * Returns results sorted by combined weighted score (best match first).
220
+ */
221
+ export declare function searchKeys(query: string, keyTexts: Array<Array<string>>, weights: Array<number>, options?: SearchOptions | undefined | null): Array<KeySearchResult>
222
+
223
+ /** Options for the search function. */
224
+ export interface SearchOptions {
225
+ /** Maximum number of results to return. */
226
+ maxResults?: number
227
+ /** Minimum normalized score (0.0-1.0) to include in results. */
228
+ minScore?: number
229
+ /** If true, include matched character positions in results. */
230
+ includePositions?: boolean
231
+ /**
232
+ * If true, matching is case-sensitive. Default is smart case
233
+ * (case-insensitive unless the query contains uppercase characters).
234
+ */
235
+ isCaseSensitive?: boolean
236
+ }
129
237
 
130
238
  /** A single fuzzy search result with the matched item and its score. */
131
239
  export interface SearchResult {
@@ -135,6 +243,11 @@ export interface SearchResult {
135
243
  score: number
136
244
  /** The index of the item in the original input array. */
137
245
  index: number
246
+ /**
247
+ * Indices of matched characters in the item string.
248
+ * Empty unless `includePositions` is set to true in SearchOptions.
249
+ */
250
+ positions: Array<number>
138
251
  }
139
252
 
140
253
  /**
@@ -158,3 +271,78 @@ export declare function sorensenDiceBatch(pairs: Array<Array<string>>): Array<nu
158
271
  * Returns an array of similarity scores, one per candidate, in the same order as the input.
159
272
  */
160
273
  export declare function sorensenDiceMany(reference: string, candidates: Array<string>): Array<number>
274
+
275
+ /**
276
+ * Compute the token set ratio between two strings.
277
+ *
278
+ * Compares the intersection and differences of token sets from both strings.
279
+ * Returns the maximum similarity among comparisons of the intersection with
280
+ * each remainder. Highly effective for strings with shared tokens but
281
+ * different lengths. Returns a value between 0.0 and 1.0.
282
+ */
283
+ export declare function tokenSetRatio(a: string, b: string): number
284
+
285
+ /**
286
+ * Compute the token set ratio for multiple pairs of strings in a single call.
287
+ *
288
+ * Returns an array of similarity scores in the same order as the input pairs.
289
+ */
290
+ export declare function tokenSetRatioBatch(pairs: Array<Array<string>>): Array<number>
291
+
292
+ /**
293
+ * Compute the token set ratio from one reference string to many candidates.
294
+ *
295
+ * Returns an array of similarity scores, one per candidate, in the same order as the input.
296
+ */
297
+ export declare function tokenSetRatioMany(reference: string, candidates: Array<string>): Array<number>
298
+
299
+ /**
300
+ * Compute the token sort ratio between two strings.
301
+ *
302
+ * Splits both strings into tokens, sorts them alphabetically, then computes
303
+ * the normalized Levenshtein similarity. This makes the comparison
304
+ * order-independent, ideal for matching names or addresses where word order varies.
305
+ * Returns a value between 0.0 (completely different) and 1.0 (identical after sorting).
306
+ */
307
+ export declare function tokenSortRatio(a: string, b: string): number
308
+
309
+ /**
310
+ * Compute the token sort ratio for multiple pairs of strings in a single call.
311
+ *
312
+ * Returns an array of similarity scores in the same order as the input pairs.
313
+ */
314
+ export declare function tokenSortRatioBatch(pairs: Array<Array<string>>): Array<number>
315
+
316
+ /**
317
+ * Compute the token sort ratio from one reference string to many candidates.
318
+ *
319
+ * Returns an array of similarity scores, one per candidate, in the same order as the input.
320
+ */
321
+ export declare function tokenSortRatioMany(reference: string, candidates: Array<string>): Array<number>
322
+
323
+ /**
324
+ * Compute the weighted ratio between two strings.
325
+ *
326
+ * Returns the maximum score across normalized Levenshtein, token sort ratio,
327
+ * token set ratio, and partial ratio. This provides a single "best effort"
328
+ * similarity score that automatically selects the most appropriate algorithm.
329
+ * Returns a value between 0.0 and 1.0.
330
+ */
331
+ export declare function weightedRatio(a: string, b: string): number
332
+
333
+ /**
334
+ * Compute the weighted ratio for multiple pairs of strings in a single call.
335
+ *
336
+ * Returns an array of similarity scores in the same order as the input pairs.
337
+ */
338
+ export declare function weightedRatioBatch(pairs: Array<Array<string>>): Array<number>
339
+
340
+ /**
341
+ * Compute the weighted ratio from one reference string to many candidates.
342
+ *
343
+ * Returns an array of similarity scores, one per candidate, in the same order as the input.
344
+ */
345
+ export declare function weightedRatioMany(reference: string, candidates: Array<string>): Array<number>
346
+
347
+ // --- JS utilities (appended by scripts/patch-binding.js) ---
348
+ export { highlight, highlightRanges, HighlightRange } from './highlight';
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
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.1.1' && 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.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
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.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
@@ -576,6 +576,7 @@ if (!nativeBinding) {
576
576
  }
577
577
 
578
578
  module.exports = nativeBinding
579
+ module.exports.FuzzyIndex = nativeBinding.FuzzyIndex
579
580
  module.exports.closest = nativeBinding.closest
580
581
  module.exports.damerauLevenshtein = nativeBinding.damerauLevenshtein
581
582
  module.exports.damerauLevenshteinBatch = nativeBinding.damerauLevenshteinBatch
@@ -592,7 +593,25 @@ module.exports.levenshteinMany = nativeBinding.levenshteinMany
592
593
  module.exports.normalizedLevenshtein = nativeBinding.normalizedLevenshtein
593
594
  module.exports.normalizedLevenshteinBatch = nativeBinding.normalizedLevenshteinBatch
594
595
  module.exports.normalizedLevenshteinMany = nativeBinding.normalizedLevenshteinMany
596
+ module.exports.partialRatio = nativeBinding.partialRatio
597
+ module.exports.partialRatioBatch = nativeBinding.partialRatioBatch
598
+ module.exports.partialRatioMany = nativeBinding.partialRatioMany
595
599
  module.exports.search = nativeBinding.search
600
+ module.exports.searchKeys = nativeBinding.searchKeys
596
601
  module.exports.sorensenDice = nativeBinding.sorensenDice
597
602
  module.exports.sorensenDiceBatch = nativeBinding.sorensenDiceBatch
598
603
  module.exports.sorensenDiceMany = nativeBinding.sorensenDiceMany
604
+ module.exports.tokenSetRatio = nativeBinding.tokenSetRatio
605
+ module.exports.tokenSetRatioBatch = nativeBinding.tokenSetRatioBatch
606
+ module.exports.tokenSetRatioMany = nativeBinding.tokenSetRatioMany
607
+ module.exports.tokenSortRatio = nativeBinding.tokenSortRatio
608
+ module.exports.tokenSortRatioBatch = nativeBinding.tokenSortRatioBatch
609
+ module.exports.tokenSortRatioMany = nativeBinding.tokenSortRatioMany
610
+ module.exports.weightedRatio = nativeBinding.weightedRatio
611
+ module.exports.weightedRatioBatch = nativeBinding.weightedRatioBatch
612
+ module.exports.weightedRatioMany = nativeBinding.weightedRatioMany
613
+
614
+ // --- JS utilities (appended by scripts/patch-binding.js) ---
615
+ const _hl = require('./highlight.js');
616
+ module.exports.highlight = _hl.highlight;
617
+ module.exports.highlightRanges = _hl.highlightRanges;
package/index.mjs CHANGED
@@ -4,7 +4,9 @@ const require = createRequire(import.meta.url);
4
4
  const binding = require('./index.js');
5
5
 
6
6
  export const {
7
+ FuzzyIndex,
7
8
  closest,
9
+ searchKeys,
8
10
  damerauLevenshtein,
9
11
  damerauLevenshteinBatch,
10
12
  damerauLevenshteinMany,
@@ -20,8 +22,24 @@ export const {
20
22
  normalizedLevenshtein,
21
23
  normalizedLevenshteinBatch,
22
24
  normalizedLevenshteinMany,
25
+ partialRatio,
26
+ partialRatioBatch,
27
+ partialRatioMany,
23
28
  search,
24
29
  sorensenDice,
25
30
  sorensenDiceBatch,
26
31
  sorensenDiceMany,
27
- } = binding;
32
+ tokenSetRatio,
33
+ tokenSetRatioBatch,
34
+ tokenSetRatioMany,
35
+ tokenSortRatio,
36
+ tokenSortRatioBatch,
37
+ tokenSortRatioMany,
38
+ weightedRatio,
39
+ weightedRatioBatch,
40
+ weightedRatioMany,
41
+ highlight,
42
+ highlightRanges,
43
+ } = { ...binding, ...require('./highlight.js') };
44
+
45
+ export const { searchObjects } = require('./objects.js');
package/objects.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type { SearchOptions } from './index';
2
+
3
+ export interface KeyConfig {
4
+ name: string;
5
+ weight?: number;
6
+ }
7
+
8
+ export interface ObjectSearchOptions extends SearchOptions {
9
+ keys: Array<string | KeyConfig>;
10
+ }
11
+
12
+ export interface ObjectSearchResult<T> {
13
+ item: T;
14
+ index: number;
15
+ score: number;
16
+ keyScores: Array<number>;
17
+ }
18
+
19
+ /**
20
+ * Perform fuzzy search across object arrays with weighted keys.
21
+ *
22
+ * Wraps `searchKeys()` with an ergonomic API that accepts row-oriented
23
+ * objects and returns matched items directly.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const users = [
28
+ * { name: 'John Smith', email: 'john@example.com' },
29
+ * { name: 'Jane Doe', email: 'jane@example.com' },
30
+ * ];
31
+ *
32
+ * const results = searchObjects('john', users, {
33
+ * keys: [{ name: 'name', weight: 2.0 }, 'email'],
34
+ * });
35
+ * // results[0].item → { name: 'John Smith', email: 'john@example.com' }
36
+ * ```
37
+ */
38
+ export declare function searchObjects<T>(
39
+ query: string,
40
+ items: Array<T>,
41
+ options: ObjectSearchOptions,
42
+ ): Array<ObjectSearchResult<T>>;
package/objects.js ADDED
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const { searchKeys } = require('./index.js');
4
+
5
+ /**
6
+ * Get a nested property value from an object using a dot-separated path.
7
+ * @param {Record<string, unknown>} obj
8
+ * @param {string} path
9
+ * @returns {string}
10
+ */
11
+ function getNestedValue(obj, path) {
12
+ let current = obj;
13
+ for (const key of path.split('.')) {
14
+ if (current == null) return '';
15
+ current = current[key];
16
+ }
17
+ return current == null ? '' : String(current);
18
+ }
19
+
20
+ /**
21
+ * Perform fuzzy search across object arrays with weighted keys.
22
+ *
23
+ * Wraps `searchKeys()` with an ergonomic API that accepts row-oriented
24
+ * objects and returns matched items directly.
25
+ *
26
+ * @template T
27
+ * @param {string} query - The search query.
28
+ * @param {T[]} items - Array of objects to search.
29
+ * @param {object} options - Search options with keys configuration.
30
+ * @param {Array<string | { name: string; weight?: number }>} options.keys - Keys to search.
31
+ * @param {number} [options.maxResults] - Maximum results to return.
32
+ * @param {number} [options.minScore] - Minimum score threshold.
33
+ * @param {boolean} [options.isCaseSensitive] - Enable case-sensitive matching.
34
+ * @returns {Array<{ item: T; index: number; score: number; keyScores: number[] }>}
35
+ */
36
+ function searchObjects(query, items, options) {
37
+ const { keys, ...searchOpts } = options;
38
+
39
+ const normalizedKeys = keys.map((k) =>
40
+ typeof k === 'string' ? { name: k, weight: 1.0 } : { name: k.name, weight: k.weight ?? 1.0 },
41
+ );
42
+
43
+ const keyTexts = normalizedKeys.map((k) => items.map((item) => getNestedValue(item, k.name)));
44
+ const weights = normalizedKeys.map((k) => k.weight);
45
+
46
+ const nativeOpts = Object.keys(searchOpts).length > 0 ? searchOpts : undefined;
47
+
48
+ const results = searchKeys(query, keyTexts, weights, nativeOpts);
49
+
50
+ return results.map((r) => ({
51
+ item: items[r.index],
52
+ index: r.index,
53
+ score: r.score,
54
+ keyScores: r.keyScores,
55
+ }));
56
+ }
57
+
58
+ module.exports = { searchObjects };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rapid-fuzzy",
3
- "version": "0.2.0",
3
+ "version": "0.4.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",
@@ -46,7 +46,12 @@
46
46
  "index.mjs",
47
47
  "index.d.ts",
48
48
  "index.d.mts",
49
- "browser.js"
49
+ "objects.js",
50
+ "objects.d.ts",
51
+ "browser.js",
52
+ "highlight.js",
53
+ "highlight.mjs",
54
+ "highlight.d.ts"
50
55
  ],
51
56
  "napi": {
52
57
  "binaryName": "rapid-fuzzy",
@@ -68,6 +73,7 @@
68
73
  "packageManager": "pnpm@10.32.1",
69
74
  "scripts": {
70
75
  "build": "napi build --manifest-path crates/core/Cargo.toml --platform --release --js index.js --dts index.d.ts --output-dir .",
76
+ "postbuild": "node scripts/patch-binding.js",
71
77
  "build:debug": "napi build --manifest-path crates/core/Cargo.toml --platform --js index.js --dts index.d.ts --output-dir .",
72
78
  "build:wasm": "napi build --manifest-path crates/core/Cargo.toml --platform --release --js index.js --dts index.d.ts --output-dir . --target wasm32-wasip1-threads",
73
79
  "artifacts": "napi artifacts",
@@ -80,8 +86,12 @@
80
86
  "typecheck": "tsc --noEmit",
81
87
  "test": "vitest run",
82
88
  "test:wasm": "vitest run __test__/wasm.spec.ts",
89
+ "test:browser": "playwright test",
90
+ "test:bun": "bun test e2e/wasm-bun.test.ts",
91
+ "test:deno": "deno test --allow-read --allow-env --allow-net --node-modules-dir=auto e2e/wasm-deno.test.ts",
83
92
  "test:watch": "vitest watch",
84
93
  "bench": "vitest bench",
94
+ "bench:readme": "npx tsx scripts/update-bench-readme.ts",
85
95
  "publint": "publint",
86
96
  "bench:rust": "cargo bench -p rapid-fuzzy-bench",
87
97
  "verify": "pnpm run check && cargo clippy -- -W clippy::all && cargo test && pnpm run build && pnpm run typecheck && pnpm test",
@@ -91,12 +101,14 @@
91
101
  "devDependencies": {
92
102
  "@biomejs/biome": "^2.4.6",
93
103
  "@changesets/cli": "^2.30.0",
104
+ "@codspeed/vitest-plugin": "^5.2.0",
94
105
  "@commitlint/cli": "^20.4.4",
95
106
  "@commitlint/config-conventional": "^20.4.4",
96
107
  "@emnapi/core": "^1.9.0",
97
108
  "@emnapi/runtime": "^1.9.0",
98
109
  "@napi-rs/cli": "^3.5.1",
99
110
  "@napi-rs/wasm-runtime": "^1.1.1",
111
+ "@playwright/test": "^1.58.2",
100
112
  "@tybys/wasm-util": "^0.10.1",
101
113
  "@types/node": "^24.0.0",
102
114
  "@types/string-similarity": "^4.0.2",
@@ -109,6 +121,7 @@
109
121
  "publint": "^0.3.18",
110
122
  "string-similarity": "^4.0.4",
111
123
  "typescript": "^5.9.3",
124
+ "vite": "^8.0.0",
112
125
  "vitest": "^4.1.0"
113
126
  },
114
127
  "pnpm": {
@@ -117,14 +130,14 @@
117
130
  ]
118
131
  },
119
132
  "optionalDependencies": {
120
- "rapid-fuzzy-darwin-x64": "0.2.0",
121
- "rapid-fuzzy-darwin-arm64": "0.2.0",
122
- "rapid-fuzzy-linux-x64-gnu": "0.2.0",
123
- "rapid-fuzzy-linux-x64-musl": "0.2.0",
124
- "rapid-fuzzy-linux-arm64-gnu": "0.2.0",
125
- "rapid-fuzzy-linux-arm64-musl": "0.2.0",
126
- "rapid-fuzzy-win32-x64-msvc": "0.2.0",
127
- "rapid-fuzzy-win32-arm64-msvc": "0.2.0",
128
- "rapid-fuzzy-wasm32-wasi": "0.2.0"
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"
129
142
  }
130
143
  }