Skip to content

Commit

Permalink
♻️ move sentinels to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Apr 2, 2024
1 parent b79cb79 commit 0b1ae82
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
29 changes: 29 additions & 0 deletions lib/src/enums/sentinel.dart
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions lib/src/extensions/decode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions lib/src/qs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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';
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 {
Expand Down Expand Up @@ -139,10 +141,10 @@ final class QS {
if (options.charset == latin1) {
/// encodeURIComponent('&#10003;')
/// 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,
Expand Down
37 changes: 12 additions & 25 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ import 'package:qs_dart/src/models/undefined.dart';
part 'constants/hex_table.dart';

final class Utils {
static void compactQueue(List<Map> 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, [
Expand Down Expand Up @@ -361,13 +350,24 @@ final class Utils {
}
}

compactQueue(queue);
_compactQueue(queue);

removeUndefinedFromMap(value);

return value;
}

static void _compactQueue(List<Map> 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) {
Expand Down Expand Up @@ -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('&#10003;')

/// 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('✓')
}

0 comments on commit 0b1ae82

Please sign in to comment.