INNER CODE UNIT · TypeScript
search
dolanmiu/docx · demo/index.ts:29
const search = (term: string): readonly string[] => {
const words = term.toLowerCase().split(/\s+/).filter(Boolean);
return demos.filter((demo) => words.every((word) => demo.includes(word)));
};
// A path to a demo works too, such as demo/tables/basic-table.ts
const term = process.argv
.slice(2)
.join(" ")
.replace(/^(\.\/)?demo\//, "")
.replace(/\.ts$/, "");
const topics = new Set(demos.map((demo) => path.dirname(demo)));
// A demo's name runs it, but a topic's name lists its demos, even when one of them has the topic's name.
// Searching for "text/" rather than "text" leaves out the demos of other topics, such as text-boxes
const query = topics.has(term) ? `${term}/` : term;
const found = demos.includes(term) ? [term] : topics.has(term) ? [] : demos.filter((demo) => demo.endsWith(`/${term}`));
const matches = found.length === 1 ? found : search(query);