Skip to content

Commit 4ce4adc

Browse files
committed
Fix string_extensions
1 parent d6ce16b commit 4ce4adc

File tree

4 files changed

+172
-28
lines changed

4 files changed

+172
-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: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ 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].
1618
bool get isNotBlank => !isBlank;
1719

18-
/// The [Characters] of this string.
19-
Characters get characters => Characters(this);
20-
2120
/// Returns last N characters.
2221
/// When the string does not have N characters, returns rest.
2322
String lastNCharacters(int limit) {
@@ -28,39 +27,29 @@ extension StringExtensions on String {
2827
extension NullableStringExtensions on String? {
2928
/// Returns true when `this` is `String` which is `isBlank`.
3029
/// When the value is `null`, returns `false`.
30+
///
31+
/// * String? is blank when it has a value and contains only whitespaces.
3132
bool get isBlank {
32-
if (this case final nonNull?) {
33-
return nonNull.isBlank;
34-
}
33+
return this?.isBlank ?? false;
34+
}
3535

36-
return false;
36+
/// Returns true when `this` is either `null` or `isBlank`.
37+
bool get isNullOrBlank {
38+
return this?.isBlank ?? true;
3739
}
3840

39-
/// Returns true when `this` is `String` which is `isBlank`.
40-
/// When the value is `null`, returns `false`.
41+
/// Returns negation of [isBlank].
4142
bool get isNotBlank {
42-
if (this case final nonNull?) {
43-
return nonNull.isNotBlank;
44-
}
45-
46-
return false;
43+
return !isBlank;
4744
}
4845

4946
/// Returns true when `this` is either `null` or `isBlank`.
5047
bool get isNullOrEmpty {
51-
if (this case final nonNull?) {
52-
return nonNull.isEmpty;
53-
}
54-
55-
return true;
48+
return this?.isEmpty ?? true;
5649
}
5750

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;
51+
/// Returns negation of [isNullOrEmpty].
52+
bool get isNotNullNorEmpty {
53+
return !isNullOrEmpty;
6554
}
6655
}

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

0 commit comments

Comments
 (0)