Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 07f3a9c

Browse files
committed
Code Refactor & Updated plugins
2 parents 96fb2ab + 133cb47 commit 07f3a9c

File tree

10 files changed

+134
-26
lines changed

10 files changed

+134
-26
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# The .vscode folder contains launch configuration and tasks you configure in
1919
# VS Code which you may wish to be included in version control, so this line
2020
# is commented out by default.
21-
#.vscode/
21+
.vscode/
2222

2323
# Flutter/Dart/Pub related
2424
**/doc/api/

CHANGELOG.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
## [1.1.2]
2-
- Code Refactor & Updated plugins
3-
- Add String extensions for firstNChars, lastNChars, truncateMiddle.
4-
- Add Map extensions for getSDMap which returns Map<String, dynamic>.
5-
- Add Map extensions for getBoolWithDefaultValue which returns bool.
6-
- Add List<String> extensions for check containWithoutCase which returns bool.
2+
- Add String extensions for `firstNChars`, `lastNChars`, `truncateMiddle`.
3+
- Add Map extensions for `getSDMap` which returns `Map<String, dynamic>`.
4+
- Add Map extensions for `getBoolWithDefaultValue` which returns bool.
5+
- Add List\<String> extensions for check `containWithoutCase` which returns bool.
76

87
## [1.1.1]
98
- Updated plugins

lib/constants.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const String defaultString = '';
1616

1717
/// Default value for Map
1818
///
19-
// ignore: always_specify_types
2019
const Map defaultMap = {};
2120

21+
/// Default value for Map<String, dynamic>
22+
///
23+
const Map<String, dynamic> defaultSDMap = {};
24+
2225
/// Default value for List
2326
///
24-
// ignore: always_specify_types
2527
const List defaultList = [];

lib/extensions/context.dart

-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ extension ContextExtensions on BuildContext {
7777
name: screen.toString(),
7878
);
7979
if (transparent) {
80-
// ignore: always_specify_types
8180
return Navigator.of(this).pushReplacement(
8281
TransparentRoute(
8382
builder: (_) => screen,
@@ -86,15 +85,13 @@ extension ContextExtensions on BuildContext {
8685
);
8786
} else {
8887
if (isCupertino) {
89-
// ignore: always_specify_types
9088
return Navigator.of(this).pushReplacement(
9189
CupertinoPageRoute(
9290
builder: (_) => screen,
9391
settings: settings,
9492
),
9593
);
9694
}
97-
// ignore: always_specify_types
9895
return Navigator.of(this).pushReplacement(
9996
MaterialPageRoute(
10097
builder: (_) => screen,
@@ -149,7 +146,6 @@ extension ContextExtensions on BuildContext {
149146
/// Pops the top-most route off the navigator till the first route.
150147
///
151148
void popToFirst() {
152-
// ignore: always_specify_types
153149
Navigator.of(this).popUntil((predicate) {
154150
return predicate.isFirst;
155151
});

lib/extensions/list.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import '../src.dart';
55

66
/// extension methods for List
77
///
8-
// ignore: always_specify_types
98
extension ListExtensions on List {
109
///List to JSON using[json.encode]
1110
///
@@ -31,3 +30,10 @@ extension ListExtensions on List {
3130
return data;
3231
}
3332
}
33+
34+
extension ListStringExtension on List<String> {
35+
/// check if list contains value without considering case
36+
bool containWithoutCase(String value) {
37+
return any((element) => element.toLowerCase() == value.toLowerCase());
38+
}
39+
}

lib/extensions/map.dart

+28-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import '../src.dart';
55

66
/// extension methods for Map
77
///
8-
// ignore: always_specify_types
98
extension MapExtensions on Map {
109
/// Reads a [key] value of [bool] type from [Map].
1110
///
@@ -57,7 +56,6 @@ extension MapExtensions on Map {
5756
///
5857
/// If value is NULL or not [List] type return empty List[defaultList]
5958
///
60-
// ignore: always_specify_types
6159
List getList(String key) {
6260
if (containsKey(key)) {
6361
if (this[key] is List) {
@@ -72,7 +70,6 @@ extension MapExtensions on Map {
7270
///
7371
/// If value is NULL or not [List] type return default value [defaultString]
7472
///
75-
// ignore: always_specify_types
7673
Map getMap(String key) {
7774
if (containsKey(key)) {
7875
if (this[key] is Map) {
@@ -83,6 +80,20 @@ extension MapExtensions on Map {
8380
return defaultMap;
8481
}
8582

83+
/// Reads a [key] value of [Map<String, dynamic>] type from [Map].
84+
///
85+
/// If value is NULL or not [Map<String, dynamic>] type return default value [defaultSDMap]
86+
///
87+
Map<String, dynamic> getSDMap(String key) {
88+
if (containsKey(key)) {
89+
if (this[key] is Map) {
90+
return this[key] ?? defaultSDMap;
91+
}
92+
}
93+
errorLogsNS("Map.getMap[$key] has incorrect data : ${this[key]}");
94+
return defaultSDMap;
95+
}
96+
8697
///Add value to map if value is not null
8798
///
8899
T add<T>({required String? key, required T? value}) =>
@@ -115,13 +126,22 @@ extension MapExtensions on Map {
115126

116127
/// Returns a new map with null keys or values removed
117128
///
118-
// ignore: always_specify_types
119-
Map<String, dynamic> get nullProtected => {...this}
120-
// ignore: always_specify_types
121-
..removeWhere((String key, value) => value == null);
129+
Map<String, dynamic> get nullProtected =>
130+
{...this}..removeWhere((String key, value) => value == null);
131+
132+
/// Reads a [key] value of [bool] type from [Map].
133+
/// If value is NULL or not [bool] type return default value [defaultBool]
134+
///
135+
bool getBoolWithDefaultValue(String key, {bool defaultValue = defaultBool}) {
136+
if (containsKey(key)) {
137+
if (this[key] is bool) {
138+
return this[key] ?? defaultValue;
139+
}
140+
}
141+
return defaultValue;
142+
}
122143
}
123144

124-
// ignore: always_specify_types, type_annotate_public_apis
125145
dynamic toEncodable(object) {
126146
if (object is String ||
127147
object is num ||

lib/extensions/string.dart

+30-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import '../src.dart';
99
extension StringExtensions on String {
1010
///JSON String to Map using[json.decode]
1111
///
12-
// ignore: always_specify_types
1312
Map toMap() {
1413
try {
1514
if (isEmpty) return defaultMap;
@@ -22,7 +21,6 @@ extension StringExtensions on String {
2221

2322
///JSON String to List using[json.decode]
2423
///
25-
// ignore: always_specify_types
2624
List toList() {
2725
try {
2826
return json.decode(this);
@@ -120,6 +118,36 @@ extension StringExtensions on String {
120118
///Add prefix if not empty else return empty string
121119
///
122120
String get showDashIfEmpty => isEmpty ? '-' : this;
121+
122+
/// this will give last n characters of string
123+
String lastNChars({int n = 1}) {
124+
if (n > length || n < 1) return this;
125+
126+
return substring(length - n);
127+
}
128+
129+
///this will give first n characters of string
130+
String firstNChars({int n = 1}) {
131+
if (n > length || n < 1) return this;
132+
return substring(0, n);
133+
}
134+
135+
/// Truncates a long `String` in the middle while retaining the beginning and the end.
136+
///
137+
/// Example :
138+
/// 'Hello World'.truncateMiddle(3) // 'Hel...rld'
139+
///
140+
String truncateMiddle({int maxChars = 3, String truncateKey = '...'}) {
141+
if (isEmptyOrNull || maxChars < 1) {
142+
return this;
143+
}
144+
145+
if ((maxChars * 2) >= length) {
146+
return this;
147+
}
148+
149+
return '${firstNChars(n: maxChars)}$truncateKey${lastNChars(n: maxChars)}';
150+
}
123151
}
124152

125153
extension StringNullExtensions on String? {

test/extensions/list_test.dart

+6
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@ void main() {
88
expect([].toJson(), '[]');
99
expect(['123', '1234'].toJson(), '["123","1234"]');
1010
});
11+
12+
test('containWithoutCase', () {
13+
List<String> list = ['Test', 'User'];
14+
expect(list.containWithoutCase('test'), true);
15+
expect(list.containWithoutCase('User1'), false);
16+
});
1117
});
1218
}

test/extensions/map_test.dart

+26-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ void main() {
3434
expect(data.getDouble('key'), 0);
3535
});
3636
test('getString', () {
37-
// ignore: omit_local_variable_types
3837
Map data = {'id': null};
3938
expect(data.getString('key'), '');
4039
expect(data.getString('id'), '');
@@ -91,8 +90,7 @@ void main() {
9190
"phone": "+1 (985) 469-2418",
9291
"address": "181 Tilden Avenue, Hamilton, Idaho, 2062",
9392
"about":
94-
// ignore: lines_longer_than_80_chars
95-
"Fugiat consectetur exercitation ex eiusmod officia ex exercitation et est enim cillum consectetur deserunt fugiat. Ex aliqua cupidatat consectetur reprehenderit. Voluptate nisi ipsum magna aliqua deserunt consectetur dolor occaecat fugiat labore. Sit Lorem elit duis deserunt ipsum adipisicing amet. Ullamco esse commodo et laborum minim. Sunt et exercitation veniam magna aute.",
93+
"""Fugiat consectetur exercitation ex eiusmod officia ex exercitation et est enim cillum consectetur deserunt fugiat. Ex aliqua cupidatat consectetur reprehenderit. Voluptate nisi ipsum magna aliqua deserunt consectetur dolor occaecat fugiat labore. Sit Lorem elit duis deserunt ipsum adipisicing amet. Ullamco esse commodo et laborum minim. Sunt et exercitation veniam magna aute.""",
9694
"registered": "Thursday, November 2, 2017 10:07 PM",
9795
"latitude": "-36.620306",
9896
"longitude": "128.527698",
@@ -120,5 +118,30 @@ void main() {
120118
};
121119
expect(data.nullProtected, {"key": 1});
122120
});
121+
122+
test("getSDMap", (() {
123+
Map data = {
124+
'key': 1,
125+
'key2': null,
126+
'key3': {
127+
'key4': 1,
128+
'key5': null,
129+
}
130+
};
131+
expect(data.getSDMap('key'), {});
132+
expect(data.getSDMap('key2'), isA<Map<String, dynamic>>());
133+
expect(data.getSDMap('key3'), {"key4": 1, "key5": null});
134+
}));
135+
136+
test("getBoolWithDefaultValue", (() {
137+
Map data = {
138+
'key': false,
139+
'key2': null,
140+
'key3': true,
141+
};
142+
expect(data.getBoolWithDefaultValue('key', defaultValue: true), false);
143+
expect(data.getBoolWithDefaultValue('key2', defaultValue: true), true);
144+
expect(data.getBoolWithDefaultValue('key3', defaultValue: true), true);
145+
}));
123146
});
124147
}

test/extensions/string_test.dart

+28
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,33 @@ void main() {
5959
String value = DateTime(2020, 08, 01).toIso8601String();
6060
expect(value.toDateTime(), DateTime(2020, 08, 01));
6161
});
62+
63+
test('truncate Middle', () {
64+
expect(''.truncateMiddle(), '');
65+
expect('12'.truncateMiddle(), '12');
66+
expect('123456'.truncateMiddle(), '123456');
67+
expect('1234567'.truncateMiddle(), '123...567');
68+
expect('1234567890'.truncateMiddle(maxChars: 2), '12...90');
69+
expect(
70+
'1234567890'.truncateMiddle(maxChars: 2, truncateKey: "-"),
71+
'12-90',
72+
);
73+
});
74+
75+
test('first N char', () {
76+
expect(''.firstNChars(), '');
77+
expect('12'.firstNChars(n: 5), '12');
78+
expect('123456'.firstNChars(n: 2), '12');
79+
expect('1234567'.firstNChars(n: 20), '1234567');
80+
expect('1234567'.firstNChars(n: -20), '1234567');
81+
});
82+
83+
test('last N char', () {
84+
expect(''.lastNChars(), '');
85+
expect('12'.lastNChars(n: 5), '12');
86+
expect('123456'.lastNChars(n: 2), '56');
87+
expect('1234567'.lastNChars(n: 20), '1234567');
88+
expect('1234567'.lastNChars(n: -20), '1234567');
89+
});
6290
});
6391
}

0 commit comments

Comments
 (0)