Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions frontend/lib/utils/normalizeAssetId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { normalizeAssetId } from "./normalizeAssetId";

describe("normalizeAssetId utility", () => {
it("should handle numeric string IDs", () => {
expect(normalizeAssetId("123")).toBe("123");
});

it("should strip 'asset-' prefix", () => {
expect(normalizeAssetId("asset-456")).toBe("456");
});

it("should convert numeric input to string", () => {
expect(normalizeAssetId(789)).toBe("789");
});

it("should strip custom prefixes like 'id-'", () => {
expect(normalizeAssetId("id-999")).toBe("999");
});

it("should trim spaces and normalize properly", () => {
expect(normalizeAssetId(" asset-321 ")).toBe("321");
});

it("should throw error for null or undefined", () => {
expect(() => normalizeAssetId(null as any)).toThrow();
expect(() => normalizeAssetId(undefined as any)).toThrow();
});

it("should keep UUIDs intact", () => {
const uuid = "550e8400-e29b-41d4-a716-446655440000";
expect(normalizeAssetId(uuid)).toBe(uuid);
});
});
28 changes: 28 additions & 0 deletions frontend/lib/utils/normalizeAssetId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Utility to normalize asset IDs into a consistent string format.
* Supports numeric, UUID, and prefixed IDs (e.g., "asset-123").
*
* @param id - The input ID (string or number)
* @returns A standardized string ID
*
* Examples:
* normalizeAssetId("123") → "123"
* normalizeAssetId("asset-456") → "456"
* normalizeAssetId(789) → "789"
*/

export function normalizeAssetId(id: string | number): string {
if (id === null || id === undefined) {
throw new Error("Invalid ID: value cannot be null or undefined");
}

// Convert to string
let idStr = String(id).trim();

// Remove any "asset-" or similar prefixes (case insensitive)
idStr = idStr.replace(/^(asset-|id-|a-)/i, "");

// Optionally handle UUIDs or mixed IDs (leave them untouched)
// Only clean simple prefixed or numeric IDs
return idStr;
}