Skip to content

Commit

Permalink
Cleanup package:http utils (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed Sep 14, 2023
1 parent eafbbb0 commit d7e4375
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions pkgs/http/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import 'byte_stream.dart';
///
/// mapToQuery({"foo": "bar", "baz": "bang"});
/// //=> "foo=bar&baz=bang"
String mapToQuery(Map<String, String> map, {Encoding? encoding}) {
var pairs = <List<String>>[];
map.forEach((key, value) => pairs.add([
Uri.encodeQueryComponent(key, encoding: encoding ?? utf8),
Uri.encodeQueryComponent(value, encoding: encoding ?? utf8)
]));
return pairs.map((pair) => '${pair[0]}=${pair[1]}').join('&');
}
String mapToQuery(Map<String, String> map, {required Encoding encoding}) =>
map.entries
.map((e) => '${Uri.encodeQueryComponent(e.key, encoding: encoding)}'
'=${Uri.encodeQueryComponent(e.value, encoding: encoding)}')
.join('&');

/// Returns the [Encoding] that corresponds to [charset].
///
Expand All @@ -34,8 +31,6 @@ Encoding encodingForCharset(String? charset, [Encoding fallback = latin1]) {
///
/// Throws a [FormatException] if no [Encoding] was found that corresponds to
/// [charset].
///
/// [charset] may not be null.
Encoding requiredEncodingForCharset(String charset) =>
Encoding.getByName(charset) ??
(throw FormatException('Unsupported encoding "$charset".'));
Expand All @@ -53,9 +48,8 @@ bool isPlainAscii(String string) => _asciiOnly.hasMatch(string);
/// If [input] is a [TypedData], this just returns a view on [input].
Uint8List toUint8List(List<int> input) {
if (input is Uint8List) return input;
if (input is TypedData) {
// TODO(nweiz): remove "as" when issue 11080 is fixed.
return Uint8List.view((input as TypedData).buffer);
if (input case TypedData data) {
return Uint8List.view(data.buffer);
}
return Uint8List.fromList(input);
}
Expand Down

0 comments on commit d7e4375

Please sign in to comment.