Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Created April 15, 2022 19:46
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Janiczek/7400910d3425abcf0414d78cf41b0361 to your computer and use it in GitHub Desktop.
Save Janiczek/7400910d3425abcf0414d78cf41b0361 to your computer and use it in GitHub Desktop.
elm-make-readable
import fs from 'fs/promises';
const rules = [
{
rule: /var ([^=]+)( = F\d\([^f]+function)[^(]\(/gmi,
replacement: `var $1$2 __$1( `,
},
{
rule: /(\sA\d\([\s]+)([^,]+)(,[\s]+)(function[^(]+)\(/gm,
replacement: `$1$2$3$4___$2(`,
},
];
const inputPath = process.argv[2];
const outputPath = process.argv[3];
const inputContents = await fs.readFile(inputPath, 'utf8');
const outputContents = rules.reduce(
(currentString, {rule, replacement}) => currentString.replace(rule, replacement),
inputContents
);
await fs.writeFile(outputPath, outputContents);
@jhbrown-veradept
Copy link

Quoting Janiczek from Slack: you use this with:

node ./elm-make-readable.mjs in.js out.js

@miniBill
Copy link

miniBill commented Apr 3, 2024

As @lydell observed, this might produce syntax errors and is best replaced by

       .replace(/var ([^=]+)( = F\d\([^f]+function)[^(]\(/gim, `var $1$2 __$1( `)
       .replace(
         /(\sA\d\([\s]+)([^,]+)(,[\s]+)(function[^(]+)\(/gm,
         (_, $1, $2, $3, $4) =>
           `${$1}${$2}${$3}${$4}___${$2.replace(/\./g, "_")}(`,
       )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment