Skip to content

Commit

Permalink
🐛 encode any object by calling toString
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Apr 4, 2024
1 parent 7ac822d commit 52adab3
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 8 deletions.
39 changes: 33 additions & 6 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,39 @@ final class Utils {
static dynamic apply<T>(dynamic val, T Function(T) fn) =>
val is Iterable ? val.map((item) => fn(item)) : fn(val);

static bool isNonNullishPrimitive(dynamic val, [bool skipNulls = false]) =>
(val is String && (skipNulls ? val.isNotEmpty : true)) ||
val is num ||
val is bool ||
val is BigInt ||
(val is Uri && (skipNulls ? val.toString().isNotEmpty : true));
static bool isNonNullishPrimitive(dynamic val, [bool skipNulls = false]) {
if (val is String) {
return skipNulls ? val.isNotEmpty : true;
}

if (val is num ||
val is BigInt ||
val is bool ||
val is Enum ||
val is DateTime ||
val is Duration) {
return true;
}

if (val is Uri) {
return skipNulls ? val.toString().isNotEmpty : true;

Check warning on line 419 in lib/src/utils.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/utils.dart#L419

Added line #L419 was not covered by tests
}

if (val is Object) {
if (val is Iterable ||
val is Map ||
val is Runes ||
val is Symbol ||
val is Record ||
val is Future ||
val is Never ||
val is Undefined) {
return false;
}
}

return false;
}

static bool isEmpty(dynamic val) =>
val == null ||
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/dummy_enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum DummyEnum {
lorem,
ipsum,
dolor;

@override
String toString() => name;
}
66 changes: 66 additions & 0 deletions test/unit/encode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:qs_dart/src/utils.dart';
import 'package:test/test.dart';

import '../fixtures/data/empty_test_cases.dart';
import '../fixtures/dummy_enum.dart';

void main() {
group('encode', () {
Expand Down Expand Up @@ -3005,4 +3006,69 @@ void main() {
);
});
});

group('encode non-Strings', () {
test('encodes a null value', () {
expect(QS.encode({'a': null}), equals('a='));
});

test('encodes a boolean value', () {
expect(QS.encode({'a': true}), equals('a=true'));
expect(QS.encode({'a': false}), equals('a=false'));
});

test('encodes a number value', () {
expect(QS.encode({'a': 0}), equals('a=0'));
expect(QS.encode({'a': 1}), equals('a=1'));
expect(QS.encode({'a': 1.1}), equals('a=1.1'));
});

test('encodes a buffer value', () {
expect(QS.encode({'a': utf8.encode('test').buffer}), equals('a=test'));
});

test('encodes a date value', () {
final DateTime now = DateTime.now();
final String str = 'a=${Uri.encodeComponent(now.toIso8601String())}';
expect(QS.encode({'a': now}), equals(str));
});

test('encodes a list value', () {
expect(
QS.encode({
'a': [1, 2, 3]
}),
equals('a%5B0%5D=1&a%5B1%5D=2&a%5B2%5D=3'));
});

test('encodes a map value', () {
expect(
QS.encode({
'a': {'b': 'c'}
}),
equals('a%5Bb%5D=c'));
});

test('encodes a map with a null map as a child', () {
final Map<String, dynamic> obj = {
'a': {},
};
obj['a']['b'] = 'c';
expect(QS.encode(obj), equals('a%5Bb%5D=c'));
});

test('encodes a map with an enum as a child', () {
final Map<String, dynamic> obj = {
'a': DummyEnum.lorem,
'b': 'foo',
'c': 1,
'd': 1.234,
'e': true,
};
expect(
QS.encode(obj),
equals('a=lorem&b=foo&c=1&d=1.234&e=true'),
);
});
});
}
30 changes: 28 additions & 2 deletions test/unit/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import 'dart:convert' show latin1, utf8;

import 'package:qs_dart/src/enums/format.dart';
import 'package:qs_dart/src/models/undefined.dart';
import 'package:qs_dart/qs_dart.dart';
import 'package:qs_dart/src/utils.dart';
import 'package:test/test.dart';

import '../fixtures/dummy_enum.dart';

void main() {
group('Utils', () {
test('encode', () {
Expand All @@ -24,6 +25,31 @@ void main() {
Utils.encode('foo(bar)', format: Format.rfc1738),
equals('foo(bar)'),
);
expect(DummyEnum.lorem, isA<Enum>());
expect(Utils.encode(DummyEnum.lorem), equals('lorem'));
expect(
Utils.encode({
'foo': 'bar',
'baz': [
{'a': 'b'},
{'c': DummyEnum.dolor},
],
}),
equals(
'%7Bfoo%3A%20bar%2C%20baz%3A%20%5B%7Ba%3A%20b%7D%2C%20%7Bc%3A%20dolor%7D%5D%7D',
),
);
expect(
Utils.encode({
'filters': {
'name': 'foo',
'example': DummyEnum.lorem,
}
}),
equals(
'%7Bfilters%3A%20%7Bname%3A%20foo%2C%20example%3A%20lorem%7D%7D',
),
);
});

test('decode', () {
Expand Down

0 comments on commit 52adab3

Please sign in to comment.