-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
142 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
...2/5-incubator/active/data-structures/src/20graph/tree/selectors--representation--lines.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* Convert a tree data structure | ||
* into a visual text representation using unicode | ||
*/ | ||
|
||
import { Immutable } from '@offirmo-private/ts-types' | ||
|
||
///////////////////////////////////////////////// | ||
|
||
interface TreeForRL { | ||
isꓽroot(): boolean | ||
getꓽrepresentationⵧlines(depth: number): string[] | ||
getꓽchildren(): TreeForRL[] | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
|
||
|
||
function _getꓽrepresentationⵧlines(node: Immutable<TreeForRL>, prefix: string = '', depth = 0): string[] { | ||
const result = node.getꓽrepresentationⵧlines(depth).map(l => prefix + l) | ||
|
||
const children = node.getꓽchildren() | ||
children.forEach((child, index) => { | ||
const r = _getꓽrepresentationⵧlines(child, '', depth + 1) | ||
const is_last_child = index === children.length - 1 | ||
result.push(...r.map((l, i) => { | ||
const is_first_line = i === 0 | ||
if (is_first_line) { | ||
if (is_last_child) { | ||
return '└ ' + l | ||
} | ||
else { | ||
return '├ ' + l | ||
} | ||
} | ||
|
||
if (is_last_child) { | ||
return ' ' + l | ||
} | ||
else { | ||
return '│ ' + l | ||
} | ||
})) | ||
}) | ||
|
||
return result | ||
} | ||
|
||
function getꓽrepresentationⵧlines(tree: Immutable<TreeForRL>): string[] { | ||
if (tree.getꓽchildren().length === 0) { | ||
return [ '[empty tree' ] | ||
} | ||
|
||
// TODO check orphans | ||
// TODO check cycles | ||
|
||
return _getꓽrepresentationⵧlines(tree) | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
|
||
export { | ||
type TreeForRL, | ||
getꓽrepresentationⵧlines, | ||
} |
2 changes: 1 addition & 1 deletion
2
stack--2022/5-incubator/active/data-structures/src/20graph/tree/types.ts
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