Move `doSearch` to `search.js`

This commit is contained in:
Jos de Jong 2020-07-22 13:46:50 +02:00
parent 35983df136
commit be87b1e4cd
3 changed files with 16 additions and 13 deletions

View File

@ -57,7 +57,7 @@
top: 100%;
right: $search-box-offset + 20px; // keep space for scrollbar
margin-top: $search-box-offset;
z-index: 1;
z-index: 2;
}
}

View File

@ -246,17 +246,16 @@
}
}
function doSearch(doc, searchText) {
return search(null, doc, searchText)
}
// TODO: refactor the search solution and move it in a separate component
// in: doc, searchText, activeSearchResultIndex
// out: searchResultWithActive
// callbacks: change searchText, change doc, change activeSearchResultIndex
let searchResult
let activeSearchResult = undefined
let activeSearchResultIndex
let flatSearchResult
let searchResultWithActive
$: searchResult = searchText ? doSearch(doc, searchText) : undefined
$: searchResult = searchText ? search(doc, searchText) : undefined
$: flatSearchResult = flattenSearch(searchResult)
$: {

View File

@ -2,30 +2,34 @@ import { isNumber } from 'lodash-es'
import { STATE_SEARCH_PROPERTY, STATE_SEARCH_VALUE } from '../constants.js'
import { valueType } from './typeUtils.js'
export function search (key, value, searchText) {
export function search (doc, searchText) {
return searchRecursive(null, doc, searchText)
}
function searchRecursive (key, doc, searchText) {
let results = undefined
if (typeof key === 'string' && containsCaseInsensitive(key, searchText)) {
results = createOrAdd(results, STATE_SEARCH_PROPERTY, 'search')
}
const type = valueType(value)
const type = valueType(doc)
if (type === 'array') {
value.forEach((item, index) => {
let childResults = search(index, item, searchText)
doc.forEach((item, index) => {
let childResults = searchRecursive(index, item, searchText)
if (childResults) {
results = createOrAdd(results, index, childResults)
}
})
} else if (type === 'object') {
Object.keys(value).forEach(prop => {
let childResults = search(prop, value[prop], searchText)
Object.keys(doc).forEach(prop => {
let childResults = searchRecursive(prop, doc[prop], searchText)
if (childResults) {
results = createOrAdd(results, prop, childResults)
}
})
} else { // type is a value
if (containsCaseInsensitive(value, searchText)) {
if (containsCaseInsensitive(doc, searchText)) {
results = createOrAdd(results, STATE_SEARCH_VALUE, 'search')
}
}