nested-fuzzy-search
Version:
[](https://www.npmjs.com/package/nested-fuzzy-search) [](https://opensource.org/licenses/MIT)
47 lines (43 loc) • 1.14 kB
JavaScript
import {
searchInArray,
searchInArrayStream,
searchInObject,
searchInObjectStream,
} from "./utils/index.js";
// Main search function
function search(
data,
query,
options = {
threshold: 0.6,
outputMode: "flat",
excludeKeys: [],
exact: false,
}
) {
const { threshold, outputMode, excludeKeys, exact } = options;
if (Array.isArray(data) && outputMode === "tree") {
return searchInArray(data, query, { threshold, excludeKeys, exact });
} else if (data && typeof data === "object") {
return searchInObject(data, query, { threshold, excludeKeys, exact });
}
return [];
}
async function* searchStream(
data,
query,
options = {
threshold: 0.6,
outputMode: "flat",
excludeKeys: [],
exact: false,
}
) {
const { threshold, outputMode, excludeKeys, exact } = options;
if (Array.isArray(data) && outputMode === "tree") {
yield* searchInArrayStream(data, query, { threshold, excludeKeys, exact });
} else if (data && typeof data === "object") {
yield* searchInObjectStream(data, query, { threshold, excludeKeys, exact });
}
}
export { search, searchStream };