@bitbaum/searchkit 0.0.0-stage → 0.2.2

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.
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Show WHY a result is there: the parts of its text that matched.
3
+ *
4
+ * From a hit's ranges when you have them (they include typo matches — "cqto"
5
+ * marks "Cato" whole — and mark only the typed start of a prefix match). From
6
+ * the query alone otherwise: every word that STARTS with a typed word, the way
7
+ * the engine matches, never letters in the middle of a word ("lo" marks
8
+ * "local", not "Velo").
9
+ *
10
+ * Plain parts, never HTML: the caller renders them, so a result's own text can
11
+ * never be injected as markup.
12
+ */
13
+ import { tokenize } from "./normalize.js";
14
+ /** Split `text` into marked and unmarked runs by `ranges` (overlaps merged). */
15
+ export function highlightRanges(text, ranges) {
16
+ if (!text) {
17
+ return [];
18
+ }
19
+ const sorted = ranges
20
+ .map(r => ({ start: Math.max(0, r.start), end: Math.min(text.length, r.end) }))
21
+ .filter(r => r.end > r.start)
22
+ .sort((a, b) => a.start - b.start);
23
+ const parts = [];
24
+ let at = 0;
25
+ for (const r of sorted) {
26
+ if (r.end <= at) {
27
+ continue;
28
+ }
29
+ const start = Math.max(r.start, at);
30
+ if (start > at) {
31
+ parts.push({ text: text.slice(at, start), match: false });
32
+ }
33
+ parts.push({ text: text.slice(start, r.end), match: true });
34
+ at = r.end;
35
+ }
36
+ if (at < text.length) {
37
+ parts.push({ text: text.slice(at), match: false });
38
+ }
39
+ return parts;
40
+ }
41
+ /**
42
+ * Mark every word of `text` that starts with a word of `query`, by the typed
43
+ * length. For text that was not part of the search (a snippet fetched later).
44
+ */
45
+ export function highlightQuery(text, query) {
46
+ const typed = tokenize(query).map(t => t.term);
47
+ if (!text || typed.length === 0) {
48
+ return text ? [{ text, match: false }] : [];
49
+ }
50
+ const ranges = [];
51
+ for (const word of tokenize(text)) {
52
+ const longest = typed
53
+ .filter(t => word.term.startsWith(t))
54
+ .sort((a, b) => b.length - a.length)[0];
55
+ if (longest) {
56
+ // Folding can change length (ß → ss); never mark past the word.
57
+ ranges.push({
58
+ start: word.start,
59
+ end: Math.min(word.end, word.start + Array.from(longest).length),
60
+ });
61
+ }
62
+ }
63
+ return highlightRanges(text, ranges);
64
+ }
65
+ /**
66
+ * The part of a long text worth showing for a match: a window that starts a
67
+ * little before the first matched word, cut at word boundaries, with "…"
68
+ * where it was cut. Without a match it is the text's beginning. For a
69
+ * description whose third paragraph is what matched.
70
+ */
71
+ export function snippet(text, ranges, { maxLength = 160, lead = 40 } = {}) {
72
+ if (text.length <= maxLength) {
73
+ return { text, ranges: [...ranges] };
74
+ }
75
+ const first = ranges.reduce((min, r) => Math.min(min, r.start), Infinity);
76
+ let start = Number.isFinite(first) ? Math.max(0, first - lead) : 0;
77
+ if (start > 0) {
78
+ const space = text.indexOf(' ', start);
79
+ start = space >= 0 && space < first ? space + 1 : start;
80
+ }
81
+ let end = Math.min(text.length, start + maxLength);
82
+ if (end < text.length) {
83
+ const space = text.lastIndexOf(' ', end);
84
+ end = space > start ? space : end;
85
+ }
86
+ const before = start > 0 ? '…' : '';
87
+ const after = end < text.length ? '…' : '';
88
+ const shift = before.length - start;
89
+ return {
90
+ text: before + text.slice(start, end) + after,
91
+ ranges: ranges
92
+ .filter(r => r.end > start && r.start < end)
93
+ .map(r => ({ start: Math.max(r.start, start) + shift, end: Math.min(r.end, end) + shift })),
94
+ };
95
+ }
@@ -0,0 +1,8 @@
1
+ export { fold, tokenize, words, umlautSpelling, joinedPairs, writtenLengths, type Token, } from './normalize.ts';
2
+ export { editDistance, prefixDistance } from './distance.ts';
3
+ export { typoBudget, DEFAULT_TYPO_POLICY, type TypoPolicy } from './typo.ts';
4
+ export { createVocabulary, candidates, type Candidate, type CandidateOptions, type Vocabulary, } from './vocabulary.ts';
5
+ export { createSearch, search, compareHits, rerank, type Hit, type Range, type SearchField, type SearchOptions, type SearchSpec, type Searcher, } from './engine.ts';
6
+ export { highlightRanges, highlightQuery, snippet, type HighlightPart, type Snippet, } from './highlight.ts';
7
+ export { expandQuery, corrections, didYouMean, type ExpandedWord, type ExpandOptions, } from './expand.ts';
8
+ export { typoVariants, typoCases, evaluate, type TypoKind, type EvalCase, type EvalReport, } from './evaluate.ts';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { fold, tokenize, words, umlautSpelling, joinedPairs, writtenLengths, } from "./normalize.js";
2
+ export { editDistance, prefixDistance } from "./distance.js";
3
+ export { typoBudget, DEFAULT_TYPO_POLICY } from "./typo.js";
4
+ export { createVocabulary, candidates, } from "./vocabulary.js";
5
+ export { createSearch, search, compareHits, rerank, } from "./engine.js";
6
+ export { highlightRanges, highlightQuery, snippet, } from "./highlight.js";
7
+ export { expandQuery, corrections, didYouMean, } from "./expand.js";
8
+ export { typoVariants, typoCases, evaluate, } from "./evaluate.js";
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Text to words, the same way on both sides of a search.
3
+ *
4
+ * Everything a person might type differently from how a title is written must
5
+ * be folded here, once, or the matcher has to forgive it later as a "typo" —
6
+ * which spends the typo budget on something that was not a mistake:
7
+ *
8
+ * - case and accents: "zurich" is "Zürich", "cafe" is "Café";
9
+ * - letters with no decomposition: ß → ss, æ → ae, ø → o, ł → l;
10
+ * - German spelled without umlauts: "Mueller" is "Müller" (handled at
11
+ * indexing: a word with ä/ö/ü is also indexed with ae/oe/ue);
12
+ * - joined and split words: "OrangeCat", "orange_cat", "orange-cat" and
13
+ * "orange cat" are one name (camelCase and separators split; adjacent
14
+ * words are also indexed joined, see `joinedPairs`).
15
+ */
16
+ /** Casefold and strip accents. "Straße Zürich" → "strasse zurich". */
17
+ export declare function fold(text: string): string;
18
+ /** One word of a text, with where it sits in the original string. */
19
+ export interface Token {
20
+ /** Folded form, the thing compared. */
21
+ term: string;
22
+ /** Position among the text's words, 0-based. */
23
+ index: number;
24
+ /** Offsets into the ORIGINAL text, for highlighting. */
25
+ start: number;
26
+ end: number;
27
+ }
28
+ /** The words of a text, folded, with their original offsets. */
29
+ export declare function tokenize(text: string): Token[];
30
+ /** Just the folded words. */
31
+ export declare function words(text: string): string[];
32
+ /**
33
+ * The other spelling of a German word written with umlauts: "müller" →
34
+ * "mueller". Returns null when the word has none. Applied to the ORIGINAL
35
+ * word (before folding, which turns ü into u).
36
+ */
37
+ export declare function umlautSpelling(original: string): string | null;
38
+ /**
39
+ * Adjacent words joined: ["orange", "cat", "shop"] → ["orangecat", "catshop"].
40
+ * Indexed alongside the words so a query typed without the space still finds
41
+ * the name typed with it.
42
+ */
43
+ export declare function joinedPairs(terms: string[]): string[];
44
+ /** Each typed word after the site's spelling rule, with how long it was as written. */
45
+ export declare function writtenLengths(query: string, normalize: ((word: string) => string) | undefined): Map<string, number>;
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Text to words, the same way on both sides of a search.
3
+ *
4
+ * Everything a person might type differently from how a title is written must
5
+ * be folded here, once, or the matcher has to forgive it later as a "typo" —
6
+ * which spends the typo budget on something that was not a mistake:
7
+ *
8
+ * - case and accents: "zurich" is "Zürich", "cafe" is "Café";
9
+ * - letters with no decomposition: ß → ss, æ → ae, ø → o, ł → l;
10
+ * - German spelled without umlauts: "Mueller" is "Müller" (handled at
11
+ * indexing: a word with ä/ö/ü is also indexed with ae/oe/ue);
12
+ * - joined and split words: "OrangeCat", "orange_cat", "orange-cat" and
13
+ * "orange cat" are one name (camelCase and separators split; adjacent
14
+ * words are also indexed joined, see `joinedPairs`).
15
+ */
16
+ /** Letters that NFKD does not decompose, mapped the way their languages spell them in ASCII. */
17
+ const LETTERS = {
18
+ ß: 'ss',
19
+ ẞ: 'ss',
20
+ æ: 'ae',
21
+ Æ: 'ae',
22
+ œ: 'oe',
23
+ Œ: 'oe',
24
+ ø: 'o',
25
+ Ø: 'o',
26
+ ł: 'l',
27
+ Ł: 'l',
28
+ đ: 'd',
29
+ Đ: 'd',
30
+ ð: 'd',
31
+ þ: 'th',
32
+ ı: 'i',
33
+ };
34
+ const SPECIAL = new RegExp(`[${Object.keys(LETTERS).join('')}]`, 'g');
35
+ /** Casefold and strip accents. "Straße Zürich" → "strasse zurich". */
36
+ export function fold(text) {
37
+ return text
38
+ .replace(SPECIAL, c => LETTERS[c] ?? c)
39
+ .normalize('NFKD')
40
+ .replace(/\p{M}+/gu, '')
41
+ .toLowerCase();
42
+ }
43
+ // A word is a run of letters and digits. camelCase boundaries ("OrangeCat",
44
+ // "myHTMLParser" → my HTML Parser) also split, before case is folded away.
45
+ const WORD = /[\p{L}\p{N}]+/gu;
46
+ const CAMEL = /(?<=\p{Ll})(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}\p{Ll})|(?<=\p{L})(?=\p{N})|(?<=\p{N})(?=\p{L})/u;
47
+ /** The words of a text, folded, with their original offsets. */
48
+ export function tokenize(text) {
49
+ const tokens = [];
50
+ for (const match of text.matchAll(WORD)) {
51
+ let offset = match.index ?? 0;
52
+ for (const part of match[0].split(CAMEL)) {
53
+ if (part) {
54
+ tokens.push({
55
+ term: fold(part),
56
+ index: tokens.length,
57
+ start: offset,
58
+ end: offset + part.length,
59
+ });
60
+ }
61
+ offset += part.length;
62
+ }
63
+ }
64
+ return tokens;
65
+ }
66
+ /** Just the folded words. */
67
+ export function words(text) {
68
+ return tokenize(text).map(t => t.term);
69
+ }
70
+ /**
71
+ * The other spelling of a German word written with umlauts: "müller" →
72
+ * "mueller". Returns null when the word has none. Applied to the ORIGINAL
73
+ * word (before folding, which turns ü into u).
74
+ */
75
+ export function umlautSpelling(original) {
76
+ if (!/[äöüÄÖÜ]/.test(original)) {
77
+ return null;
78
+ }
79
+ return fold(original.replace(/[äÄ]/g, 'ae').replace(/[öÖ]/g, 'oe').replace(/[üÜ]/g, 'ue'));
80
+ }
81
+ /**
82
+ * Adjacent words joined: ["orange", "cat", "shop"] → ["orangecat", "catshop"].
83
+ * Indexed alongside the words so a query typed without the space still finds
84
+ * the name typed with it.
85
+ */
86
+ export function joinedPairs(terms) {
87
+ const out = [];
88
+ for (let i = 0; i + 1 < terms.length; i++) {
89
+ out.push(terms[i] + terms[i + 1]);
90
+ }
91
+ return out;
92
+ }
93
+ /** Each typed word after the site's spelling rule, with how long it was as written. */
94
+ export function writtenLengths(query, normalize) {
95
+ const out = new Map();
96
+ for (const t of tokenize(query)) {
97
+ const term = normalize ? normalize(t.term) : t.term;
98
+ if (term) {
99
+ out.set(term, Math.max(out.get(term) ?? 0, Array.from(t.term).length));
100
+ }
101
+ }
102
+ return out;
103
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The React half: search as you type, and the marked text that says why a
3
+ * result is there. Optional — the core has no React in it.
4
+ */
5
+ import { type ReactNode } from 'react';
6
+ import type { Range } from '../engine.ts';
7
+ export interface SearchAsYouTypeOptions {
8
+ /**
9
+ * Pause after the last keystroke. 100 ms: under Nielsen's 0.1 s a response
10
+ * still feels instant, and a burst of typing costs one request, not five.
11
+ */
12
+ debounceMs?: number;
13
+ /** Below this many characters, do not search. Default 1. */
14
+ minLength?: number;
15
+ /** Search at all (e.g. only while a dialog is open). Default true. */
16
+ enabled?: boolean;
17
+ }
18
+ export interface SearchAsYouTypeState<R> {
19
+ results: R | null;
20
+ /** A search for the current text is in flight. */
21
+ loading: boolean;
22
+ error: unknown;
23
+ }
24
+ /**
25
+ * Run `search` for the latest text only. Each keystroke aborts the request
26
+ * before it (the signal is passed in), and a response that arrives after a
27
+ * newer one was asked for is dropped — so a slow "ca" can never overwrite a
28
+ * fast "cato".
29
+ */
30
+ export declare function useSearchAsYouType<R>(text: string, search: (text: string, signal: AbortSignal) => Promise<R>, options?: SearchAsYouTypeOptions): SearchAsYouTypeState<R>;
31
+ export interface HighlightProps {
32
+ text: string;
33
+ /** Mark these ranges (from a hit) … */
34
+ ranges?: readonly Range[];
35
+ /** … or the words that start with what was typed. */
36
+ query?: string;
37
+ /** Class for each <mark>; unstyled by default so it takes your tokens. */
38
+ markClassName?: string;
39
+ }
40
+ /** Text with the matched parts in <mark>. Never renders the text as HTML. */
41
+ export declare function Highlight({ text, ranges, query, markClassName }: HighlightProps): ReactNode;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * The React half: search as you type, and the marked text that says why a
3
+ * result is there. Optional — the core has no React in it.
4
+ */
5
+ import { createElement, Fragment, useEffect, useRef, useState } from 'react';
6
+ import { highlightQuery, highlightRanges } from "../highlight.js";
7
+ /**
8
+ * Run `search` for the latest text only. Each keystroke aborts the request
9
+ * before it (the signal is passed in), and a response that arrives after a
10
+ * newer one was asked for is dropped — so a slow "ca" can never overwrite a
11
+ * fast "cato".
12
+ */
13
+ export function useSearchAsYouType(text, search, options = {}) {
14
+ const { debounceMs = 100, minLength = 1, enabled = true } = options;
15
+ const [state, setState] = useState({
16
+ results: null,
17
+ loading: false,
18
+ error: null,
19
+ });
20
+ const searchRef = useRef(search);
21
+ searchRef.current = search;
22
+ useEffect(() => {
23
+ const trimmed = text.trim();
24
+ if (!enabled || trimmed.length < minLength) {
25
+ setState({ results: null, loading: false, error: null });
26
+ return;
27
+ }
28
+ const controller = new AbortController();
29
+ setState(s => ({ ...s, loading: true }));
30
+ const timer = setTimeout(() => {
31
+ searchRef
32
+ .current(trimmed, controller.signal)
33
+ .then(results => {
34
+ if (!controller.signal.aborted) {
35
+ setState({ results, loading: false, error: null });
36
+ }
37
+ })
38
+ .catch((error) => {
39
+ if (!controller.signal.aborted) {
40
+ setState({ results: null, loading: false, error });
41
+ }
42
+ });
43
+ }, debounceMs);
44
+ return () => {
45
+ clearTimeout(timer);
46
+ controller.abort();
47
+ };
48
+ }, [text, enabled, minLength, debounceMs]);
49
+ return state;
50
+ }
51
+ /** Text with the matched parts in <mark>. Never renders the text as HTML. */
52
+ export function Highlight({ text, ranges, query = '', markClassName }) {
53
+ const parts = ranges
54
+ ? highlightRanges(text, ranges)
55
+ : highlightQuery(text, query);
56
+ return createElement(Fragment, null, ...parts.map((part, i) => part.match
57
+ ? createElement('mark', { key: i, className: markClassName }, part.text)
58
+ : part.text));
59
+ }
package/dist/typo.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * How many typos a word of a given length may carry and still match.
3
+ *
4
+ * The SoundCloud failure — one wrong letter in an artist's name and nothing
5
+ * comes back — is what a budget of zero does. The opposite failure is a short
6
+ * word matching half the dictionary: two edits turn "cat" into "car", "hat",
7
+ * "cut", "at"… So the budget grows with the word, the way Algolia, Typesense
8
+ * and Meilisearch all settled on: none below 4 letters, one from 4, two from 8.
9
+ *
10
+ * Four letters is deliberately the first length with a typo: "cqto" must find
11
+ * "Cato". Pure trigram similarity cannot promise that — "cqto" and "cato"
12
+ * share 2 of 5 trigrams, 0.4, under any useful threshold — which is why this
13
+ * package counts edits instead.
14
+ */
15
+ export interface TypoPolicy {
16
+ /** Shortest word allowed one typo. Default 4. */
17
+ oneTypoFrom: number;
18
+ /** Shortest word allowed two typos. Default 8. */
19
+ twoTyposFrom: number;
20
+ }
21
+ export declare const DEFAULT_TYPO_POLICY: TypoPolicy;
22
+ /** Typos allowed for a query word of this many letters. */
23
+ export declare function typoBudget(length: number, policy?: TypoPolicy): number;
package/dist/typo.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * How many typos a word of a given length may carry and still match.
3
+ *
4
+ * The SoundCloud failure — one wrong letter in an artist's name and nothing
5
+ * comes back — is what a budget of zero does. The opposite failure is a short
6
+ * word matching half the dictionary: two edits turn "cat" into "car", "hat",
7
+ * "cut", "at"… So the budget grows with the word, the way Algolia, Typesense
8
+ * and Meilisearch all settled on: none below 4 letters, one from 4, two from 8.
9
+ *
10
+ * Four letters is deliberately the first length with a typo: "cqto" must find
11
+ * "Cato". Pure trigram similarity cannot promise that — "cqto" and "cato"
12
+ * share 2 of 5 trigrams, 0.4, under any useful threshold — which is why this
13
+ * package counts edits instead.
14
+ */
15
+ export const DEFAULT_TYPO_POLICY = { oneTypoFrom: 4, twoTyposFrom: 8 };
16
+ /** Typos allowed for a query word of this many letters. */
17
+ export function typoBudget(length, policy = DEFAULT_TYPO_POLICY) {
18
+ if (length >= policy.twoTyposFrom) {
19
+ return 2;
20
+ }
21
+ return length >= policy.oneTypoFrom ? 1 : 0;
22
+ }
@@ -0,0 +1,38 @@
1
+ import { type TypoPolicy } from './typo.ts';
2
+ /** How a vocabulary word answers a typed word. */
3
+ export interface Candidate {
4
+ term: string;
5
+ /** Edits between what was typed and the word (or its start, for a prefix). */
6
+ typos: number;
7
+ /** The typed word matched only the start of this word. */
8
+ prefix: boolean;
9
+ /** How often the word occurs in the collection. */
10
+ count: number;
11
+ }
12
+ export interface Vocabulary {
13
+ /** Every distinct word, sorted, for prefix ranges. */
14
+ readonly terms: readonly string[];
15
+ readonly counts: ReadonlyMap<string, number>;
16
+ }
17
+ /** A vocabulary from words (repeats are counted) or from [word, count] pairs. */
18
+ export declare function createVocabulary(entries: Iterable<string | readonly [string, number]>): Vocabulary;
19
+ export interface CandidateOptions {
20
+ /** Let the typed word match the start of a longer word. Default true. */
21
+ prefix?: boolean;
22
+ policy?: TypoPolicy;
23
+ /** At most this many, best first. Default 50. */
24
+ limit?: number;
25
+ /**
26
+ * How many letters the word had as written, when a site spelling rule
27
+ * (`SearchSpec.normalize`) shortened it: the typo budget is the written
28
+ * word's. Heidi reads doubled letters as one, so "vill" arrives as "vil" —
29
+ * and a three-letter word would get no typo at all.
30
+ */
31
+ writtenLength?: number;
32
+ }
33
+ /**
34
+ * The vocabulary words a typed (folded) word could mean, best first: exact,
35
+ * then exact prefixes, then within the typo budget — fewer typos first, then
36
+ * the more common word.
37
+ */
38
+ export declare function candidates(vocab: Vocabulary, typed: string, options?: CandidateOptions): Candidate[];
@@ -0,0 +1,118 @@
1
+ /**
2
+ * The words a collection actually contains, and which of them a typed word
3
+ * could mean.
4
+ *
5
+ * Correcting against the collection's OWN vocabulary is what makes typo
6
+ * tolerance safe: "cqto" is corrected to "cato" because Cato is here, never to
7
+ * a dictionary word nobody listed. It is also what lets a database without
8
+ * typo tolerance have it: expand the typed words into the vocabulary words
9
+ * they could mean (`expandQuery`), and send those.
10
+ */
11
+ import { editDistance, prefixDistance } from "./distance.js";
12
+ import { DEFAULT_TYPO_POLICY, typoBudget } from "./typo.js";
13
+ /** A vocabulary from words (repeats are counted) or from [word, count] pairs. */
14
+ export function createVocabulary(entries) {
15
+ const counts = new Map();
16
+ for (const entry of entries) {
17
+ const [term, n] = typeof entry === 'string' ? [entry, 1] : entry;
18
+ if (term) {
19
+ counts.set(term, (counts.get(term) ?? 0) + n);
20
+ }
21
+ }
22
+ return { terms: [...counts.keys()].sort(), counts };
23
+ }
24
+ /** First index in sorted `terms` not less than `key`. */
25
+ function lowerBound(terms, key) {
26
+ let lo = 0;
27
+ let hi = terms.length;
28
+ while (lo < hi) {
29
+ const mid = (lo + hi) >>> 1;
30
+ if (terms[mid] < key) {
31
+ lo = mid + 1;
32
+ }
33
+ else {
34
+ hi = mid;
35
+ }
36
+ }
37
+ return lo;
38
+ }
39
+ /**
40
+ * The vocabulary words a typed (folded) word could mean, best first: exact,
41
+ * then exact prefixes, then within the typo budget — fewer typos first, then
42
+ * the more common word.
43
+ */
44
+ export function candidates(vocab, typed, options = {}) {
45
+ const { prefix = true, policy = DEFAULT_TYPO_POLICY, limit = 50, writtenLength } = options;
46
+ if (!typed) {
47
+ return [];
48
+ }
49
+ const found = new Map();
50
+ const add = (term, typos, isPrefix) => {
51
+ const seen = found.get(term);
52
+ if (!seen || typos < seen.typos || (typos === seen.typos && seen.prefix && !isPrefix)) {
53
+ found.set(term, { term, typos, prefix: isPrefix, count: vocab.counts.get(term) ?? 0 });
54
+ }
55
+ };
56
+ if (vocab.counts.has(typed)) {
57
+ add(typed, 0, false);
58
+ }
59
+ if (prefix) {
60
+ for (let i = lowerBound(vocab.terms, typed); i < vocab.terms.length; i++) {
61
+ const term = vocab.terms[i];
62
+ if (!term.startsWith(typed)) {
63
+ break;
64
+ }
65
+ if (term !== typed) {
66
+ add(term, 0, true);
67
+ }
68
+ }
69
+ }
70
+ const length = Array.from(typed).length;
71
+ const budgetLength = Math.max(length, writtenLength ?? 0);
72
+ let budget = typoBudget(budgetLength, policy);
73
+ // The budget belongs to the word that was MEANT. Drop a letter from "Loki"
74
+ // and what was typed ("lki") is too short for a typo — so when a short word
75
+ // matches nothing at all, it may still be one slip from a word long enough
76
+ // to be allowed one. Only then: "cat" must stay "cat" when "cat" exists.
77
+ let minTermLength = 0;
78
+ if (budget === 0 && found.size === 0 && budgetLength >= policy.oneTypoFrom - 1) {
79
+ budget = 1;
80
+ minTermLength = policy.oneTypoFrom;
81
+ }
82
+ if (budget > 0) {
83
+ for (const term of vocab.terms) {
84
+ if (found.get(term)?.typos === 0) {
85
+ continue;
86
+ }
87
+ const termLength = term.length;
88
+ if (termLength < minTermLength) {
89
+ continue;
90
+ }
91
+ if (termLength + budget < length) {
92
+ continue;
93
+ }
94
+ if (termLength <= length + budget) {
95
+ const d = editDistance(typed, term, budget);
96
+ if (d <= budget) {
97
+ add(term, d, false);
98
+ continue;
99
+ }
100
+ }
101
+ // A slip in a word's START is not forgiven on the fallback: "cqt" would
102
+ // be one edit from the start of every word beginning "ca".
103
+ if (prefix && minTermLength === 0 && termLength > length) {
104
+ const d = prefixDistance(typed, term, budget);
105
+ if (d <= budget) {
106
+ add(term, d, true);
107
+ }
108
+ }
109
+ }
110
+ }
111
+ return [...found.values()]
112
+ .sort((a, b) => a.typos - b.typos ||
113
+ Number(a.prefix) - Number(b.prefix) ||
114
+ b.count - a.count ||
115
+ a.term.length - b.term.length ||
116
+ (a.term < b.term ? -1 : 1))
117
+ .slice(0, limit);
118
+ }
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@bitbaum/searchkit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.2",
4
+ "description": "Search that finds what was meant: typo budgets by word length, prefix while typing, German spellings without umlauts, joined and split words, Algolia-order tie-break ranking, highlight ranges, query expansion for any database, and a generated typo regression suite. Zero dependencies; React optional.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/bitbaum/searchkit.git"
9
+ },
10
+ "type": "module",
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./react": {
19
+ "types": "./dist/react/index.d.ts",
20
+ "default": "./dist/react/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "src",
26
+ "README.md"
27
+ ],
28
+ "sideEffects": false,
29
+ "peerDependencies": {
30
+ "react": ">=18"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "react": {
34
+ "optional": true
35
+ }
36
+ },
37
+ "scripts": {
38
+ "build": "rm -rf dist && tsc -p tsconfig.json",
39
+ "type-check": "tsc --noEmit -p tsconfig.json",
40
+ "test": "node --test test/*.test.ts",
41
+ "verify": "npm run type-check && npm run build && npm test"
42
+ },
43
+ "homepage": "https://github.com/bitbaum/searchkit#readme",
44
+ "bugs": {
45
+ "url": "https://github.com/bitbaum/searchkit/issues"
46
+ },
47
+ "packageManager": "pnpm@11.25.0",
48
+ "devDependencies": {
49
+ "typescript": "^6.0.3",
50
+ "@types/react": "^19.3.0"
51
+ }
52
+ }