2024-04-22 17:20:23 +03:00
|
|
|
export const capitalizeFirstLetter = (text: string): string => {
|
|
|
|
|
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
|
|
|
};
|
2024-10-02 01:05:16 +03:00
|
|
|
|
|
|
|
|
export const capitalizeAll = (text: string): string => {
|
|
|
|
|
return text
|
2024-11-05 18:29:07 +03:00
|
|
|
.replaceAll("_", " ")
|
2024-10-02 01:05:16 +03:00
|
|
|
.split(" ")
|
|
|
|
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
|
|
|
.join(" ");
|
|
|
|
|
};
|
2025-10-27 22:58:31 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a fixed-length hash from a camera name for use as a valid camera identifier.
|
|
|
|
|
* Works safely with Unicode input while outputting Latin-only identifiers.
|
|
|
|
|
*
|
|
|
|
|
* @param name - The original camera name/display name
|
|
|
|
|
* @param prefix - Optional prefix for the identifier
|
|
|
|
|
* @returns A valid camera identifier (lowercase, alphanumeric, max 8 chars)
|
|
|
|
|
*/
|
|
|
|
|
export function generateFixedHash(name: string, prefix: string = "id"): string {
|
|
|
|
|
// Safely encode Unicode as UTF-8 bytes
|
|
|
|
|
const utf8Bytes = new TextEncoder().encode(name);
|
|
|
|
|
|
|
|
|
|
// Convert to base64 manually
|
|
|
|
|
let binary = "";
|
|
|
|
|
for (const byte of utf8Bytes) {
|
|
|
|
|
binary += String.fromCharCode(byte);
|
|
|
|
|
}
|
|
|
|
|
const base64 = btoa(binary);
|
|
|
|
|
|
|
|
|
|
// Strip out non-alphanumeric characters and truncate
|
|
|
|
|
const cleanHash = base64.replace(/[^a-zA-Z0-9]/g, "").substring(0, 8);
|
|
|
|
|
|
|
|
|
|
return `${prefix}_${cleanHash.toLowerCase()}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if a string is a valid camera name identifier.
|
|
|
|
|
* Valid camera names contain only ASCII letters, numbers, underscores, and hyphens.
|
|
|
|
|
*
|
|
|
|
|
* @param name - The camera name to validate
|
|
|
|
|
* @returns True if the name is valid, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
export function isValidId(name: string): boolean {
|
2025-11-04 17:57:47 +03:00
|
|
|
return /^[a-zA-Z0-9_-]+$/.test(name) && !/^\d+$/.test(name);
|
2025-10-27 22:58:31 +03:00
|
|
|
}
|