Skip to content

Commit

Permalink
Rename double.approximately to double.around
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Oct 23, 2023
1 parent d2d8212 commit ecdab86
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 7 deletions.
8 changes: 8 additions & 0 deletions sugar/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## `sugar.collection`
- Add `Lists.toggleAll(...)`
- Add `SplayTreeMaps.firstValueAfter(...)`
- Add `SplayTreeMaps.lastValueBefore(...)`

## `sugar.core`
- Add `System.currentDateTime`
Expand All @@ -11,6 +13,12 @@
- **Breaking** - Remove `Maybe` extension.
- Remove `Iterables.indexed` since it already exists in Dart 3.

## `sugar.crdt`
- Add `StringIndex`

## `sugar.math`
- **Breaking** Change `double.approximately(...)` to `double.around(...)`

## `sugar.time`
- Add optional `DateUnit` parameter to `LocalDate.now(...)`
- Add optional `TemporalUnit` parameter to `LocalDateTime.now(...)`
Expand Down
2 changes: 2 additions & 0 deletions sugar/dartdoc_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dartdoc:
name: "Core"
"Collection":
name: "Collection"
"CRDT":
name: "CRDT"
"Time":
name: "Time"
categoryOrder:
Expand Down
1 change: 1 addition & 0 deletions sugar/lib/crdt.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// TODO: add documentation.
export 'src/crdt/string_index.dart';
17 changes: 17 additions & 0 deletions sugar/lib/src/collection/splay_tree_maps.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import 'dart:collection';

/// Provides functions for working [SplayTreeMap]s.
extension SplayTreeMaps<K, V> on SplayTreeMap<K, V> {

/// Returns the value associated with the first key after [key], or null if there are no keys after [key].
///
/// ```dart
/// final map = SplayTreeMap.of({1: 'A', 2: 'B', 3: 'C'});
///
/// print(map.firstValueAfter(1)); // 'B'
/// print(map.firstValueAfter(3)); // null
/// ```
V? firstValueAfter(K key) => switch (firstKeyAfter(key)) {
null => null,
final key => this[key],
};

/// Returns the value associated with the last key before [key], or null if there are no keys before [key].
///
/// ```dart
/// final map = SplayTreeMap.of({1: 'A', 2: 'B', 3: 'C'});
///
/// print(map.lastValueBefore(3)); // 'B'
/// print(map.lastValueBefore(1)); // null
/// ```
V? lastValueBefore(K key) => switch (lastKeyBefore(key)) {
null => null,
final key => this[key],
Expand Down
6 changes: 3 additions & 3 deletions sugar/lib/src/math/numbers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ extension Doubles on double {
/// Returns true if this and [other] are within the [tolerance] of each other.
///
/// ```dart
/// 1.0002.approximately(1.0, 0.01); // true
/// 1.0002.around(1.0, 0.01); // true
///
/// 1.2.approximately(1.0, 0.01); // false
/// 1.2.around(1.0, 0.01); // false
/// ```
@useResult bool approximately(double other, double tolerance) => (this - other).abs() <= tolerance;
@useResult bool around(num other, double tolerance) => (this - other).abs() <= tolerance;

}
2 changes: 1 addition & 1 deletion sugar/lib/src/time/zone/dynamic_timezone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import 'package:sugar/sugar.dart';
/// ## Contract
/// The transitions, offsets, abbreviations and DSTs should be non-empty and have the same length.
DynamicTimezone(super._name, this._initial, this._transitions, this._offsets, this._unit, this._abbreviations, this._dsts):
_range = Interval.empty(0),
_range = const Interval.empty(0),
_timezone = _initial,
super.from();

Expand Down
22 changes: 22 additions & 0 deletions sugar/test/src/collection/splay_tree_maps_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:collection';

import 'package:sugar/sugar.dart';
import 'package:test/test.dart';

void main() {
group('SplayTreeMaps', () {
final map = SplayTreeMap.of({1: 'A', 2: 'B', 3: 'C'});

group('firstValueAfter(...)', () {
test('key exists', () => expect(map.firstValueAfter(1), 'B'));

test('key does not exist', () => expect(map.firstValueAfter(3), null));
});

group('lastValueBefore(...)', () {
test('key exists', () => expect(map.lastValueBefore(3), 'B'));

test('key does not exist', () => expect(map.lastValueBefore(1), null));
});
});
}
2 changes: 1 addition & 1 deletion sugar/test/src/core/range/interval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {
});

group('empty(...)', () {
final interval = Interval.empty(1);
const interval = Interval.empty(1);

test('empty', () => expect(interval.contains(1), false));

Expand Down
8 changes: 6 additions & 2 deletions sugar/test/src/math/numbers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ void main() {
});

group('Doubles', () {
test('approximately true', () => expect(1.0002.approximately(1.0, 0.01), true));
test('around int in tolerance', () => expect(1.0002.around(1, 0.01), true));

test('approximately false', () => expect(1.2.approximately(1.0, 0.01), false));
test('around int outside tolerance', () => expect(1.2.around(2, 0.01), false));

test('around double in tolerance', () => expect(1.0002.around(1.0, 0.01), true));

test('around double outside tolerance', () => expect(1.2.around(1.0, 0.01), false));
});

}

0 comments on commit ecdab86

Please sign in to comment.