Skip to content

Commit f95743a

Browse files
author
travis
committed
style: format code and improve documentation clarity across codebase
1 parent 478353f commit f95743a

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.1.11
2+
3+
- Ran dart format on the codebase
4+
- Code formatting and documentation improvements across the codebase
5+
- Removed unnecessary TODOs and improved code comments
6+
- Enhanced code style consistency in method parameters and documentation
7+
18
## 0.1.10
29

310
- Removed unused import.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Add this to your package's `pubspec.yaml` file:
3535

3636
```yaml
3737
dependencies:
38-
json_son: ^0.1.9 # Or the latest version
38+
json_son: ^0.1.11 # Or the latest version
3939
```
4040
4141
Then, run `flutter pub get` or `dart pub get`.

lib/json_son.dart

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
/// A Dart utility package providing helper functions to flexibly parse JSON values
2-
/// that might have inconsistent data types (e.g., strings to numbers,
1+
/// A Dart utility package providing helper functions to flexibly parse JSON values
2+
/// that might have inconsistent data types (e.g., strings to numbers,
33
/// strings/numbers to booleans, or dates in various formats).
4-
///
5-
/// These functions are designed to be used with JSON deserialization, often in
4+
///
5+
/// These functions are designed to be used with JSON deserialization, often in
66
/// conjunction with code generation libraries like `json_serializable` (used by `freezed`),
77
/// by annotating DTO fields with `@JsonKey(fromJson: ...)`.
88
library;
99

1010
export 'src/json_son_base.dart';
1111

12-
// TODO: Export any libraries intended for clients of this package.
13-
14-
/// Parses a [dynamic] value into an [int]?.
12+
/// Parses a [dynamic] value into an [int]?.
1513
/// Handles `null`, `int`, `double` (truncates), and `String` representations.
1614
/// An empty string or a string that fails to parse will result in `null`.
1715
int? flexibleIntFromJson(dynamic value) {
@@ -60,7 +58,7 @@ bool? flexibleBoolFromJson(dynamic value) {
6058
}
6159

6260
/// Parses a [dynamic] value into a [String]?.
63-
/// Handles `null` and `String`. Converts other types (like `int`, `double`, `bool`)
61+
/// Handles `null` and `String`. Converts other types (like `int`, `double`, `bool`)
6462
/// to their string representation using `.toString()`.
6563
String? flexibleStringFromJson(dynamic value) {
6664
if (value == null) return null;
@@ -91,7 +89,7 @@ num? flexibleNumFromJson(dynamic value) {
9189
/// If the value is an `int`, assumes it's milliseconds since epoch (UTC).
9290
/// If the value is a `String`:
9391
/// - Tries direct parsing via `DateTime.tryParse` (for ISO 8601 and similar).
94-
/// - If direct parsing fails and the string is purely numeric,
92+
/// - If direct parsing fails and the string is purely numeric,
9593
/// treats it as milliseconds since epoch.
9694
/// An empty string or unparseable string format will result in `null`.
9795
DateTime? flexibleDateTimeFromJson(dynamic value) {
@@ -105,7 +103,7 @@ DateTime? flexibleDateTimeFromJson(dynamic value) {
105103
// Try standard ISO 8601 parsing first
106104
DateTime? dt = DateTime.tryParse(value);
107105
if (dt != null) return dt;
108-
106+
109107
// If it's a purely numeric string, try parsing as milliseconds since epoch
110108
final intValue = int.tryParse(value);
111109
if (intValue != null) {
@@ -125,6 +123,6 @@ Uri? flexibleUriFromJson(dynamic value) {
125123
return Uri.tryParse(value);
126124
}
127125
// If the value is already a Uri, though less common from raw JSON
128-
if (value is Uri) return value;
126+
if (value is Uri) return value;
129127
return null;
130128
}

lib/src/json_son_base.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// TODO: Put public facing types in this file.
2-
3-
/// Parses a [dynamic] value into an [int]?.
1+
/// Parses a [dynamic] value into an [int]?.
42
/// Handles `null`, `int`, `double` (truncates), and `String` representations.
53
/// An empty string or a string that fails to parse will result in `null`.
64
int? flexibleIntFromJson(dynamic value) {
@@ -49,7 +47,7 @@ bool? flexibleBoolFromJson(dynamic value) {
4947
}
5048

5149
/// Parses a [dynamic] value into a [String]?.
52-
/// Handles `null` and `String`. Converts other types (like `int`, `double`, `bool`)
50+
/// Handles `null` and `String`. Converts other types (like `int`, `double`, `bool`)
5351
/// to their string representation using `.toString()`.
5452
String? flexibleStringFromJson(dynamic value) {
5553
if (value == null) return null;
@@ -77,7 +75,7 @@ num? flexibleNumFromJson(dynamic value) {
7775
/// If the value is an `int`, assumes it's milliseconds since epoch (UTC).
7876
/// If the value is a `String`:
7977
/// - Tries direct parsing via `DateTime.tryParse` (for ISO 8601 and similar).
80-
/// - If direct parsing fails and the string is purely numeric,
78+
/// - If direct parsing fails and the string is purely numeric,
8179
/// treats it as milliseconds since epoch.
8280
/// An empty string or unparseable string format will result in `null`.
8381
DateTime? flexibleDateTimeFromJson(dynamic value) {
@@ -91,7 +89,7 @@ DateTime? flexibleDateTimeFromJson(dynamic value) {
9189
// Try standard ISO 8601 parsing first
9290
DateTime? dt = DateTime.tryParse(value);
9391
if (dt != null) return dt;
94-
92+
9593
// If it's a purely numeric string, try parsing as milliseconds since epoch
9694
final intValue = int.tryParse(value);
9795
if (intValue != null) {
@@ -111,7 +109,7 @@ Uri? flexibleUriFromJson(dynamic value) {
111109
return Uri.tryParse(value);
112110
}
113111
// If the value is already a Uri, though less common from raw JSON
114-
if (value is Uri) return value;
112+
if (value is Uri) return value;
115113
return null;
116114
}
117115

@@ -121,22 +119,25 @@ Uri? flexibleUriFromJson(dynamic value) {
121119

122120
/// Ensures result is always a List, even if API returns single item or null.
123121
/// Useful for APIs that inconsistently return arrays vs single items for a field.
124-
/// Returns `null` if the input `value` is null or if the `itemParser`
122+
/// Returns `null` if the input `value` is null or if the `itemParser`
125123
/// results in `null` for a single item input.
126124
/// Filters out `null` items from the list if the input is a list.
127-
///
125+
///
128126
/// Example:
129127
/// ```dart
130128
/// @JsonKey(fromJson: (v) => flexibleListFromJson(v, flexibleIntFromJson))
131129
/// List<int?>? numbers;
132130
/// ```
133131
List<T?>? flexibleListFromJson<T>(
134-
dynamic value,
132+
dynamic value,
135133
T? Function(dynamic) itemParser,
136134
) {
137135
if (value == null) return null;
138136
if (value is List) {
139-
return value.map((item) => itemParser(item)).where((parsedItem) => parsedItem != null).toList();
137+
return value
138+
.map((item) => itemParser(item))
139+
.where((parsedItem) => parsedItem != null)
140+
.toList();
140141
}
141142
// Single item? Wrap it in a list if the parsed item is not null.
142143
final T? parsedSingleItem = itemParser(value);
@@ -146,14 +147,14 @@ List<T?>? flexibleListFromJson<T>(
146147
/// For when APIs return empty arrays as null, or you want to guarantee a non-null list.
147148
/// Returns empty list instead of null if the input is null or if parsing results in nulls.
148149
/// Filters out `null` items from the list if the input is a list.
149-
///
150+
///
150151
/// Example:
151152
/// ```dart
152153
/// @JsonKey(fromJson: (v) => flexibleListNotNullFromJson(v, flexibleStringFromJson))
153154
/// List<String> tags; // Guarantees a List<String>, never null.
154155
/// ```
155156
List<T> flexibleListNotNullFromJson<T>(
156-
dynamic value,
157+
dynamic value,
157158
T? Function(dynamic) itemParser,
158159
) {
159160
if (value == null) return <T>[];
@@ -176,7 +177,7 @@ List<T> flexibleListNotNullFromJson<T>(
176177
/// Handles `null` input.
177178
/// If input is already a list, its elements are converted to strings.
178179
/// Trims whitespace from each part and filters out empty strings after splitting.
179-
///
180+
///
180181
/// Example:
181182
/// ```dart
182183
/// @JsonKey(fromJson: flexibleCommaSeparatedListFromJson)
@@ -185,7 +186,10 @@ List<T> flexibleListNotNullFromJson<T>(
185186
List<String>? flexibleCommaSeparatedListFromJson(dynamic value) {
186187
if (value == null) return null;
187188
if (value is List) {
188-
return value.map((e) => e.toString().trim()).where((e) => e.isNotEmpty).toList();
189+
return value
190+
.map((e) => e.toString().trim())
191+
.where((e) => e.isNotEmpty)
192+
.toList();
189193
}
190194
if (value is String) {
191195
if (value.trim().isEmpty) return null;
@@ -202,10 +206,10 @@ List<String>? flexibleCommaSeparatedListFromJson(dynamic value) {
202206
// STRING NORMALIZATION
203207
// ============================================================================
204208

205-
/// Trims whitespace from the input string (if it's a string) and
209+
/// Trims whitespace from the input string (if it's a string) and
206210
/// treats empty or whitespace-only strings as `null`.
207211
/// If the input is not a string but not null, it's converted via `.toString()` first.
208-
///
212+
///
209213
/// Example:
210214
/// ```dart
211215
/// @JsonKey(fromJson: flexibleTrimmedStringFromJson)
@@ -220,7 +224,7 @@ String? flexibleTrimmedStringFromJson(dynamic value) {
220224

221225
/// Normalizes a string to lowercase after trimming and handling null/empty strings.
222226
/// Uses `flexibleTrimmedStringFromJson` internally.
223-
///
227+
///
224228
/// Example:
225229
/// ```dart
226230
/// @JsonKey(fromJson: flexibleLowerStringFromJson)
@@ -233,7 +237,7 @@ String? flexibleLowerStringFromJson(dynamic value) {
233237

234238
/// Normalizes a string to uppercase after trimming and handling null/empty strings.
235239
/// Uses `flexibleTrimmedStringFromJson` internally.
236-
///
240+
///
237241
/// Example:
238242
/// ```dart
239243
/// @JsonKey(fromJson: flexibleUpperStringFromJson)

0 commit comments

Comments
 (0)