Skip to content

Commit a196686

Browse files
Optimize PrimitiveMapper for numeric types (#340)
1 parent ed2e6c7 commit a196686

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

packages/dart_mappable/lib/src/mapper_container.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,13 @@ abstract class MapperContainer {
103103
PrimitiveMapper<Object>((v) => v, dynamic),
104104
PrimitiveMapper<Object>((v) => v, Object),
105105
PrimitiveMapper<String>((v) => v.toString()),
106-
PrimitiveMapper<int>((v) => num.parse(v.toString()).round()),
107-
PrimitiveMapper<double>((v) => double.parse(v.toString())),
108-
PrimitiveMapper<num>((v) => num.parse(v.toString()), num),
106+
PrimitiveMapper<int>(
107+
(v) => v is num ? v.round() : num.parse(v.toString()).round(),
108+
),
109+
PrimitiveMapper<double>(
110+
(v) => v is num ? v.toDouble() : double.parse(v.toString()),
111+
),
112+
PrimitiveMapper<num>((v) => v is num ? v : num.parse(v.toString()), num),
109113
PrimitiveMapper<bool>((v) => v is num ? v != 0 : v.toString() == 'true'),
110114
DateTimeMapper(),
111115
IterableMapper<List>(<T>(i) => i.toList(), <T>(f) => f<List<T>>()),

0 commit comments

Comments
 (0)