Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(key_signature): ✨ add incrementBy method #428

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/src/key/key_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ final class KeySignature implements Comparable<KeySignature> {

static const _noteNotation = EnglishNoteNotation(showNatural: true);

/// Returns a new [KeySignature] incrementing its fifths [distance].
///
/// Example:
/// ```dart
/// KeySignature.empty.incrementBy(1) == KeySignature([Note.f.sharp])
///
/// KeySignature([Note.f.sharp, Note.c.sharp]).incrementBy(3)
/// == KeySignature.fromDistance(5)
///
/// KeySignature.fromDistance(-3).incrementBy(-1)
/// == KeySignature([Note.b.flat, Note.e.flat])
///
/// KeySignature([Note.e.flat]).incrementBy(1) == null
/// ```
KeySignature? incrementBy(int distance) {
final cachedDistance = this.distance;
if (cachedDistance == null) return null;

return KeySignature.fromDistance(cachedDistance.incrementBy(distance));
}

@override
String toString() => '$distance (${notes.map(
(note) => note.toString(system: _noteNotation),
Expand Down
21 changes: 21 additions & 0 deletions test/src/key/key_signature_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,27 @@ void main() {
});
});

group('.incrementBy()', () {
test('returns a new KeyGignature increasing the fifths distance', () {
expect(KeySignature.empty.incrementBy(-1), KeySignature([Note.b.flat]));
expect(KeySignature.empty.incrementBy(0), KeySignature.empty);
expect(KeySignature.empty.incrementBy(1), KeySignature([Note.f.sharp]));

expect(
KeySignature([Note.f.sharp, Note.c.sharp]).incrementBy(3),
KeySignature.fromDistance(5),
);
expect(
KeySignature.fromDistance(-3).incrementBy(-1),
KeySignature([Note.b.flat, Note.e.flat]),
);
});

test('returns null when this KeySignature is not canonical', () {
expect(KeySignature([Note.e.flat]).incrementBy(1), isNull);
});
});

group('.toString()', () {
test('returns the string representation of this KeySignature', () {
expect(
Expand Down