Skip to content

Commit

Permalink
Parse chords with monzos with comma-separated components
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed May 20, 2023
1 parent 2047d46 commit c89c8e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ describe('Chord parser', () => {
expect(cpsFactors[2].monzo.valueOf()).toBeCloseTo(5);
expect(cpsFactors[3].monzo.valueOf()).toBeCloseTo(7);
});

it('parses mixed comma-separated monzos with comma-separated components', () => {
const monzos = parseChord(
'[0, 0, 0>, [-2, 0, 1>,[-1, 1, 0>:[1 0 0>',
3,
/,|:/
).map(interval =>
interval.monzo.vector.map(component => component.valueOf())
);
expect(arraysEqual(monzos[0], [0, 0, 0])).toBeTruthy();
expect(arraysEqual(monzos[1], [-2, 0, 1])).toBeTruthy();
expect(arraysEqual(monzos[2], [-1, 1, 0])).toBeTruthy();
expect(arraysEqual(monzos[3], [1, 0, 0])).toBeTruthy();
});
});

describe('Scale parse', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,13 @@ export function parseChord(
separator: string | RegExp = ':',
options?: IntervalOptions
) {
// Protect commas inside monzos
input = input.replace(/\[.*?>/g, match => match.replace(/,/g, '¤'));

const chord: Interval[] = [];
input.split(separator).forEach(line => {
line = line.trim();
// Restore commas
line = line.trim().replace(/¤/g, ',');
if (!line.length) {
return;
}
Expand Down

0 comments on commit c89c8e7

Please sign in to comment.