Created
March 6, 2025 00:10
-
-
Save vantm/84fac536cd0a34eab0ab1723caafac62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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