Shorten identifiers

This commit is contained in:
SPRINX0\prochazka
2025-09-25 10:38:14 +02:00
parent 9d77cac4bb
commit 78026f7fa5
7 changed files with 35 additions and 13 deletions

View File

@@ -32,6 +32,7 @@
"typescript": "^4.4.3"
},
"dependencies": {
"blueimp-md5": "^2.19.0",
"dbgate-query-splitter": "^4.11.7",
"dbgate-sqltree": "^6.0.0-alpha.1",
"debug": "^4.3.4",

View File

@@ -9,6 +9,7 @@ import _isEmpty from 'lodash/isEmpty';
import _omitBy from 'lodash/omitBy';
import { DataEditorTypesBehaviour } from 'dbgate-types';
import isPlainObject from 'lodash/isPlainObject';
import md5 from 'blueimp-md5';
export const MAX_GRID_TEXT_LENGTH = 1000; // maximum length of text in grid cell, longer text is truncated
@@ -737,3 +738,12 @@ export function setSqlFrontMatter(text: string, data: { [key: string]: any }, ya
const frontMatterContent = `-- >>>\n${yamlContentMapped}\n-- <<<\n`;
return frontMatterContent + (textClean || '');
}
export function shortenIdentifier(s: string, maxLength?: number) {
if (!maxLength || maxLength < 10) return s;
if (s.length <= maxLength) return s;
const hash = md5(s).substring(0, 8);
const partLength = Math.floor((maxLength - 9) / 2);
const restLength = maxLength - 10 - partLength;
return s.substring(0, partLength) + '_' + hash + '_' + s.substring(s.length - restLength);
}