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

Fix string_extensions #21

Merged
merged 3 commits into from
Feb 5, 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.1.0
- Add `isNotNullNorEmpty` to `String?` extension.
- Fix implementation and doc comments in `string_extensions`.

## 1.0.1
- Fix readme.

Expand Down
49 changes: 22 additions & 27 deletions packages/netglade_utils/lib/src/extensions/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ 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].
///
/// * String is blank when it contains only whitespaces.
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) {
Expand All @@ -28,39 +29,33 @@ 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`.
///
/// * String? is blank when it has a value and contains only whitespaces.
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].
///
/// * String? is blank when it has a value and contains only whitespaces.
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;
}
}
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.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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
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);
});
});
});
}
Loading