All files / utils parseVoiceCommand.ts

100% Statements 25/25
83.33% Branches 5/6
100% Functions 1/1
100% Lines 25/25

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            1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 8x 8x 8x 8x 8x   5x 5x 8x   5x 5x 5x 8x 8x 8x  
export type VoiceCommand = {
	state: boolean;
	name: string;
	room?: string;
};
 
const actionMap: Record<string, boolean> = {
	on: true,
	off: false,
	open: true,
	close: false,
	start: true,
	stop: false,
	enable: true,
	disable: false,
};
 
export const parseVoiceCommand = (input: string): VoiceCommand | null => {
	const cleaned = input.trim().replace(/\s+/g, " ");
	const match = cleaned.match(
		/(on|off|open|close|start|stop|enable|disable) (\w+)(?: in (\w+))?/i,
	);
	if (!match) return null;
 
	const [_, action, name, room] = match;
	const mappedState = actionMap[action.toLowerCase()];
	if (mappedState === undefined) return null;
 
	return {
		state: mappedState,
		name: name.toLowerCase(),
		room: room?.toLowerCase(),
	};
};