OpenAI is shockingly good at unminifying code
Written by Frank Fiegel on .
While browsing the Internet for inspiration, I came across an interesting-looking component.
I thought the block with the running ASCII art looked cool, but I couldn't quite figure out how it was implemented, so I started looking at the source code.
I found the code that looked like it might be doing the job, but it was minified.
Usually, I would just powerthrough reading the minimized code to understand the implementation (or look for a .map
file to restore the original code). However, I realized that I never tried asking ChatGPT to do it for me... So I copied all of the above code and asked ChatGPT to "explain the code". Here is the response:
The provided code is quite complex, but I'll break it down into a more understandable format, explaining its different parts and their functionalities.
Overview
The code defines several functions and components using JavaScript, specifically in the context of a React application. It deals with generating some dynamic text content, presumably ASCII art or some kind of visual effect using characters from a predefined set. Additionally, it includes utility functions for event handling, performance timing, and HTML content conversion.
Key Components and Functions
- Character Set Selection:
Copyconst { floor: ra, abs: KE, min: QE } = Math; const O5 = ["reactive.network REACTIVE.NETWORK", "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. .:â–‘â–’â–“â–ˆ"]; const G7 = Date.now() % 3 ? O5[1] : O5[0]; const V5 = G7.length;
- This part of the code imports some common Math functions (
floor
,abs
, andmin
) and defines two sets of characters, stored inO5
.- Based on the current time (
Date.now() % 3
), it selects one of these sets (G7
) to use later.- The length of the selected character set is stored in
V5
.- Dynamic Character Selection Function:
Copyfunction eT(e, t, n, r) { const i = t.time * 8e-5; const s = QE(t.cols, t.rows); const o = t.metrics.aspect * 0.2; const l = { x: ((4 * (e.x - t.cols / 6.25)) / s) * o, y: (5 * (e.y - t.rows / 4)) / s }; const u = ra(KE(YE(l) - i) * V5 + (ra(e.x / 1) % 2) * 2) % V5; return G7[u]; }
- The
eT
function generates a character based on the input position (e.x
,e.y
) and the current time (t.time
).- It adjusts the position using some aspect ratio calculations and selects a character from
G7
based on a combination of the position and time.- The purpose of
YE
function isn't directly clear from the code, but it seems to be a custom function used in this calculation.- React Component (
tT
) for Displaying Dynamic Content:Copyconst tT = () => { const e = j.useRef(null); const [t, n] = j.useState({ height: null, width: null }); j.useEffect(() => { function r() { n({ height: window.innerHeight, width: window.innerWidth }); } if (typeof window < "u") { n({ height: window.innerHeight, width: window.innerWidth }); window.addEventListener("resize", r); return () => window.removeEventListener("resize", r); } }, []); j.useEffect(() => { const r = e.current; if (!r) return; const i = 12; const s = ra(t.width / i) * 1.6; const o = ra(t.height / i); const l = { aspect: s / o }; const u = setInterval(() => { let c = ""; for (let d = 0; d < o; d++) { for (let f = 0; f < s; f++) c += eT({ x: f, y: d }, { cols: s, rows: o, metrics: l, time: Date.now() }); c += ` `; } r.textContent = c; }, 1e3 / JE.fps); return () => clearInterval(u); }, [t]); return a.jsx("div", { style: { position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }, children: a.jsx("div", { ref: e, style: { width: "100%", height: "100%", whiteSpace: "pre", overflow: "hidden" } }) }); };
- This is a React component that sets up an interval to periodically update the content of a
div
element (e.current
) with dynamically generated characters.- It calculates the number of columns (
s
) and rows (o
) based on the window size and then fills thediv
with characters generated by theeT
function.- The effect runs at a specified frames per second (
JE.fps
).
I followed up asking to "implement equivalent code in TypeScript and make it human readable" and got the following response:
To my surprise, the response is not only good enough, but it is also very readable.
Here is the above component in action:
Comparing the outputs, it looks like LLM response overlooked a few implementation details, but it is still a good enough implementation to learn from. Definitely a neat use case I've not considered before!
Update (2024-08-29): Initially, I thought that the LLM didn't replicate the logic accurately because the output was missing a few characters visible in the original component (e.g.,
░▒▓█
). However, a user on HN forum pointed out that it was likely a copy-paste error.Upon further investigation, I discovered that the original code contains different characters than what I pasted into ChatGPT. This appears to be an encoding issue, as I was able to get the correct characters after downloading the script. After updating the code to use the correct characters, the output is now identical to the original component.
I apologize, GPT-4, for mistakenly accusing you of making mistakes.
Written by Frank Fiegel (@punkpeye)