export function resolveBp()

in src/data/breakpoints.ts [48:90]


export function resolveBp(query: string, bp: string | number) {
	// Helper function to parse breakpoint into pixels
	// Non-strict null check to avoid '0' being evaluated as falsey
	const parseBp = (bp: string | number) =>
		BREAKPOINTS[bp] != null ? BREAKPOINTS[bp] : parseInt(bp as string);

	// Extract lower and upper-bounds from query
	// Normalise string and comma/whitespace separation
	const [lower, upper] = query
		.toString()
		.replace(/,?\s/, ',')
		.split(',');

	// Lower-bound should always exist; either as named breakpoint or px value
	const lowerWidth = parseBp(lower);

	// Get upper-bound (i.e. next breakpoint threshold), or parse as int,
	// or, failing that, use Infinity as a fallback
	const upperWidth = getBpUpperLimit(upper) || parseInt(upper) || Infinity;

	// Width of current breakpoint
	const bpWidth = parseBp(bp);

	// Validate query by checking if lower bound exists
	// Account for '0' lowerWidth, which would otherwise evaluate as falsy
	if (!lowerWidth && lowerWidth !== 0) {
		console.warn(
			`Invalid breakpoint query '${query}' provided. Query must be in format \`\${lower}, \${upper}\` (comma separated), where lower/upper are either named breakpoints or px values. Upper is optional.`,
		);
		return false;
	} else if (!bpWidth && bpWidth !== 0) {
		console.warn(
			`Invalid breakpoint value '${bp}' provided. Breakpoint should either be named, or a pixel value.`,
		);
		return false;
	}

	// Note that lower-bound is inclusive
	const moreThanLower = bpWidth >= lowerWidth;
	const lessThanUpper = bpWidth < upperWidth;

	return moreThanLower && lessThanUpper;
}