Skip to content

Commit

Permalink
♻️ replace Utils.pushToArray with more explicit code
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Apr 2, 2024
1 parent f4d7c51 commit 937fafd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
58 changes: 31 additions & 27 deletions lib/src/extensions/encode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,34 +179,38 @@ extension _$Encode on QS {
sideChannel[object] = step;
final WeakMap valueSideChannel = WeakMap();
valueSideChannel.add(key: _sentinel, value: sideChannel);
Utils.pushToArray(
values,
_encode(
value,
undefined: valueUndefined,
prefix: keyPrefix,
generateArrayPrefix: generateArrayPrefix,
commaRoundTrip: commaRoundTrip,
allowEmptyArrays: allowEmptyArrays,
strictNullHandling: strictNullHandling,
skipNulls: skipNulls,
encodeDotInKeys: encodeDotInKeys,
encoder: generateArrayPrefix == ListFormat.comma.generator &&
encodeValuesOnly &&
obj is Iterable
? null
: encoder,
serializeDate: serializeDate,
filter: filter,
sort: sort,
allowDots: allowDots,
format: format,
formatter: formatter,
encodeValuesOnly: encodeValuesOnly,
charset: charset,
sideChannel: valueSideChannel,
),

final encoded = _encode(
value,
undefined: valueUndefined,
prefix: keyPrefix,
generateArrayPrefix: generateArrayPrefix,
commaRoundTrip: commaRoundTrip,
allowEmptyArrays: allowEmptyArrays,
strictNullHandling: strictNullHandling,
skipNulls: skipNulls,
encodeDotInKeys: encodeDotInKeys,
encoder: generateArrayPrefix == ListFormat.comma.generator &&
encodeValuesOnly &&
obj is Iterable
? null
: encoder,
serializeDate: serializeDate,
filter: filter,
sort: sort,
allowDots: allowDots,
format: format,
formatter: formatter,
encodeValuesOnly: encodeValuesOnly,
charset: charset,
sideChannel: valueSideChannel,
);

if (encoded is Iterable) {
values.addAll(encoded);
} else {
values.add(encoded);
}
}

return values;
Expand Down
51 changes: 27 additions & 24 deletions lib/src/qs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,34 @@ final class QS {
continue;
}

Utils.pushToArray(
keys,
_$Encode._encode(
obj[key],
undefined: !obj.containsKey(key),
prefix: key,
generateArrayPrefix: options.listFormat.generator,
commaRoundTrip: commaRoundTrip,
allowEmptyArrays: options.allowEmptyLists,
strictNullHandling: options.strictNullHandling,
skipNulls: options.skipNulls,
encodeDotInKeys: options.encodeDotInKeys,
encoder: options.encode ? options.encoder : null,
serializeDate: options.serializeDate,
filter: options.filter,
sort: options.sort,
allowDots: options.allowDots,
format: options.format,
formatter: options.formatter,
encodeValuesOnly: options.encodeValuesOnly,
charset: options.charset,
addQueryPrefix: options.addQueryPrefix,
sideChannel: sideChannel,
),
final encoded = _$Encode._encode(
obj[key],
undefined: !obj.containsKey(key),
prefix: key,
generateArrayPrefix: options.listFormat.generator,
commaRoundTrip: commaRoundTrip,
allowEmptyArrays: options.allowEmptyLists,
strictNullHandling: options.strictNullHandling,
skipNulls: options.skipNulls,
encodeDotInKeys: options.encodeDotInKeys,
encoder: options.encode ? options.encoder : null,
serializeDate: options.serializeDate,
filter: options.filter,
sort: options.sort,
allowDots: options.allowDots,
format: options.format,
formatter: options.formatter,
encodeValuesOnly: options.encodeValuesOnly,
charset: options.charset,
addQueryPrefix: options.addQueryPrefix,
sideChannel: sideChannel,
);

if (encoded is Iterable) {
keys.addAll(encoded);
} else {
keys.add(encoded);
}
}

final String joined = keys.join(options.delimiter);
Expand Down
5 changes: 0 additions & 5 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,6 @@ final class Utils {
static dynamic maybeMap<T>(dynamic val, T Function(T) fn) =>
val is Iterable ? val.map((item) => fn(item)) : fn(val);

static void pushToArray(List arr, dynamic valueOrArray) =>
valueOrArray is Iterable
? arr.addAll(valueOrArray)
: arr.add(valueOrArray);

static bool isNonNullishPrimitive(dynamic val, [bool skipNulls = false]) =>
(val is String && (skipNulls ? val.isNotEmpty : true)) ||
val is num ||
Expand Down

0 comments on commit 937fafd

Please sign in to comment.