prism-react-editor
Version:
Lightweight, extensible code editor component for React apps
51 lines (50 loc) • 1.64 kB
JavaScript
"use client";
import { c, f, r, u } from "../../tooltip-P6N5ZtU4.js";
const fuzzyFilter = (query, option) => {
const optionLen = option.length;
const queryLen = query.length;
if (queryLen > optionLen) return;
if (queryLen == 1 || queryLen == optionLen) return strictFilter(query, option);
const queryLc = query.toLowerCase();
const optionLc = option.toLowerCase();
const matched = [];
const fullMatch = optionLc.indexOf(queryLc);
let score = 0;
let i = 0;
let prevPos = 0;
let j = 0;
for (; i < queryLen; i++) {
const char = queryLc[i];
const pos = fullMatch > -1 ? i + fullMatch : optionLc.indexOf(char, prevPos);
const hasSkipped = pos > prevPos;
if (pos < 0) return;
if (hasSkipped) score -= 800;
if (hasSkipped || !j) {
matched[j++] = pos;
matched[j++] = pos + 1;
} else {
matched[j - 1] = pos + 1;
}
if (char != query[i] ^ optionLc[pos] != option[pos]) score -= 100;
prevPos = pos + 1;
}
return [score - optionLen - (queryLen < optionLen ? 100 : 0), matched];
};
const strictFilter = (query, option) => {
const optionLen = option.length;
const queryLen = query.length;
if (queryLen > optionLen) return;
const start = option.slice(0, queryLen);
const score = start == query ? 0 : query.toLowerCase() == start.toLowerCase() ? -200 : null;
if (score == null) return;
return [query ? score - optionLen - (queryLen < optionLen ? 100 : 0) : 0, [0, queryLen]];
};
export {
c as completeFromList,
f as findWords,
fuzzyFilter,
r as registerCompletions,
strictFilter,
u as useAutoComplete
};
//# sourceMappingURL=index.js.map