Skip to content

Commit

Permalink
feat(interval): add descending method (#103)
Browse files Browse the repository at this point in the history
* feat(interval): add `descending` method

* refactor(tonality): use `Interval.descending` method
  • Loading branch information
albertms10 committed May 9, 2023
1 parent c4db414 commit 429f904
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
13 changes: 13 additions & 0 deletions lib/src/interval/interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ class Interval implements MusicItem {
/// ```
bool get isDescending => size.isNegative;

/// Returns a copy of this [Interval] based on [isDescending].
///
/// Example:
/// ```dart
/// Interval.minorSecond.descending() == -Interval.minorSecond
/// Interval.majorThird.descending(isDescending: false) == Interval.majorThird
/// (-Interval.perfectFifth).descending() == -Interval.perfectFifth
/// (-Interval.majorSeventh).descending(isDescending: false)
/// == Interval.majorSeventh
/// ```
Interval descending({bool isDescending = true}) =>
this.isDescending != isDescending ? -this : Interval._(size, quality);

/// Returns the inverted of this [Interval].
///
/// Example:
Expand Down
6 changes: 2 additions & 4 deletions lib/src/tonality/key_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ class KeySignature implements Comparable<KeySignature> {
/// const KeySignature(2, Accidental.sharp).majorNote == Note.d
/// ```
Note get majorNote {
final fifthInterval = Interval.perfect(
5 * (accidental == Accidental.flat ? -1 : 1),
PerfectQuality.perfect,
);
final fifthInterval = Interval.perfectFifth
.descending(isDescending: accidental == Accidental.flat);

return EnharmonicNote(
(fifthInterval.semitones * accidentals + 1).chromaticModExcludeZero,
Expand Down
5 changes: 1 addition & 4 deletions lib/src/tonality/tonality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ class Tonality implements Comparable<Tonality> {
/// ```
Tonality get relative => Tonality(
note.transposeBy(
Interval.imperfect(
3 * (mode == TonalMode.major ? -1 : 1),
ImperfectQuality.minor,
),
Interval.minorThird.descending(isDescending: mode == TonalMode.major),
),
mode.opposite,
);
Expand Down
38 changes: 38 additions & 0 deletions test/src/interval/interval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,44 @@ void main() {
});
});

group('.descending()', () {
test(
'should return the descending Interval based on isDescending',
() {
expect(Interval.majorSecond.descending(), -Interval.majorSecond);
expect(
Interval.minorThird.descending(isDescending: false),
Interval.minorThird,
);
expect(
(-Interval.minorSixth).descending(isDescending: false),
Interval.minorSixth,
);
expect(
(-Interval.perfectOctave).descending(),
-Interval.perfectOctave,
);
},
);

test(
'should return a copy of this Interval based on isDescending',
() {
const ascendingInterval = Interval.perfectFourth;
expect(
identical(ascendingInterval.descending(), ascendingInterval),
isFalse,
);

final descendingInterval = -Interval.minorThird;
expect(
identical(descendingInterval.descending(), descendingInterval),
isFalse,
);
},
);
});

group('.inverted', () {
test('should return the inverted of this Interval', () {
expect(Interval.diminishedUnison.inverted, Interval.augmentedOctave);
Expand Down

0 comments on commit 429f904

Please sign in to comment.