Skip to content

Instantly share code, notes, and snippets.

@merlox
Created March 21, 2022 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merlox/5e0c7d90829316f72e7d1cb8d3091905 to your computer and use it in GitHub Desktop.
Save merlox/5e0c7d90829316f72e7d1cb8d3091905 to your computer and use it in GitHub Desktop.
Adds comments in the middle of the code. Reads from a "code.txt" file
const fs = require('fs')
const { join } = require('path')
function getFirstGroup(regexp, str) {
return Array.from(str.matchAll(regexp), m => m[1])
}
const file = fs.readFileSync(join(__dirname, 'code.txt'), 'utf-8')
const myRegex = /(.*)\n/g
let matches = file.matchAll(myRegex)
let finalFile = ''
let counter = 1
let tabs = ''
let myMatches = []
for (const match of matches) {
myMatches.push(match)
}
for (const match of myMatches) {
console.log(match[1])
const tabs = getFirstGroup(/(\t+)/g, match[1])
console.log(tabs + 'console.log(' + counter + ')')
counter++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment