Skip to content

Commit

Permalink
feat: limit the maximum number of search results
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jun 30, 2021
1 parent 4469695 commit 952adb6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import {
CONTEXT_MENU_HEIGHT,
CONTEXT_MENU_WIDTH,
MAX_SEARCH_RESULTS,
SCROLL_DURATION,
SEARCH_UPDATE_THROTTLE,
SIMPLE_MODAL_OPTIONS,
Expand Down Expand Up @@ -209,7 +210,7 @@
debug('searching...', searchText)
// console.time('search') // TODO: cleanup
const flatResults = search(searchText, json, state)
const flatResults = search(searchText, json, state, MAX_SEARCH_RESULTS)
searchResult = updateSearchResult(json, flatResults, searchResult)
// console.timeEnd('search') // TODO: cleanup
Expand Down
17 changes: 12 additions & 5 deletions src/logic/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,23 @@ export function searchPrevious (searchResult) {
}

// TODO: comment
export function search (searchText, json, state) {
export function search (searchText, json, state, maxResults = Infinity) {
const results = []
const path = [] // we reuse the same Array recursively, this is *much* faster than creating a new path every time

// TODO: implement maxResults

function searchRecursive (searchTextLowerCase, json, state) {
if (Array.isArray(json)) {
const level = path.length
path.push(0)

for (let i = 0; i < json.length; i++) {
path[level] = i

searchRecursive(searchTextLowerCase, json[i], state ? state[i] : undefined)

if (results.length >= maxResults) {
return
}
}

path.pop()
Expand All @@ -140,16 +143,20 @@ export function search (searchText, json, state) {
for (const key of keys) {
path[level] = key

if (containsCaseInsensitive(key, searchTextLowerCase)) {
if (containsCaseInsensitive(key, searchTextLowerCase) && results.length < maxResults) {
results.push(path.concat([STATE_SEARCH_PROPERTY]))
}

searchRecursive(searchTextLowerCase, json[key], state ? state[key] : undefined)

if (results.length >= maxResults) {
return
}
}

path.pop()
} else { // type is a value
if (containsCaseInsensitive(json, searchTextLowerCase)) {
if (containsCaseInsensitive(json, searchTextLowerCase) && results.length < maxResults) {
results.push(path.concat([STATE_SEARCH_VALUE]))
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/logic/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ describe('search', () => {
])
})

it('should limit search results to the provided max', () => {
const count = 10
const json = Array(count).fill(42)

const resultsAll = search('42', json, undefined)
assert.deepStrictEqual(resultsAll.length, count)

const maxResults = 4
const results = search('42', json, undefined, maxResults)
assert.deepStrictEqual(results.length, maxResults)
})

it('should generate recursive search results from flat results', () => {
// Based on document:
const json = {
Expand Down

0 comments on commit 952adb6

Please sign in to comment.