You could use a regular expression to do part of the parsing, and use the replace
callback to keep/eliminate the relevant parts:
function clean(input) {
let keep;
return input.replace(/^\s*digraph\s+("[^"]*")\s*\{|\s*("[^"]+")\s*->\s*"[^"]+"\s*;|([^])/gm,
(m, a, b, c) => a && (keep = a) || b === keep || c ? m : ""
);
}
// Example:
var input = `digraph "com.a:test:jar:1.0" {
"com.a:test:jar:1.0" ->
"org.apache.httpcomponents:httpclient:jar:4.5.5:compile";
"com.a:test:jar:1.0" -> "com.google.code.gson:gson:jar:2.8.2:compile";
"com.a:test:jar:1.0" -> "info.picocli:picocli:jar:2.3.0:compile";
"com.a:test:jar:1.0" -> "log4j:log4j:jar:1.2.17:compile";
"com.a:test:jar:1.0" -> "org.xerial:sqlite-jdbc:jar:3.21.0:compile";
"org.apache.httpcomponents:httpclient:jar:4.5.5:compile" ->
"org.apache.httpcomponents:httpcore:jar:4.4.9:compile" ;
"org.apache.httpcomponents:httpclient:jar:4.5.5:compile" -> "commons-logging:commons-logging:jar:1.2:compile" ;
"org.apache.httpcomponents:httpclient:jar:4.5.5:compile" -> "commons-codec:commons-codec:jar:1.10:compile" ;
}`
console.log(clean(input));
0
solved Format text in javascript