From 7d59c72724ca4ae3d27860be9a5112f905772157 Mon Sep 17 00:00:00 2001 From: Duncan <1658485+ddycai@users.noreply.github.com> Date: Sat, 19 Aug 2023 21:44:43 -0700 Subject: [PATCH] Add support for flat added tones and tones in parens. Also organized tests a bit better. --- dist/Chord.js | 2 +- dist/index.js | 6 ------ src/Chord.ts | 2 +- src/index.ts | 7 ------- test/test.ts | 46 ++++++++++++++++++++++++++++++++++++---------- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/dist/Chord.js b/dist/Chord.js index f2d87ef..b826ad1 100644 --- a/dist/Chord.js +++ b/dist/Chord.js @@ -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 = `(?\\(?${TRIAD_PATTERN}?${ADDED_TONE_PATTERN}*\\)?)`; const BASS_PATTERN = "(\\/(?[A-G](#|b)?))?"; exports.ROOT_PATTERN = "(?[A-G](#|b)?)"; diff --git a/dist/index.js b/dist/index.js index ee1d043..d880084 100644 --- a/dist/index.js +++ b/dist/index.js @@ -77,8 +77,6 @@ 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) { @@ -86,7 +84,6 @@ function tokenize(text) { if (!isTokenEmpty && Chord_1.isChord(token)) { const chord = Chord_1.Chord.parse(token); newLine.push(chord); - chordCount++; lastTokenWasString = false; } else { @@ -96,9 +93,6 @@ function tokenize(text) { else { newLine.push(token); } - if (!isTokenEmpty) { - tokenCount++; - } lastTokenWasString = true; } } diff --git a/src/Chord.ts b/src/Chord.ts index 73cfc6b..89ae536 100644 --- a/src/Chord.ts +++ b/src/Chord.ts @@ -29,7 +29,7 @@ export const CHORD_RANKS: Map = 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 = `(?\\(?${TRIAD_PATTERN}?${ADDED_TONE_PATTERN}*\\)?)`; const BASS_PATTERN = "(\\/(?[A-G](#|b)?))?"; diff --git a/src/index.ts b/src/index.ts index 662baee..f1763e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,8 +97,6 @@ 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) { @@ -106,7 +104,6 @@ function tokenize(text: string): Token[][] { if (!isTokenEmpty && isChord(token)) { const chord: Chord = Chord.parse(token); newLine.push(chord); - chordCount++; lastTokenWasString = false; } else { if (lastTokenWasString) { @@ -114,10 +111,6 @@ function tokenize(text: string): Token[][] { } else { newLine.push(token); } - - if (!isTokenEmpty) { - tokenCount++; - } lastTokenWasString = true; } } diff --git a/test/test.ts b/test/test.ts index 3cf76a8..e963a6a 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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", () => { @@ -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", () => {