nv-cli-ifelse
Version:
cli,to write many if else
59 lines (50 loc) • 1.47 kB
JavaScript
const {add_repr} = require("nv-facutil-basic");
const Branch = require("./branch");
const Forest = require("nv-data-tree-csp-forest");
function _repr(that) {
let sedfs = that.$sedfs_.slice(1);
sedfs.pop();
let ary = sedfs.map(
r=>{
if(r[1]==='open') {
let s = (r[0].open_str_)
if(r[0].in_path) {
s = s.replace("<","[");
s = s.replace(">","]");
} else {}
return(s)
} else {
return(r[0].close_str_)
}
}
)
let s = "\n" + ary.join("\n\n")
s = s.replace(/[\n]{2,}/g,"\n\n")
return(s)
}
add_repr(Branch,_repr);
////
function build(args,size=10000,always_with_else=false) {
if(args[0]==='else') {args.unshift('if')}
let forest = new Forest(size)
let rt = forest.node(Branch);
let p = rt;
for(let each of args) {
if(each === 'if') {
let oldp = p;
p = p.$append_child(); //ifnd
p.in_path = true;
if(always_with_else) {
oldp.$append_child();
} //elnd
} else if(each === 'else') {
p.$append_child(); //ifnd
p = p.$append_child(); //elnd
p.in_path = true;
} else {
//do nothing
}
}
return(rt)
}
module.exports = build;