[pbs-devel] [PATCH widget-toolkit v4 1/7] utils: add base64 conversion helper
Gabriel Goller
g.goller at proxmox.com
Tue Nov 26 11:31:56 CET 2024
On 17.09.2024 09:37, Thomas Lamprecht wrote:
>On 13/09/2024 15:10, Gabriel Goller wrote:
>> Add helper functions to convert from a utf8 string to a base64 string
>> and vice-versa. Using the TextEncoder/TextDecoder we can support unicode
>> such as emojis as well [0].
>>
>> [0]: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
>>
>> Signed-off-by: Gabriel Goller <g.goller at proxmox.com>
>
>Reviewed-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
>
>One small nit inline though.
>
>> ---
>> src/Utils.js | 18 ++++++++++++++++++
>> 1 file changed, 18 insertions(+)
>>
>> diff --git a/src/Utils.js b/src/Utils.js
>> index 7dd034a5e56f..3badb6aaf606 100644
>> --- a/src/Utils.js
>> +++ b/src/Utils.js
>> @@ -1356,6 +1356,24 @@ utilities: {
>> );
>> },
>>
>> + // Convert utf-8 string to base64.
>> + // This also escapes unicode characters such as emojis.
>> + utf8ToBase64: function(string) {
>> + let bytes = new TextEncoder().encode(string);
>> + const escapedString = Array.from(bytes, (byte) =>
>> + String.fromCodePoint(byte),
>> + ).join("");
>
>FWIW this could be a bit shorter by using map (which typed arrays
>also support [0]):
>
> const escapedString = bytes.map(b => String.fromCodePoint(b)).join('');
>
>But for that we really need no new revision.
>
>[0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/map
I just tried this and it sadly doesn't work. `String.fromCodePoint`
returns a string, which can't be stored in the `Uint8Array` which is `bytes`.
More information about the pbs-devel
mailing list