Created
December 13, 2018 19:37
-
-
Save gerrard00/dd76f8104d68fe216bf9e1fc9e5ae43c to your computer and use it in GitHub Desktop.
Standardizes stuff with leading or trailing dots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const program = require('commander'); | |
function makeTrailingDots(gremlin) { | |
const lines = gremlin | |
.split('\n') | |
.filter(line => line.length); | |
const lastLineIndex = lines.length - 1; | |
return lines | |
.map((line, index, allLines) => { | |
let cleanLine = line.trim(); | |
if (cleanLine.startsWith('.')) { | |
cleanLine = cleanLine.substring(1); | |
} | |
if (cleanLine.endsWith('.')) { | |
return cleanLine; | |
} | |
if (index < lastLineIndex && allLines[index + 1].trim().startsWith('.')) { | |
cleanLine += '.'; | |
} | |
return cleanLine; | |
}).join('\n'); | |
} | |
function makeLeadingDots(gremlin) { | |
const lines = gremlin.split('\n'); | |
// console.log(lines.length); | |
// console.log(); | |
return lines | |
.filter(line => line.trim().length) | |
.reverse() | |
.map((line, index, allLines) => { | |
let cleanLine = line; | |
if (cleanLine.endsWith('.')) { | |
cleanLine = cleanLine.substring(0, cleanLine.length - 1); | |
} | |
if (cleanLine.startsWith('.')) { | |
return cleanLine; | |
} | |
if ((index < allLines.length - 1) && allLines[index + 1].endsWith('.')) { | |
cleanLine = `.${cleanLine}`; | |
} | |
return cleanLine; | |
}).reverse().join('\n'); | |
} | |
program | |
.option('-d, --dots [dots]', 'trailing or leading dots', /^(t|trailing|l|leading)$/i, 'trailing') | |
.parse(process.argv); | |
const dotAdjustmentFunction = (program.dots.startsWith('l')) ? makeLeadingDots : makeTrailingDots; | |
const contents = fs.readFileSync(0, 'utf-8'); | |
const finalGremlin = dotAdjustmentFunction(contents); | |
console.log(finalGremlin); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment