story-research-zapwall/lib/urlGenerator.ts
2026-01-06 14:17:55 +01:00

84 lines
2.2 KiB
TypeScript

/**
* Generate URLs for objects in the format:
* https://zapwall.fr/<object_type_name>/<id_hash>_<index>_<version>
*
* - object_type_name: author, series, publication, review
* - id_hash: SHA-256 hash of the object
* - index: number of objects of the same type with the same hash (0 by default)
* - version: version number (0 by default)
*/
export type ObjectTypeName = 'author' | 'series' | 'publication' | 'review' | 'purchase' | 'sponsoring' | 'review_tip'
/**
* Generate URL for an object
*/
export function generateObjectUrl(
objectType: ObjectTypeName,
idHash: string,
index: number = 0,
version: number = 0
): string {
return `https://zapwall.fr/${objectType}/${idHash}_${index}_${version}`
}
/**
* Parse URL to extract object information
*/
export function parseObjectUrl(url: string): {
objectType: ObjectTypeName | null
idHash: string | null
index: number | null
version: number | null
} {
const match = url.match(/https?:\/\/zapwall\.fr\/(author|series|publication|review|purchase|sponsoring|review_tip)\/([a-f0-9]+)_(\d+)_(\d+)/i)
if (!match?.[1] || !match[2] || !match[3] || !match[4]) {
return { objectType: null, idHash: null, index: null, version: null }
}
return {
objectType: match[1] as ObjectTypeName,
idHash: match[2],
index: parseInt(match[3], 10),
version: parseInt(match[4], 10),
}
}
/**
* Get the object type name from tag type
*/
export function getObjectTypeName(tagType: 'author' | 'series' | 'publication' | 'quote'): ObjectTypeName {
// Map 'quote' to 'review' for URLs
if (tagType === 'quote') {
return 'review'
}
return tagType
}
/**
* Build object ID in the format: <hash>_<index>_<version>
*/
export function buildObjectId(hash: string, index: number = 0, version: number = 0): string {
return `${hash}_${index}_${version}`
}
/**
* Parse object ID to extract hash, index, and version
*/
export function parseObjectId(id: string): {
hash: string | null
index: number | null
version: number | null
} {
const match = id.match(/^([a-f0-9]+)_(\d+)_(\d+)$/i)
if (!match?.[1] || !match[2] || !match[3]) {
return { hash: null, index: null, version: null }
}
return {
hash: match[1],
index: parseInt(match[2], 10),
version: parseInt(match[3], 10),
}
}