Skip to content

Commit

Permalink
wip, type problem
Browse files Browse the repository at this point in the history
  • Loading branch information
armantorkzaban committed Mar 7, 2022
1 parent 6bfda8b commit a2f1ae3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/src/geojson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class GeometryCollection extends GeometryObject {
);
}

/// Feature, as specified here https://tools.ietf.org/html/rfc7946#section-3.2
/// Feature, as specified here https://tools.ietf.o rg/html/rfc7946#section-3.2

This comment has been minimized.

Copy link
@lukas-h

lukas-h Mar 7, 2022

Member

typo

class Feature<T extends GeometryObject> extends GeoJSONObject {
dynamic id;
Map<String, dynamic>? properties;
Expand Down
17 changes: 13 additions & 4 deletions lib/src/invariant.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:turf/turf.dart';

///
Expand Down Expand Up @@ -43,24 +45,31 @@ Position getCoord(dynamic coord) {
/// var coords = turf.getCoords(poly);
/// //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
///
List getCoords(dynamic coords) {
List<dynamic> getCoords(dynamic coords) {
if (coords == null) {
throw Exception("coords is required");
}

if (coords is List) {
return coords;
}

if (coords is Feature && coords.geometry != null) {
_getCoordsForGeometry(coords.geometry!);
}

if (coords is List) {
return coords;
if (coords is GeometryType) {
_getCoordsForGeometry(coords.coordinates);
}
return _getCoordsForGeometry(coords);

throw Exception(
"{Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array");

This comment has been minimized.

Copy link
@lukas-h

lukas-h Mar 7, 2022

Member

this error message is still the turf one.
can you replace Array with List and also not use {} and |

}

_getCoordsForGeometry(GeometryObject geom) {
if (geom is Point || geom is GeometryCollection) {
throw Exception("Type must contain a list of Positions e.g Polygon");
}

return (geom as GeometryType).coordinates;
}
24 changes: 19 additions & 5 deletions test/components/invariant_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:math';

import 'package:test/test.dart';
import 'package:turf/helpers.dart';
import 'package:turf/src/invariant.dart';
// import 'package:collection/collection.dart';

main() {
LineString line1 = LineString(coordinates: [Position(1, 2), Position(3, 4)]);
Expand All @@ -18,12 +21,23 @@ main() {
test("invariant -- getCoords", () {
var feature2 = Feature<LineString>(geometry: line1);
expect(() => getCoords(null), throwsA(isA<Exception>()));
expect(() => getCoords(feature1), throwsA(isA<Exception>()));
expect(
getCoords([
[119.32, -8.7],

This comment has been minimized.

Copy link
@lukas-h

lukas-h Mar 7, 2022

Member

Very important!! turf dart does not use lists for coordinates, but the Position class,

This comment has been minimized.

Copy link
@lukas-h

lukas-h Mar 7, 2022

Member

So it would basically be a List of Positions

[119.55, -8.69],
[119.51, -8.54],
[119.32, -8.7]
]),
equals([
[119.32, -8.7],
[119.55, -8.69],
[119.51, -8.54],
[119.32, -8.7]
]));
expect(() => getCoords(feature1), throwsA(isA<Exception>()));
expect(() => getCoords(feature1), throwsA(isA<Exception>()));
var coords = getCoords(feature2);
expect(coords.length, feature2.geometry!.coordinates.length);
expect(coords.first, feature2.geometry!.coordinates.first);
expect(coords.last, feature2.geometry!.coordinates.last);

expect(coords, equals([Position(1, 2), Position(3, 4)]));

This comment has been minimized.

Copy link
@lukas-h

lukas-h Mar 7, 2022

Member

does this equals matched take care of nested types like lists? “Deep equality”

});
/*
Expand Down

0 comments on commit a2f1ae3

Please sign in to comment.