Skip to content

Commit

Permalink
Add chord-related method docs to Transpose class (#19)
Browse files Browse the repository at this point in the history
This commit contains,
* Add js doc for Transpose class methods
* Update unit tests with more tests.
* Update version to 0.3.1.
  • Loading branch information
nadunindunil authored Feb 3, 2024
1 parent cd62ee6 commit ecbc185
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
40 changes: 38 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export class Transpose {
this.song = song;
}

/**
* Finds chords in a given line of text.
*
* @param line - The line of text to search for chords.
* @param offset - The offset of the line within the larger text.
* @returns An array of Chord objects containing the chord and its position.
*/
private findChords(line: string, offset: number): Chord[] {
const modifiedLine = line
.replace(/\||\t|\-|\/|\(|\)|\,|\s/g, " ")
Expand Down Expand Up @@ -45,6 +52,12 @@ export class Transpose {
return chordsWithPositions;
}

/**
* Parses a string containing chords and returns an array of Chord objects.
*
* @param src - The string to be parsed.
* @returns An array of Chord objects.
*/
private parse(src: string): Chord[] {
// Parse a text with chords and highlight guitar chords
const src_textArray = src.split(/\r\n|\r|\n/g);
Expand All @@ -60,6 +73,11 @@ export class Transpose {
return chords;
}

/**
* Adds HTML tags to highlight chords in the song.
*
* @returns The updated song string with chord highlighting tags.
*/
getWithTags(): string {
// Add '<span class="chords-highlighted">...</span>' into the code
let offset = 0;
Expand Down Expand Up @@ -94,6 +112,12 @@ export class Transpose {
return updatedSong;
}

/**
* Shifts the scale of the chords by the specified amount.
*
* @param shiftBy The amount to shift the scale by. This could be a positive or negative number.
* @returns An array of updated chords with the shifted scale.
*/
private shiftScale(shiftBy: number): Chord[] {
let refArrangement: string[] | null = null;
const updatedChords = this.chords.map((chord) => {
Expand Down Expand Up @@ -131,13 +155,25 @@ export class Transpose {

return updatedChords;
}


/**
* Shifts the scale of the chords by the specified amount.
*
* @param shiftBy The amount to shift the scale by. This could be either positive or negative number.
* @returns The instance of the class with the shifted scale.
*/
shiftScaleBy(shiftBy: number): this {
this.chords = this.shiftScale(shiftBy);

return this;
}

/**
* Shifts the scale from one chord to another chord.
*
* @param from The chord to shift from.
* @param to The chord to shift to.
* @returns The updated instance of the ChordsTransposer class.
*/
shiftScaleFromTo(from: string, to: string): this {
const arrangement =
chordsArrangement.indexOf(from) !== -1 &&
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chords-transposer",
"version": "0.3.0",
"version": "0.3.1",
"description": "The Chord Transposer is a TypeScript/JavaScript library designed to manipulate and transpose chords within text-based songs.",
"main": "lib/index.ts",
"files": [
Expand Down
36 changes: 27 additions & 9 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ test("parse test", () => {
);
});

test("shiftScaleBy test", () => {
const song = "Gm F Gm Eb B F B Gm";
const songObj = new Transposer(song);

expect(songObj.shiftScaleBy(1).getWithTags()).toEqual(
'<span class="chords-highlighted">Abm</span> <span class="chords-highlighted">Gb</span> <span class="chords-highlighted">Abm</span> <span class="chords-highlighted">E</span> <span class="chords-highlighted">C</span> <span class="chords-highlighted">Gb</span> <span class="chords-highlighted">C</span> <span class="chords-highlighted">Abm</span>',
);
});

test("shiftScaleFromTo test", () => {
const song = "Gm F Gm Eb B F B Gm";

Expand All @@ -42,3 +33,30 @@ test("shiftScaleBy complex test 1", () => {
'| <span class="chords-highlighted">Bbm</span> | <span class="chords-highlighted">Ab</span> | <span class="chords-highlighted">Bbm</span> | <span class="chords-highlighted">Gb</span> |',
);
});

test("shiftScaleBy test", () => {
const song = "Gm F Gm Eb B F B Gm";
const songObj = new Transposer(song);

expect(songObj.shiftScaleBy(1).getWithTags()).toEqual(
'<span class="chords-highlighted">Abm</span> <span class="chords-highlighted">Gb</span> <span class="chords-highlighted">Abm</span> <span class="chords-highlighted">E</span> <span class="chords-highlighted">C</span> <span class="chords-highlighted">Gb</span> <span class="chords-highlighted">C</span> <span class="chords-highlighted">Abm</span>',
);
});

test("shiftScaleBy test with negative shift", () => {
const song = "Gm F Gm Eb B F B Gm";
const songObj = new Transposer(song);

expect(songObj.shiftScaleBy(-1).getWithTags()).toEqual(
'<span class=\"chords-highlighted\">Gbm</span> <span class=\"chords-highlighted\">E</span> <span class=\"chords-highlighted\">Gbm</span> <span class=\"chords-highlighted\">D</span> <span class=\"chords-highlighted\">Bb</span> <span class=\"chords-highlighted\">E</span> <span class=\"chords-highlighted\">Bb</span> <span class=\"chords-highlighted\">Gbm</span>',
);
});

test("shiftScaleBy test with zero shift", () => {
const song = "Gm F Gm Eb B F B Gm";
const songObj = new Transposer(song);

expect(songObj.shiftScaleBy(0).getWithTags()).toEqual(
'<span class="chords-highlighted">Gm</span> <span class="chords-highlighted">F</span> <span class="chords-highlighted">Gm</span> <span class="chords-highlighted">Eb</span> <span class="chords-highlighted">B</span> <span class="chords-highlighted">F</span> <span class="chords-highlighted">B</span> <span class="chords-highlighted">Gm</span>',
);
});

0 comments on commit ecbc185

Please sign in to comment.