Skip to content

Commit

Permalink
refactor(utils): ♻️ move nonZeroSign method from int to num ext…
Browse files Browse the repository at this point in the history
…ension (#511)

Signed-off-by: Albert Mañosa <26429103+albertms10@users.noreply.github.com>
  • Loading branch information
albertms10 committed Jun 18, 2024
1 parent 04c3b1e commit f5598a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
14 changes: 2 additions & 12 deletions lib/src/utils/int_extension.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'num_extension.dart';

/// An int extension.
extension IntExtension on int {
/// This [int] incremented by [step].
Expand All @@ -24,16 +26,4 @@ extension IntExtension on int {

return mod == 0 ? n : mod;
}

/// The sign of this integer.
///
/// Like [sign] except that it returns 1 for zero (considering it positive).
///
/// Example:
/// ```dart
/// 5.nonZeroSign == 1
/// 0.nonZeroSign == 1
/// (-2).nonZeroSign == -1
/// ```
int get nonZeroSign => isNegative ? -1 : 1;
}
12 changes: 12 additions & 0 deletions lib/src/utils/num_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ extension NumExtension on num {
/// (-5).toDeltaString() == '-5'
/// ```
String toDeltaString() => isNegative ? '$this' : '+$this';

/// The sign of this integer.
///
/// Like [sign] except that it returns 1 for zero (considering it positive).
///
/// Example:
/// ```dart
/// 5.nonZeroSign == 1
/// 0.nonZeroSign == 1
/// (-2).nonZeroSign == -1
/// ```
int get nonZeroSign => isNegative ? -1 : 1;
}
8 changes: 0 additions & 8 deletions test/utils/int_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,5 @@ void main() {
expect(0.nonZeroMod(5), 5);
});
});

group('.nonZeroSign', () {
test('returns the non-zero sign of this int', () {
expect(5.nonZeroSign, 1);
expect(0.nonZeroSign, 1);
expect((-2).nonZeroSign, -1);
});
});
});
}
10 changes: 10 additions & 0 deletions test/utils/num_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@ void main() {
expect((-10.02).toDeltaString(), '-10.02');
});
});

group('.nonZeroSign', () {
test('returns the non-zero sign of this num', () {
expect(5.nonZeroSign, 1);
expect(345.2345.nonZeroSign, 1);
expect(0.nonZeroSign, 1);
expect((-2).nonZeroSign, -1);
expect((-2.56).nonZeroSign, -1);
});
});
});
}

0 comments on commit f5598a1

Please sign in to comment.