mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-26 22:26:08 +08:00
util: stringToUnicodeEntities func
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
parent
3162c096bd
commit
e26a82d0aa
@ -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()}`;
|
||||
|
@ -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('');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user