From 361d3505d2eb445dd59378a0178a7e759c0bfdfc Mon Sep 17 00:00:00 2001 From: Martijn Versluis Date: Tue, 10 Dec 2024 15:17:59 +0100 Subject: [PATCH] Test and implement more chord suffixes (#1504) --- src/parser/chord_definition/grammar.pegjs | 16 ++++- .../chord_pro/chord_definition.test.ts | 61 +++---------------- 2 files changed, 23 insertions(+), 54 deletions(-) diff --git a/src/parser/chord_definition/grammar.pegjs b/src/parser/chord_definition/grammar.pegjs index aa4e5ecb..5c662a81 100644 --- a/src/parser/chord_definition/grammar.pegjs +++ b/src/parser/chord_definition/grammar.pegjs @@ -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 __ { diff --git a/test/chord_sheet/chord_pro/chord_definition.test.ts b/test/chord_sheet/chord_pro/chord_definition.test.ts index a638ae09..6dc5205f 100644 --- a/test/chord_sheet/chord_pro/chord_definition.test.ts +++ b/test/chord_sheet/chord_pro/chord_definition.test.ts @@ -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', () => { @@ -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}`); - }); - }); }); });