fix rules failing no-prefix extracts
This commit is contained in:
parent
8c18398ae4
commit
fa524a5976
4 changed files with 54 additions and 5 deletions
16
lib/rule.ts
16
lib/rule.ts
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue