Skip to content

Commit fe945f2

Browse files
authored
♻️ refactor parsing logic in qs.dart for improved readability and efficiency (#29)
1 parent 76253f3 commit fe945f2

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

lib/src/qs.dart

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ final class QS {
4646
// Iterate over the keys and setup the new object
4747
if (tempObj?.isNotEmpty ?? false) {
4848
for (final MapEntry<String, dynamic> entry in tempObj!.entries) {
49-
final newObj = _$Decode._parseKeys(
50-
entry.key,
51-
entry.value,
52-
options,
53-
input is String,
54-
);
55-
5649
obj = Utils.merge(
5750
obj,
58-
newObj,
51+
_$Decode._parseKeys(
52+
entry.key,
53+
entry.value,
54+
options,
55+
input is String,
56+
),
5957
options,
6058
);
6159
}
@@ -74,14 +72,14 @@ final class QS {
7472
return '';
7573
}
7674

77-
late Map<String, dynamic> obj;
78-
if (object is Map<String, dynamic>) {
79-
obj = {...?object as Map<String, dynamic>?};
80-
} else if (object is Iterable) {
81-
obj = object.toList().asMap().map((k, v) => MapEntry(k.toString(), v));
82-
} else {
83-
obj = {};
84-
}
75+
Map<String, dynamic> obj = switch (object) {
76+
Map<String, dynamic> map => {...map},
77+
Iterable iterable => iterable
78+
.toList()
79+
.asMap()
80+
.map((int k, dynamic v) => MapEntry(k.toString(), v)),
81+
_ => <String, dynamic>{},
82+
};
8583

8684
final List keys = [];
8785

@@ -97,10 +95,6 @@ final class QS {
9795
objKeys = List.of(options.filter);
9896
}
9997

100-
final bool commaRoundTrip =
101-
options.listFormat.generator == ListFormat.comma.generator &&
102-
options.commaRoundTrip == true;
103-
10498
objKeys ??= obj.keys.toList();
10599

106100
if (options.sort is Function) {
@@ -110,10 +104,8 @@ final class QS {
110104
final WeakMap sideChannel = WeakMap();
111105
for (int i = 0; i < objKeys.length; i++) {
112106
final key = objKeys[i];
113-
if (key is! String?) {
114-
continue;
115-
}
116-
if (obj[key] == null && options.skipNulls) {
107+
108+
if (key is! String? || (obj[key] == null && options.skipNulls)) {
117109
continue;
118110
}
119111

@@ -122,7 +114,9 @@ final class QS {
122114
undefined: !obj.containsKey(key),
123115
prefix: key,
124116
generateArrayPrefix: options.listFormat.generator,
125-
commaRoundTrip: commaRoundTrip,
117+
commaRoundTrip:
118+
options.listFormat.generator == ListFormat.comma.generator &&
119+
options.commaRoundTrip == true,
126120
allowEmptyLists: options.allowEmptyLists,
127121
strictNullHandling: options.strictNullHandling,
128122
skipNulls: options.skipNulls,
@@ -148,20 +142,28 @@ final class QS {
148142
}
149143

150144
final String joined = keys.join(options.delimiter);
151-
String prefix = options.addQueryPrefix ? '?' : '';
145+
final StringBuffer out = StringBuffer();
146+
147+
if (options.addQueryPrefix) {
148+
out.write('?');
149+
}
152150

153151
if (options.charsetSentinel) {
154-
prefix += switch (options.charset) {
152+
out.write(switch (options.charset) {
155153
/// encodeURIComponent('&#10003;')
156154
/// the "numeric entity" representation of a checkmark
157155
latin1 => '${Sentinel.iso}&',
158156

159157
/// encodeURIComponent('✓')
160158
utf8 => '${Sentinel.charset}&',
161159
_ => '',
162-
};
160+
});
161+
}
162+
163+
if (joined.isNotEmpty) {
164+
out.write(joined);
163165
}
164166

165-
return joined.isNotEmpty ? prefix + joined : '';
167+
return out.toString();
166168
}
167169
}

0 commit comments

Comments
 (0)