Skip to content

Commit

Permalink
add missing src/transform.mjs from template
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Jun 20, 2024
1 parent 562717f commit f783c40
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/transform.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export * from "./properties-transformer.mjs";
export * from "./expression-transformer.mjs";
export * from "./matcher.mjs";

import { ContentEntry} from "content-entry";

/**
* Apply transformers.
* @param {AsyncIterable<ContentEntry>} source
* @param {Transformer[]} transformers
* @param {boolean} onlyMatching filter out all none matching entries
*/
export async function* transform(source, transformers = [], onlyMatching) {
const usedTransformers = new Set();

for await (let entry of source) {
let didMatch = false;
for (const t of transformers) {
if (t.match(entry)) {
didMatch = true;
entry = await t.transform(entry);
usedTransformers.add(t);
}
}

if (didMatch || !onlyMatching) {
yield entry;
}
}

for (const t of transformers) {
if (!usedTransformers.has(t) && t.createEntryWhenMissing !== undefined) {
yield t.transform(await t.createEntryWhenMissing());
}
}
}

0 comments on commit f783c40

Please sign in to comment.