@thereforejs/food-finder 0.0.0 → 0.0.1
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/LICENSE +21 -0
- package/README.md +129 -2
- package/dist/de/meanings.d.mts +2 -0
- package/dist/de/meanings.mjs +24 -0
- package/dist/de.d.mts +5 -0
- package/dist/de.mjs +23 -0
- package/dist/en/meanings.d.mts +2 -0
- package/dist/en/meanings.mjs +3 -0
- package/dist/en.d.mts +5 -0
- package/dist/en.mjs +13 -0
- package/dist/finder-CHsScx2h.d.mts +35 -0
- package/dist/finder-yEvLck_3.mjs +244 -0
- package/dist/meanings-DJO-9Ck9.d.mts +6 -0
- package/package.json +45 -17
- package/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Florian Heuberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,130 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">food-finder</h1>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
Find foods of <a href="https://github.com/thereforejs/foods"><code>@thereforejs/foods</code></a> by what people type<br>
|
|
5
|
+
or what a supplier calls a raw material. Best first, and it says how each hit matched.
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
"Butter" finds Süßrahmbutter, "Weizenmehl Typ 550" finds "Weizen Mehl, Type 550", and "Weizenmehl
|
|
9
|
+
T550 25 kg Sack" still finds it, with the words it could not place.
|
|
10
|
+
|
|
11
|
+
> [!WARNING]
|
|
12
|
+
> Early development (0.x). The API may still change.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm install @thereforejs/food-finder @thereforejs/foods
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Use
|
|
21
|
+
|
|
22
|
+
Create a finder once per language and search it often. The names come from foods; load them when
|
|
23
|
+
you need them:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createFinder } from "@thereforejs/food-finder/de";
|
|
27
|
+
import { meanings } from "@thereforejs/food-finder/de/meanings";
|
|
28
|
+
|
|
29
|
+
const { names } = await import("@thereforejs/foods/names/de");
|
|
30
|
+
const finder = createFinder(names, { meanings: [meanings] });
|
|
31
|
+
|
|
32
|
+
finder.search("Butter", { limit: 3 });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
You get:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
[
|
|
39
|
+
{ code: "Q630000", name: "Süßrahmbutter", match: "exact", meant: true, missed: [] },
|
|
40
|
+
{ code: "Q6A4000", name: "Butter gesalzen", match: "prefix", meant: false, missed: [] },
|
|
41
|
+
{ code: "Q611000", name: "Butter mild gesäuert", match: "prefix", meant: false, missed: [] },
|
|
42
|
+
];
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
- **`match`**: how the hit matched, best first:
|
|
46
|
+
- `exact`: the whole name. Spaces never matter: "Weizenmehl Typ 550" is "Weizen Mehl, Type 550".
|
|
47
|
+
- `prefix`: the start of the name, ending at a word: "Weizenmehl" finds "Weizen Mehl, Type 405",
|
|
48
|
+
never "Weizenmehlschwitze".
|
|
49
|
+
- `words`: every word of the query is a whole word of the name, any order.
|
|
50
|
+
- `compound`: a word of the query is the last part of a compound: "Butter" finds "Kakaobutter".
|
|
51
|
+
- `partial`: only when no name holds the whole query, the names that hold the most of it.
|
|
52
|
+
- **`meant`**: found through a meaning (below), not the name.
|
|
53
|
+
- **`missed`**: the words of the query, as read, that the name does not hold (`partial` only).
|
|
54
|
+
|
|
55
|
+
Options of `search`: `limit` (at most this many hits) and `group` (only codes starting with it, the
|
|
56
|
+
main group of the BLS).
|
|
57
|
+
|
|
58
|
+
In Vue or Pinia, keep the finder out of reactivity. It holds every name prepared for searching:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const finder = markRaw(createFinder(names, { meanings: [meanings] }));
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Raw materials
|
|
65
|
+
|
|
66
|
+
Suppliers add packaging, types and brands that no food name has. When nothing holds the whole query,
|
|
67
|
+
the finder proposes the names that hold the most of it:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
finder.search("Weizenmehl T550 25 kg Sack")[0];
|
|
71
|
+
// { code: "C214200", name: "Weizen Mehl, Type 550", match: "partial", meant: false,
|
|
72
|
+
// missed: ["t", "25", "kg", "sack"] }
|
|
73
|
+
|
|
74
|
+
finder.search("Rindergelatine")[0];
|
|
75
|
+
// { code: "R468000", name: "Gelatine/Speisegelatine", match: "partial", meant: false,
|
|
76
|
+
// missed: ["rinder"] }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
What cannot be told apart stays unfound: "Milcheiweiß" finds no "Lyoner weiß".
|
|
80
|
+
|
|
81
|
+
**Assign directly** when the first hit is `exact` and no other hit is. Anything else is a proposal: let
|
|
82
|
+
a human pick.
|
|
83
|
+
|
|
84
|
+
## Meanings
|
|
85
|
+
|
|
86
|
+
What people mean by an everyday word: "Butter" is "Süßrahmbutter", "Milch" is "Vollmilch frisch,
|
|
87
|
+
3,5 % Fett". A word that is undefined on its own means several foods, so a human picks: "Mehl" is
|
|
88
|
+
every typed flour of wheat, rye and spelt.
|
|
89
|
+
|
|
90
|
+
Pass your own before ours; the first source wins a word:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const bakery = { Mehl: ["C214200"] }; // in this bakery, "Mehl" is Type 550
|
|
94
|
+
const finder = createFinder(names, { meanings: [bakery, meanings] });
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
A meaning may name any code the names hold, your own raw materials too:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const finder = createFinder(
|
|
101
|
+
{ ...names, "own:42": "Roggenmischung nach Hausrezept" },
|
|
102
|
+
{ meanings: [{ Hausmischung: ["own:42"] }, meanings] },
|
|
103
|
+
);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Languages
|
|
107
|
+
|
|
108
|
+
One entry point per language, so a German application holds no English:
|
|
109
|
+
|
|
110
|
+
| Import | Reads |
|
|
111
|
+
| ------------------------------------ | ---------------------------------------------------------------- |
|
|
112
|
+
| `@thereforejs/food-finder/de` | German: ß/ä/ö/ü typed as ss/ae/oe/ue, "Typ" as "Type", compounds |
|
|
113
|
+
| `@thereforejs/food-finder/en` | English: accents typed without them |
|
|
114
|
+
| `.../de/meanings`, `.../en/meanings` | the meanings of each language, only when you import them |
|
|
115
|
+
|
|
116
|
+
## Size
|
|
117
|
+
|
|
118
|
+
Measured 2026-10-29, an application built with the Vite+ bundler, minified, gzip:
|
|
119
|
+
|
|
120
|
+
| Application | App part | Names, loaded on first use |
|
|
121
|
+
| ------------------------ | -------- | -------------------------- |
|
|
122
|
+
| `/de` | 2,255 B | 69,641 B |
|
|
123
|
+
| `/de` and `/de/meanings` | 2,390 B | 69,641 B |
|
|
124
|
+
| `/en` | 2,173 B | 67,923 B |
|
|
125
|
+
|
|
126
|
+
On the 7,108 German names, `createFinder` takes about 65 ms and a search about 10 to 40 ms.
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/de/meanings.ts
|
|
2
|
+
const meanings = {
|
|
3
|
+
Butter: ["Q630000"],
|
|
4
|
+
Ei: ["E111100"],
|
|
5
|
+
Mehl: [
|
|
6
|
+
"C214100",
|
|
7
|
+
"C214200",
|
|
8
|
+
"C213100",
|
|
9
|
+
"C213200",
|
|
10
|
+
"C213500",
|
|
11
|
+
"C223100",
|
|
12
|
+
"C223200",
|
|
13
|
+
"C223300",
|
|
14
|
+
"C223400",
|
|
15
|
+
"C223600",
|
|
16
|
+
"C234000",
|
|
17
|
+
"C234100",
|
|
18
|
+
"C234200"
|
|
19
|
+
],
|
|
20
|
+
Milch: ["M111300"],
|
|
21
|
+
Sahne: ["M173800", "M173900"],
|
|
22
|
+
Zucker: ["S111000"]
|
|
23
|
+
};
|
|
24
|
+
export { meanings };
|
package/dist/de.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
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-CHsScx2h.mjs";
|
|
3
|
+
/** A German finder over `names`, code to name: read once, searched often. */
|
|
4
|
+
export declare function createFinder(names: Record<string, string>, options?: FinderOptions): Finder;
|
|
5
|
+
export type { Finder, FinderOptions, Hit, Match, Meanings, SearchOptions };
|
package/dist/de.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { t as createFinder$1 } from "./finder-yEvLck_3.mjs";
|
|
2
|
+
// src/de/language.ts
|
|
3
|
+
const LETTERS = {
|
|
4
|
+
ß: "ss",
|
|
5
|
+
ä: "ae",
|
|
6
|
+
ö: "oe",
|
|
7
|
+
ü: "ue"
|
|
8
|
+
};
|
|
9
|
+
const german = {
|
|
10
|
+
locale: "de-DE",
|
|
11
|
+
spell: (text) => text.replace(/[ßäöü]/g, (char) => LETTERS[char] ?? char),
|
|
12
|
+
same: /* @__PURE__ */ new Map([["typ", "type"]]),
|
|
13
|
+
compounds: {
|
|
14
|
+
endings: ["frei"],
|
|
15
|
+
units: ["buchweizen"]
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
// src/de/index.ts
|
|
19
|
+
/** A German finder over `names`, code to name: read once, searched often. */
|
|
20
|
+
function createFinder(names, options) {
|
|
21
|
+
return createFinder$1(names, german, options);
|
|
22
|
+
}
|
|
23
|
+
export { createFinder };
|
package/dist/en.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
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-CHsScx2h.mjs";
|
|
3
|
+
/** An English finder over `names`, code to name: read once, searched often. */
|
|
4
|
+
export declare function createFinder(names: Record<string, string>, options?: FinderOptions): Finder;
|
|
5
|
+
export type { Finder, FinderOptions, Hit, Match, Meanings, SearchOptions };
|
package/dist/en.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { t as createFinder$1 } from "./finder-yEvLck_3.mjs";
|
|
2
|
+
// src/en/language.ts
|
|
3
|
+
const english = {
|
|
4
|
+
locale: "en",
|
|
5
|
+
spell: (text) => text.normalize("NFD").replace(/\p{M}/gu, "").normalize("NFC"),
|
|
6
|
+
same: /* @__PURE__ */ new Map()
|
|
7
|
+
};
|
|
8
|
+
// src/en/index.ts
|
|
9
|
+
/** An English finder over `names`, code to name: read once, searched often. */
|
|
10
|
+
function createFinder(names, options) {
|
|
11
|
+
return createFinder$1(names, english, options);
|
|
12
|
+
}
|
|
13
|
+
export { createFinder };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { t as Meanings } from "./meanings-DJO-9Ck9.mjs";
|
|
2
|
+
/**
|
|
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"), or some words of
|
|
5
|
+
* the query only.
|
|
6
|
+
*/
|
|
7
|
+
declare const MATCHES: readonly ["exact", "prefix", "words", "compound", "partial"];
|
|
8
|
+
type Match = (typeof MATCHES)[number];
|
|
9
|
+
type Hit = {
|
|
10
|
+
code: string;
|
|
11
|
+
name: string;
|
|
12
|
+
match: Match;
|
|
13
|
+
/** Found through a meaning, not the name: "Butter" is "Süßrahmbutter". */
|
|
14
|
+
meant: boolean;
|
|
15
|
+
/** The words of the query, as read, that no word of the name holds (`partial` only). */
|
|
16
|
+
missed: string[];
|
|
17
|
+
};
|
|
18
|
+
type SearchOptions = {
|
|
19
|
+
/** At most this many hits. */
|
|
20
|
+
limit?: number;
|
|
21
|
+
/** Only foods of this main group, the first letter of the code. */
|
|
22
|
+
group?: string;
|
|
23
|
+
};
|
|
24
|
+
type Finder = {
|
|
25
|
+
/**
|
|
26
|
+
* The foods whose name matches `query`, best first. It proposes; picking one stays with a human.
|
|
27
|
+
* Spaces never matter: "Weizenmehl Typ 550" is "Weizen Mehl, Type 550".
|
|
28
|
+
*/
|
|
29
|
+
search: (query: string, options?: SearchOptions) => Hit[];
|
|
30
|
+
};
|
|
31
|
+
type FinderOptions = {
|
|
32
|
+
/** What everyday words mean, the first source winning a word: `[bakery, meanings]`. */
|
|
33
|
+
meanings?: Meanings[];
|
|
34
|
+
};
|
|
35
|
+
export { Match as a, SearchOptions as i, FinderOptions as n, Hit as r, Finder as t };
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// src/compounds.ts
|
|
2
|
+
/**
|
|
3
|
+
* The words a German compound ends with: "Roggenvollkornbrot" is a "Vollkornbrot" and a "Brot".
|
|
4
|
+
*/
|
|
5
|
+
function heads(word, reading) {
|
|
6
|
+
const { known, units, found } = reading;
|
|
7
|
+
const cached = found.get(word);
|
|
8
|
+
if (cached) return cached;
|
|
9
|
+
const candidates = [];
|
|
10
|
+
for (let cut = 2; cut < word.length - 1; cut++) {
|
|
11
|
+
const stem = word.slice(0, cut);
|
|
12
|
+
const head = word.slice(cut);
|
|
13
|
+
if (!known.has(head) || !/[aeiouy]/.test(stem)) continue;
|
|
14
|
+
if (units.some((unit) => splits(word, unit, cut))) continue;
|
|
15
|
+
candidates.push({
|
|
16
|
+
head,
|
|
17
|
+
sure: known.has(stem)
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const sure = candidates.filter((candidate) => candidate.sure);
|
|
21
|
+
const leads = sure.length > 0 ? sure : candidates.slice(0, 1);
|
|
22
|
+
const result = candidates.map(({ head }) => head).filter((head) => leads.every((lead) => lead.head === head || (lead.head.length > head.length ? heads(lead.head, reading).includes(head) : heads(head, reading).includes(lead.head))));
|
|
23
|
+
found.set(word, result);
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
function splits(word, unit, cut) {
|
|
27
|
+
return word.slice(Math.max(0, cut - unit.length + 1), cut + unit.length - 1).includes(unit);
|
|
28
|
+
}
|
|
29
|
+
// src/words.ts
|
|
30
|
+
/** The words of a name or a query as they are compared; a decimal point reads as a comma. */
|
|
31
|
+
function words(text, language) {
|
|
32
|
+
return (language.spell(text.normalize("NFC").toLocaleLowerCase(language.locale)).match(/\d+(?:[.,]\d+)?|\p{L}+|\p{N}|%/gu) ?? []).map((word) => language.same.get(word) ?? word.replace(".", ","));
|
|
33
|
+
}
|
|
34
|
+
// src/meanings.ts
|
|
35
|
+
/** The meanings of every code, the first source winning a word. */
|
|
36
|
+
function readMeanings(sources, language) {
|
|
37
|
+
const taken = /* @__PURE__ */ new Set();
|
|
38
|
+
const byCode = /* @__PURE__ */ new Map();
|
|
39
|
+
for (const source of sources) for (const [key, codes] of Object.entries(source)) {
|
|
40
|
+
const read = words(key, language);
|
|
41
|
+
const id = read.join(" ");
|
|
42
|
+
if (taken.has(id)) continue;
|
|
43
|
+
taken.add(id);
|
|
44
|
+
codes.forEach((code, order) => {
|
|
45
|
+
byCode.set(code, [...byCode.get(code) ?? [], {
|
|
46
|
+
words: read,
|
|
47
|
+
order
|
|
48
|
+
}]);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return byCode;
|
|
52
|
+
}
|
|
53
|
+
// src/spelling.ts
|
|
54
|
+
/**
|
|
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"), or some words of
|
|
57
|
+
* the query only.
|
|
58
|
+
*/
|
|
59
|
+
const MATCHES = [
|
|
60
|
+
"exact",
|
|
61
|
+
"prefix",
|
|
62
|
+
"words",
|
|
63
|
+
"compound",
|
|
64
|
+
"partial"
|
|
65
|
+
];
|
|
66
|
+
function spell(words) {
|
|
67
|
+
const ends = /* @__PURE__ */ new Set();
|
|
68
|
+
const spans = /* @__PURE__ */ new Set();
|
|
69
|
+
let end = 0;
|
|
70
|
+
for (let start = 0; start < words.length; start++) {
|
|
71
|
+
end += words[start].length;
|
|
72
|
+
ends.add(end);
|
|
73
|
+
for (let last = start + 1; last <= words.length; last++) spans.add(words.slice(start, last).join(""));
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
words,
|
|
77
|
+
whole: words.join(""),
|
|
78
|
+
ends,
|
|
79
|
+
spans
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The index of the match in `MATCHES`, -1 for none. `endsOf` gives the words a word of a name ends
|
|
84
|
+
* with, if the language builds compounds.
|
|
85
|
+
*/
|
|
86
|
+
function rankOf(asked, name, endsOf) {
|
|
87
|
+
const wanted = asked.join("");
|
|
88
|
+
if (!wanted) return -1;
|
|
89
|
+
if (name.whole === wanted) return 0;
|
|
90
|
+
if (name.ends.has(wanted.length) && name.whole.startsWith(wanted)) return 1;
|
|
91
|
+
if (covers(asked, name.spans)) return 2;
|
|
92
|
+
if (!endsOf) return -1;
|
|
93
|
+
return covers(asked, piecesOf(name, endsOf)) ? 3 : -1;
|
|
94
|
+
}
|
|
95
|
+
function ask(words, endsOf) {
|
|
96
|
+
return {
|
|
97
|
+
words,
|
|
98
|
+
runs: words.map((_, end) => words.map((_, start) => words.slice(start, end + 1).join(""))),
|
|
99
|
+
tails: words.map((word) => endsOf?.(word) ?? [])
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* How much of the query a name holds when it does not hold all of it: the letters found, and the
|
|
104
|
+
* words missed. A word of the query may be found by its last part: "Rindergelatine" is a
|
|
105
|
+
* "Gelatine" with "rinder" missed. Longer words carry more meaning, so letters count, not words.
|
|
106
|
+
*/
|
|
107
|
+
function partialOf(asked, name, endsOf) {
|
|
108
|
+
const pieces = piecesOf(name, endsOf);
|
|
109
|
+
const best = [{
|
|
110
|
+
covered: 0,
|
|
111
|
+
missed: []
|
|
112
|
+
}];
|
|
113
|
+
for (let end = 1; end <= asked.words.length; end++) {
|
|
114
|
+
const word = asked.words[end - 1];
|
|
115
|
+
const before = best[end - 1];
|
|
116
|
+
const options = [{
|
|
117
|
+
covered: before.covered,
|
|
118
|
+
missed: [...before.missed, word]
|
|
119
|
+
}];
|
|
120
|
+
for (let start = 0; start < end; start++) {
|
|
121
|
+
const run = asked.runs[end - 1][start];
|
|
122
|
+
if (!pieces.has(run)) continue;
|
|
123
|
+
options.push({
|
|
124
|
+
covered: best[start].covered + run.length,
|
|
125
|
+
missed: best[start].missed
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
for (const tail of asked.tails[end - 1]) {
|
|
129
|
+
if (!pieces.has(tail) || tail.length * 2 < word.length) continue;
|
|
130
|
+
const stem = word.slice(0, -tail.length);
|
|
131
|
+
options.push({
|
|
132
|
+
covered: before.covered + tail.length,
|
|
133
|
+
missed: [...before.missed, stem]
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
best[end] = options.reduce((a, b) => b.covered > a.covered ? b : a);
|
|
137
|
+
}
|
|
138
|
+
const found = best[asked.words.length];
|
|
139
|
+
return found.covered > 0 ? found : void 0;
|
|
140
|
+
}
|
|
141
|
+
function piecesOf(name, endsOf) {
|
|
142
|
+
if (!endsOf) return name.spans;
|
|
143
|
+
name.compounds ??= /* @__PURE__ */ new Set([...name.spans, ...name.words.flatMap((word) => endsOf(word))]);
|
|
144
|
+
return name.compounds;
|
|
145
|
+
}
|
|
146
|
+
function covers(asked, spans) {
|
|
147
|
+
const reached = [true];
|
|
148
|
+
for (let end = 1; end <= asked.length; end++) reached[end] = asked.slice(0, end).some((_, start) => reached[start] === true && spans.has(asked.slice(start, end).join("")));
|
|
149
|
+
return reached[asked.length] === true;
|
|
150
|
+
}
|
|
151
|
+
// src/variants.ts
|
|
152
|
+
const ALTERNATIVES = /[^\s,()/]*\/[^\s,()]*/g;
|
|
153
|
+
/**
|
|
154
|
+
* A name and its spellings with the BLS slash synonyms, "Weizenbrot/Weißbrot", "mit
|
|
155
|
+
* Nugat/Nougat/Noisette": each alternative replaces only its own word, a shorter group keeps its
|
|
156
|
+
* last alternative.
|
|
157
|
+
*/
|
|
158
|
+
function variants(name) {
|
|
159
|
+
const groups = name.match(ALTERNATIVES) ?? [];
|
|
160
|
+
const count = Math.max(0, ...groups.map((group) => group.split("/").length));
|
|
161
|
+
return [name, ...Array.from({ length: count }, (_, index) => name.replace(ALTERNATIVES, (group) => {
|
|
162
|
+
const alternatives = group.split("/");
|
|
163
|
+
return alternatives[Math.min(index, alternatives.length - 1)];
|
|
164
|
+
}))];
|
|
165
|
+
}
|
|
166
|
+
// src/finder.ts
|
|
167
|
+
/** Reads every name once; every search reuses it. `names` is code to name. */
|
|
168
|
+
function createFinder(names, language, options = {}) {
|
|
169
|
+
const foods = Object.entries(names).map(([code, name]) => ({
|
|
170
|
+
code,
|
|
171
|
+
name,
|
|
172
|
+
spellings: variants(name).map((variant) => spell(words(variant, language)))
|
|
173
|
+
}));
|
|
174
|
+
const endsOf = language.compounds && compoundsOf(foods, language.compounds);
|
|
175
|
+
const meant = readMeanings(options.meanings ?? [], language);
|
|
176
|
+
for (const food of foods) for (const { words: read, order } of meant.get(food.code) ?? []) food.spellings.push({
|
|
177
|
+
...spell(read),
|
|
178
|
+
meant: order
|
|
179
|
+
});
|
|
180
|
+
function search(query, options = {}) {
|
|
181
|
+
const asked = words(query, language);
|
|
182
|
+
const wanted = asked.join("").length;
|
|
183
|
+
const scope = foods.filter(({ code }) => !options.group || code.startsWith(options.group));
|
|
184
|
+
let hits = collect(scope, (spelled) => {
|
|
185
|
+
const rank = rankOf(asked, spelled, endsOf);
|
|
186
|
+
return rank < 0 ? void 0 : {
|
|
187
|
+
rank,
|
|
188
|
+
covered: wanted,
|
|
189
|
+
missed: []
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
if (hits.length === 0) {
|
|
193
|
+
const read = ask(asked, endsOf);
|
|
194
|
+
hits = collect(scope, (spelled) => {
|
|
195
|
+
const partial = partialOf(read, spelled, endsOf);
|
|
196
|
+
return partial && {
|
|
197
|
+
rank: PARTIAL,
|
|
198
|
+
...partial
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
hits.sort((a, b) => better(a, b) || a.name.localeCompare(b.name, language.locale) || a.code.localeCompare(b.code));
|
|
203
|
+
return hits.slice(0, options.limit).map(({ code, name, rank, meant, missed }) => ({
|
|
204
|
+
code,
|
|
205
|
+
name,
|
|
206
|
+
match: MATCHES[rank],
|
|
207
|
+
meant: meant !== NOT_MEANT,
|
|
208
|
+
missed
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
return { search };
|
|
212
|
+
}
|
|
213
|
+
const PARTIAL = MATCHES.indexOf("partial");
|
|
214
|
+
const NOT_MEANT = Infinity;
|
|
215
|
+
const better = (a, b) => a.rank - b.rank || b.covered - a.covered || a.meant - b.meant || a.length - b.length;
|
|
216
|
+
function collect(foods, read) {
|
|
217
|
+
return foods.flatMap(({ code, name, spellings }) => {
|
|
218
|
+
const found = spellings.flatMap((spelled) => {
|
|
219
|
+
const result = read(spelled);
|
|
220
|
+
if (!result) return [];
|
|
221
|
+
return [{
|
|
222
|
+
...result,
|
|
223
|
+
meant: spelled.meant ?? NOT_MEANT,
|
|
224
|
+
length: spelled.whole.length
|
|
225
|
+
}];
|
|
226
|
+
}).sort(better)[0];
|
|
227
|
+
return found ? [{
|
|
228
|
+
code,
|
|
229
|
+
name,
|
|
230
|
+
...found
|
|
231
|
+
}] : [];
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
function compoundsOf(foods, rules) {
|
|
235
|
+
const known = new Set(foods.flatMap((food) => food.spellings.flatMap((spelled) => spelled.words)));
|
|
236
|
+
for (const ending of rules.endings) known.add(ending);
|
|
237
|
+
const reading = {
|
|
238
|
+
known,
|
|
239
|
+
units: rules.units,
|
|
240
|
+
found: /* @__PURE__ */ new Map()
|
|
241
|
+
};
|
|
242
|
+
return (word) => heads(word, reading);
|
|
243
|
+
}
|
|
244
|
+
export { createFinder as t };
|
package/package.json
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thereforejs/food-finder",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"main": "index.js",
|
|
3
|
+
"version": "0.0.1",
|
|
5
4
|
"description": "Find foods of @thereforejs/foods by what people type: search, aliases and language rules, loaded per language",
|
|
6
|
-
"publishConfig": {
|
|
7
|
-
"access": "public"
|
|
8
|
-
},
|
|
9
|
-
"author": "Florian Heuberger",
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"homepage": "https://github.com/thereforejs/food-finder#readme",
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/thereforejs/food-finder.git"
|
|
15
|
-
},
|
|
16
|
-
"bugs": {
|
|
17
|
-
"url": "https://github.com/thereforejs/food-finder/issues"
|
|
18
|
-
},
|
|
19
5
|
"keywords": [
|
|
20
6
|
"bls",
|
|
21
7
|
"food-database",
|
|
@@ -23,5 +9,47 @@
|
|
|
23
9
|
"fuzzy-search",
|
|
24
10
|
"nutrition",
|
|
25
11
|
"search"
|
|
26
|
-
]
|
|
27
|
-
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/thereforejs/food-finder#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/thereforejs/food-finder/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Florian Heuberger",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/thereforejs/food-finder.git"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"exports": {
|
|
29
|
+
"./de": "./dist/de.mjs",
|
|
30
|
+
"./de/meanings": "./dist/de/meanings.mjs",
|
|
31
|
+
"./en": "./dist/en.mjs",
|
|
32
|
+
"./en/meanings": "./dist/en/meanings.mjs",
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"provenance": true
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@thereforejs/foods": "^0.0.1",
|
|
41
|
+
"@types/node": "^26.6.5",
|
|
42
|
+
"fast-check": "^4.10.2",
|
|
43
|
+
"typescript": "^7.0.2",
|
|
44
|
+
"vite-plus": "1.4.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@thereforejs/foods": ">=0.0.1 <1"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "vp pack",
|
|
51
|
+
"dev": "vp pack --watch",
|
|
52
|
+
"test": "vp test",
|
|
53
|
+
"check": "vp check"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = {};
|