Enforce codestyle
This commit is contained in:
@@ -18,8 +18,8 @@ export class Edge implements Hashable {
|
||||
|
||||
public hash(): string {
|
||||
if (!this.cachedHash) {
|
||||
let previousHash = (this.source && this.source.sourceEdge) ? this.source.sourceEdge.hash() : ''
|
||||
this.cachedHash = 'H' + sha1(previousHash + this.name)
|
||||
const previousHash = (this.source && this.source.sourceEdge) ? this.source.sourceEdge.hash() : ''
|
||||
this.cachedHash = `H${sha1(previousHash + this.name)}`
|
||||
}
|
||||
|
||||
return this.cachedHash
|
||||
@@ -28,8 +28,8 @@ export class Edge implements Hashable {
|
||||
public firstEdge(): Edge {
|
||||
if (this.source && this.source.sourceEdge) {
|
||||
return this.source.sourceEdge.firstEdge()
|
||||
} else {
|
||||
return this
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ export class TreeNode extends EventEmitter {
|
||||
}
|
||||
|
||||
public hash(): string {
|
||||
return 'N' + (this.sourceEdge ? this.sourceEdge.hash() : '')
|
||||
return `N${(this.sourceEdge ? this.sourceEdge.hash() : '')}`
|
||||
}
|
||||
|
||||
public firstNode(): TreeNode {
|
||||
@@ -49,8 +49,8 @@ export class TreeNode extends EventEmitter {
|
||||
this.emit('update')
|
||||
}
|
||||
|
||||
public branch(): Array<TreeNode> {
|
||||
let previous = this.previous()
|
||||
public branch(): TreeNode[] {
|
||||
const previous = this.previous()
|
||||
if (!previous) {
|
||||
return [this]
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export class TreeNode extends EventEmitter {
|
||||
this.emit('update')
|
||||
}
|
||||
|
||||
public leafes(): Array<TreeNode> {
|
||||
public leafes(): TreeNode[] {
|
||||
if (Object.values(this.edges).length === 0) {
|
||||
return [this]
|
||||
}
|
||||
@@ -78,9 +78,9 @@ export class TreeNode extends EventEmitter {
|
||||
}
|
||||
|
||||
private mergeEdges(node: TreeNode) {
|
||||
let edgeKeys = Object.keys(node.edges)
|
||||
for (let edgeKey of edgeKeys) {
|
||||
let matchingEdge = this.edges[edgeKey]
|
||||
const edgeKeys = Object.keys(node.edges)
|
||||
for (const edgeKey of edgeKeys) {
|
||||
const matchingEdge = this.edges[edgeKey]
|
||||
if (matchingEdge) {
|
||||
matchingEdge.target.updateWithNode(node.edges[edgeKey].target)
|
||||
} else {
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { Edge, Message, Tree, TreeNode } from './'
|
||||
|
||||
export abstract class TreeNodeFactory {
|
||||
public static fromEdgesAndValue(edgeNames: Array<string>, value: any): TreeNode {
|
||||
public static fromEdgesAndValue(edgeNames: string[], value: any): TreeNode {
|
||||
let currentNode: TreeNode = new Tree()
|
||||
for (const edgeName of edgeNames) {
|
||||
const edge = new Edge(edgeName)
|
||||
let newNode = new TreeNode(edge)
|
||||
const newNode = new TreeNode(edge)
|
||||
edge.target = newNode
|
||||
currentNode.addEdge(edge)
|
||||
currentNode = newNode
|
||||
}
|
||||
|
||||
currentNode.setMessage({ value: value })
|
||||
currentNode.setMessage({ value })
|
||||
return currentNode
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,34 +4,34 @@ import 'mocha'
|
||||
|
||||
describe('Edge', () => {
|
||||
it('should contain a name', () => {
|
||||
let e = new Edge('foo')
|
||||
expect(e.name).to.equal('foo')
|
||||
});
|
||||
const e = new Edge('foo')
|
||||
expect(e.name).to.equal('foo')
|
||||
})
|
||||
|
||||
it('firstEdge should retireve the first edge', () => {
|
||||
const topics = 'foo/bar/baz'.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 5)
|
||||
let bazEdge = leaf.sourceEdge
|
||||
const bazEdge = leaf.sourceEdge
|
||||
|
||||
if (!bazEdge) {
|
||||
expect.fail('should not be undefined')
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
expect(bazEdge.name).to.eq('baz')
|
||||
expect(bazEdge.firstEdge().name).to.eq('foo')
|
||||
});
|
||||
})
|
||||
|
||||
it('hash should not be empty', () => {
|
||||
let e = new Edge('bar')
|
||||
expect(e.hash().length).to.be.gt(0)
|
||||
});
|
||||
const e = new Edge('bar')
|
||||
expect(e.hash().length).to.be.gt(0)
|
||||
})
|
||||
|
||||
it('hash should be stable', () => {
|
||||
let e = new Edge('bar')
|
||||
let previousHash = e.hash()
|
||||
expect(e.hash()).to.eq(previousHash)
|
||||
});
|
||||
const e = new Edge('bar')
|
||||
const previousHash = e.hash()
|
||||
expect(e.hash()).to.eq(previousHash)
|
||||
})
|
||||
|
||||
it('hash should include change if parents are different', () => {
|
||||
const topics1 = 'foo/bar/baz'.split('/')
|
||||
@@ -45,5 +45,5 @@ describe('Edge', () => {
|
||||
}
|
||||
|
||||
expect(bazEdge1.hash()).to.not.eq(bazEdge2.hash())
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,7 +12,7 @@ describe('Tree', () => {
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 3)
|
||||
|
||||
tree.updateWithNode(leaf.firstNode())
|
||||
let expectedNode = tree.findNode('foo/bar')
|
||||
const expectedNode = tree.findNode('foo/bar')
|
||||
expect(expectedNode).to.eq(leaf)
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
@@ -9,17 +9,17 @@ describe('TreeNode.findNode', () => {
|
||||
const topics = 'foo/bar/baz'.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 5)
|
||||
|
||||
let root = leaf.firstNode()
|
||||
const root = leaf.firstNode()
|
||||
expect(root.sourceEdge).to.eq(undefined)
|
||||
|
||||
let barNode = root.findNode('foo/bar')
|
||||
const barNode = root.findNode('foo/bar')
|
||||
if (!barNode) {
|
||||
expect.fail('did not find node')
|
||||
return
|
||||
}
|
||||
expect(barNode.sourceEdge && barNode.sourceEdge.name).to.eq('bar')
|
||||
|
||||
let bazNode = root.findNode('foo/bar/baz')
|
||||
const bazNode = root.findNode('foo/bar/baz')
|
||||
if (!bazNode) {
|
||||
expect.fail('did not find node')
|
||||
return
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { TreeNode } from '../'
|
||||
declare module '../' {
|
||||
interface TreeNode {
|
||||
findNode(path: String): TreeNode | undefined
|
||||
findNode(path: String): TreeNode | undefined
|
||||
}
|
||||
}
|
||||
|
||||
TreeNode.prototype.findNode = function(path: String): TreeNode | undefined {
|
||||
TreeNode.prototype.findNode = function (path: String): TreeNode | undefined {
|
||||
const topics = path.split('/')
|
||||
let edge = this.edges[topics[0]]
|
||||
let remainingTopics = topics.slice(1, topics.length)
|
||||
const edge = this.edges[topics[0]]
|
||||
const remainingTopics = topics.slice(1, topics.length)
|
||||
if (edge && remainingTopics.length === 0) {
|
||||
return edge.target
|
||||
} else if (edge) {
|
||||
|
||||
@@ -17,7 +17,7 @@ describe('TreeNode', () => {
|
||||
expect(leaf.message && leaf.message.value).to.eq(3)
|
||||
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics, 5)
|
||||
|
||||
let root = leaf.firstNode()
|
||||
const root = leaf.firstNode()
|
||||
root.updateWithNode(updateLeave.firstNode())
|
||||
|
||||
expect(root.sourceEdge).to.eq(undefined)
|
||||
@@ -33,7 +33,7 @@ describe('TreeNode', () => {
|
||||
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics2, 5)
|
||||
leaf.firstNode().updateWithNode(updateLeave.firstNode())
|
||||
|
||||
let barNode = leaf.firstNode().findNode('foo/bar')
|
||||
const barNode = leaf.firstNode().findNode('foo/bar')
|
||||
expect(barNode && barNode.sourceEdge && barNode.sourceEdge.name).to.eq('bar')
|
||||
expect(barNode && barNode.message && barNode.message.value).to.eq(5)
|
||||
|
||||
@@ -50,10 +50,10 @@ describe('TreeNode', () => {
|
||||
|
||||
leaf1.firstNode().updateWithNode(leaf2.firstNode())
|
||||
|
||||
let expectedNode = leaf1.firstNode().findNode('foo/bar/baz')
|
||||
const expectedNode = leaf1.firstNode().findNode('foo/bar/baz')
|
||||
if (!expectedNode) {
|
||||
expect.fail('merge seems to have failed')
|
||||
return
|
||||
}
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
@@ -5,17 +5,17 @@ import './TreeNode.findNode'
|
||||
|
||||
describe('TreeNodeFactory', () => {
|
||||
it('root node must not have a sourceEdge', () => {
|
||||
let topic = 'foo/bar'
|
||||
let edges = topic.split('/')
|
||||
let leaf = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
const topic = 'foo/bar'
|
||||
const edges = topic.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
|
||||
expect(leaf.firstNode().sourceEdge).to.eq(undefined)
|
||||
});
|
||||
})
|
||||
|
||||
it('should create node', () => {
|
||||
let topic = 'foo/bar'
|
||||
let edges = topic.split('/')
|
||||
let node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
const topic = 'foo/bar'
|
||||
const edges = topic.split('/')
|
||||
const node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
|
||||
if (!node.sourceEdge || !node.sourceEdge.source || !node.message) {
|
||||
expect.fail('should not happen')
|
||||
@@ -26,14 +26,14 @@ describe('TreeNodeFactory', () => {
|
||||
expect(node.sourceEdge.name).to.eq('bar')
|
||||
expect(node.message.value).to.eq(5)
|
||||
|
||||
let foo = node.firstNode().findNode('foo')
|
||||
const foo = node.firstNode().findNode('foo')
|
||||
expect(foo && foo.sourceEdge && foo.sourceEdge.name).to.eq('foo')
|
||||
});
|
||||
})
|
||||
|
||||
it('node should contain edges in order', () => {
|
||||
let topic = 'foo/bar/baz'
|
||||
let edges = topic.split('/')
|
||||
let node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
const topic = 'foo/bar/baz'
|
||||
const edges = topic.split('/')
|
||||
const node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
|
||||
if (!node.sourceEdge || !node.sourceEdge.source || !node.message) {
|
||||
expect.fail('should not happen')
|
||||
@@ -56,5 +56,5 @@ describe('TreeNodeFactory', () => {
|
||||
return
|
||||
}
|
||||
expect(fooNode.sourceEdge.name).to.eq('foo')
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user