@whiteslove/parsing-lexicon 0.9.7 → 0.9.8
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/package.json +1 -1
- package/src/alias-prefilter.js +11 -2
package/package.json
CHANGED
package/src/alias-prefilter.js
CHANGED
|
@@ -113,8 +113,13 @@ function indexFor(entries) {
|
|
|
113
113
|
* Equivalent to `entries.find((entry) => entry.re.test(text))`, including the
|
|
114
114
|
* "first in list order wins" tie-break, but only compiles and runs the regexes
|
|
115
115
|
* of entries the text could plausibly contain.
|
|
116
|
+
*
|
|
117
|
+
* `accept(entry, matchedText)` optionally vets each hit before it wins. It
|
|
118
|
+
* receives the substring the alias regex actually matched, which is what a
|
|
119
|
+
* caller needs to tell a genuine name from an alias that merely repeats some
|
|
120
|
+
* other place's name; rejecting a hit continues the scan rather than ending it.
|
|
116
121
|
*/
|
|
117
|
-
export function matchFirstEntry(entries, text) {
|
|
122
|
+
export function matchFirstEntry(entries, text, accept) {
|
|
118
123
|
if (!Array.isArray(entries) || !entries.length) return undefined;
|
|
119
124
|
const value = String(text || '');
|
|
120
125
|
if (!value) return undefined;
|
|
@@ -130,7 +135,11 @@ export function matchFirstEntry(entries, text) {
|
|
|
130
135
|
|
|
131
136
|
for (const i of [...candidates].sort((a, b) => a - b)) {
|
|
132
137
|
const entry = entries[i];
|
|
133
|
-
|
|
138
|
+
// `re` carries no /g flag, so exec() is stateless and safe to reuse here.
|
|
139
|
+
const match = entry?.re?.exec(value);
|
|
140
|
+
if (!match) continue;
|
|
141
|
+
if (accept && !accept(entry, match[0])) continue;
|
|
142
|
+
return entry;
|
|
134
143
|
}
|
|
135
144
|
return undefined;
|
|
136
145
|
}
|