From 037502e752a2d5e24794b4e1803b05f912ad7178 Mon Sep 17 00:00:00 2001 From: "o.pakers" Date: Mon, 20 Feb 2017 15:10:29 +0200 Subject: [PATCH 1/3] attempt to make support for chords in square brackets [C] --- index.js | 3 +-- test.js | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d2542f9..c33f7d9 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,7 @@ function transpose(text, mapper, currentKey, formatter) { var newLine = "", chordCount = 0, tokenCount = 0; - var tokens = lines[k].split(/(\s+|-)/g); + var tokens = lines[k].split(/(\s|-|]|\[)/g); for (var i = 0; i < tokens.length; i++) { // Check for all whitespace. @@ -134,7 +134,6 @@ function transpose(text, mapper, currentKey, formatter) { chordCount++; continue; } - // If symbol is chord, transpose it. if (chordRegex.test(tokens[i])) { parts = parse(tokens[i]); diff --git a/test.js b/test.js index b00121b..15c9f01 100644 --- a/test.js +++ b/test.js @@ -241,4 +241,9 @@ describe('Transposer', function() { expect(Transposer.transpose("A-E-F#m-D").up(1).text) .to.equal("Bb-F-Gm-Eb"); }); + + it ("Handles sequence of chords wrapped with square brackets", function() { + var text2 = "[C]Suddenly\n[D]You're [C]here"; + expect(Transposer.transpose(text2).up(1).text).to.equal("[F]Suddenly\n[G]You're [F]here"); + }); }); From b56abacd82faa16034dd6ee2c6980de5dd0e69b6 Mon Sep 17 00:00:00 2001 From: Oskars Pakers Date: Tue, 13 Apr 2021 16:23:38 +0300 Subject: [PATCH 2/3] [issue/17]: Add support for inlined chords within square brackets (ChordPro) --- dist/index.js | 9 ++------- src/index.ts | 9 ++------- test/test.ts | 7 +++++++ 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/dist/index.js b/dist/index.js index 5312f55..e01061e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -86,7 +86,7 @@ function tokenize(text, threshold) { const newLine = []; let chordCount = 0; let tokenCount = 0; - const tokens = line.split(/(\s+|-)/g); + const tokens = line.split(/(\s+|-|]|\[)/g); let lastTokenWasString = false; for (const token of tokens) { const isTokenEmpty = token.trim() === ""; @@ -109,12 +109,7 @@ function tokenize(text, threshold) { lastTokenWasString = true; } } - if (chordCount / tokenCount >= threshold) { - newText.push(newLine); - } - else { - newText.push([line]); - } + newText.push(newLine); } return newText; } diff --git a/src/index.ts b/src/index.ts index fec4ff6..99852ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,8 +106,7 @@ function tokenize(text: string, threshold?: number): Token[][] { const newLine: Token[] = []; let chordCount: number = 0; let tokenCount: number = 0; - const tokens: string[] = line.split(/(\s+|-)/g); - + const tokens: string[] = line.split(/(\s+|-|]|\[)/g); let lastTokenWasString: boolean = false; for (const token of tokens) { const isTokenEmpty = token.trim() === ""; @@ -129,11 +128,7 @@ function tokenize(text: string, threshold?: number): Token[][] { lastTokenWasString = true; } } - if (chordCount / tokenCount >= threshold) { - newText.push(newLine); - } else { - newText.push([line]); - } + newText.push(newLine); } return newText; } diff --git a/test/test.ts b/test/test.ts index d6b33ac..846b924 100644 --- a/test/test.ts +++ b/test/test.ts @@ -240,6 +240,13 @@ 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]" + ); + + }); + it("preserves whitespace", () => { expect(transpose("C D C\nHello world!").toKey("F").toString()).toEqual( "F G F\nHello world!" From 73f1ca491f4040b8f20a2f1a3426b169827072a4 Mon Sep 17 00:00:00 2001 From: Oskars Pakers Date: Tue, 13 Apr 2021 16:32:26 +0300 Subject: [PATCH 3/3] [issue/17]: Remove threshold parameter and default fields --- dist/index.js | 9 +-------- src/index.ts | 9 +-------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/dist/index.js b/dist/index.js index e01061e..ee1d043 100644 --- a/dist/index.js +++ b/dist/index.js @@ -71,15 +71,8 @@ function transposeKey(currentKey, semitones) { return KeySignatures_1.KeySignatures.forRank(newRank); } /** Tokenize the given text into chords. - * - * The ratio of chords to non-chord tokens in each line must be greater than - * the given threshold in order for the line to be transposed. The threshold - * is set to 0.5 by default. */ -function tokenize(text, threshold) { - if (threshold === undefined) { - threshold = 0.5; - } +function tokenize(text) { const lines = text.split("\n"); const newText = []; for (const line of lines) { diff --git a/src/index.ts b/src/index.ts index 99852ef..662baee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,15 +89,8 @@ function transposeKey( } /** Tokenize the given text into chords. - * - * The ratio of chords to non-chord tokens in each line must be greater than - * the given threshold in order for the line to be transposed. The threshold - * is set to 0.5 by default. */ -function tokenize(text: string, threshold?: number): Token[][] { - if (threshold === undefined) { - threshold = 0.5; - } +function tokenize(text: string): Token[][] { const lines: string[] = text.split("\n"); const newText: Token[][] = [];