refactor: removed escape character from the string (#3726)

This commit is contained in:
Rajat Dabade 2023-10-12 12:21:04 +05:30 committed by GitHub
parent 2494b64ccd
commit 7fa50070ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -21,7 +21,12 @@ import { ILog } from 'types/api/logs/log';
import ActionItem, { ActionItemProps } from './ActionItem';
import FieldRenderer from './FieldRenderer';
import { flattenObject, jsonToDataNodes, recursiveParseJSON } from './utils';
import {
flattenObject,
jsonToDataNodes,
recursiveParseJSON,
removeEscapeCharacters,
} from './utils';
// Fields which should be restricted from adding it to query
const RESTRICTED_FIELDS = ['timestamp'];
@ -58,7 +63,7 @@ function TableView({
.map((key) => ({
key,
field: key,
value: JSON.stringify(flattenLogData[key]),
value: removeEscapeCharacters(JSON.stringify(flattenLogData[key])),
}));
const onTraceHandler = (record: DataType) => (): void => {

View File

@ -226,3 +226,17 @@ export const getDataTypes = (value: unknown): DataTypes => {
return determineType(value);
};
export const removeEscapeCharacters = (str: string): string =>
str.replace(/\\([ntfr'"\\])/g, (_: string, char: string) => {
const escapeMap: Record<string, string> = {
n: '\n',
t: '\t',
f: '\f',
r: '\r',
"'": "'",
'"': '"',
'\\': '\\',
};
return escapeMap[char as keyof typeof escapeMap];
});