brackt/app/lib/flag-types.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

export type RawFlagConfig = { pattern: string; colors: string[] };
export const FLAG_PATTERNS = [
"triband-h",
"triband-v",
"diagonal",
"nordic-cross",
"cross",
"canton",
"triangle",
"chevron",
"quartered",
"bordered",
"circle",
"star",
] as const;
export type FlagPattern = (typeof FLAG_PATTERNS)[number];
export interface FlagConfig {
pattern: FlagPattern;
colors: string[];
}
export type UserAvatarType = "flag" | "uploaded";
export type TeamAvatarType = "owner" | "flag" | "uploaded";
export type AvatarData =
| { type: "image"; url: string }
| { type: "flag"; config: FlagConfig };
const FLAG_PATTERN_SET = new Set<string>(FLAG_PATTERNS);
const HEX_COLOR_RE = /^#[0-9a-fA-F]{6}$/;
export function isFlagPattern(value: unknown): value is FlagPattern {
return typeof value === "string" && FLAG_PATTERN_SET.has(value);
}
export function isFlagConfig(value: unknown): value is FlagConfig {
if (!value || typeof value !== "object") return false;
const config = value as { pattern?: unknown; colors?: unknown };
return (
isFlagPattern(config.pattern) &&
Array.isArray(config.colors) &&
config.colors.length === 3 &&
config.colors.every((color) => typeof color === "string" && HEX_COLOR_RE.test(color))
);
}
export function parseFlagConfig(value: unknown): FlagConfig | null {
return isFlagConfig(value) ? value : null;
}