pretty print enhancements + comparison of generic functions
This commit is contained in:
parent
bbac7858ae
commit
9e1f679dba
15 changed files with 93 additions and 45 deletions
|
|
@ -3,19 +3,19 @@
|
|||
import createRBTree from "functional-red-black-tree";
|
||||
import { inspect } from "util";
|
||||
|
||||
function defaultPrintf(depth, options, inspect) {
|
||||
const entries = [];
|
||||
this.tree.forEach((key, val) => { entries.push(`${inspect(key)} => ${inspect(val)}`); });
|
||||
return `RBTree(${this.tree.length}) {${entries.join(', ')}}`;
|
||||
}
|
||||
|
||||
export class RBTreeWrapper {
|
||||
constructor(tree) {
|
||||
constructor(tree, printf = defaultPrintf) {
|
||||
this.tree = tree;
|
||||
this[inspect.custom] = printf;
|
||||
}
|
||||
|
||||
static new(compareFn) {
|
||||
return new RBTreeWrapper(createRBTree(compareFn))
|
||||
}
|
||||
|
||||
// pretty print to console
|
||||
[inspect.custom](depth, options, inspect) {
|
||||
const entries = [];
|
||||
this.tree.forEach((key, val) => { entries.push(`${inspect(key)} => ${inspect(val)}`); });
|
||||
return `RBTree(${this.tree.length}) {${entries.join(', ')}}`;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue