diff --git a/src/chordChart.ts b/src/chordChart.ts index 2f58093..0b947cc 100644 --- a/src/chordChart.ts +++ b/src/chordChart.ts @@ -3,12 +3,31 @@ import GraphNode from "./lib/Graph/GraphNode"; import Graph from "./lib/Graph/index"; import { ChordQuality, EventData } from "./lib/helpers/index"; +// graph node variant with logic specific to handling this data type +class ChordNode extends GraphNode { + constructor(data: EventData, id = -1, edges?: Edge[]) { + super(data, id, edges); + } + + override print(excludeOrphans = false) { + if (!excludeOrphans) { + console.log(this.data['root' as keyof EventData], ChordQuality[this.data['quality' as keyof EventData] as number]); + } + + if (this.edges.length) { + for (let edge of this.edges) { + console.log(`${this.data['root' as keyof EventData]} ${ChordQuality[this.data['quality' as keyof EventData] as number]} --> ${edge.end.data['root' as keyof EventData]} ${ChordQuality[edge.end.data['quality' as keyof EventData] as number]} ${edge.weight ? '(' + edge.weight + ')' : ''} `); + } + } + } +} + // factory to create data to populate graph -function createChord(root: string, quality: ChordQuality): EventData { - return { +function createChord(root: string, quality: ChordQuality): GraphNode { + return new ChordNode({ root: root, quality: quality - } + }); } // primary usable triads in C major @@ -30,45 +49,27 @@ const bbmaj = createChord("Bb", ChordQuality.Major); const bmaj = createChord("B", ChordQuality.Major); // graph to host music theory logic -class ChordNode extends GraphNode { - constructor(data: EventData, id = -1, edges?: Edge[]) { - super(data, id, edges); - } - - override print() { - if (this.edges.length) { - for (let edge of this.edges) { - console.log(this); - console.log(`${this.data.root, ChordQuality[this.data.quality]} --> ${edge.end.data.root, ChordQuality[edge.end.data.quality]}`); - } - } - - console.log(this.data.root, ChordQuality[this.data.quality]); - } -} - const chordChart = new Graph(true, true); chordChart.addPoints( - new ChordNode(cmaj), - new ChordNode(dmin), - new ChordNode(emin), - new ChordNode(fmaj), - new ChordNode(gmaj), - new ChordNode(amin), - new ChordNode(dmaj), - new ChordNode(emaj), - new ChordNode(ebmaj), - new ChordNode(fmin), - new ChordNode(gmin), - new ChordNode(abmaj), - new ChordNode(bbmaj), - new ChordNode(bmaj) + cmaj, + dmin, + emin, + fmaj, + gmaj, + amin, + dmaj, + emaj, + ebmaj, + fmin, + gmin, + abmaj, + bbmaj, + bmaj ); -const cmajnode = chordChart.getPointByData(cmaj) as ChordNode; -const dminnode = chordChart.getPointByData(dmin) as ChordNode; - -chordChart.addEdge(cmajnode, dminnode); +chordChart.addEdge(cmaj, dmin, 15); +chordChart.addEdge(cmaj, gmaj, 50); +chordChart.addEdge(fmaj, gmaj, 50); export default chordChart; \ No newline at end of file diff --git a/src/lib/Graph/GraphNode.ts b/src/lib/Graph/GraphNode.ts index 6ff1990..160c248 100644 --- a/src/lib/Graph/GraphNode.ts +++ b/src/lib/Graph/GraphNode.ts @@ -23,13 +23,13 @@ export default class GraphNode extends Node { this.edges.filter((value: Edge) => value.end !== destination) } - override print() { + override print(excludeOrphans = false) { if (this.edges.length) { for (let edge of this.edges) { console.log(`Event ${this.id} --> Event ${edge.end.id}`); } } else { - console.log(`Event ${this.id} -->`); + if (!excludeOrphans) console.log(`Event ${this.id} -->`); } } } \ No newline at end of file diff --git a/src/lib/Graph/index.ts b/src/lib/Graph/index.ts index 716a451..54ffa69 100644 --- a/src/lib/Graph/index.ts +++ b/src/lib/Graph/index.ts @@ -73,9 +73,9 @@ export default class Graph { } } - print() { + print(excludeOrphans = false) { for (let each of this.points) { - each.print(); + each.print(excludeOrphans); } } } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 1412ee6..ea1d5c1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,3 @@ import chordChart from "./chordChart"; -chordChart.print(); +chordChart.print(true);