Skip to content

Commit 263acff

Browse files
authored
Merge pull request #21 from netglade/fix/string_extensions
Fix string_extensions
2 parents d6ce16b + d3980c0 commit 263acff

File tree

4 files changed

+177
-28
lines changed

4 files changed

+177
-28
lines changed

packages/netglade_utils/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.0
2+
- Add `isNotNullNorEmpty` to `String?` extension.
3+
- Fix implementation and doc comments in `string_extensions`.
4+
15
## 1.0.1
26
- Fix readme.
37

packages/netglade_utils/lib/src/extensions/string_extensions.dart

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ extension StringExtensions on String {
1010
/// - '\t'
1111
///
1212
/// When you only need to check exactly empty, use `isEmpty`.
13+
///
14+
/// * String is blank when it contains only whitespaces.
1315
bool get isBlank => trim().isEmpty;
1416

15-
/// Returns true when value is not empty and contains some characters except whitespaces.
17+
/// Returns negation of [isBlank].
18+
///
19+
/// * String is blank when it contains only whitespaces.
1620
bool get isNotBlank => !isBlank;
1721

18-
/// The [Characters] of this string.
19-
Characters get characters => Characters(this);
20-
2122
/// Returns last N characters.
2223
/// When the string does not have N characters, returns rest.
2324
String lastNCharacters(int limit) {
@@ -28,39 +29,33 @@ extension StringExtensions on String {
2829
extension NullableStringExtensions on String? {
2930
/// Returns true when `this` is `String` which is `isBlank`.
3031
/// When the value is `null`, returns `false`.
32+
///
33+
/// * String? is blank when it has a value and contains only whitespaces.
3134
bool get isBlank {
32-
if (this case final nonNull?) {
33-
return nonNull.isBlank;
34-
}
35+
return this?.isBlank ?? false;
36+
}
3537

36-
return false;
38+
/// Returns true when `this` is either `null` or `isBlank`.
39+
///
40+
/// * String? is blank when it has a value and contains only whitespaces.
41+
bool get isNullOrBlank {
42+
return this?.isBlank ?? true;
3743
}
3844

39-
/// Returns true when `this` is `String` which is `isBlank`.
40-
/// When the value is `null`, returns `false`.
45+
/// Returns negation of [isBlank].
46+
///
47+
/// * String? is blank when it has a value and contains only whitespaces.
4148
bool get isNotBlank {
42-
if (this case final nonNull?) {
43-
return nonNull.isNotBlank;
44-
}
45-
46-
return false;
49+
return !isBlank;
4750
}
4851

4952
/// Returns true when `this` is either `null` or `isBlank`.
5053
bool get isNullOrEmpty {
51-
if (this case final nonNull?) {
52-
return nonNull.isEmpty;
53-
}
54-
55-
return true;
54+
return this?.isEmpty ?? true;
5655
}
5756

58-
/// Returns true when `this` is either `null` or `isBlank`.
59-
bool get isNullOrBlank {
60-
if (this case final nonNull?) {
61-
return nonNull.isBlank;
62-
}
63-
64-
return true;
57+
/// Returns negation of [isNullOrEmpty].
58+
bool get isNotNullNorEmpty {
59+
return !isNullOrEmpty;
6560
}
6661
}

packages/netglade_utils/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: netglade_utils
2-
version: 1.0.1
2+
version: 1.1.0
33
description: Dart utils used internally at netglade.
44
repository: https://github.com/netglade/flutter_core/tree/main/packages/netglade_utils
55
issue_tracker: https://github.com/netglade/flutter_core/issues
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import 'package:netglade_utils/netglade_utils.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group('lastNCharacters', () {
6+
test('with negative limit', () {
7+
expect('abcdefgh'.lastNCharacters(-2), equals(''));
8+
});
9+
10+
test('with zero limit', () {
11+
expect('abcdefgh'.lastNCharacters(0), equals(''));
12+
});
13+
14+
test('with positive limit', () {
15+
expect('abcdefgh'.lastNCharacters(2), equals('gh'));
16+
});
17+
18+
test('with positive limit, but over length', () {
19+
expect('abcdefgh'.lastNCharacters(666), equals('abcdefgh'));
20+
});
21+
});
22+
23+
group('isBlank', () {
24+
group('on String', () {
25+
test('empty message is isBlank', () {
26+
expect(''.isBlank, isTrue);
27+
});
28+
29+
test('message with whitespaces is isBlank', () {
30+
expect(' '.isBlank, isTrue);
31+
});
32+
33+
test('message with non-whitespace characters is not isBlank', () {
34+
expect('xxx'.isBlank, isFalse);
35+
});
36+
});
37+
38+
group('on String?', () {
39+
test('empty message is isBlank', () {
40+
expect(''.isBlank, isTrue);
41+
});
42+
43+
test('null message is not isBlank', () {
44+
expect(null.isBlank, isFalse);
45+
});
46+
47+
test('message with whitespaces is isBlank', () {
48+
expect(' '.isBlank, isTrue);
49+
});
50+
51+
test('message with non-whitespace characters is not isBlank', () {
52+
expect('xxx'.isBlank, isFalse);
53+
});
54+
});
55+
});
56+
57+
group('isNotBlank', () {
58+
group('on String', () {
59+
test('empty message is not isNotBlank', () {
60+
expect(''.isNotBlank, isFalse);
61+
});
62+
63+
test('message with whitespaces is not isNotBlank', () {
64+
expect(' '.isNotBlank, isFalse);
65+
});
66+
67+
test('message with non-whitespace characters is isNotBlank', () {
68+
expect('xxx'.isNotBlank, isTrue);
69+
});
70+
});
71+
72+
group('on String?', () {
73+
test('empty message is not isNotBlank', () {
74+
expect(''.isNotBlank, isFalse);
75+
});
76+
77+
test('message is null is isNotBlank', () {
78+
expect(null.isNotBlank, isTrue);
79+
});
80+
81+
test('message has whitespaces is not isNotBlank', () {
82+
expect(' '.isNotBlank, isFalse);
83+
});
84+
85+
test('message has non-whitespace characters is isNotBlank', () {
86+
expect('xxx'.isNotBlank, isTrue);
87+
});
88+
});
89+
});
90+
91+
group('isNullOrBlank', () {
92+
group('on String?', () {
93+
test('empty message is isNullOrBlank', () {
94+
expect(''.isNullOrBlank, isTrue);
95+
});
96+
97+
test('null message is isNullOrBlank', () {
98+
expect(null.isNullOrBlank, isTrue);
99+
});
100+
101+
test('message with whitespaces is isNullOrBlank', () {
102+
expect(' '.isNullOrBlank, isTrue);
103+
});
104+
105+
test('message with non-whitespace characters is not isNullOrBlank', () {
106+
expect('xxx'.isNullOrBlank, isFalse);
107+
});
108+
});
109+
});
110+
111+
group('isNullOrEmpty', () {
112+
group('on String?', () {
113+
test('empty message is isNullOrEmpty', () {
114+
expect(''.isNullOrEmpty, isTrue);
115+
});
116+
117+
test('null message is isNullOrEmpty', () {
118+
expect(null.isNullOrEmpty, isTrue);
119+
});
120+
121+
test('message with whitespaces is not isNullOrEmpty', () {
122+
expect(' '.isNullOrEmpty, isFalse);
123+
});
124+
125+
test('message with non-whitespace characters is not isNullOrEmpty', () {
126+
expect('xxx'.isNullOrEmpty, isFalse);
127+
});
128+
});
129+
});
130+
131+
group('isNotNullNorEmpty', () {
132+
group('on String?', () {
133+
test('empty message is not isNotNullNorEmpty', () {
134+
expect(''.isNotNullNorEmpty, isFalse);
135+
});
136+
137+
test('null message is not isNotNullNorEmpty', () {
138+
expect(null.isNotNullNorEmpty, isFalse);
139+
});
140+
141+
test('message with whitespaces is isNotNullNorEmpty', () {
142+
expect(' '.isNotNullNorEmpty, isTrue);
143+
});
144+
145+
test('message with non-whitespace characters is isNotNullNorEmpty', () {
146+
expect('xxx'.isNotNullNorEmpty, isTrue);
147+
});
148+
});
149+
});
150+
}

0 commit comments

Comments
 (0)