diff --git a/lib/src/enums/sentinel.dart b/lib/src/enums/sentinel.dart new file mode 100644 index 0000000..ce4af15 --- /dev/null +++ b/lib/src/enums/sentinel.dart @@ -0,0 +1,29 @@ +enum Sentinel { + /// This is what browsers will submit when the ✓ character occurs in an + /// application/x-www-form-urlencoded body and the encoding of the page containing + /// the form is iso-8859-1, or when the submitted form has an accept-charset + /// attribute of iso-8859-1. Presumably also with other charsets that do not contain + /// the ✓ character, such as us-ascii. + iso( + value: r'✓', + encoded: r'utf8=%26%2310003%3B', + ), + + /// These are the percent-encoded utf-8 octets representing a checkmark, + /// indicating that the request actually is utf-8 encoded. + charset( + value: r'✓', + encoded: r'utf8=%E2%9C%93', + ); + + const Sentinel({ + required this.value, + required this.encoded, + }); + + final String value; + final String encoded; + + @override + String toString() => encoded; +} diff --git a/lib/src/extensions/decode.dart b/lib/src/extensions/decode.dart index 0b0926a..65dc0e6 100644 --- a/lib/src/extensions/decode.dart +++ b/lib/src/extensions/decode.dart @@ -35,9 +35,9 @@ extension _$Decode on QS { if (options.charsetSentinel) { for (i = 0; i < parts.length; ++i) { if (parts.elementAt(i).startsWith('utf8=')) { - if (parts.elementAt(i) == Utils.charsetSentinel) { + if (parts.elementAt(i) == Sentinel.charset.toString()) { charset = utf8; - } else if (parts.elementAt(i) == Utils.isoSentinel) { + } else if (parts.elementAt(i) == Sentinel.iso.toString()) { charset = latin1; } skipIndex = i; diff --git a/lib/src/qs.dart b/lib/src/qs.dart index 39bbc96..db8eca6 100644 --- a/lib/src/qs.dart +++ b/lib/src/qs.dart @@ -4,6 +4,7 @@ import 'dart:typed_data'; import 'package:qs_dart/src/enums/duplicates.dart'; import 'package:qs_dart/src/enums/format.dart'; import 'package:qs_dart/src/enums/list_format.dart'; +import 'package:qs_dart/src/enums/sentinel.dart'; import 'package:qs_dart/src/models/decode_options.dart'; import 'package:qs_dart/src/models/encode_options.dart'; import 'package:qs_dart/src/models/undefined.dart'; @@ -11,6 +12,7 @@ import 'package:qs_dart/src/utils.dart'; import 'package:weak_map/weak_map.dart'; part 'extensions/decode.dart'; + part 'extensions/encode.dart'; final class QS { @@ -139,10 +141,10 @@ final class QS { if (options.charset == latin1) { /// encodeURIComponent('✓') /// the "numeric entity" representation of a checkmark - prefix += '${Utils.isoSentinel}&'; + prefix += '${Sentinel.iso}&'; } else if (options.charset == utf8) { /// encodeURIComponent('✓') - prefix += '${Utils.charsetSentinel}&'; + prefix += '${Sentinel.charset}&'; } else { throw ArgumentError.value( options.charset, diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 0429c83..b1979e6 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -12,17 +12,6 @@ import 'package:qs_dart/src/models/undefined.dart'; part 'constants/hex_table.dart'; final class Utils { - static void compactQueue(List queue) { - while (queue.length > 1) { - final Map item = queue.removeLast(); - final dynamic obj = item['obj'][item['prop']]; - - if (obj is List) { - item['obj'][item['prop']] = obj.whereNotUndefined(); - } - } - } - static dynamic merge( dynamic target, dynamic source, [ @@ -361,13 +350,24 @@ final class Utils { } } - compactQueue(queue); + _compactQueue(queue); removeUndefinedFromMap(value); return value; } + static void _compactQueue(List queue) { + while (queue.length > 1) { + final Map item = queue.removeLast(); + final dynamic obj = item['obj'][item['prop']]; + + if (obj is List) { + item['obj'][item['prop']] = obj.whereNotUndefined(); + } + } + } + @visibleForTesting static void removeUndefinedFromList(dynamic value) { if (value is List) { @@ -429,17 +429,4 @@ final class Utils { (val is String && val.isEmpty) || (val is Iterable && val.isEmpty) || (val is Map && val.isEmpty); - - /// This is what browsers will submit when the ✓ character occurs in an - /// application/x-www-form-urlencoded body and the encoding of the page containing - /// the form is iso-8859-1, or when the submitted form has an accept-charset - /// attribute of iso-8859-1. Presumably also with other charsets that do not contain - /// the ✓ character, such as us-ascii. - static const String isoSentinel = - r'utf8=%26%2310003%3B'; // encodeURIComponent('✓') - - /// These are the percent-encoded utf-8 octets representing a checkmark, - /// indicating that the request actually is utf-8 encoded. - static const String charsetSentinel = - r'utf8=%E2%9C%93'; // encodeURIComponent('✓') }