From e4ebbf0daba65066c704aaf0735f0c34ab1575be Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Wed, 7 Feb 2024 17:06:14 +0100 Subject: [PATCH] Add if empty and blank, add tests, remove callbacks --- packages/netglade_utils/CHANGELOG.md | 4 + .../lib/src/extensions/future_extensions.dart | 5 +- .../lib/src/extensions/string_extensions.dart | 21 ++ .../lib/src/testing/mock_extensions.dart | 9 +- .../lib/src/typedefs/callbacks.dart | 7 - .../lib/src/typedefs/typedefs.dart | 1 - packages/netglade_utils/pubspec.yaml | 2 +- .../extensions/string_extensions_test.dart | 186 +++++++++++++++--- 8 files changed, 193 insertions(+), 42 deletions(-) delete mode 100644 packages/netglade_utils/lib/src/typedefs/callbacks.dart delete mode 100644 packages/netglade_utils/lib/src/typedefs/typedefs.dart diff --git a/packages/netglade_utils/CHANGELOG.md b/packages/netglade_utils/CHANGELOG.md index 300f645..87a9f9e 100644 --- a/packages/netglade_utils/CHANGELOG.md +++ b/packages/netglade_utils/CHANGELOG.md @@ -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`. diff --git a/packages/netglade_utils/lib/src/extensions/future_extensions.dart b/packages/netglade_utils/lib/src/extensions/future_extensions.dart index df6c483..0481c26 100644 --- a/packages/netglade_utils/lib/src/extensions/future_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/future_extensions.dart @@ -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 on Future { // 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 onTakingTooLong(Duration duration, VoidCallback callback) async { + Future onTakingTooLong(Duration duration, OnTakingTooLongCallback callback) async { final timer = Timer(duration, callback); try { diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index 77adf0e..c069301 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -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? { @@ -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. @@ -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; + } } diff --git a/packages/netglade_utils/lib/src/testing/mock_extensions.dart b/packages/netglade_utils/lib/src/testing/mock_extensions.dart index 8cfa562..6ba4ec1 100644 --- a/packages/netglade_utils/lib/src/testing/mock_extensions.dart +++ b/packages/netglade_utils/lib/src/testing/mock_extensions.dart @@ -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> { void thenAnswerWithVoid() => thenAnswer((_) async => {}); @@ -46,14 +45,14 @@ extension StreamSubscriptionAnswer on When> { void thenAnswerWithProvidedCallback() => thenAnswer((i) { final callback = i.positionalArguments.singleOrNull; - // ignore: no-empty-block, it needs to be empty - return Stream.fromIterable([]).listen(callback != null ? callback as Setter : (_) {}); + // ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API + return Stream.fromIterable([]).listen(callback != null ? callback as void Function(T value) : (_) {}); }); void thenAnswerWithCustomStream(Stream 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 : (_) {}); + // 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) : (_) {}); }); } diff --git a/packages/netglade_utils/lib/src/typedefs/callbacks.dart b/packages/netglade_utils/lib/src/typedefs/callbacks.dart deleted file mode 100644 index e4db552..0000000 --- a/packages/netglade_utils/lib/src/typedefs/callbacks.dart +++ /dev/null @@ -1,7 +0,0 @@ -typedef VoidCallback = void Function(); - -typedef Getter = T Function(); - -typedef Setter = void Function(T value); - -typedef Change = Setter; diff --git a/packages/netglade_utils/lib/src/typedefs/typedefs.dart b/packages/netglade_utils/lib/src/typedefs/typedefs.dart deleted file mode 100644 index cf41e9a..0000000 --- a/packages/netglade_utils/lib/src/typedefs/typedefs.dart +++ /dev/null @@ -1 +0,0 @@ -export 'callbacks.dart'; diff --git a/packages/netglade_utils/pubspec.yaml b/packages/netglade_utils/pubspec.yaml index 1c11589..b67ca69 100644 --- a/packages/netglade_utils/pubspec.yaml +++ b/packages/netglade_utils/pubspec.yaml @@ -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 diff --git a/packages/netglade_utils/test/src/extensions/string_extensions_test.dart b/packages/netglade_utils/test/src/extensions/string_extensions_test.dart index 0898b03..3344e16 100644 --- a/packages/netglade_utils/test/src/extensions/string_extensions_test.dart +++ b/packages/netglade_utils/test/src/extensions/string_extensions_test.dart @@ -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'; @@ -23,33 +25,40 @@ 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); }); }); }); @@ -57,33 +66,40 @@ void main() { 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); }); }); }); @@ -91,19 +107,23 @@ void main() { 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); }); }); }); @@ -111,19 +131,23 @@ void main() { 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); }); }); }); @@ -131,19 +155,129 @@ void main() { 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)); }); }); });