Last active
September 22, 2016 16:04
-
-
Save SmetDenis/6c53813e398a5409affd61ebd9d51ffd to your computer and use it in GitHub Desktop.
Javascript helpers for hexlet courses
This file contains 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 dump = (vars, label) => console.log(label ? label : 'dump', ':', vars); | |
// Or stringify | |
const dump = (vars, label = 'dump', prettyPrint = false) => { | |
if (prettyPrint) { | |
console.log(`${label}:`, JSON.stringify(vars, null, 3)); | |
} else { | |
console.log(`${label}:`, vars); | |
} | |
}; | |
// OR for lists | |
const dump = function(vars, type, label) { | |
label = label ? label : 'dump'; | |
if (type == 'h') { | |
console.log(label, 'html:', toString(vars)); | |
} else if (type == 'l') { | |
console.log(label, 'list:', listToString(vars)); // добавить import { toString as listToString } from 'hexlet-pairs-data'; | |
} else if (type == 'n') { | |
console.log(label, 'name:', name(vars)); | |
} else if (type == 'v') { | |
console.log(label, 'value:', value(vars)); | |
} else { | |
console.log(label, ':', vars.toString()); | |
} | |
}; | |
// dump for Tree | |
print() { | |
const iter = (tree, acc) => { | |
if (!tree) { | |
return acc; | |
} | |
let childsAcc = tree.getChildren().reduce((childAcc, child) => { | |
childAcc.push(iter(child, {})); | |
return childAcc; | |
}, []); | |
acc[tree.getKey()] = childsAcc; | |
return acc; | |
}; | |
let res = iter(this.tree, {}); | |
dump(res, 'Tree', true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment