Skip to content

Instantly share code, notes, and snippets.

@skflowne
Last active August 5, 2020 00:19
Show Gist options
  • Save skflowne/38975dc7a556d5095d914fae9163c216 to your computer and use it in GitHub Desktop.
Save skflowne/38975dc7a556d5095d914fae9163c216 to your computer and use it in GitHub Desktop.
import { derived } from "svelte/store"
import fileSearch from "./fileSearch"
import files from "./files"
import fileIndex from "./fileIndex"
const results = derived([fileSearch, files], ([$fileSearch, $files]) => {
const isSearchEmpty = $fileSearch.trim().length === 0
const allFiles = $files.filter((t) => t.type === "file")
fileIndex.set(0)
if (isSearchEmpty) {
return allFiles
} else {
return allFiles
.filter((f) => {
const filename = f.name.toLocaleLowerCase()
const search = $fileSearch.toLocaleLowerCase()
return filename.includes(search)
})
.map((f) => {
const pattern = new RegExp($fileSearch, "gi")
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