Skip to content

Commit

Permalink
Test and implement more chord suffixes (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Dec 10, 2024
1 parent 84b33d8 commit 361d350
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 54 deletions.
16 changes: 13 additions & 3 deletions src/parser/chord_definition/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ ChordDefinitionValue
}

ChordDefinitionName
= name:$([A-Za-z0-9/-♭\(\)\+]+) {
return name;
}
= $(ChordDefinitionNameBase bass:ChordDefinitionNameBass?)

ChordDefinitionNameBase
= $(ChordDefinitionNote ChordDefinitionSuffix?)

ChordDefinitionNameBass
= $("/" ChordDefinitionNote)

ChordDefinitionNote
= $([A-Ga-g]([b#♭♯] / "es" / "s" / "is")?)

ChordDefinitionSuffix
= $([a-zA-Z0-9#♯b♭\(\)\+\-\/øΔ−]+)

BaseFret
= "base-fret" __ baseFret:FretNumber __ {
Expand Down
61 changes: 10 additions & 51 deletions test/chord_sheet/chord_pro/chord_definition.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 Expand Up @@ -48,57 +49,15 @@ describe('ChordDefinition', () => {
expect(chordDefinition.fingers).toEqual([1, 2, 3, 4, 5, 6]);
});

[
'D7 base-fret 1 frets N 3 2 3 1 N fingers a 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers A 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers x 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers X 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers n 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers N 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers - 2 3 4 5 6',
'D7 base-fret 1 frets N 3 2 3 1 N fingers 0 2 3 4 5 6',
].forEach((definitionString) => {
it(`can parse ${definitionString}`, () => {
expect(() => ChordDefinition.parse(definitionString)).not.toThrow();
Object
.keys(SUFFIX_MAPPING)
.filter((suffix) => suffix !== '[blank]')
.forEach((suffix) => {
it(`can parse a chord definition with suffix ${suffix}`, () => {
const chord = `Db${suffix}/A#`;
const chordDefinition = ChordDefinition.parse(`${chord} base-fret 3 frets x 3 2 3 1 x`);
expect(chordDefinition.name).toEqual(chord);
});
});
});

// From: https://en.wikipedia.org/wiki/Chord_(music)#Symbols
[
'm',
'min',
'−',
'M',
'Ma',
'Maj',
'Δ',
'+',
'aug',
'o',
'dim',
'ø',
'2',
'3',
'4',
'5',
'6',
'7',
'9',
'11',
'13',
'6/9',
'sus4',
'sus2',
'(♭9)',
'add',
'alt',
'omit5',
'no5',
].forEach((suffix) => {
it(`can parse a chord definition with suffix ${suffix}`, () => {
const chordDefinition = ChordDefinition.parse(`Db${suffix} base-fret 3 frets x 3 2 3 1 x`);
expect(chordDefinition.name).toEqual(`Db${suffix}`);
});
});
});
});

0 comments on commit 361d350

Please sign in to comment.