Skip to content
Snippets Groups Projects
Unverified Commit fce0d8b7 authored by Sean Hatfield's avatar Sean Hatfield Committed by GitHub
Browse files

Improve search of document picker (#2097)

improve search of document picker
parent 26959563
No related branches found
No related tags found
No related merge requests found
import strDistance from "js-levenshtein"; import strDistance from "js-levenshtein";
const LEVENSHTEIN_MIN = 8; const LEVENSHTEIN_MIN = 2;
// Regular expression pattern to match the v4 UUID and the ending .json // Regular expression pattern to match the v4 UUID and the ending .json
const uuidPattern = const uuidPattern =
...@@ -18,20 +18,33 @@ export const stripUuidAndJsonFromString = (input = "") => { ...@@ -18,20 +18,33 @@ export const stripUuidAndJsonFromString = (input = "") => {
export function filterFileSearchResults(files = [], searchTerm = "") { export function filterFileSearchResults(files = [], searchTerm = "") {
if (!searchTerm) return files?.items || []; if (!searchTerm) return files?.items || [];
const normalizedSearchTerm = searchTerm.toLowerCase().trim();
const searchResult = []; const searchResult = [];
for (const folder of files?.items) { for (const folder of files?.items) {
// If folder is a good match then add all its children const folderNameNormalized = folder.name.toLowerCase();
if (strDistance(folder.name, searchTerm) <= LEVENSHTEIN_MIN) {
// Check for exact match first, then fuzzy match
if (folderNameNormalized.includes(normalizedSearchTerm)) {
searchResult.push(folder); searchResult.push(folder);
continue; continue;
} }
// Otherwise check children for good results // Check children for matches
const fileSearchResults = []; const fileSearchResults = [];
for (const file of folder?.items) { for (const file of folder?.items) {
if ( const fileNameNormalized = stripUuidAndJsonFromString(
strDistance(stripUuidAndJsonFromString(file.name), searchTerm) <= file.name
LEVENSHTEIN_MIN ).toLowerCase();
// Exact match check
if (fileNameNormalized.includes(normalizedSearchTerm)) {
fileSearchResults.push(file);
}
// Fuzzy match only if no exact matches found
else if (
fileSearchResults.length === 0 &&
strDistance(fileNameNormalized, normalizedSearchTerm) <= LEVENSHTEIN_MIN
) { ) {
fileSearchResults.push(file); fileSearchResults.push(file);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment