From 6d1569b3bdb6110188cb7fffc88e0848b2bd52f7 Mon Sep 17 00:00:00 2001 From: EP Date: Wed, 20 Aug 2025 18:36:13 -0700 Subject: [PATCH] Transforms: make Base64/Base64URL and Binary UTF-8 safe (TextEncoder/TextDecoder) for reliable round-trips --- js/transforms.js | 52 +++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/js/transforms.js b/js/transforms.js index e52017a..37d1839 100644 --- a/js/transforms.js +++ b/js/transforms.js @@ -212,28 +212,23 @@ const transforms = { binary: { name: 'Binary', func: function(text) { - return [...text].map(c => c.charCodeAt(0).toString(2).padStart(8, '0')).join(' '); + const bytes = new TextEncoder().encode(text || ''); + return Array.from(bytes).map(b => b.toString(2).padStart(8,'0')).join(' '); }, preview: function(text) { - if (!text) return '[binary]'; - const firstChar = text.charAt(0); - return firstChar.charCodeAt(0).toString(2).padStart(8, '0') + '...'; + return this.func(text || 'abc'); }, reverse: function(text) { - // Remove spaces and ensure we have valid binary - const binText = text.replace(/\s+/g, ''); - let result = ''; - - // Process 8 bits at a time - for (let i = 0; i < binText.length; i += 8) { - const byte = binText.substr(i, 8); - if (byte.length === 8) { - result += String.fromCharCode(parseInt(byte, 2)); - } + const binText = (text || '').replace(/\s+/g, ''); + const bytes = []; + for (let i=0;i