Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add if empty and blank, add tests, remove callbacks #22

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/netglade_utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0
- Add `ifEmpty` and `ifBlank` to string extensions.
- Remove typedefs. (private)

## 1.1.0
- Add `isNotNullNorEmpty` to `String?` extension.
- Fix implementation and doc comments in `string_extensions`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:async';

import 'package:clock/clock.dart';
import 'package:netglade_utils/src/typedefs/typedefs.dart';

typedef OnTakingTooLongCallback = void Function();

extension FutureExtensions<T> on Future<T> {
// ignore: comment_references, see https://github.com/dart-lang/linter/issues/2079
/// If [this] future taking longer than [duration] to execute - [callback] is called.
Future<T> onTakingTooLong(Duration duration, VoidCallback callback) async {
Future<T> onTakingTooLong(Duration duration, OnTakingTooLongCallback callback) async {
final timer = Timer(duration, callback);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ extension StringExtensions on String {
String lastNCharacters(int limit) {
return characters.getRange(max(0, length - limit)).toString();
}

String? ifEmpty([String? defaultValue]) {
return isEmpty ? defaultValue : this;
}

String? ifBlank([String? defaultValue]) {
return isBlank ? defaultValue : this;
}
}

extension NullableStringExtensions on String? {
Expand All @@ -42,6 +50,11 @@ extension NullableStringExtensions on String? {
return this?.isBlank ?? true;
}

/// Returns negation of [isNullOrBlank].
bool get isNotNullNorBlank {
return !isNullOrBlank;
}

/// Returns negation of [isBlank].
///
/// * String? is blank when it has a value and contains only whitespaces.
Expand All @@ -58,4 +71,12 @@ extension NullableStringExtensions on String? {
bool get isNotNullNorEmpty {
return !isNullOrEmpty;
}

String? ifEmpty([String? defaultValue]) {
return (this?.isEmpty ?? false) ? defaultValue : this;
}

String? ifBlank([String? defaultValue]) {
return (this?.isBlank ?? false) ? defaultValue : this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:async';

import 'package:mocktail/mocktail.dart';
import 'package:netglade_utils/src/result/result.dart';
import 'package:netglade_utils/src/typedefs/typedefs.dart';

extension FutureVoidAnswer on When<Future<void>> {
void thenAnswerWithVoid() => thenAnswer((_) async => <void>{});
Expand Down Expand Up @@ -46,14 +45,14 @@ extension StreamSubscriptionAnswer<T> on When<StreamSubscription<T>> {
void thenAnswerWithProvidedCallback() => thenAnswer((i) {
final callback = i.positionalArguments.singleOrNull;

// ignore: no-empty-block, it needs to be empty
return Stream.fromIterable(<T>[]).listen(callback != null ? callback as Setter<T> : (_) {});
// ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API
return Stream.fromIterable(<T>[]).listen(callback != null ? callback as void Function(T value) : (_) {});
});

void thenAnswerWithCustomStream(Stream<T> stream) => thenAnswer((i) {
final callback = i.positionalArguments.singleOrNull;

// ignore: no-empty-block, it needs to be empty
return stream.listen(callback != null ? callback as Setter<T> : (_) {});
// ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API
return stream.listen(callback != null ? callback as void Function(T value) : (_) {});
});
}
7 changes: 0 additions & 7 deletions packages/netglade_utils/lib/src/typedefs/callbacks.dart

This file was deleted.

1 change: 0 additions & 1 deletion packages/netglade_utils/lib/src/typedefs/typedefs.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/netglade_utils/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: netglade_utils
version: 1.1.0
version: 1.2.0
description: Dart utils used internally at netglade.
repository: https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils
issue_tracker: https://github.com/netglade/flutter_core/issues
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: prefer_const_declarations, unnecessary_nullable_for_final_variable_declarations, omit_local_variable_types

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

Expand All @@ -23,127 +25,259 @@ void main() {
group('isBlank', () {
group('on String', () {
test('empty message is isBlank', () {
expect(''.isBlank, isTrue);
final String value = '';
expect(value.isBlank, isTrue);
});

test('message with whitespaces is isBlank', () {
expect(' '.isBlank, isTrue);
final String value = ' ';
expect(value.isBlank, isTrue);
});

test('message with non-whitespace characters is not isBlank', () {
expect('xxx'.isBlank, isFalse);
final String value = 'xxx';
expect(value.isBlank, isFalse);
});
});

group('on String?', () {
test('empty message is isBlank', () {
expect(''.isBlank, isTrue);
final String? value = '';
expect(value.isBlank, isTrue);
});

test('null message is not isBlank', () {
expect(null.isBlank, isFalse);
final String? value = null;
expect(value.isBlank, isFalse);
});

test('message with whitespaces is isBlank', () {
expect(' '.isBlank, isTrue);
final String? value = ' ';
expect(value.isBlank, isTrue);
});

test('message with non-whitespace characters is not isBlank', () {
expect('xxx'.isBlank, isFalse);
final String? value = 'xxx';
expect(value.isBlank, isFalse);
});
});
});

group('isNotBlank', () {
group('on String', () {
test('empty message is not isNotBlank', () {
expect(''.isNotBlank, isFalse);
final String value = '';
expect(value.isNotBlank, isFalse);
});

test('message with whitespaces is not isNotBlank', () {
expect(' '.isNotBlank, isFalse);
final String value = ' ';
expect(value.isNotBlank, isFalse);
});

test('message with non-whitespace characters is isNotBlank', () {
expect('xxx'.isNotBlank, isTrue);
final String value = 'xxx';
expect(value.isNotBlank, isTrue);
});
});

group('on String?', () {
test('empty message is not isNotBlank', () {
expect(''.isNotBlank, isFalse);
final String? value = '';
expect(value.isNotBlank, isFalse);
});

test('message is null is isNotBlank', () {
expect(null.isNotBlank, isTrue);
final String? value = null;
expect(value.isNotBlank, isTrue);
});

test('message has whitespaces is not isNotBlank', () {
expect(' '.isNotBlank, isFalse);
final String? value = ' ';
expect(value.isNotBlank, isFalse);
});

test('message has non-whitespace characters is isNotBlank', () {
expect('xxx'.isNotBlank, isTrue);
final String? value = 'xxx';
expect(value.isNotBlank, isTrue);
});
});
});

group('isNullOrBlank', () {
group('on String?', () {
test('empty message is isNullOrBlank', () {
expect(''.isNullOrBlank, isTrue);
final String? value = '';
expect(value.isNullOrBlank, isTrue);
});

test('null message is isNullOrBlank', () {
expect(null.isNullOrBlank, isTrue);
final String? value = null;
expect(value.isNullOrBlank, isTrue);
});

test('message with whitespaces is isNullOrBlank', () {
expect(' '.isNullOrBlank, isTrue);
final String? value = ' ';
expect(value.isNullOrBlank, isTrue);
});

test('message with non-whitespace characters is not isNullOrBlank', () {
expect('xxx'.isNullOrBlank, isFalse);
final String? value = 'xxx';
expect(value.isNullOrBlank, isFalse);
});
});
});

group('isNullOrEmpty', () {
group('on String?', () {
test('empty message is isNullOrEmpty', () {
expect(''.isNullOrEmpty, isTrue);
final String? value = '';
expect(value.isNullOrEmpty, isTrue);
});

test('null message is isNullOrEmpty', () {
expect(null.isNullOrEmpty, isTrue);
final String? value = null;
expect(value.isNullOrEmpty, isTrue);
});

test('message with whitespaces is not isNullOrEmpty', () {
expect(' '.isNullOrEmpty, isFalse);
final String? value = ' ';
expect(value.isNullOrEmpty, isFalse);
});

test('message with non-whitespace characters is not isNullOrEmpty', () {
expect('xxx'.isNullOrEmpty, isFalse);
final String? value = 'xxx';
expect(value.isNullOrEmpty, isFalse);
});
});
});

group('isNotNullNorEmpty', () {
group('on String?', () {
test('empty message is not isNotNullNorEmpty', () {
expect(''.isNotNullNorEmpty, isFalse);
final String? value = '';
expect(value.isNotNullNorEmpty, isFalse);
});

test('null message is not isNotNullNorEmpty', () {
expect(null.isNotNullNorEmpty, isFalse);
final String? value = null;
expect(value.isNotNullNorEmpty, isFalse);
});

test('message with whitespaces is isNotNullNorEmpty', () {
expect(' '.isNotNullNorEmpty, isTrue);
final String? value = ' ';
expect(value.isNotNullNorEmpty, isTrue);
});

test('message with non-whitespace characters is isNotNullNorEmpty', () {
expect('xxx'.isNotNullNorEmpty, isTrue);
final String? value = 'xxx';
expect(value.isNotNullNorEmpty, isTrue);
});
});
});

group('isNotNullNorBlank', () {
group('on String?', () {
test('empty message is not isNotNullNorBlank', () {
final String? value = '';
expect(value.isNotNullNorBlank, isFalse);
});

test('null message is not isNotNullNorBlank', () {
final String? value = null;
expect(value.isNotNullNorBlank, isFalse);
});

test('message with whitespaces is not isNotNullNorBlank', () {
final String? value = ' ';
expect(value.isNotNullNorBlank, isFalse);
});

test('message with non-whitespace characters is isNotNullNorBlank', () {
final String? value = 'xxx';
expect(value.isNotNullNorBlank, isTrue);
});
});
});

group('ifEmpty', () {
group('on String', () {
test('empty message', () {
final String value = '';
expect(value.ifEmpty('aaa'), equals('aaa'));
});

test('message with whitespaces', () {
final String value = ' ';
expect(value.ifEmpty('aaa'), equals(value));
});

test('message with non-whitespace characters', () {
final String value = 'xxx';
expect(value.ifEmpty('aaa'), equals(value));
});
});

group('on String?', () {
test('empty message', () {
final String? value = '';
expect(value.ifEmpty('aaa'), equals('aaa'));
});

test('null message', () {
final String? value = null;
expect(value.ifEmpty('aaa'), equals(value));
});

test('message with whitespaces', () {
final String? value = ' ';
expect(value.ifEmpty('aaa'), equals(value));
});

test('message with non-whitespace characters', () {
final String? value = 'xxx';
expect(value.ifEmpty('aaa'), equals(value));
});
});
});

group('ifBlank', () {
group('on String', () {
test('empty message', () {
final String value = '';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('message with whitespaces', () {
final String value = ' ';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('message with non-whitespace characters', () {
final String value = 'xxx';
expect(value.ifBlank('aaa'), equals(value));
});
});

group('on String?', () {
test('empty message', () {
final String? value = '';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('null message', () {
final String? value = null;
expect(value.ifBlank('aaa'), equals(value));
});

test('message with whitespaces', () {
final String? value = ' ';
expect(value.ifBlank('aaa'), equals('aaa'));
});

test('message with non-whitespace characters', () {
final String? value = 'xxx';
expect(value.ifBlank('aaa'), equals(value));
});
});
});
Expand Down
Loading