Files
callstack__agent-device/packages/contracts/src/string-enum.ts
Michał Pierzchała 0ee2a86129 refactor: extract contracts workspace package (#1499)
* refactor: extract contracts workspace package

* fix: preserve screenshot diff result contract

* test: stabilize Android keyboard smoke
2026-07-30 17:07:46 +02:00

22 lines
787 B
TypeScript

import { AppError } from '@agent-device/kernel/errors';
export function defineStringEnum<const T extends readonly string[]>(
values: T,
options: {
normalize?: (value: string) => string;
message: (value: string) => string;
} = { message: (value) => `Invalid value: ${value}` },
) {
type Value = T[number];
const allowed = new Set<string>(values);
const normalize = options.normalize ?? ((value: string) => value);
return {
is: (value: unknown): value is Value => typeof value === 'string' && allowed.has(value),
parse: (value: string | undefined): Value => {
const normalized = normalize(value ?? '');
if (allowed.has(normalized)) return normalized as Value;
throw new AppError('INVALID_ARGS', options.message(value ?? ''));
},
};
}