export default function fuzzysearch()

in src/lib/utils/search.ts [7:36]


export default function fuzzysearch<T>(options: {
	needle: string;
	haystack: T[];
	property: keyof T | ((item: T) => string);
}): T[] {
	const { needle, haystack, property } = options;

	if (!Array.isArray(haystack)) {
		throw new Error("Haystack must be an array");
	}

	if (!property) {
		throw new Error("Property selector is required");
	}

	// Convert needle to lowercase for case-insensitive matching
	const lowerNeedle = needle.toLowerCase();

	// Filter the haystack to find matching items
	return haystack.filter(item => {
		// Extract the string value from the item based on the property selector
		const value = typeof property === "function" ? property(item) : String(item[property]);

		// Convert to lowercase for case-insensitive matching
		const lowerValue = value.toLowerCase();

		// Perform the fuzzy search
		return fuzzyMatchString(lowerNeedle, lowerValue);
	});
}