printTree.js 679 B

123456789101112131415161718192021
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.printTree = void 0;
  4. const printTree = (tab = '', children) => {
  5. let str = '';
  6. let last = children.length - 1;
  7. for (; last >= 0; last--)
  8. if (children[last])
  9. break;
  10. for (let i = 0; i <= last; i++) {
  11. const fn = children[i];
  12. if (!fn)
  13. continue;
  14. const isLast = i === last;
  15. const child = fn(tab + (isLast ? ' ' : '│') + ' ');
  16. const branch = child ? (isLast ? '└─' : '├─') : '│';
  17. str += '\n' + tab + branch + (child ? ' ' + child : '');
  18. }
  19. return str;
  20. };
  21. exports.printTree = printTree;