-
Notifications
You must be signed in to change notification settings - Fork 0
/
chords.js
88 lines (73 loc) · 1.49 KB
/
chords.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const MAJOR_CHORDS = new Map();
const MINOR_CHORDS = new Map();
class Chord {
constructor(first, third, quint) {
this.first = first;
this.third = third;
this.quint = quint;
}
full() {
return this.first && this.third && this.quint;
}
toArray() {
const array = [];
this.first && array.push(this.first);
this.third && array.push(this.third);
this.quint && array.push(this.quint);
return array;
}
toSet() {
return new Set(this.toArray());
}
toStr() {
return this.toArray().map(k => k.toStr());
}
play(key) {
if (!this.first) {
this.first = key;
return;
}
if (!this.third) {
this.third = key;
return;
}
if (!this.quint) {
this.quint = key;
}
}
size() {
return this.toArray().length;
}
forEach(func) {
this.toArray().forEach(func);
}
has(key) {
const set = this.toSet();
return set.has(key) || set.has(findSame(key));
}
equals(other) {
if (!other || !other.full()) {
return false;
}
if (this.size() !== other.size()) {
return false;
}
if (!other.has(this.first)) {
return false;
}
if (!other.has(this.third)) {
return false;
}
if (!other.has(this.quint)) {
return false;
}
return true;
}
}
function addChord(minor, first, third, quint) {
if (minor) {
MINOR_CHORDS.set(first, new Chord(first, third, quint));
} else {
MAJOR_CHORDS.set(first, new Chord(first, third, quint));
}
}