All files / lib bhk-parse.ts

90% Statements 9/10
90% Branches 9/10
100% Functions 1/1
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                        1x             13x 3x     10x 10x 4x     6x 6x         6x    
/**
 * BHK count parser for project propertySize strings.
 *
 * Matches patterns like: "3 BHK", "2bhk", "4-bedroom", "3-BHK duplex",
 * "5+ BHK". For "1.5 BHK" we return 1 (floor) because schema.org and
 * Indian real-estate norms don't have a half-BHK concept as a filter
 * dimension — it's closest to a 1 BHK for page-filtering purposes.
 *
 * Returns null for anything that doesn't match (sqft-only strings,
 * studio, Hindi/Telugu text, empty values, etc.).
 */
 
const BHK_REGEX = /(\d+(?:\.\d+)?)\s*[+-]?\s*(?:BHK|bhk|bedroom|bed)\b/i;
 
/**
 * Parse a free-text propertySize string into a normalised BHK count.
 * Returns null when no confident match is found.
 */
export function parseBhkCount(propertySize: string | null | undefined): number | null {
	if (!propertySize || propertySize.trim() === "") {
		return null;
	}
 
	const match = BHK_REGEX.exec(propertySize);
	if (!match) {
		return null;
	}
 
	const parsed = parseFloat(match[1]);
	Iif (isNaN(parsed) || parsed <= 0) {
		return null;
	}
 
	// Floor fractional values (e.g. "1.5 BHK" → 1)
	return Math.floor(parsed);
}