init
This commit is contained in:
commit
8c18398ae4
6 changed files with 111 additions and 0 deletions
28
lib/lib.ts
Normal file
28
lib/lib.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { Rule } from "./rule.ts";
|
||||
import { PairElts, StructOfArr } from "./types.ts";
|
||||
import { Extractor } from "./extractor.ts"
|
||||
|
||||
export function extr<A extends string[]>(...args: A): Extractor<StructOfArr<PairElts<A>, string>> {
|
||||
const seps = [] as string[];
|
||||
const vars = [] as string[];
|
||||
|
||||
let pair = true;
|
||||
for (const arg of args) {
|
||||
if (pair) seps.push(arg);
|
||||
else vars.push(arg);
|
||||
pair = !pair;
|
||||
}
|
||||
|
||||
const vars_ = vars as PairElts<A>;
|
||||
const rule = new Rule(seps, vars_);
|
||||
return new Extractor(text => rule.get(text))
|
||||
}
|
||||
|
||||
Deno.test("types", async () => {
|
||||
const { assertEquals } = await import("https://deno.land/std@0.224.0/assert/assert_equals.ts");
|
||||
const vars = extr()
|
||||
.or(extr("bonjour, ", 'animal', ", poilu"))
|
||||
.or(extr("bonjour, ", 'arbre', ", feuillu"))
|
||||
.get("bonjour, le chêne, feuillu");
|
||||
assertEquals({ vars }, { vars: { "arbre": "le chêne" } })
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue