Skip to content

Commit

Permalink
Expose song chord definitions with defaults (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Dec 29, 2024
1 parent 160c1e6 commit 8a1745c
Show file tree
Hide file tree
Showing 12 changed files with 1,042 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fret } from '../../constants';
import { parse } from '../../parser/chord_definition/peg_parser';
import { Fret } from '../constants';
import { parse } from '../parser/chord_definition/peg_parser';

/**
* Represents a chord definition.
Expand Down
51 changes: 51 additions & 0 deletions src/chord_definition/chord_definition_set.ts
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;
904 changes: 904 additions & 0 deletions src/chord_definition/defaults.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/chord_sheet/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import Tag, {
START_OF_CHORUS,
} from './tag';
import SongBuilder from '../song_builder';
import ChordDefinition from './chord_pro/chord_definition';
import ChordDefinition from '../chord_definition/chord_definition';
import Chord from '../chord';
import FormattingContext from '../formatter/formatting_context';
import { testSelector } from '../helpers';
import ChordDefinitionSet from '../chord_definition/chord_definition_set';

type EachItemCallback = (_item: Item) => void;

Expand Down Expand Up @@ -479,6 +480,10 @@ Or set the song key before changing key:
return chordDefinitions;
}

get chordDefinitions(): ChordDefinitionSet {
return new ChordDefinitionSet(this.getChordDefinitions());
}

/**
* Change the song contents inline. Return a new {@link Line} to replace it. Return `null` to remove it.
* @example
Expand Down
2 changes: 1 addition & 1 deletion src/chord_sheet/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import AstComponent from './ast_component';
import TraceInfo from './trace_info';
import ChordDefinition from './chord_pro/chord_definition';
import ChordDefinition from '../chord_definition/chord_definition';

import {
ABC, BRIDGE, GRID, LILYPOND, TAB, VERSE,
Expand Down
2 changes: 1 addition & 1 deletion src/chord_sheet_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './serialized_types';
import SoftLineBreak from './chord_sheet/soft_line_break';
import { warn } from './utilities';
import ChordDefinition from './chord_sheet/chord_pro/chord_definition';
import ChordDefinition from './chord_definition/chord_definition';
import SongBuilder from './song_builder';

const CHORD_LYRICS_PAIR = 'chordLyricsPair';
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Chord from './chord';
import ChordDefinition from './chord_sheet/chord_pro/chord_definition';
import ChordDefinition from './chord_definition/chord_definition';
import ChordLyricsPair from './chord_sheet/chord_lyrics_pair';
import ChordProFormatter from './formatter/chord_pro_formatter';
import PdfFormatter from './formatter/pdf_formatter';
Expand Down Expand Up @@ -51,7 +51,7 @@ import {
} from './constants';

export { default as Chord } from './chord';
export { default as ChordDefinition } from './chord_sheet/chord_pro/chord_definition';
export { default as ChordDefinition } from './chord_definition/chord_definition';
export { default as ChordLyricsPair } from './chord_sheet/chord_lyrics_pair';
export { default as ChordProFormatter } from './formatter/chord_pro_formatter';
export { default as PdfFormatter } from './formatter/pdf_formatter';
Expand Down
2 changes: 1 addition & 1 deletion src/parser/chord_definition/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Fret
}

FretNumber
= number:[1-9] {
= number:[0-9] {
return parseInt(number, 10);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChordDefinition } from '../../../src';
import { Fret } from '../../../src/constants';
import SUFFIX_MAPPING from '../../../src/normalize_mappings/suffix-normalize-mapping';
import { ChordDefinition } from '../../src';
import { Fret } from '../../src/constants';
import SUFFIX_MAPPING from '../../src/normalize_mappings/suffix-normalize-mapping';

describe('ChordDefinition', () => {
describe('#clone', () => {
Expand Down
50 changes: 50 additions & 0 deletions test/chord_definition/chord_definition_set.test.ts
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);
});
});
});
20 changes: 20 additions & 0 deletions test/chord_sheet/song.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,24 @@ describe('Song', () => {
expect(song.getChordDefinitions({ configuration, metadata })).toEqual({ Dm: dm });
});
});

describe('#chordDefinitions', () => {
it('returns the unique chord definitions in a song', () => {
const cm7 = createChordDefinition('CM7', 3, ['x', '0', 1]);
const dm = createChordDefinition('Dm', 3, ['x', 3, 5]);

const song = createSong([
createLine([
createTag('chord', 'CM7', cm7),
]),
createLine([]),
createLine([
createTag('define', 'Dm', dm),
]),
]);

expect(song.chordDefinitions.get('CM7')).toEqual(cm7);
expect(song.chordDefinitions.get('Dm')).toEqual(dm);
});
});
});
2 changes: 1 addition & 1 deletion test/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Item from '../src/chord_sheet/item';
import { ChordType, Fret, Modifier } from '../src/constants';
import Key from '../src/key';
import ChordSheetSerializer from '../src/chord_sheet_serializer';
import ChordDefinition from '../src/chord_sheet/chord_pro/chord_definition';
import ChordDefinition from '../src/chord_definition/chord_definition';

import {
ContentType,
Expand Down

0 comments on commit 8a1745c

Please sign in to comment.