fix rules failing no-prefix extracts

This commit is contained in:
JOLIMAITRE Matthieu 2024-08-07 11:18:17 +02:00
parent 8c18398ae4
commit fa524a5976
4 changed files with 54 additions and 5 deletions

View file

@ -17,18 +17,26 @@ export class Rule<V extends string[]> {
if (text.length > 0) return null;
else return {} as StructOfArr<V, string>;
}
if (!text.includes(first_sep.word)) return null;
let [val, rest] = text.split(first_sep.word, 2);
let [val, rest] = split_once(text, first_sep.word);
if (rest === null) return null;
const result = {} as Record<string, string>;
for (const [key, sep] of zip(this.vars, this.separators.slice(1))) {
if (sep.kind === "end") {
result[key] = rest;
break;
}
if (!rest.includes(sep.word)) return null;
[val, rest] = rest.split(sep.word, 2);
[val, rest] = split_once(rest, sep.word);
if (rest === null) return null;
result[key] = val;
}
return result as StructOfArr<V, string>;
}
}
function split_once(text: string, separator: string) {
const cursor = text.indexOf(separator);
if (cursor === -1) return [text, null] as const;
const left = text.slice(0, cursor);
const right = text.slice(cursor + separator.length);
return [left, right] as const;
}