Skip to content

Commit

Permalink
feat(base_note): ✨ add next and previous getters (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Mar 18, 2024
1 parent bf64d73 commit cb0b9a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/src/note/base_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,31 @@ enum BaseNote implements Comparable<BaseNote> {
/// ```dart
/// BaseNote.g.transposeBySize(Size.unison) == BaseNote.g
/// BaseNote.g.transposeBySize(Size.fifth) == BaseNote.d
/// BaseNote.a.transposeBySize(-3) == BaseNote.f
/// BaseNote.a.transposeBySize(-Size.third) == BaseNote.f
/// ```
BaseNote transposeBySize(Size size) =>
BaseNote.fromOrdinal(ordinal + size.incrementBy(-1));

/// The next ordinal [BaseNote].
///
/// Example:
/// ```dart
/// BaseNote.c.next == BaseNote.d
/// BaseNote.f.next == BaseNote.a
/// BaseNote.b.next == BaseNote.c
/// ```
BaseNote get next => transposeBySize(Size.second);

/// The previous ordinal [BaseNote].
///
/// Example:
/// ```dart
/// BaseNote.e.previous == BaseNote.d
/// BaseNote.g.previous == BaseNote.f
/// BaseNote.c.previous == BaseNote.b
/// ```
BaseNote get previous => transposeBySize(-Size.second);

/// The string representation of this [BaseNote] based on [system].
///
/// See [NoteNotation] for all system implementations.
Expand Down
18 changes: 18 additions & 0 deletions test/src/note/base_note_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ void main() {
});
});

group('.next', () {
test('returns the next ordinal BaseNote', () {
expect(BaseNote.c.next, BaseNote.d);
expect(BaseNote.e.next, BaseNote.f);
expect(BaseNote.f.next, BaseNote.g);
expect(BaseNote.b.next, BaseNote.c);
});
});

group('.previous', () {
test('returns the previous ordinal BaseNote', () {
expect(BaseNote.b.previous, BaseNote.a);
expect(BaseNote.f.previous, BaseNote.e);
expect(BaseNote.d.previous, BaseNote.c);
expect(BaseNote.c.previous, BaseNote.b);
});
});

group('.compareTo()', () {
test('sorts BaseNotes in a collection', () {
final orderedSet = SplayTreeSet<BaseNote>.of({
Expand Down

0 comments on commit cb0b9a1

Please sign in to comment.