Skip to content

Commit

Permalink
Add support for flat added tones and tones in parens.
Browse files Browse the repository at this point in the history
Also organized tests a bit better.
  • Loading branch information
ddycai committed Aug 20, 2023
1 parent 5e4369f commit 7d59c72
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion dist/Chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.CHORD_RANKS = new Map([
]);
// Regex for recognizing chords
const TRIAD_PATTERN = "(M|maj|major|m|min|minor|dim|sus|dom|aug|\\+|-)";
const ADDED_TONE_PATTERN = "(([/\\.\\+\\#]|add)?\\d+[\\+-]?)";
const ADDED_TONE_PATTERN = "(\\(?([/\\.\\+]|add)?[#b]?\\d+[\\+-]?\\)?)";
const SUFFIX_PATTERN = `(?<suffix>\\(?${TRIAD_PATTERN}?${ADDED_TONE_PATTERN}*\\)?)`;
const BASS_PATTERN = "(\\/(?<bass>[A-G](#|b)?))?";
exports.ROOT_PATTERN = "(?<root>[A-G](#|b)?)";
Expand Down
6 changes: 0 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,13 @@ function tokenize(text) {
const newText = [];
for (const line of lines) {
const newLine = [];
let chordCount = 0;
let tokenCount = 0;
const tokens = line.split(/(\s+|-|]|\[)/g);
let lastTokenWasString = false;
for (const token of tokens) {
const isTokenEmpty = token.trim() === "";
if (!isTokenEmpty && Chord_1.isChord(token)) {
const chord = Chord_1.Chord.parse(token);
newLine.push(chord);
chordCount++;
lastTokenWasString = false;
}
else {
Expand All @@ -96,9 +93,6 @@ function tokenize(text) {
else {
newLine.push(token);
}
if (!isTokenEmpty) {
tokenCount++;
}
lastTokenWasString = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Chord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const CHORD_RANKS: Map<string, number> = new Map([

// Regex for recognizing chords
const TRIAD_PATTERN = "(M|maj|major|m|min|minor|dim|sus|dom|aug|\\+|-)";
const ADDED_TONE_PATTERN = "(([/\\.\\+\\#]|add)?\\d+[\\+-]?)";
const ADDED_TONE_PATTERN = "(\\(?([/\\.\\+]|add)?[#b]?\\d+[\\+-]?\\)?)";
const SUFFIX_PATTERN = `(?<suffix>\\(?${TRIAD_PATTERN}?${ADDED_TONE_PATTERN}*\\)?)`;
const BASS_PATTERN = "(\\/(?<bass>[A-G](#|b)?))?";

Expand Down
7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,20 @@ function tokenize(text: string): Token[][] {

for (const line of lines) {
const newLine: Token[] = [];
let chordCount: number = 0;
let tokenCount: number = 0;
const tokens: string[] = line.split(/(\s+|-|]|\[)/g);
let lastTokenWasString: boolean = false;
for (const token of tokens) {
const isTokenEmpty = token.trim() === "";
if (!isTokenEmpty && isChord(token)) {
const chord: Chord = Chord.parse(token);
newLine.push(chord);
chordCount++;
lastTokenWasString = false;
} else {
if (lastTokenWasString) {
newLine.push(newLine.pop() + token);
} else {
newLine.push(token);
}

if (!isTokenEmpty) {
tokenCount++;
}
lastTokenWasString = true;
}
}
Expand Down
46 changes: 36 additions & 10 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ describe("Transposer", () => {

it("transposes chords inlined in square brackets (ChordPro)", () => {
expect(transpose("[C]Hello [D] world! [C]").toKey("F").toString()).toEqual(
"[F]Hello [G] world! [F]"
"[F]Hello [G] world! [F]"
);

});

it("preserves whitespace", () => {
Expand All @@ -267,20 +266,47 @@ describe("Transposer", () => {
expect(transpose("C Am Em").toKey("Db").toString()).toEqual("Db Bbm Fm");
});

it("transposes various types of chords", () => {
expect(transpose("C Cmaj CM").toKey("F").toString()).toEqual("F Fmaj FM");
expect(transpose("Cm Cmin C-").toKey("F").toString()).toEqual("Dm Dmin D-");
expect(transpose("Cdim").toKey("F").toString()).toEqual("Fdim");
it("tranposes major and minor chords", () => {
expect(transpose("C Cmaj Cmaj7 CM").toKey("F").toString()).toEqual(
"F Fmaj Fmaj7 FM"
);
expect(transpose("Cm Cmin Cmin7").toKey("F").toString()).toEqual(
"Dm Dmin Dmin7"
);
});

it("tranposes augmented and diminished chords", () => {
expect(transpose("Caug C+ C+5").toKey("F").toString()).toEqual(
"Faug F+ F+5"
);
expect(transpose("Asus4 Asus6").up(2).toString()).toEqual("Bsus4 Bsus6");
expect(transpose("Cdim C-").toKey("F").toString()).toEqual("Fdim F-");
});

it("tranposes sus chords", () => {
expect(transpose("Asus4 Asus2").up(2).toString()).toEqual("Bsus4 Bsus2");
});

it("transposes chords with added tones", () => {
expect(transpose("Cadd9 C9 C6 C+9 Cadd13 C+13").toKey("F").toString()).toEqual(
"Fadd9 F9 F6 F+9 Fadd13 F+13"
);
expect(
transpose("C7/9 Cm7/5- C7/9/11+ C7+/9 C7.11+").toKey("F").toString()
).toEqual("F7/9 Fm7/5- F7/9/11+ F7+/9 F7.11+");
});

it("transposes chords with flat and sharp added tones", () => {
expect(
transpose("C7b9 C7b13 C7(b9) E7#9 E7#11").toKey("F").toString()
).toEqual("F7b9 F7b13 F7(b9) A7#9 A7#11");
});

it("transposes chords with complex added tones", () => {
it("transposes chords with added tones in parentheses", () => {
expect(
transpose("Cadd9 C7/9 Cm7/5- C7/9/11+ C7+/9 C7.11+ E7#9").toKey("F").toString()
).toEqual("Fadd9 F7/9 Fm7/5- F7/9/11+ F7+/9 F7.11+ A7#9");
transpose("C7(b9) C7(add13) C7(b5) C7(#9) C7b9(#11)")
.toKey("Db")
.toString()
).toEqual("Db7(b9) Db7(add13) Db7(b5) Db7(#9) Db7b9(#11)");
});

it("transposes bass chords to the right key", () => {
Expand Down

0 comments on commit 7d59c72

Please sign in to comment.