fixes for print
This commit is contained in:
@@ -3,12 +3,31 @@ import GraphNode from "./lib/Graph/GraphNode";
|
|||||||
import Graph from "./lib/Graph/index";
|
import Graph from "./lib/Graph/index";
|
||||||
import { ChordQuality, EventData } from "./lib/helpers/index";
|
import { ChordQuality, EventData } from "./lib/helpers/index";
|
||||||
|
|
||||||
|
// graph node variant with logic specific to handling this data type
|
||||||
|
class ChordNode<EventData> extends GraphNode<EventData> {
|
||||||
|
constructor(data: EventData, id = -1, edges?: Edge<EventData>[]) {
|
||||||
|
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
|
// factory to create data to populate graph
|
||||||
function createChord(root: string, quality: ChordQuality): EventData {
|
function createChord(root: string, quality: ChordQuality): GraphNode<EventData> {
|
||||||
return {
|
return new ChordNode<EventData>({
|
||||||
root: root,
|
root: root,
|
||||||
quality: quality
|
quality: quality
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// primary usable triads in C major
|
// primary usable triads in C major
|
||||||
@@ -30,45 +49,27 @@ const bbmaj = createChord("Bb", ChordQuality.Major);
|
|||||||
const bmaj = createChord("B", ChordQuality.Major);
|
const bmaj = createChord("B", ChordQuality.Major);
|
||||||
|
|
||||||
// graph to host music theory logic
|
// graph to host music theory logic
|
||||||
class ChordNode<EventData> extends GraphNode<EventData> {
|
|
||||||
constructor(data: EventData, id = -1, edges?: Edge<EventData>[]) {
|
|
||||||
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<EventData>(true, true);
|
const chordChart = new Graph<EventData>(true, true);
|
||||||
|
|
||||||
chordChart.addPoints(
|
chordChart.addPoints(
|
||||||
new ChordNode<EventData>(cmaj),
|
cmaj,
|
||||||
new ChordNode<EventData>(dmin),
|
dmin,
|
||||||
new ChordNode<EventData>(emin),
|
emin,
|
||||||
new ChordNode<EventData>(fmaj),
|
fmaj,
|
||||||
new ChordNode<EventData>(gmaj),
|
gmaj,
|
||||||
new ChordNode<EventData>(amin),
|
amin,
|
||||||
new ChordNode<EventData>(dmaj),
|
dmaj,
|
||||||
new ChordNode<EventData>(emaj),
|
emaj,
|
||||||
new ChordNode<EventData>(ebmaj),
|
ebmaj,
|
||||||
new ChordNode<EventData>(fmin),
|
fmin,
|
||||||
new ChordNode<EventData>(gmin),
|
gmin,
|
||||||
new ChordNode<EventData>(abmaj),
|
abmaj,
|
||||||
new ChordNode<EventData>(bbmaj),
|
bbmaj,
|
||||||
new ChordNode<EventData>(bmaj)
|
bmaj
|
||||||
);
|
);
|
||||||
|
|
||||||
const cmajnode = chordChart.getPointByData(cmaj) as ChordNode<EventData>;
|
chordChart.addEdge(cmaj, dmin, 15);
|
||||||
const dminnode = chordChart.getPointByData(dmin) as ChordNode<EventData>;
|
chordChart.addEdge(cmaj, gmaj, 50);
|
||||||
|
chordChart.addEdge(fmaj, gmaj, 50);
|
||||||
chordChart.addEdge(cmajnode, dminnode);
|
|
||||||
|
|
||||||
export default chordChart;
|
export default chordChart;
|
||||||
@@ -23,13 +23,13 @@ export default class GraphNode<T> extends Node<T> {
|
|||||||
this.edges.filter((value: Edge<T>) => value.end !== destination)
|
this.edges.filter((value: Edge<T>) => value.end !== destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
override print() {
|
override print(excludeOrphans = false) {
|
||||||
if (this.edges.length) {
|
if (this.edges.length) {
|
||||||
for (let edge of this.edges) {
|
for (let edge of this.edges) {
|
||||||
console.log(`Event ${this.id} --> Event ${edge.end.id}`);
|
console.log(`Event ${this.id} --> Event ${edge.end.id}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`Event ${this.id} -->`);
|
if (!excludeOrphans) console.log(`Event ${this.id} -->`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,9 +73,9 @@ export default class Graph<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print() {
|
print(excludeOrphans = false) {
|
||||||
for (let each of this.points) {
|
for (let each of this.points) {
|
||||||
each.print();
|
each.print(excludeOrphans);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
import chordChart from "./chordChart";
|
import chordChart from "./chordChart";
|
||||||
|
|
||||||
chordChart.print();
|
chordChart.print(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user