Skip to content

Commit c973323

Browse files
authored
Merge pull request #22 from netglade/feat/if-empty-and-blank
Add if empty and blank, add tests, remove callbacks
2 parents 263acff + e4ebbf0 commit c973323

File tree

8 files changed

+193
-42
lines changed

8 files changed

+193
-42
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.2.0
2+
- Add `ifEmpty` and `ifBlank` to string extensions.
3+
- Remove typedefs. (private)
4+
15
## 1.1.0
26
- Add `isNotNullNorEmpty` to `String?` extension.
37
- Fix implementation and doc comments in `string_extensions`.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import 'dart:async';
22

33
import 'package:clock/clock.dart';
4-
import 'package:netglade_utils/src/typedefs/typedefs.dart';
4+
5+
typedef OnTakingTooLongCallback = void Function();
56

67
extension FutureExtensions<T> on Future<T> {
78
// ignore: comment_references, see https://github.com/dart-lang/linter/issues/2079
89
/// If [this] future taking longer than [duration] to execute - [callback] is called.
9-
Future<T> onTakingTooLong(Duration duration, VoidCallback callback) async {
10+
Future<T> onTakingTooLong(Duration duration, OnTakingTooLongCallback callback) async {
1011
final timer = Timer(duration, callback);
1112

1213
try {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ extension StringExtensions on String {
2424
String lastNCharacters(int limit) {
2525
return characters.getRange(max(0, length - limit)).toString();
2626
}
27+
28+
String? ifEmpty([String? defaultValue]) {
29+
return isEmpty ? defaultValue : this;
30+
}
31+
32+
String? ifBlank([String? defaultValue]) {
33+
return isBlank ? defaultValue : this;
34+
}
2735
}
2836

2937
extension NullableStringExtensions on String? {
@@ -42,6 +50,11 @@ extension NullableStringExtensions on String? {
4250
return this?.isBlank ?? true;
4351
}
4452

53+
/// Returns negation of [isNullOrBlank].
54+
bool get isNotNullNorBlank {
55+
return !isNullOrBlank;
56+
}
57+
4558
/// Returns negation of [isBlank].
4659
///
4760
/// * String? is blank when it has a value and contains only whitespaces.
@@ -58,4 +71,12 @@ extension NullableStringExtensions on String? {
5871
bool get isNotNullNorEmpty {
5972
return !isNullOrEmpty;
6073
}
74+
75+
String? ifEmpty([String? defaultValue]) {
76+
return (this?.isEmpty ?? false) ? defaultValue : this;
77+
}
78+
79+
String? ifBlank([String? defaultValue]) {
80+
return (this?.isBlank ?? false) ? defaultValue : this;
81+
}
6182
}

packages/netglade_utils/lib/src/testing/mock_extensions.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:async';
44

55
import 'package:mocktail/mocktail.dart';
66
import 'package:netglade_utils/src/result/result.dart';
7-
import 'package:netglade_utils/src/typedefs/typedefs.dart';
87

98
extension FutureVoidAnswer on When<Future<void>> {
109
void thenAnswerWithVoid() => thenAnswer((_) async => <void>{});
@@ -46,14 +45,14 @@ extension StreamSubscriptionAnswer<T> on When<StreamSubscription<T>> {
4645
void thenAnswerWithProvidedCallback() => thenAnswer((i) {
4746
final callback = i.positionalArguments.singleOrNull;
4847

49-
// ignore: no-empty-block, it needs to be empty
50-
return Stream.fromIterable(<T>[]).listen(callback != null ? callback as Setter<T> : (_) {});
48+
// ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API
49+
return Stream.fromIterable(<T>[]).listen(callback != null ? callback as void Function(T value) : (_) {});
5150
});
5251

5352
void thenAnswerWithCustomStream(Stream<T> stream) => thenAnswer((i) {
5453
final callback = i.positionalArguments.singleOrNull;
5554

56-
// ignore: no-empty-block, it needs to be empty
57-
return stream.listen(callback != null ? callback as Setter<T> : (_) {});
55+
// ignore: no-empty-block, it needs to be empty, prefer-typedefs-for-callbacks, private API
56+
return stream.listen(callback != null ? callback as void Function(T value) : (_) {});
5857
});
5958
}

packages/netglade_utils/lib/src/typedefs/callbacks.dart

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/netglade_utils/lib/src/typedefs/typedefs.dart

Lines changed: 0 additions & 1 deletion
This file was deleted.

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.1.0
2+
version: 1.2.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

packages/netglade_utils/test/src/extensions/string_extensions_test.dart

Lines changed: 160 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: prefer_const_declarations, unnecessary_nullable_for_final_variable_declarations, omit_local_variable_types
2+
13
import 'package:netglade_utils/netglade_utils.dart';
24
import 'package:test/test.dart';
35

@@ -23,127 +25,259 @@ void main() {
2325
group('isBlank', () {
2426
group('on String', () {
2527
test('empty message is isBlank', () {
26-
expect(''.isBlank, isTrue);
28+
final String value = '';
29+
expect(value.isBlank, isTrue);
2730
});
2831

2932
test('message with whitespaces is isBlank', () {
30-
expect(' '.isBlank, isTrue);
33+
final String value = ' ';
34+
expect(value.isBlank, isTrue);
3135
});
3236

3337
test('message with non-whitespace characters is not isBlank', () {
34-
expect('xxx'.isBlank, isFalse);
38+
final String value = 'xxx';
39+
expect(value.isBlank, isFalse);
3540
});
3641
});
3742

3843
group('on String?', () {
3944
test('empty message is isBlank', () {
40-
expect(''.isBlank, isTrue);
45+
final String? value = '';
46+
expect(value.isBlank, isTrue);
4147
});
4248

4349
test('null message is not isBlank', () {
44-
expect(null.isBlank, isFalse);
50+
final String? value = null;
51+
expect(value.isBlank, isFalse);
4552
});
4653

4754
test('message with whitespaces is isBlank', () {
48-
expect(' '.isBlank, isTrue);
55+
final String? value = ' ';
56+
expect(value.isBlank, isTrue);
4957
});
5058

5159
test('message with non-whitespace characters is not isBlank', () {
52-
expect('xxx'.isBlank, isFalse);
60+
final String? value = 'xxx';
61+
expect(value.isBlank, isFalse);
5362
});
5463
});
5564
});
5665

5766
group('isNotBlank', () {
5867
group('on String', () {
5968
test('empty message is not isNotBlank', () {
60-
expect(''.isNotBlank, isFalse);
69+
final String value = '';
70+
expect(value.isNotBlank, isFalse);
6171
});
6272

6373
test('message with whitespaces is not isNotBlank', () {
64-
expect(' '.isNotBlank, isFalse);
74+
final String value = ' ';
75+
expect(value.isNotBlank, isFalse);
6576
});
6677

6778
test('message with non-whitespace characters is isNotBlank', () {
68-
expect('xxx'.isNotBlank, isTrue);
79+
final String value = 'xxx';
80+
expect(value.isNotBlank, isTrue);
6981
});
7082
});
7183

7284
group('on String?', () {
7385
test('empty message is not isNotBlank', () {
74-
expect(''.isNotBlank, isFalse);
86+
final String? value = '';
87+
expect(value.isNotBlank, isFalse);
7588
});
7689

7790
test('message is null is isNotBlank', () {
78-
expect(null.isNotBlank, isTrue);
91+
final String? value = null;
92+
expect(value.isNotBlank, isTrue);
7993
});
8094

8195
test('message has whitespaces is not isNotBlank', () {
82-
expect(' '.isNotBlank, isFalse);
96+
final String? value = ' ';
97+
expect(value.isNotBlank, isFalse);
8398
});
8499

85100
test('message has non-whitespace characters is isNotBlank', () {
86-
expect('xxx'.isNotBlank, isTrue);
101+
final String? value = 'xxx';
102+
expect(value.isNotBlank, isTrue);
87103
});
88104
});
89105
});
90106

91107
group('isNullOrBlank', () {
92108
group('on String?', () {
93109
test('empty message is isNullOrBlank', () {
94-
expect(''.isNullOrBlank, isTrue);
110+
final String? value = '';
111+
expect(value.isNullOrBlank, isTrue);
95112
});
96113

97114
test('null message is isNullOrBlank', () {
98-
expect(null.isNullOrBlank, isTrue);
115+
final String? value = null;
116+
expect(value.isNullOrBlank, isTrue);
99117
});
100118

101119
test('message with whitespaces is isNullOrBlank', () {
102-
expect(' '.isNullOrBlank, isTrue);
120+
final String? value = ' ';
121+
expect(value.isNullOrBlank, isTrue);
103122
});
104123

105124
test('message with non-whitespace characters is not isNullOrBlank', () {
106-
expect('xxx'.isNullOrBlank, isFalse);
125+
final String? value = 'xxx';
126+
expect(value.isNullOrBlank, isFalse);
107127
});
108128
});
109129
});
110130

111131
group('isNullOrEmpty', () {
112132
group('on String?', () {
113133
test('empty message is isNullOrEmpty', () {
114-
expect(''.isNullOrEmpty, isTrue);
134+
final String? value = '';
135+
expect(value.isNullOrEmpty, isTrue);
115136
});
116137

117138
test('null message is isNullOrEmpty', () {
118-
expect(null.isNullOrEmpty, isTrue);
139+
final String? value = null;
140+
expect(value.isNullOrEmpty, isTrue);
119141
});
120142

121143
test('message with whitespaces is not isNullOrEmpty', () {
122-
expect(' '.isNullOrEmpty, isFalse);
144+
final String? value = ' ';
145+
expect(value.isNullOrEmpty, isFalse);
123146
});
124147

125148
test('message with non-whitespace characters is not isNullOrEmpty', () {
126-
expect('xxx'.isNullOrEmpty, isFalse);
149+
final String? value = 'xxx';
150+
expect(value.isNullOrEmpty, isFalse);
127151
});
128152
});
129153
});
130154

131155
group('isNotNullNorEmpty', () {
132156
group('on String?', () {
133157
test('empty message is not isNotNullNorEmpty', () {
134-
expect(''.isNotNullNorEmpty, isFalse);
158+
final String? value = '';
159+
expect(value.isNotNullNorEmpty, isFalse);
135160
});
136161

137162
test('null message is not isNotNullNorEmpty', () {
138-
expect(null.isNotNullNorEmpty, isFalse);
163+
final String? value = null;
164+
expect(value.isNotNullNorEmpty, isFalse);
139165
});
140166

141167
test('message with whitespaces is isNotNullNorEmpty', () {
142-
expect(' '.isNotNullNorEmpty, isTrue);
168+
final String? value = ' ';
169+
expect(value.isNotNullNorEmpty, isTrue);
143170
});
144171

145172
test('message with non-whitespace characters is isNotNullNorEmpty', () {
146-
expect('xxx'.isNotNullNorEmpty, isTrue);
173+
final String? value = 'xxx';
174+
expect(value.isNotNullNorEmpty, isTrue);
175+
});
176+
});
177+
});
178+
179+
group('isNotNullNorBlank', () {
180+
group('on String?', () {
181+
test('empty message is not isNotNullNorBlank', () {
182+
final String? value = '';
183+
expect(value.isNotNullNorBlank, isFalse);
184+
});
185+
186+
test('null message is not isNotNullNorBlank', () {
187+
final String? value = null;
188+
expect(value.isNotNullNorBlank, isFalse);
189+
});
190+
191+
test('message with whitespaces is not isNotNullNorBlank', () {
192+
final String? value = ' ';
193+
expect(value.isNotNullNorBlank, isFalse);
194+
});
195+
196+
test('message with non-whitespace characters is isNotNullNorBlank', () {
197+
final String? value = 'xxx';
198+
expect(value.isNotNullNorBlank, isTrue);
199+
});
200+
});
201+
});
202+
203+
group('ifEmpty', () {
204+
group('on String', () {
205+
test('empty message', () {
206+
final String value = '';
207+
expect(value.ifEmpty('aaa'), equals('aaa'));
208+
});
209+
210+
test('message with whitespaces', () {
211+
final String value = ' ';
212+
expect(value.ifEmpty('aaa'), equals(value));
213+
});
214+
215+
test('message with non-whitespace characters', () {
216+
final String value = 'xxx';
217+
expect(value.ifEmpty('aaa'), equals(value));
218+
});
219+
});
220+
221+
group('on String?', () {
222+
test('empty message', () {
223+
final String? value = '';
224+
expect(value.ifEmpty('aaa'), equals('aaa'));
225+
});
226+
227+
test('null message', () {
228+
final String? value = null;
229+
expect(value.ifEmpty('aaa'), equals(value));
230+
});
231+
232+
test('message with whitespaces', () {
233+
final String? value = ' ';
234+
expect(value.ifEmpty('aaa'), equals(value));
235+
});
236+
237+
test('message with non-whitespace characters', () {
238+
final String? value = 'xxx';
239+
expect(value.ifEmpty('aaa'), equals(value));
240+
});
241+
});
242+
});
243+
244+
group('ifBlank', () {
245+
group('on String', () {
246+
test('empty message', () {
247+
final String value = '';
248+
expect(value.ifBlank('aaa'), equals('aaa'));
249+
});
250+
251+
test('message with whitespaces', () {
252+
final String value = ' ';
253+
expect(value.ifBlank('aaa'), equals('aaa'));
254+
});
255+
256+
test('message with non-whitespace characters', () {
257+
final String value = 'xxx';
258+
expect(value.ifBlank('aaa'), equals(value));
259+
});
260+
});
261+
262+
group('on String?', () {
263+
test('empty message', () {
264+
final String? value = '';
265+
expect(value.ifBlank('aaa'), equals('aaa'));
266+
});
267+
268+
test('null message', () {
269+
final String? value = null;
270+
expect(value.ifBlank('aaa'), equals(value));
271+
});
272+
273+
test('message with whitespaces', () {
274+
final String? value = ' ';
275+
expect(value.ifBlank('aaa'), equals('aaa'));
276+
});
277+
278+
test('message with non-whitespace characters', () {
279+
final String? value = 'xxx';
280+
expect(value.ifBlank('aaa'), equals(value));
147281
});
148282
});
149283
});

0 commit comments

Comments
 (0)