If the code looks like it was generated by ChatGPT, is becaust it was: I wanted a quick soluton as I got a mistyped CNPJ in a message and I had to find the correct one. ChatGPT provided one and I'm happy with the result.
I'm publishing this (after some cleaning of its code to add modularity and improve some issues) as I think it should be a standard feature on most (if not all) websites asking for a CNPJ number. In fact, I think the same code could be adapted for CPF too. The way it implemented variations is crude, but since there is just 140-ish checks to be made, the code is fast. If I were to implement it myself I would try to use algebra, but brute forcing worked here.
If you have Deno installed, you can try out the example by running this command:
deno run -A https://gist.github.com/qgustavor/193ebe5ddedffe45a9897955625be90e/raw/check-cnpj-example.js
Which will return
The CNPJ 01.005.727/0001-23 is invalid.
Did you mean:
01.005.727/0001-24