From 4ce4adc2e17262b98fe9e3a39900a50d40030377 Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Mon, 5 Feb 2024 18:37:57 +0100 Subject: [PATCH 1/3] Fix string_extensions --- packages/netglade_utils/CHANGELOG.md | 4 + .../lib/src/extensions/string_extensions.dart | 43 ++--- packages/netglade_utils/pubspec.yaml | 2 +- .../extensions/string_extensions_test.dart | 151 ++++++++++++++++++ 4 files changed, 172 insertions(+), 28 deletions(-) create mode 100644 packages/netglade_utils/test/src/extensions/string_extensions_test.dart diff --git a/packages/netglade_utils/CHANGELOG.md b/packages/netglade_utils/CHANGELOG.md index 1e90f79..300f645 100644 --- a/packages/netglade_utils/CHANGELOG.md +++ b/packages/netglade_utils/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.0 +- Add `isNotNullNorEmpty` to `String?` extension. +- Fix implementation and doc comments in `string_extensions`. + ## 1.0.1 - Fix readme. diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index 53ee431..76af29c 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -10,14 +10,13 @@ extension StringExtensions on String { /// - '\t' /// /// When you only need to check exactly empty, use `isEmpty`. + /// + /// * String is blank when it contains only whitespaces. bool get isBlank => trim().isEmpty; - /// Returns true when value is not empty and contains some characters except whitespaces. + /// Returns negation of [isBlank]. bool get isNotBlank => !isBlank; - /// The [Characters] of this string. - Characters get characters => Characters(this); - /// Returns last N characters. /// When the string does not have N characters, returns rest. String lastNCharacters(int limit) { @@ -28,39 +27,29 @@ extension StringExtensions on String { extension NullableStringExtensions on String? { /// Returns true when `this` is `String` which is `isBlank`. /// When the value is `null`, returns `false`. + /// + /// * String? is blank when it has a value and contains only whitespaces. bool get isBlank { - if (this case final nonNull?) { - return nonNull.isBlank; - } + return this?.isBlank ?? false; + } - return false; + /// Returns true when `this` is either `null` or `isBlank`. + bool get isNullOrBlank { + return this?.isBlank ?? true; } - /// Returns true when `this` is `String` which is `isBlank`. - /// When the value is `null`, returns `false`. + /// Returns negation of [isBlank]. bool get isNotBlank { - if (this case final nonNull?) { - return nonNull.isNotBlank; - } - - return false; + return !isBlank; } /// Returns true when `this` is either `null` or `isBlank`. bool get isNullOrEmpty { - if (this case final nonNull?) { - return nonNull.isEmpty; - } - - return true; + return this?.isEmpty ?? true; } - /// Returns true when `this` is either `null` or `isBlank`. - bool get isNullOrBlank { - if (this case final nonNull?) { - return nonNull.isBlank; - } - - return true; + /// Returns negation of [isNullOrEmpty]. + bool get isNotNullNorEmpty { + return !isNullOrEmpty; } } diff --git a/packages/netglade_utils/pubspec.yaml b/packages/netglade_utils/pubspec.yaml index d20a290..1c11589 100644 --- a/packages/netglade_utils/pubspec.yaml +++ b/packages/netglade_utils/pubspec.yaml @@ -1,5 +1,5 @@ name: netglade_utils -version: 1.0.1 +version: 1.1.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 new file mode 100644 index 0000000..c38748b --- /dev/null +++ b/packages/netglade_utils/test/src/extensions/string_extensions_test.dart @@ -0,0 +1,151 @@ +import 'package:characters/characters.dart'; +import 'package:netglade_utils/netglade_utils.dart'; +import 'package:test/test.dart'; + +void main() { + group('lastNCharacters', () { + test('with negative limit', () { + expect('abcdefgh'.lastNCharacters(-2), equals('')); + }); + + test('with zero limit', () { + expect('abcdefgh'.lastNCharacters(0), equals('')); + }); + + test('with positive limit', () { + expect('abcdefgh'.lastNCharacters(2), equals('gh')); + }); + + test('with positive limit, but over length', () { + expect('abcdefgh'.lastNCharacters(666), equals('abcdefgh')); + }); + }); + + group('isBlank', () { + group('on String', () { + test('empty message is isBlank', () { + expect(''.isBlank, isTrue); + }); + + test('message with whitespaces is isBlank', () { + expect(' '.isBlank, isTrue); + }); + + test('message with non-whitespace characters is not isBlank', () { + expect('xxx'.isBlank, isFalse); + }); + }); + + group('on String?', () { + test('empty message is isBlank', () { + expect(''.isBlank, isTrue); + }); + + test('null message is not isBlank', () { + expect(null.isBlank, isFalse); + }); + + test('message with whitespaces is isBlank', () { + expect(' '.isBlank, isTrue); + }); + + test('message with non-whitespace characters is not isBlank', () { + expect('xxx'.isBlank, isFalse); + }); + }); + }); + + group('isNotBlank', () { + group('on String', () { + test('empty message is not isNotBlank', () { + expect(''.isNotBlank, isFalse); + }); + + test('message with whitespaces is not isNotBlank', () { + expect(' '.isNotBlank, isFalse); + }); + + test('message with non-whitespace characters is isNotBlank', () { + expect('xxx'.isNotBlank, isTrue); + }); + }); + + group('on String?', () { + test('empty message is not isNotBlank', () { + expect(''.isNotBlank, isFalse); + }); + + test('message is null is isNotBlank', () { + expect(null.isNotBlank, isTrue); + }); + + test('message has whitespaces is not isNotBlank', () { + expect(' '.isNotBlank, isFalse); + }); + + test('message has non-whitespace characters is isNotBlank', () { + expect('xxx'.isNotBlank, isTrue); + }); + }); + }); + + group('isNullOrBlank', () { + group('on String?', () { + test('empty message is isNullOrBlank', () { + expect(''.isNullOrBlank, isTrue); + }); + + test('null message is isNullOrBlank', () { + expect(null.isNullOrBlank, isTrue); + }); + + test('message with whitespaces is isNullOrBlank', () { + expect(' '.isNullOrBlank, isTrue); + }); + + test('message with non-whitespace characters is not isNullOrBlank', () { + expect('xxx'.isNullOrBlank, isFalse); + }); + }); + }); + + group('isNullOrEmpty', () { + group('on String?', () { + test('empty message is isNullOrEmpty', () { + expect(''.isNullOrEmpty, isTrue); + }); + + test('null message is isNullOrEmpty', () { + expect(null.isNullOrEmpty, isTrue); + }); + + test('message with whitespaces is not isNullOrEmpty', () { + expect(' '.isNullOrEmpty, isFalse); + }); + + test('message with non-whitespace characters is not isNullOrEmpty', () { + expect('xxx'.isNullOrEmpty, isFalse); + }); + }); + }); + + group('isNotNullNorEmpty', () { + group('on String?', () { + test('empty message is not isNotNullNorEmpty', () { + expect(''.isNotNullNorEmpty, isFalse); + }); + + test('null message is not isNotNullNorEmpty', () { + expect(null.isNotNullNorEmpty, isFalse); + }); + + test('message with whitespaces is isNotNullNorEmpty', () { + expect(' '.isNotNullNorEmpty, isTrue); + }); + + test('message with non-whitespace characters is isNotNullNorEmpty', () { + expect('xxx'.isNotNullNorEmpty, isTrue); + }); + }); + }); +} From b3a8ad80a4ad32fe8014859e78a3161f3934509d Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Mon, 5 Feb 2024 18:41:15 +0100 Subject: [PATCH 2/3] Fix format --- .../lib/src/extensions/string_extensions.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/netglade_utils/lib/src/extensions/string_extensions.dart b/packages/netglade_utils/lib/src/extensions/string_extensions.dart index 76af29c..77adf0e 100644 --- a/packages/netglade_utils/lib/src/extensions/string_extensions.dart +++ b/packages/netglade_utils/lib/src/extensions/string_extensions.dart @@ -10,11 +10,13 @@ extension StringExtensions on String { /// - '\t' /// /// When you only need to check exactly empty, use `isEmpty`. - /// + /// /// * String is blank when it contains only whitespaces. bool get isBlank => trim().isEmpty; /// Returns negation of [isBlank]. + /// + /// * String is blank when it contains only whitespaces. bool get isNotBlank => !isBlank; /// Returns last N characters. @@ -27,18 +29,22 @@ extension StringExtensions on String { extension NullableStringExtensions on String? { /// Returns true when `this` is `String` which is `isBlank`. /// When the value is `null`, returns `false`. - /// - /// * String? is blank when it has a value and contains only whitespaces. + /// + /// * String? is blank when it has a value and contains only whitespaces. bool get isBlank { return this?.isBlank ?? false; } /// Returns true when `this` is either `null` or `isBlank`. + /// + /// * String? is blank when it has a value and contains only whitespaces. bool get isNullOrBlank { return this?.isBlank ?? true; } /// Returns negation of [isBlank]. + /// + /// * String? is blank when it has a value and contains only whitespaces. bool get isNotBlank { return !isBlank; } From d3980c09a919b75395cbd43010aba253da76dbfa Mon Sep 17 00:00:00 2001 From: Honza Bittner Date: Mon, 5 Feb 2024 18:43:31 +0100 Subject: [PATCH 3/3] Remove unused import --- .../test/src/extensions/string_extensions_test.dart | 1 - 1 file changed, 1 deletion(-) 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 c38748b..0898b03 100644 --- a/packages/netglade_utils/test/src/extensions/string_extensions_test.dart +++ b/packages/netglade_utils/test/src/extensions/string_extensions_test.dart @@ -1,4 +1,3 @@ -import 'package:characters/characters.dart'; import 'package:netglade_utils/netglade_utils.dart'; import 'package:test/test.dart';