Skip to content

Commit

Permalink
feat: content fuzzy find
Browse files Browse the repository at this point in the history
  • Loading branch information
Bbanez committed Oct 27, 2023
1 parent e00a90b commit b81653e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/handlers/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,44 @@ export function createBcmsMostContentHandler({
}
return null;
},
fuzzy: {
async find(query, skipStatusCheck) {
const contentCache = await cache.content.get(false, skipStatusCheck);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output: any[] = [];
for (const template in contentCache) {
if (contentCache[template]) {
for (let i = 0; i < contentCache[template].length; i++) {
const item = contentCache[template][i];
if (await query(item, contentCache)) {
output.push(item);
}
}
} else {
// eslint-disable-next-line no-console
console.warn(`Template "${template}" does not exist.`);
}
}
return output;
},
async findOne(query, skipStatusCheck) {
const contentCache = await cache.content.get(false, skipStatusCheck);
for (const template in contentCache) {
if (contentCache[template]) {
for (let i = 0; i < contentCache[template].length; i++) {
const item = contentCache[template][i];
if (await query(item, contentCache)) {
return item as never;
}
}
} else {
// eslint-disable-next-line no-console
console.warn(`Template "${template}" does not exist.`);
}
}
return null;
},
},
},
};
}
10 changes: 10 additions & 0 deletions src/types/handlers/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ export interface BCMSMostContentHandler {
query: BCMSMostContentEntryQueryFunction<unknown>,
skipStatusCheck?: boolean,
): Promise<EntryType[]>;
fuzzy: {
findOne<EntryType extends BCMSEntryParsed = BCMSEntryParsed>(
query: BCMSMostContentEntryQueryFunction<unknown>,
skipStatusCheck?: boolean,
): Promise<EntryType | null>;
find<EntryType extends BCMSEntryParsed = BCMSEntryParsed>(
query: BCMSMostContentEntryQueryFunction<unknown>,
skipStatusCheck?: boolean,
): Promise<EntryType[]>;
}
};
}

0 comments on commit b81653e

Please sign in to comment.