From deb2f14b118c1ccb50b089611b4e6fe58ca40b3f Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Fri, 19 Feb 2021 20:50:37 +0300 Subject: [PATCH 1/3] fix(js): trim source code lines --- lib/utils/string.ts | 18 ++++++++++++++++++ workers/javascript/src/index.ts | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 lib/utils/string.ts diff --git a/lib/utils/string.ts b/lib/utils/string.ts new file mode 100644 index 00000000..876a3ef7 --- /dev/null +++ b/lib/utils/string.ts @@ -0,0 +1,18 @@ +/** + * String helpers + */ + +/** + * Trims source code line content to max length + * Adds «...» in case of trimming + * + * @param content - content to trim + * @param maxLen - trimming criteria + */ +export function rightTrim(content: string, maxLen = 140): string { + if (content.length <= maxLen) { + return content; + } + + return content.substr(0, maxLen) + '…'; +} diff --git a/workers/javascript/src/index.ts b/workers/javascript/src/index.ts index fc27349c..b931fd0a 100644 --- a/workers/javascript/src/index.ts +++ b/workers/javascript/src/index.ts @@ -11,6 +11,7 @@ import * as pkg from '../package.json'; import { JavaScriptEventWorkerTask } from '../types/javascript-event-worker-task'; import HawkCatcher from '@hawk.so/nodejs'; import Crypto from '../../../lib/utils/crypto'; +import { rightTrim } from '../../../lib/utils/string'; /** * Worker for handling Javascript events @@ -273,7 +274,7 @@ export default class JavascriptEventWorker extends EventWorker { return focusedLines.map((line, idx) => { return { line: Math.max(original.line - margin + idx, 1), - content: line, + content: rightTrim(line), } as SourceCodeLine; }); } From 53fcdc7df1d552fc6d4c0cecffc7a2eaf5569c13 Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Fri, 19 Feb 2021 20:54:20 +0300 Subject: [PATCH 2/3] Update string.ts --- lib/utils/string.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/string.ts b/lib/utils/string.ts index 876a3ef7..a382d050 100644 --- a/lib/utils/string.ts +++ b/lib/utils/string.ts @@ -3,7 +3,7 @@ */ /** - * Trims source code line content to max length + * Trims string to max length * Adds «...» in case of trimming * * @param content - content to trim From a7b73a3ff6084beb4c60b465b4ca72b87ba8c79b Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Fri, 19 Feb 2021 20:54:49 +0300 Subject: [PATCH 3/3] Update lib/utils/string.ts Co-authored-by: Taly --- lib/utils/string.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/string.ts b/lib/utils/string.ts index a382d050..8d8a4e94 100644 --- a/lib/utils/string.ts +++ b/lib/utils/string.ts @@ -14,5 +14,5 @@ export function rightTrim(content: string, maxLen = 140): string { return content; } - return content.substr(0, maxLen) + '…'; + return content.substr(0, maxLen) + '…'; }