Skip to content

Commit

Permalink
s/safeFirst/firstOrNull
Browse files Browse the repository at this point in the history
  • Loading branch information
frank06 committed May 9, 2024
1 parent 23a4c23 commit e54a2e5
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/builders/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension ClassElementX on ClassElement {
ConstructorElement? get freezedConstructor => constructors
.where((c) => c.isFactory && c.displayName == name)
// ignore: invalid_use_of_visible_for_testing_member
.safeFirst;
.firstOrNull;

// unique collection of constructor arguments and fields
Iterable<VariableElement> get relationshipFields {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
/// Finds model of type [T] by [key] in local storage.
T? findOneLocal(String? key) {
if (key == null) return null;
return findManyLocal([key]).safeFirst;
return findManyLocal([key]).firstOrNull;
}

T? findOneLocalById(Object id) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/relationship/belongs_to.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BelongsTo<E extends DataModelMixin<E>> extends Relationship<E, E?> {
}

/// Obtains the single [E] value of this relationship (`null` if not present).
E? get value => _iterable.safeFirst;
E? get value => _iterable.firstOrNull;

/// Sets the single [E] value of this relationship, replacing any previous [value].
///
Expand All @@ -60,7 +60,7 @@ class BelongsTo<E extends DataModelMixin<E>> extends Relationship<E, E?> {
}

/// Returns the [value]'s `key`.
String? get key => super._keys.safeFirst;
String? get key => super._keys.firstOrNull;

Object? get id => key != null ? _adapter.core.getIdForKey(key!) : null;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/relationship/has_many.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class HasMany<E extends DataModelMixin<E>> extends Relationship<E, Set<E>> {

E get first => _iterable.first;

E? get safeFirst => isNotEmpty ? first : null;
E? get firstOrNull => _iterable.firstOrNull;

bool get isEmpty => length == 0;

Expand Down
10 changes: 3 additions & 7 deletions lib/src/utils/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ part of flutter_data;
extension IterableX<T> on Iterable<T> {
@protected
@visibleForTesting
T? get safeFirst => isNotEmpty ? first : null;

@protected
@visibleForTesting
bool containsFirst(T model) => safeFirst == model;
bool containsFirst(T model) => firstOrNull == model;

@protected
@visibleForTesting
Expand Down Expand Up @@ -55,9 +51,9 @@ extension StringUtilsX on String {
return '$prefix:$this';
}

String? get namespace => split(':').safeFirst;
String? get namespace => split(':').firstOrNull;

String? get type => split('#').safeFirst;
String? get type => split('#').firstOrNull;

String denamespace() {
// need to re-join with : in case there were other :s in the text
Expand Down
4 changes: 2 additions & 2 deletions test/utils/misc_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void main() async {
});

test('iterable utils', () {
expect([1, 2, 3].safeFirst, 1);
expect([].safeFirst, isNull);
expect([1, 2, 3].firstOrNull, 1);
expect([].firstOrNull, isNull);

expect([1, 2, 3].containsFirst(1), isTrue);
expect([1, 2, 3].containsFirst(2), isFalse);
Expand Down

0 comments on commit e54a2e5

Please sign in to comment.