Last active
August 6, 2020 17:58
-
-
Save skflowne/46a2c221b40ffb223f668cd115398481 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 { derived } from "svelte/store" | |
import fileSearch from "./fileSearch" | |
import files from "./files" | |
const results = derived([fileSearch, files], ([$fileSearch, $files]) => { | |
const isSearchEmpty = $fileSearch.trim().length === 0 | |
const allFiles = $files.filter((t) => t.type === "file") | |
if (isSearchEmpty) { | |
return allFiles | |
} else { | |
const pattern = new RegExp($fileSearch, "gi") | |
return allFiles | |
.filter((f) => { | |
const filename = f.name.toLocaleLowerCase() | |
const search = $fileSearch.toLocaleLowerCase() | |
return filename.includes(search) | |
}) | |
.map((f) => { | |
return { | |
...f, | |
name: f.name.replace(pattern, (match) => `<mark>${match}</mark>`), | |
} | |
}) | |
} | |
}) | |
export default results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment