Skip to content

Instantly share code, notes, and snippets.

@vantm
Created March 6, 2025 00:10
Show Gist options
  • Select an option

  • Save vantm/84fac536cd0a34eab0ab1723caafac62 to your computer and use it in GitHub Desktop.

Select an option

Save vantm/84fac536cd0a34eab0ab1723caafac62 to your computer and use it in GitHub Desktop.
import { chain, isEmpty, toLower, omit, CollectionChain } from 'lodash';
const filter = '';
interface HasFilter {
filter: string
}
const filterTerms = <V extends HasFilter>(docs: CollectionChain<V>, terms: string[]) => {
let docChain = docs;
if (terms.length > 0) {
docChain = docChain.filter((x) =>
terms.some((s) => x.filter.indexOf(s) != -1)
);
}
return docChain
.take(10)
.map((x) => omit(x, 'filter'))
.value();
};
const terms = chain(filter)
.split(' ')
.filter((x) => !isEmpty(x))
.map(toLower)
.value();
const docs = chain(getNames()).map((name, idx) => ({
id: idx + 1,
name,
filter: toLower(name),
}));
filterTerms(docs, terms);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment