刘光辉
3 小时以前 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
function lines(label: string): string[] {
  return (label || '').replaceAll(/<\\n>|\\n/g, '\n').split('\n');
}
 
export function getTableChildColumnLabel(parentLabel: string, childLabel: string): string {
  const [parentName, parentEnglish] = lines(parentLabel);
  const [childName, ...englishLines] = lines(childLabel);
  const english = englishLines.join('\n');
  const prefix = parentEnglish ? `${parentEnglish.trim()} - ` : '';
  const childEnglish = prefix && english.startsWith(prefix) ? english.slice(prefix.length) : english;
  return `${parentName}-${childName}${childEnglish ? `\n${childEnglish}` : ''}`;
}
 
export function getTableChildHeaderLabel(parentLabel: string, columnLabel: string, originalChildLabel = ''): string {
  const separator = columnLabel.indexOf('-');
  const childLabel = separator === -1 ? columnLabel : columnLabel.slice(separator + 1);
  const [, parentEnglish] = lines(parentLabel);
  const prefix = parentEnglish ? `${parentEnglish.trim()} - ` : '';
  const [childName, ...englishLines] = lines(childLabel);
  const english = englishLines.join('\n');
  const originalEnglish = lines(originalChildLabel).slice(1).join('\n');
  const childEnglish =
    originalEnglish && english.endsWith(` - ${originalEnglish}`)
      ? originalEnglish
      : prefix && english.startsWith(prefix)
        ? english.slice(prefix.length)
        : english;
  return `${childName}${childEnglish ? `\n${childEnglish}` : ''}`;
}