Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Breck Yunits authored and Breck Yunits committed Apr 11, 2024
1 parent 86926c6 commit f6e9892
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
17 changes: 10 additions & 7 deletions products/TreeNode.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var FileFormat
FileFormat["tsv"] = "tsv"
FileFormat["tree"] = "tree"
})(FileFormat || (FileFormat = {}))
const TN_WORD_BREAK_SYMBOL = " "
const TN_EDGE_SYMBOL = " "
const TN_NODE_BREAK_SYMBOL = "\n"
class AbstractTreeEvent {
constructor(targetNode) {
this.targetNode = targetNode
Expand Down Expand Up @@ -84,7 +87,7 @@ class ParserCombinator {
}
return obj
}
_getParser(line, contextNode, wordBreakSymbol = " ") {
_getParser(line, contextNode, wordBreakSymbol = TN_WORD_BREAK_SYMBOL) {
return this._getFirstWordMap().get(this._getFirstWord(line, wordBreakSymbol)) || this._getParserFromRegexTests(line) || this._getCatchAllParser(contextNode)
}
_getCatchAllParser(contextNode) {
Expand Down Expand Up @@ -1290,10 +1293,10 @@ class TreeNode extends AbstractNode {
return this.toDelimited("\t")
}
get nodeBreakSymbol() {
return "\n"
return TN_NODE_BREAK_SYMBOL
}
get wordBreakSymbol() {
return " "
return TN_WORD_BREAK_SYMBOL
}
get edgeSymbolRegex() {
return new RegExp(this.edgeSymbol, "g")
Expand All @@ -1302,7 +1305,7 @@ class TreeNode extends AbstractNode {
return new RegExp(this.nodeBreakSymbol, "g")
}
get edgeSymbol() {
return " "
return TN_EDGE_SYMBOL
}
_textToContentAndChildrenTuple(text) {
const lines = text.split(this.nodeBreakSymbolRegex)
Expand Down Expand Up @@ -2512,8 +2515,8 @@ class TreeNode extends AbstractNode {
return headerRow
}
static nest(str, xValue) {
const NodeBreakSymbol = "\n"
const WordBreakSymbol = " "
const NodeBreakSymbol = TN_NODE_BREAK_SYMBOL
const WordBreakSymbol = TN_WORD_BREAK_SYMBOL
const indent = NodeBreakSymbol + WordBreakSymbol.repeat(xValue)
return str ? indent + str.replace(/\n/g, indent) : ""
}
Expand Down Expand Up @@ -2563,7 +2566,7 @@ class AbstractExtendibleTreeNode extends TreeNode {
this.forEach(node => {
const path = node._getAncestorsArray().map(node => node.id)
path.reverse()
tree.touchNode(path.join(" "))
tree.touchNode(path.join(TN_EDGE_SYMBOL))
})
return tree
}
Expand Down
17 changes: 10 additions & 7 deletions products/TreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var FileFormat
FileFormat["tsv"] = "tsv"
FileFormat["tree"] = "tree"
})(FileFormat || (FileFormat = {}))
const TN_WORD_BREAK_SYMBOL = " "
const TN_EDGE_SYMBOL = " "
const TN_NODE_BREAK_SYMBOL = "\n"
class AbstractTreeEvent {
constructor(targetNode) {
this.targetNode = targetNode
Expand Down Expand Up @@ -74,7 +77,7 @@ class ParserCombinator {
}
return obj
}
_getParser(line, contextNode, wordBreakSymbol = " ") {
_getParser(line, contextNode, wordBreakSymbol = TN_WORD_BREAK_SYMBOL) {
return this._getFirstWordMap().get(this._getFirstWord(line, wordBreakSymbol)) || this._getParserFromRegexTests(line) || this._getCatchAllParser(contextNode)
}
_getCatchAllParser(contextNode) {
Expand Down Expand Up @@ -1280,10 +1283,10 @@ class TreeNode extends AbstractNode {
return this.toDelimited("\t")
}
get nodeBreakSymbol() {
return "\n"
return TN_NODE_BREAK_SYMBOL
}
get wordBreakSymbol() {
return " "
return TN_WORD_BREAK_SYMBOL
}
get edgeSymbolRegex() {
return new RegExp(this.edgeSymbol, "g")
Expand All @@ -1292,7 +1295,7 @@ class TreeNode extends AbstractNode {
return new RegExp(this.nodeBreakSymbol, "g")
}
get edgeSymbol() {
return " "
return TN_EDGE_SYMBOL
}
_textToContentAndChildrenTuple(text) {
const lines = text.split(this.nodeBreakSymbolRegex)
Expand Down Expand Up @@ -2502,8 +2505,8 @@ class TreeNode extends AbstractNode {
return headerRow
}
static nest(str, xValue) {
const NodeBreakSymbol = "\n"
const WordBreakSymbol = " "
const NodeBreakSymbol = TN_NODE_BREAK_SYMBOL
const WordBreakSymbol = TN_WORD_BREAK_SYMBOL
const indent = NodeBreakSymbol + WordBreakSymbol.repeat(xValue)
return str ? indent + str.replace(/\n/g, indent) : ""
}
Expand Down Expand Up @@ -2553,7 +2556,7 @@ class AbstractExtendibleTreeNode extends TreeNode {
this.forEach(node => {
const path = node._getAncestorsArray().map(node => node.id)
path.reverse()
tree.touchNode(path.join(" "))
tree.touchNode(path.join(TN_EDGE_SYMBOL))
})
return tree
}
Expand Down
22 changes: 12 additions & 10 deletions treeNode/TreeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ enum FileFormat {
tree = "tree"
}

const TN_WORD_BREAK_SYMBOL = " "
const TN_EDGE_SYMBOL = " "
const TN_NODE_BREAK_SYMBOL = "\n"

declare type removeAfterRunning = boolean

declare type TreeEventHandler = (event: AbstractTreeEvent) => removeAfterRunning
Expand Down Expand Up @@ -97,7 +101,7 @@ class ParserCombinator {
return obj
}

_getParser(line: string, contextNode: treeNotationTypes.treeNode, wordBreakSymbol = " "): treeNotationTypes.TreeParser {
_getParser(line: string, contextNode: treeNotationTypes.treeNode, wordBreakSymbol = TN_WORD_BREAK_SYMBOL): treeNotationTypes.TreeParser {
return this._getFirstWordMap().get(this._getFirstWord(line, wordBreakSymbol)) || this._getParserFromRegexTests(line) || this._getCatchAllParser(contextNode)
}

Expand Down Expand Up @@ -1536,11 +1540,11 @@ class TreeNode extends AbstractNode {
}

get nodeBreakSymbol(): string {
return "\n"
return TN_NODE_BREAK_SYMBOL
}

get wordBreakSymbol(): string {
return " "
return TN_WORD_BREAK_SYMBOL
}

get edgeSymbolRegex() {
Expand All @@ -1552,7 +1556,7 @@ class TreeNode extends AbstractNode {
}

get edgeSymbol(): string {
return " "
return TN_EDGE_SYMBOL
}

protected _textToContentAndChildrenTuple(text: string) {
Expand Down Expand Up @@ -1799,9 +1803,7 @@ class TreeNode extends AbstractNode {
}

findLast(fn: treeNotationTypes.filterFn) {
return this.getChildren()
.reverse()
.find(fn)
return this.getChildren().reverse().find(fn)
}

every(fn: treeNotationTypes.everyFn) {
Expand Down Expand Up @@ -3035,8 +3037,8 @@ class TreeNode extends AbstractNode {
}

static nest(str: string, xValue: int) {
const NodeBreakSymbol = "\n"
const WordBreakSymbol = " "
const NodeBreakSymbol = TN_NODE_BREAK_SYMBOL
const WordBreakSymbol = TN_WORD_BREAK_SYMBOL
const indent = NodeBreakSymbol + WordBreakSymbol.repeat(xValue)
return str ? indent + str.replace(/\n/g, indent) : ""
}
Expand Down Expand Up @@ -3079,7 +3081,7 @@ abstract class AbstractExtendibleTreeNode extends TreeNode {
this.forEach(node => {
const path = node._getAncestorsArray().map((node: AbstractExtendibleTreeNode) => node.id)
path.reverse()
tree.touchNode(path.join(" "))
tree.touchNode(path.join(TN_EDGE_SYMBOL))
})
return tree
}
Expand Down

0 comments on commit f6e9892

Please sign in to comment.