Skip to content

Commit

Permalink
Test directive selector
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis committed Dec 14, 2024
1 parent f76228b commit ecbaa7d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/formatter/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import MetadataConfiguration, {
MetadataConfigurationProperties,
} from './metadata_configuration';

import InstrumentConfiguration, { InstrumentConfigurationProperties } from './instrument_configuration';
import UserConfiguration, { UserConfigurationProperties } from './user_configuration';

export type Delegate = (_string: string) => string;
export const defaultDelegate: Delegate = (string: string) => string;

Expand All @@ -17,6 +20,8 @@ export type ConfigurationProperties = Record<string, any> & {
useUnicodeModifiers: boolean,
normalizeChords: boolean,
delegates: Partial<Record<ContentType, Delegate>>;
instrument?: InstrumentConfigurationProperties;
user?: UserConfigurationProperties;
};

export const defaultConfiguration: ConfigurationProperties = {
Expand All @@ -41,8 +46,6 @@ class Configuration {

key: Key | null;

configuration: Record<string, any>;

expandChorusDirective: boolean;

useUnicodeModifiers: boolean;
Expand All @@ -51,6 +54,10 @@ class Configuration {

delegates: Partial<Record<ContentType, Delegate>>;

instrument?: InstrumentConfiguration;

user?: UserConfiguration;

get metadataSeparator(): string {
return this.metadata.separator ?? '';
}
Expand All @@ -64,7 +71,8 @@ class Configuration {
this.metadata = new MetadataConfiguration(configuration.metadata);
this.key = configuration.key ? Key.wrap(configuration.key) : null;
this.delegates = { ...defaultConfiguration.delegates, ...configuration.delegates };
this.configuration = { configuration, delegates: this.delegates };
this.instrument = configuration.instrument ? new InstrumentConfiguration(configuration.instrument) : undefined;
this.user = configuration.user ? new UserConfiguration(configuration.user) : undefined;
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/formatter/configuration/instrument_configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface InstrumentConfigurationProperties {
type?: string;
description?: string;
}

class InstrumentConfiguration {
type?: string;

description?: string;

constructor(instrumentConfiguration: Partial<InstrumentConfigurationProperties> = {}) {
this.type = instrumentConfiguration.type;
this.description = instrumentConfiguration.description;
}
}

export default InstrumentConfiguration;
4 changes: 2 additions & 2 deletions src/formatter/configuration/metadata_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const defaultMetadataConfiguration: MetadataConfigurationProperties = {
class MetadataConfiguration {
separator?: string;

constructor(metadataConfiguration: MetadataConfigurationProperties = defaultMetadataConfiguration) {
this.separator = metadataConfiguration.separator;
constructor(metadataConfiguration: Partial<MetadataConfigurationProperties> = {}) {
this.separator = metadataConfiguration.separator || defaultMetadataConfiguration.separator;
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/formatter/configuration/user_configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface UserConfigurationProperties {
name?: string;
fullname?: string;
}

class UserConfiguration {
name?: string;

fullname?: string;

constructor(userConfiguration: Partial<UserConfigurationProperties> = {}) {
this.name = userConfiguration.name;
this.fullname = userConfiguration.fullname;
}
}

export default UserConfiguration;
16 changes: 16 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { capos, majorKeys, minorKeys } from './key_config';
import Song from './chord_sheet/song';
import { CAPO, CHORD_STYLE, ChordType } from './chord_sheet/tag';
import Line from './chord_sheet/line';
import Configuration from './formatter/configuration/configuration';
import Metadata from './chord_sheet/metadata';

export function transposeDistance(transposeKey: string, songKey: string): number {
if (/^\d+$/.test(transposeKey)) {
Expand Down Expand Up @@ -109,3 +111,17 @@ export function getKeys(key: Key | string): string[] {
const chordType = keyObj.type === 'solfege' ? 'solfege' : 'symbol';
return keyObj.isMinor() ? minorKeys[chordType] : majorKeys[chordType];
}

export function testSelector(selector: string, configuration: Configuration, metadata: Metadata) {
if (selector === configuration.instrument?.type) {
return true;
}

if (selector === configuration.user?.name) {
return true;
}

const metadataValue = metadata.getSingle(selector);

return !!(metadataValue && metadataValue !== '');
}
55 changes: 54 additions & 1 deletion test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Song from '../src/chord_sheet/song';
import { createLine } from './utilities';
import { renderChord } from '../src/helpers';
import { renderChord, testSelector } from '../src/helpers';
import Key from '../src/key';
import Metadata from '../src/chord_sheet/metadata';
import Configuration from '../src/formatter/configuration/configuration';

describe('renderChord', () => {
it('correctly normalizes when a capo is set', () => {
Expand Down Expand Up @@ -29,3 +31,54 @@ describe('renderChord', () => {
expect(renderChord('Dm7', line, song, { renderKey: Key.parse('B'), useUnicodeModifier: true })).toEqual('G♯m7');
});
});

describe('testSelector', () => {
it('returns true when the selector matches the configured instrument type', () => {
const configuration = new Configuration({ instrument: { type: 'guitar' } });
const metadata = new Metadata();

expect(testSelector('guitar', configuration, metadata)).toEqual(true);
});

it('returns false when the selector does not match the configured instrument type', () => {
const configuration = new Configuration({ instrument: { type: 'guitar' } });
const metadata = new Metadata();

expect(testSelector('piano', configuration, metadata)).toEqual(false);
});

it('return true when the selector matches the configured user name', () => {
const configuration = new Configuration({ user: { name: 'john' } });
const metadata = new Metadata();

expect(testSelector('john', configuration, metadata)).toEqual(true);
});

it('returns false when the selector does not match the configured user name', () => {
const configuration = new Configuration({ user: { name: 'john' } });
const metadata = new Metadata();

expect(testSelector('jane', configuration, metadata)).toEqual(false);
});

it('returns true when the selector matches a truthy metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata({ 'horns': 'true' });

expect(testSelector('horns', configuration, metadata)).toEqual(true);
});

it('returns false when the selector matches an empty metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata({ 'horns': '' });

expect(testSelector('horns', configuration, metadata)).toEqual(false);
});

it('returns false when the selector does not match a metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata();

expect(testSelector('horns', configuration, metadata)).toEqual(false);
});
});

0 comments on commit ecbaa7d

Please sign in to comment.