刘光辉
6 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}