microgrok/lib/extractor.ts
2024-08-07 10:17:45 +02:00

21 lines
523 B
TypeScript

export class Extractor<O> {
private extract: (text: string) => O | null;
public constructor(extract: (text: string) => O | null) {
this.extract = extract;
}
public get(text: string) {
return this.extract(text);
}
public or<T>(other: Extractor<T>) {
const this_extract = this.extract;
return new Extractor(text => {
const r1 = this_extract(text);
if (r1 !== null) return r1;
else return other.extract(text);
});
}
}