@thereforejs/food-finder 0.0.3 → 0.0.4

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
@@ -49,6 +49,9 @@ You get:
49
49
  - `words`: every word of the query is a whole word of the name, any order. Singular and plural
50
50
  are one word: "Zwiebel" finds "Zwiebeln gebraten", "Tomaten" finds "Tomate roh".
51
51
  - `compound`: a word of the query is the last part of a compound: "Butter" finds "Kakaobutter".
52
+ - `start`: only when no name holds every word whole, the last word still being typed, from three
53
+ letters or two digits: "sulta" finds "Rosine/Sultanine", "butt" "Süßrahmbutter", "roggenmehl
54
+ 11" "Roggen Mehl, Type 1150". "ei" stays "Hühnerei".
52
55
  - `partial`: only when no name holds the whole query, the names that hold the most of it. A part
53
56
  that is only a preparation ("gedünstet") or a short word ("der") is no hit.
54
57
  - `fuzzy`: only when nothing else is found, every word no name holds read as its nearest known
package/dist/de.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as Meanings } from "./meanings-DJO-9Ck9.mjs";
2
- import { a as Match, i as SearchOptions, n as FinderOptions, r as Hit, t as Finder } from "./finder-CvhfeMAo.mjs";
2
+ import { a as Match, i as SearchOptions, n as FinderOptions, r as Hit, t as Finder } from "./finder-BDvIlKo2.mjs";
3
3
  /** A German finder over `names`, code to name: read once, searched often. */
4
4
  export declare function createFinder(names: Record<string, string>, options?: FinderOptions): Finder;
5
5
  export type { Finder, FinderOptions, Hit, Match, Meanings, SearchOptions };
package/dist/de.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as createFinder$1 } from "./finder-Dv8sg_xg.mjs";
1
+ import { t as createFinder$1 } from "./finder-Dy0IOqpK.mjs";
2
2
  // src/de/language.ts
3
3
  const LETTERS = {
4
4
  ß: "ss",
package/dist/en.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as Meanings } from "./meanings-DJO-9Ck9.mjs";
2
- import { a as Match, i as SearchOptions, n as FinderOptions, r as Hit, t as Finder } from "./finder-CvhfeMAo.mjs";
2
+ import { a as Match, i as SearchOptions, n as FinderOptions, r as Hit, t as Finder } from "./finder-BDvIlKo2.mjs";
3
3
  /** An English finder over `names`, code to name: read once, searched often. */
4
4
  export declare function createFinder(names: Record<string, string>, options?: FinderOptions): Finder;
5
5
  export type { Finder, FinderOptions, Hit, Match, Meanings, SearchOptions };
package/dist/en.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as createFinder$1 } from "./finder-Dv8sg_xg.mjs";
1
+ import { t as createFinder$1 } from "./finder-Dy0IOqpK.mjs";
2
2
  // src/en/language.ts
3
3
  const english = {
4
4
  locale: "en",
@@ -1,10 +1,11 @@
1
1
  import { t as Meanings } from "./meanings-DJO-9Ck9.mjs";
2
2
  /**
3
3
  * How a hit matched the query, best first: the whole name, its start, its words, its words with a
4
- * word of the query as the last part of a compound ("Butter" in "Süßrahmbutter"), some words of the
5
- * query only, or, when nothing else is found, a mistyped word read as the nearest known one.
4
+ * word of the query as the last part of a compound ("Butter" in "Süßrahmbutter"), its words with the
5
+ * last word of the query still being typed ("sulta" for "Sultanine"), some words of the query only,
6
+ * or, when nothing else is found, a mistyped word read as the nearest known one.
6
7
  */
7
- declare const MATCHES: readonly ["exact", "prefix", "words", "compound", "partial", "fuzzy"];
8
+ declare const MATCHES: readonly ["exact", "prefix", "words", "compound", "start", "partial", "fuzzy"];
8
9
  type Match = (typeof MATCHES)[number];
9
10
  type Hit = {
10
11
  code: string;
@@ -53,14 +53,16 @@ function readMeanings(sources, language) {
53
53
  // src/spelling.ts
54
54
  /**
55
55
  * How a hit matched the query, best first: the whole name, its start, its words, its words with a
56
- * word of the query as the last part of a compound ("Butter" in "Süßrahmbutter"), some words of the
57
- * query only, or, when nothing else is found, a mistyped word read as the nearest known one.
56
+ * word of the query as the last part of a compound ("Butter" in "Süßrahmbutter"), its words with the
57
+ * last word of the query still being typed ("sulta" for "Sultanine"), some words of the query only,
58
+ * or, when nothing else is found, a mistyped word read as the nearest known one.
58
59
  */
59
60
  const MATCHES = [
60
61
  "exact",
61
62
  "prefix",
62
63
  "words",
63
64
  "compound",
65
+ "start",
64
66
  "partial",
65
67
  "fuzzy"
66
68
  ];
@@ -114,6 +116,48 @@ function startsAsStem(asked, name, stem) {
114
116
  }
115
117
  return false;
116
118
  }
119
+ /**
120
+ * How far a name is from what is being typed, when the last word of the query is unfinished: the
121
+ * letters before it in the name and the letters still to type. The other words are whole words of
122
+ * the name, as in `rankOf`. Undefined when the name does not hold it so.
123
+ */
124
+ function startOf(asked, name, endsOf, stem = same) {
125
+ const last = asked.at(-1);
126
+ if (!last || !/^(\p{L}{3,}|\p{N}{2,})$/u.test(last)) return void 0;
127
+ const front = asked.slice(0, -1);
128
+ if (!covers(front, piecesOf(name, endsOf, stem), stem)) return void 0;
129
+ const lead = front.join("").length;
130
+ let nearest;
131
+ for (const [piece, places] of placesOf(name, endsOf, stem)) {
132
+ if (!piece.startsWith(last)) continue;
133
+ for (const at of places) {
134
+ const away = Math.abs(at - lead) + piece.length - last.length;
135
+ if (nearest === void 0 || away < nearest) nearest = away;
136
+ }
137
+ }
138
+ return nearest;
139
+ }
140
+ function placesOf(name, endsOf, stem = same) {
141
+ if (name.places) return name.places;
142
+ const places = /* @__PURE__ */ new Map();
143
+ const add = (piece, at) => {
144
+ places.set(piece, [...places.get(piece) ?? [], at]);
145
+ if (stem(piece) !== piece) places.set(stem(piece), [...places.get(stem(piece)) ?? [], at]);
146
+ };
147
+ let at = 0;
148
+ for (let start = 0; start < name.words.length; start++) {
149
+ const word = name.words[start];
150
+ let run = "";
151
+ for (const next of name.words.slice(start)) {
152
+ run += next;
153
+ add(run, at);
154
+ }
155
+ for (const head of endsOf?.(word) ?? []) add(head, at + word.length - head.length);
156
+ at += word.length;
157
+ }
158
+ name.places = places;
159
+ return places;
160
+ }
117
161
  function ask(words, endsOf) {
118
162
  return {
119
163
  words,
@@ -257,6 +301,16 @@ function createFinder(names, language, options = {}) {
257
301
  if (meant.length > 0) return unique([...meant, ...hits]);
258
302
  if (hits.length > 0) return hits;
259
303
  if (byPreparation.length > 0) return byPreparation;
304
+ const started = collect(scope, (spelled) => {
305
+ const away = startOf(asked, spelled, endsOf, stem);
306
+ return away === void 0 ? void 0 : {
307
+ rank: START,
308
+ covered: wanted,
309
+ missed: [],
310
+ away
311
+ };
312
+ });
313
+ if (started.length > 0) return started.sort(order);
260
314
  return partials(asked, scope).sort(order);
261
315
  }
262
316
  function mistyped(asked, scope) {
@@ -339,10 +393,11 @@ function createFinder(names, language, options = {}) {
339
393
  }
340
394
  return { search };
341
395
  }
396
+ const START = MATCHES.indexOf("start");
342
397
  const PARTIAL = MATCHES.indexOf("partial");
343
398
  const FUZZY = MATCHES.indexOf("fuzzy");
344
399
  const NOT_MEANT = Infinity;
345
- const better = (a, b) => a.rank - b.rank || b.covered - a.covered || a.meant - b.meant || a.length - b.length;
400
+ const better = (a, b) => a.rank - b.rank || b.covered - a.covered || (a.away ?? 0) - (b.away ?? 0) || a.meant - b.meant || a.length - b.length;
346
401
  function unique(hits) {
347
402
  const seen = /* @__PURE__ */ new Set();
348
403
  return hits.filter(({ code }) => !seen.has(code) && seen.add(code));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thereforejs/food-finder",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Find foods of @thereforejs/foods by what people type: search, aliases and language rules, loaded per language",
5
5
  "keywords": [
6
6
  "bls",