Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta functions extension methods (attempt 2) #73

Merged
merged 6 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions benchmark/line_segment_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ void main() {
lineSegment(collection);
});
benchmark('segmentReduce', () {
segmentReduce(collection, (previousValue, currentSegment, initialValue,
featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
previousValue++;
segmentReduce<int>(collection, (previousValue,
currentSegment,
initialValue,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex) {
if (previousValue != null) {
previousValue++;
}
return previousValue;
}, 0, combineNestedGeometries: false);
});
Expand Down
3 changes: 3 additions & 0 deletions lib/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library turf_extensions;

export 'src/meta/extensions.dart';
2 changes: 1 addition & 1 deletion lib/meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export 'src/meta/coord.dart';
export 'src/meta/feature.dart';
export 'src/meta/flatten.dart';
export 'src/meta/geom.dart';
export 'src/line_segment.dart' show segmentEach, segmentReduce;
export 'src/line_segment.dart' hide lineSegment;
export 'src/meta/prop.dart';
2 changes: 1 addition & 1 deletion lib/src/line_segment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ typedef T? SegmentReduceCallback<T>(

T? segmentReduce<T>(
GeoJSONObject geojson,
SegmentReduceCallback callback,
SegmentReduceCallback<T> callback,
T? initialValue, {
bool combineNestedGeometries = true,
}) {
Expand Down
125 changes: 125 additions & 0 deletions lib/src/meta/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import 'package:turf/helpers.dart';
import 'package:turf/meta.dart' as meta;

extension GeoJSONObjectMetaExtension on GeoJSONObject {
void geomEach(meta.GeomEachCallback callback) {
meta.geomEach(this, callback);
}

T? geomReduce<T>(meta.GeomReduceCallback<T> callback, T? initialValue) {
meta.geomReduce<T>(
this,
callback,
initialValue,
);
}

void propEach(meta.PropEachCallback callback) {
meta.propEach(this, callback);
}

T? propReduce<T>(
meta.PropReduceCallback<T> callback,
T? initialValue,
) {
meta.propReduce<T>(
this,
callback,
initialValue,
);
}

void featureEach(meta.FeatureEachCallback callback) {
meta.featureEach(this, callback);
}

T? featureReduce<T>(
meta.FeatureReduceCallback<T> callback,
T? initialValue,
) {
meta.featureReduce<T>(
this,
callback,
initialValue,
);
}

void coordEach(meta.CoordEachCallback callback) {
meta.coordEach(this, callback);
}

T? coordReduce<T>(
meta.CoordReduceCallback<T> callback,
T? initialValue, [
bool excludeWrapCoord = false,
]) {
meta.coordReduce<T>(
this,
callback,
initialValue,
excludeWrapCoord,
);
}

List<Position?> coordAll() {
return meta.coordAll(this);
}

void flattenEach(meta.FlattenEachCallback callback) {
meta.flattenEach(this, callback);
}

T? flattenReduce<T>(
meta.FlattenReduceCallback<T> callback,
T? initialValue,
) {
meta.flattenReduce<T>(
this,
callback,
initialValue,
);
}

void segmentEach(meta.SegmentEachCallback callback) {
meta.segmentEach(this, callback);
}

T? segmentReduce<T>(
meta.SegmentReduceCallback<T> callback,
T? initialValue, {
bool combineNestedGeometries = true,
}) {
meta.segmentReduce<T>(
this,
callback,
initialValue,
combineNestedGeometries: combineNestedGeometries,
);
}
}

extension FeatureCollectionMetaExtension on FeatureCollection {
void clusterEach(
dynamic property,
meta.ClusterEachCallback callback,
) {
meta.clusterEach(this, property, callback);
}

FeatureCollection getCluster(dynamic filter) {
return meta.getCluster(this, filter);
}

T? clusterReduce<T>(
dynamic property,
meta.ClusterReduceCallback<T> callback,
dynamic initialValue,
) {
meta.clusterReduce<T>(
this,
property,
callback,
initialValue,
);
}
}
6 changes: 4 additions & 2 deletions test/components/line_segment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ main() {
});

test("segmentReduce", () {
var total = segmentReduce(poly1, (previousValue,
var total = segmentReduce<int>(poly1, (previousValue,
currentSegment,
initialValue,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex) {
previousValue++;
if (previousValue != null) {
previousValue++;
}
return previousValue;
}, 0, combineNestedGeometries: false);
expect(total, 6);
Expand Down