/src/direction.ts
import { Token } from 'nearley';
export enum Direction {
North,
East,
South,
West,
}
export function directionFromToken(t: Token): Direction {
const s = typeof t == 'string' ? t : t.value;
switch(s.slice(1)) {
case 'n':
case 'north':
return Direction.North;
break;
case 'e':
case 'east':
return Direction.East;
break;
case 's':
case 'south':
return Direction.South;
break;
case 'w':
case 'west':
return Direction.West;
break;
default:
throw new Error('Invalid direction: ' + s);
}
}
export function directionToDelta(d: Direction): [number, number] {
switch(d) {
case Direction.North:
return [0, -1];
break;
case Direction.East:
return [1, 0];
break;
case Direction.South:
return [0, 1];
break;
case Direction.West:
return [-1, 0];
break;
}
}