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%; top: 100%;
right: $search-box-offset + 20px; // keep space for scrollbar right: $search-box-offset + 20px; // keep space for scrollbar
margin-top: $search-box-offset; 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 // 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 searchResult
let activeSearchResult = undefined let activeSearchResult = undefined
let activeSearchResultIndex let activeSearchResultIndex
let flatSearchResult let flatSearchResult
let searchResultWithActive let searchResultWithActive
$: searchResult = searchText ? doSearch(doc, searchText) : undefined $: searchResult = searchText ? search(doc, searchText) : undefined
$: flatSearchResult = flattenSearch(searchResult) $: 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 { STATE_SEARCH_PROPERTY, STATE_SEARCH_VALUE } from '../constants.js'
import { valueType } from './typeUtils.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 let results = undefined
if (typeof key === 'string' && containsCaseInsensitive(key, searchText)) { if (typeof key === 'string' && containsCaseInsensitive(key, searchText)) {
results = createOrAdd(results, STATE_SEARCH_PROPERTY, 'search') results = createOrAdd(results, STATE_SEARCH_PROPERTY, 'search')
} }
const type = valueType(value) const type = valueType(doc)
if (type === 'array') { if (type === 'array') {
value.forEach((item, index) => { doc.forEach((item, index) => {
let childResults = search(index, item, searchText) let childResults = searchRecursive(index, item, searchText)
if (childResults) { if (childResults) {
results = createOrAdd(results, index, childResults) results = createOrAdd(results, index, childResults)
} }
}) })
} else if (type === 'object') { } else if (type === 'object') {
Object.keys(value).forEach(prop => { Object.keys(doc).forEach(prop => {
let childResults = search(prop, value[prop], searchText) let childResults = searchRecursive(prop, doc[prop], searchText)
if (childResults) { if (childResults) {
results = createOrAdd(results, prop, childResults) results = createOrAdd(results, prop, childResults)
} }
}) })
} else { // type is a value } else { // type is a value
if (containsCaseInsensitive(value, searchText)) { if (containsCaseInsensitive(doc, searchText)) {
results = createOrAdd(results, STATE_SEARCH_VALUE, 'search') results = createOrAdd(results, STATE_SEARCH_VALUE, 'search')
} }
} }