From b0e661b5e28dc56e7bac98c3d46aeb11669fba3e Mon Sep 17 00:00:00 2001 From: Yandry Perez Clemente <99700024+ypc-faros@users.noreply.github.com> Date: Sat, 4 Nov 2023 14:31:51 -0400 Subject: [PATCH 1/4] Move cleanAndTruncate from to Utils --- src/utils.ts | 19 +++++++++++++++++++ test/utils.test.ts | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index 210c033..dba7227 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,8 @@ import {Duration} from 'luxon'; import VError from 'verror'; +// Max length for free-form description text fields such as issue body +const MAX_TEXT_LENGTH = 1000; export class Utils { static urlWithoutTrailingSlashes(url: string): string { @@ -138,4 +140,21 @@ static toCategoryDetail( detail: category, }; } + + static cleanAndTruncate(str?: string, maxLength?: number): string | null | undefined { + if (!str) { + return str; + } + const length = maxLength ?? MAX_TEXT_LENGTH; + let result: string; + if (str.length <= length) { + result = str; + } else { + // If the last character is part of a unicode surrogate pair, include the next character + const lastChar = str.codePointAt(length - 1) ?? 0; + result = lastChar > 65535 ? str.substring(0, length + 1) : str.substring(0, length); + } + // eslint-disable-next-line no-control-regex + return result.replace(/\u0000/g, ''); + } } diff --git a/test/utils.test.ts b/test/utils.test.ts index fd3d4de..d79f688 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -210,4 +210,13 @@ describe('parse primary keys', () => { }); }); }); + + describe('clean and truncate', () => { + test('should truncate strings', () => { + const str = 'abc123😍\u0000'; + expect(sut.Utils.cleanAndTruncate(str)).toEqual('abc123😍'); + expect(sut.Utils.cleanAndTruncate(str, 3)).toEqual('abc'); + expect(sut.Utils.cleanAndTruncate(str, 7)).toEqual('abc123😍'); + }); + }); }); From 1aaac01e11c779580885cfdcedc70ab7481ab8f5 Mon Sep 17 00:00:00 2001 From: Yandry Perez Clemente <99700024+ypc-faros@users.noreply.github.com> Date: Sat, 4 Nov 2023 14:38:59 -0400 Subject: [PATCH 2/4] lint --- src/utils.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index dba7227..379e71e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -118,7 +118,7 @@ export class Utils { }); } -static toCategoryDetail( + static toCategoryDetail( enumObject: EnumType & Record, category: string, categoryMapping: Record = {} @@ -141,7 +141,10 @@ static toCategoryDetail( }; } - static cleanAndTruncate(str?: string, maxLength?: number): string | null | undefined { + static cleanAndTruncate( + str?: string, + maxLength?: number + ): string | null | undefined { if (!str) { return str; } @@ -152,7 +155,10 @@ static toCategoryDetail( } else { // If the last character is part of a unicode surrogate pair, include the next character const lastChar = str.codePointAt(length - 1) ?? 0; - result = lastChar > 65535 ? str.substring(0, length + 1) : str.substring(0, length); + result = + lastChar > 65535 + ? str.substring(0, length + 1) + : str.substring(0, length); } // eslint-disable-next-line no-control-regex return result.replace(/\u0000/g, ''); From d1c445c43d551da1d8bcbc2f641ef98829fd7edd Mon Sep 17 00:00:00 2001 From: Yandry Perez Clemente <99700024+ypc-faros@users.noreply.github.com> Date: Sat, 4 Nov 2023 14:41:21 -0400 Subject: [PATCH 3/4] lint --- src/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 379e71e..a373bfd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -153,7 +153,8 @@ export class Utils { if (str.length <= length) { result = str; } else { - // If the last character is part of a unicode surrogate pair, include the next character + // If the last character is part of a unicode surrogate pair, + // include the next character const lastChar = str.codePointAt(length - 1) ?? 0; result = lastChar > 65535 From 1d8490408602c94845089fb4c6194d3ca00f96fb Mon Sep 17 00:00:00 2001 From: Yandry Perez Clemente <99700024+ypc-faros@users.noreply.github.com> Date: Sat, 4 Nov 2023 14:42:24 -0400 Subject: [PATCH 4/4] lint --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index a373bfd..c644871 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -153,7 +153,7 @@ export class Utils { if (str.length <= length) { result = str; } else { - // If the last character is part of a unicode surrogate pair, + // If the last character is part of a unicode surrogate pair, // include the next character const lastChar = str.codePointAt(length - 1) ?? 0; result =