mirror of
https://github.com/wahyd4/heygo.git
synced 2026-08-09 05:15:52 +10:00
39 lines
993 B
TypeScript
39 lines
993 B
TypeScript
export const ALIAS_MAX_LENGTH = 100;
|
|
|
|
const ALIAS_INVALID_ERROR = 'Alias may contain only letters, numbers, underscore, and hyphen';
|
|
const ALIAS_PATTERN = /^[a-z0-9][a-z0-9_-]{0,99}$/;
|
|
|
|
type AliasValidationSuccess = {
|
|
ok: true;
|
|
value: string;
|
|
};
|
|
|
|
type AliasValidationFailure = {
|
|
ok: false;
|
|
error: string;
|
|
};
|
|
|
|
export type ValidationResult = AliasValidationSuccess | AliasValidationFailure;
|
|
|
|
export function normalizeAlias(input: string): string {
|
|
return input.trim().replace(/[A-Z]/g, (character) => character.toLowerCase());
|
|
}
|
|
|
|
export function validateAlias(input: string): ValidationResult {
|
|
const value = normalizeAlias(input);
|
|
|
|
if (value.length === 0) {
|
|
return { ok: false, error: 'Alias is required' };
|
|
}
|
|
|
|
if (value.length > ALIAS_MAX_LENGTH) {
|
|
return { ok: false, error: `Alias must be ${ALIAS_MAX_LENGTH} characters or fewer` };
|
|
}
|
|
|
|
if (!ALIAS_PATTERN.test(value)) {
|
|
return { ok: false, error: ALIAS_INVALID_ERROR };
|
|
}
|
|
|
|
return { ok: true, value };
|
|
}
|