Skip to content

Commit e26a82d

Browse files
committed
util: stringToUnicodeEntities func
Signed-off-by: CrazyMax <[email protected]>
1 parent 3162c09 commit e26a82d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

__tests__/util.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,33 @@ describe('generateRandomString', () => {
353353
});
354354
});
355355

356+
describe('stringToUnicodeEntities', () => {
357+
it('should convert a string to Unicode entities', () => {
358+
const input = 'Hello, World!';
359+
const expected = '&#x48;&#x65;&#x6c;&#x6c;&#x6f;&#x2c;&#x20;&#x57;&#x6f;&#x72;&#x6c;&#x64;&#x21;';
360+
const result = Util.stringToUnicodeEntities(input);
361+
expect(result).toEqual(expected);
362+
});
363+
it('should handle an empty string', () => {
364+
const input = '';
365+
const expected = '';
366+
const result = Util.stringToUnicodeEntities(input);
367+
expect(result).toEqual(expected);
368+
});
369+
it('should handle special characters', () => {
370+
const input = '@#^&*()';
371+
const expected = '&#x40;&#x23;&#x5e;&#x26;&#x2a;&#x28;&#x29;';
372+
const result = Util.stringToUnicodeEntities(input);
373+
expect(result).toEqual(expected);
374+
});
375+
it('should handle non-English characters', () => {
376+
const input = 'こんにちは';
377+
const expected = '&#x3053;&#x3093;&#x306b;&#x3061;&#x306f;';
378+
const result = Util.stringToUnicodeEntities(input);
379+
expect(result).toEqual(expected);
380+
});
381+
});
382+
356383
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
357384
function getInputName(name: string): string {
358385
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;

src/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,10 @@ export class Util {
179179
const bytes = crypto.randomBytes(Math.ceil(length / 2));
180180
return bytes.toString('hex').slice(0, length);
181181
}
182+
183+
public static stringToUnicodeEntities(str: string) {
184+
return Array.from(str)
185+
.map(char => `&#x${char.charCodeAt(0).toString(16)};`)
186+
.join('');
187+
}
182188
}

0 commit comments

Comments
 (0)