SHA256
78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
import { t } from '../i18n/index.js';
|
|
const MIN_LEN = 4;
|
|
const MAX_LEN = 32;
|
|
const ALLOWED_CHARS_RE = /^[A-Za-z0-9_-]+$/;
|
|
const DIGITS_ONLY_RE = /^[0-9]+$/;
|
|
|
|
export function normalizeChannelDisplayName(value) {
|
|
if (value == null) return '';
|
|
return String(value).trim();
|
|
}
|
|
|
|
export function normalizeChannelDescription(value) {
|
|
if (value == null) return '';
|
|
return String(value).trim().replace(/\s+/g, ' ');
|
|
}
|
|
|
|
export function toCanonicalChannelSlug(value) {
|
|
const normalized = normalizeChannelDisplayName(value);
|
|
if (!normalized) return '';
|
|
|
|
const lowered = normalized.toLowerCase();
|
|
if (!ALLOWED_CHARS_RE.test(lowered)) return '';
|
|
return lowered;
|
|
}
|
|
|
|
export function validateChannelDisplayName(value) {
|
|
const normalized = normalizeChannelDisplayName(value);
|
|
if (!normalized) {
|
|
return { ok: false, code: 'blank', normalized: '', slug: '' };
|
|
}
|
|
|
|
const length = Array.from(normalized).length;
|
|
if (length < MIN_LEN) {
|
|
return { ok: false, code: 'too_short', normalized, slug: '' };
|
|
}
|
|
if (length > MAX_LEN) {
|
|
return { ok: false, code: 'too_long', normalized, slug: '' };
|
|
}
|
|
if (!ALLOWED_CHARS_RE.test(normalized)) {
|
|
return { ok: false, code: 'bad_chars', normalized, slug: '' };
|
|
}
|
|
if (DIGITS_ONLY_RE.test(normalized)) {
|
|
return { ok: false, code: 'digits_only', normalized, slug: '' };
|
|
}
|
|
|
|
const slug = toCanonicalChannelSlug(normalized);
|
|
if (!slug) {
|
|
return { ok: false, code: 'bad_chars', normalized, slug: '' };
|
|
}
|
|
|
|
return { ok: true, code: '', normalized, slug };
|
|
}
|
|
|
|
export function channelNameErrorText(code) {
|
|
switch (String(code || '').trim()) {
|
|
case 'blank':
|
|
return t('remaining.channel_validation.enter_a_channel_name');
|
|
case 'too_short':
|
|
return t('remaining.channel_validation.channel_name_is_too_short_minimum_4_characters');
|
|
case 'too_long':
|
|
return t('remaining.channel_validation.channel_name_is_too_long_maximum_32_characters');
|
|
case 'bad_chars':
|
|
return t('remaining.channel_validation.only_latin_letters_digits_and_are_allowed');
|
|
case 'digits_only':
|
|
return t('remaining.channel_validation.a_channel_name_cannot_consist_of_digits_only');
|
|
default:
|
|
return t('remaining.channel_validation.invalid_channel_name');
|
|
}
|
|
}
|
|
|
|
export function channelDescriptionErrorText(value) {
|
|
const normalized = normalizeChannelDescription(value);
|
|
if (new TextEncoder().encode(normalized).length > 200) {
|
|
return t('remaining.channel_validation.description_is_too_long_maximum_200_utf_8_bytes');
|
|
}
|
|
return '';
|
|
}
|