pagamio-frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
21 lines (20 loc) • 894 B
JavaScript
/**
* Checks if a URL segment is an identifier (UUID, numeric ID, or long slug).
* @param segment - The URL path segment to check
* @returns True if the segment matches any of these patterns:
* - UUID format (e.g., '123e4567-e89b-12d3-a456-426614174000')
* - Numeric ID (e.g., '123')
* - Alphanumeric slug 30 characters or longer (for compatibility)
*/
export const isUUIDorID = (segment) => {
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const idPattern = /^\d+$/;
const slugPattern = /^[A-Za-z0-9_-]+$/;
if (uuidPattern.test(segment))
return true;
if (idPattern.test(segment))
return true;
// Use 30+ characters for better compatibility between apps
// This avoids treating route segments as IDs while still catching long identifiers
return slugPattern.test(segment) && segment.length >= 30;
};