Skip to content

Commit

Permalink
Merge pull request #19 from OskarsPakers/issue/17
Browse files Browse the repository at this point in the history
[issue/17]: Add support for inlined chords within square brackets
  • Loading branch information
ddycai authored Aug 1, 2021
2 parents 19665a8 + a2ab845 commit a703e49
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
18 changes: 3 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,15 @@ 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) {
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() === "";
Expand All @@ -109,12 +102,7 @@ function tokenize(text, threshold) {
lastTokenWasString = true;
}
}
if (chordCount / tokenCount >= threshold) {
newText.push(newLine);
}
else {
newText.push([line]);
}
newText.push(newLine);
}
return newText;
}
Expand Down
18 changes: 3 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[][] = [];
Expand All @@ -106,8 +99,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() === "";
Expand All @@ -129,11 +121,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;
}
Expand Down
7 changes: 7 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down

0 comments on commit a703e49

Please sign in to comment.