From e26a82d0aa3dc8ebc0f6854091ac41ea96b3f6fa Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:07:40 +0200 Subject: [PATCH] util: stringToUnicodeEntities func Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/util.test.ts | 27 +++++++++++++++++++++++++++ src/util.ts | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index 2e67ec2..9305a93 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -353,6 +353,33 @@ describe('generateRandomString', () => { }); }); +describe('stringToUnicodeEntities', () => { + it('should convert a string to Unicode entities', () => { + const input = 'Hello, World!'; + const expected = 'Hello, World!'; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); + it('should handle an empty string', () => { + const input = ''; + const expected = ''; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); + it('should handle special characters', () => { + const input = '@#^&*()'; + const expected = '@#^&*()'; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); + it('should handle non-English characters', () => { + const input = 'こんにちは'; + const expected = 'こんにちは'; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); +}); + // See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89 function getInputName(name: string): string { return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; diff --git a/src/util.ts b/src/util.ts index 4fe0bd2..c781ab5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -179,4 +179,10 @@ export class Util { const bytes = crypto.randomBytes(Math.ceil(length / 2)); return bytes.toString('hex').slice(0, length); } + + public static stringToUnicodeEntities(str: string) { + return Array.from(str) + .map(char => `&#x${char.charCodeAt(0).toString(16)};`) + .join(''); + } }