forked from martijnversluis/ChordSheetJS
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose song chord definitions with defaults (#864)
- Loading branch information
1 parent
160c1e6
commit 8a1745c
Showing
12 changed files
with
1,042 additions
and
12 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...chord_sheet/chord_pro/chord_definition.ts → src/chord_definition/chord_definition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import ChordDefinition from './chord_definition'; | ||
import chordDefinitions from './defaults.json'; | ||
|
||
export type DefinitionSet = Record<string, ChordDefinition>; | ||
|
||
class ChordDefinitionSet { | ||
definitions: DefinitionSet; | ||
|
||
constructor(definitions?: DefinitionSet) { | ||
this.definitions = definitions || {}; | ||
} | ||
|
||
get(chord: string): ChordDefinition | null { | ||
return this.definitions[chord] || null; | ||
} | ||
|
||
withDefaults() { | ||
const defaultDefinitions: Record<string, string> = chordDefinitions; | ||
const clone = this.clone(); | ||
|
||
Object.keys(defaultDefinitions).forEach((chord: string) => { | ||
const definition = ChordDefinition.parse(defaultDefinitions[chord]); | ||
|
||
if (!clone.has(chord)) { | ||
clone.add(chord, definition); | ||
} | ||
}); | ||
|
||
return clone; | ||
} | ||
|
||
add(chord: string, definition: ChordDefinition) { | ||
this.definitions[chord] = definition; | ||
} | ||
|
||
has(chord: string): boolean { | ||
return chord in this.definitions; | ||
} | ||
|
||
clone(): ChordDefinitionSet { | ||
const clone = new ChordDefinitionSet(); | ||
|
||
Object.keys(this.definitions).forEach((chord: string) => { | ||
clone.add(chord, this.definitions[chord].clone()); | ||
}); | ||
|
||
return clone; | ||
} | ||
} | ||
|
||
export default ChordDefinitionSet; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ Fret | |
} | ||
|
||
FretNumber | ||
= number:[1-9] { | ||
= number:[0-9] { | ||
return parseInt(number, 10); | ||
} | ||
|
||
|
6 changes: 3 additions & 3 deletions
6
..._sheet/chord_pro/chord_definition.test.ts → ...chord_definition/chord_definition.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import ChordDefinition from '../../src/chord_definition/chord_definition'; | ||
import ChordDefinitionSet from '../../src/chord_definition/chord_definition_set'; | ||
|
||
describe('ChordDefinitionSet', () => { | ||
describe('#get', () => { | ||
it('returns a chord definition when present for the chord', () => { | ||
const chordDefinition = ChordDefinition.parse(' D7 base-fret 3 frets x 3 2 3 1 x '); | ||
|
||
const definitionSet = new ChordDefinitionSet({ | ||
'D7': chordDefinition, | ||
}); | ||
|
||
expect(definitionSet.get('D7')).toEqual(chordDefinition); | ||
}); | ||
|
||
it('returns null when there is no definition for the chord', () => { | ||
const definitionSet = new ChordDefinitionSet(); | ||
|
||
expect(definitionSet.get('A')).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('#withDefaults', () => { | ||
it('loads the defaults for missing definitions', () => { | ||
const d7OpenGm = ChordDefinition.parse('D7 base-fret 0 frets 0 2 4 4 2 4'); | ||
const am = ChordDefinition.parse('Am base-fret 0 frets x 0 2 2 1 0'); | ||
|
||
const chordDefinitionSet = | ||
new ChordDefinitionSet({ | ||
'D7': d7OpenGm, | ||
'Am': am, | ||
}) | ||
.withDefaults(); | ||
|
||
expect(chordDefinitionSet.get('D7')).toEqual(d7OpenGm); | ||
expect(chordDefinitionSet.get('Am')).toEqual(am); | ||
}); | ||
}); | ||
|
||
describe('#add', () => { | ||
it('adds a chord definition for a tuning', () => { | ||
const definition = ChordDefinition.parse('A base-fret 0 frets 0 1 2 2 2 0'); | ||
const library = new ChordDefinitionSet(); | ||
|
||
library.add('A', definition); | ||
|
||
expect(library.get('A')).toEqual(definition); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters