export type ParsedTag =
|
| { fieldCode: string; groupCode?: never; instanceId?: never; rowId?: never; type: 'primary' }
|
| { fieldCode: string; groupCode?: never; instanceId: string; rowId?: never; type: 'reference' }
|
| { fieldCode: string; groupCode: string; instanceId?: never; rowId: string; type: 'repeat' };
|
|
export type RowIdRandomSource = (values: Uint32Array) => void;
|
|
export function generateOpaqueId(prefix: string, random?: RowIdRandomSource): string {
|
const values = new Uint32Array(2);
|
|
if (random) {
|
random(values);
|
} else if (globalThis.crypto?.getRandomValues) {
|
globalThis.crypto.getRandomValues(values);
|
} else {
|
values[0] = Date.now() >>> 0;
|
values[1] = Math.floor(Math.random() * 0xffffffff);
|
}
|
|
return `${prefix}_${Array.from(values, (value) => value.toString(16).padStart(8, '0')).join('')}`;
|
}
|
|
export function buildTag(fieldCode: string): string {
|
return `eln.field.${fieldCode}`;
|
}
|
|
export function buildReferenceTag(fieldCode: string, instanceId: string): string {
|
return `eln.ref.${fieldCode}.${instanceId}`;
|
}
|
|
export function parseTag(tag: unknown): ParsedTag | null {
|
const parts = typeof tag === 'string' ? tag.split('.') : [];
|
|
if (parts.length === 3 && parts[0] === 'eln' && parts[1] === 'field') {
|
return {
|
type: 'primary',
|
fieldCode: parts[2]!,
|
};
|
}
|
|
if (parts.length === 4 && parts[0] === 'eln' && parts[1] === 'ref') {
|
return {
|
type: 'reference',
|
fieldCode: parts[2]!,
|
instanceId: parts[3]!,
|
};
|
}
|
|
if (parts.length === 5 && parts[0] === 'eln' && parts[1] === 'repeat') {
|
return {
|
type: 'repeat',
|
groupCode: parts[2]!,
|
rowId: parts[3]!,
|
fieldCode: parts[4]!,
|
};
|
}
|
|
return null;
|
}
|