Skip to content

Commit 97df40e

Browse files
committed
feat: add predicates
fixes #1
1 parent 8c7f44f commit 97df40e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/loom.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@ export interface LoomConfig<
88
TInputSchema extends z.ZodType,
99
TOptionsSchema extends z.ZodType,
1010
> {
11+
/**
12+
* The schema of the input data.
13+
*/
1114
inputSchema: TInputSchema;
15+
16+
/**
17+
* The schema of the options.
18+
*/
1219
optionsSchema: TOptionsSchema;
20+
21+
/**
22+
* The template function.
23+
*/
1324
template: (
1425
ctx: LoomContext<TOptionsSchema>,
1526
item: TInputSchema["_input"]
1627
) => string;
28+
29+
/**
30+
* The predicate function.
31+
*/
32+
predicate?: (ctx: LoomContext<TOptionsSchema>, item: TInputSchema["_input"]) => boolean;
1733
}
1834

1935
export interface LoomInstance<
@@ -43,6 +59,11 @@ export function createLoom<
4359
const lines: string[] = [];
4460

4561
for (const item of validatedInput) {
62+
// if predicate is defined, and returns false, skip this item
63+
if (config.predicate && !config.predicate(ctx, item)) {
64+
continue;
65+
}
66+
4667
lines.push(config.template(ctx, item));
4768
}
4869

0 commit comments

Comments
 (0)