Skip to content

Commit 36fdfd9

Browse files
committed
Migrate @turf/line-chunk to TypeScript
1 parent a954bf9 commit 36fdfd9

3 files changed

Lines changed: 59 additions & 47 deletions

File tree

packages/turf-line-chunk/index.d.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { length } from "@turf/length";
22
import { lineSliceAlong } from "@turf/line-slice-along";
33
import { flattenEach } from "@turf/meta";
4-
import { featureCollection, isObject } from "@turf/helpers";
4+
import { featureCollection, isObject, Units } from "@turf/helpers";
5+
import {
6+
Feature,
7+
FeatureCollection,
8+
GeometryCollection,
9+
LineString,
10+
MultiLineString,
11+
} from "geojson";
512

613
/**
714
* Divides a {@link LineString} into chunks of a specified length.
@@ -22,30 +29,47 @@ import { featureCollection, isObject } from "@turf/helpers";
2229
* //addToMap
2330
* var addToMap = [chunk];
2431
*/
25-
function lineChunk(geojson, segmentLength, options) {
32+
function lineChunk<T extends LineString | MultiLineString>(
33+
geojson:
34+
| Feature<T>
35+
| FeatureCollection<T>
36+
| T
37+
| GeometryCollection
38+
| Feature<GeometryCollection>,
39+
segmentLength: number,
40+
options: {
41+
units?: Units;
42+
reverse?: boolean;
43+
} = {}
44+
): FeatureCollection<LineString> {
2645
// Optional parameters
27-
options = options || {};
2846
if (!isObject(options)) throw new Error("options is invalid");
29-
var units = options.units;
30-
var reverse = options.reverse;
47+
const { units = "kilometers", reverse = false } = options;
3148

3249
// Validation
3350
if (!geojson) throw new Error("geojson is required");
34-
if (segmentLength <= 0)
51+
if (segmentLength <= 0) {
3552
throw new Error("segmentLength must be greater than 0");
53+
}
3654

3755
// Container
38-
var results = [];
56+
const results: Feature<LineString>[] = [];
3957

4058
// Flatten each feature to simple LineString
41-
flattenEach(geojson, function (feature) {
59+
flattenEach(geojson, (feature: Feature<T>) => {
4260
// reverses coordinates to start the first chunked segment at the end
43-
if (reverse)
61+
if (reverse) {
4462
feature.geometry.coordinates = feature.geometry.coordinates.reverse();
63+
}
4564

46-
sliceLineSegments(feature, segmentLength, units, function (segment) {
47-
results.push(segment);
48-
});
65+
sliceLineSegments(
66+
feature as Feature<LineString>,
67+
segmentLength,
68+
units,
69+
(segment) => {
70+
results.push(segment);
71+
}
72+
);
4973
});
5074
return featureCollection(results);
5175
}
@@ -60,11 +84,18 @@ function lineChunk(geojson, segmentLength, options) {
6084
* @param {Function} callback iterate over sliced line segments
6185
* @returns {void}
6286
*/
63-
function sliceLineSegments(line, segmentLength, units, callback) {
87+
function sliceLineSegments(
88+
line: Feature<LineString>,
89+
segmentLength: number,
90+
units: Units,
91+
callback: (feature: Feature<LineString>) => void
92+
): void {
6493
var lineLength = length(line, { units: units });
6594

6695
// If the line is shorter than the segment length then the orginal line is returned.
67-
if (lineLength <= segmentLength) return callback(line);
96+
if (lineLength <= segmentLength) {
97+
return callback(line);
98+
}
6899

69100
var numberOfSegments = lineLength / segmentLength;
70101

@@ -80,7 +111,7 @@ function sliceLineSegments(line, segmentLength, units, callback) {
80111
segmentLength * (i + 1),
81112
{ units: units }
82113
);
83-
callback(outline, i);
114+
callback(outline);
84115
}
85116
}
86117

packages/turf-line-chunk/test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import { loadJsonFileSync } from "load-json-file";
66
import { writeJsonFileSync } from "write-json-file";
77
import { truncate } from "@turf/truncate";
88
import { featureEach } from "@turf/meta";
9-
import { lineString, featureCollection } from "@turf/helpers";
9+
import {
10+
lineString,
11+
featureCollection,
12+
point,
13+
geometryCollection,
14+
} from "@turf/helpers";
1015
import { lineChunk } from "./index.js";
16+
import { Feature } from "geojson";
1117

1218
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1319

@@ -17,7 +23,10 @@ const directories = {
1723
};
1824

1925
const fixtures = fs.readdirSync(directories.in).map((filename) => {
20-
return { filename, geojson: loadJsonFileSync(directories.in + filename) };
26+
return {
27+
filename,
28+
geojson: loadJsonFileSync(directories.in + filename) as any,
29+
};
2130
});
2231

2332
test("turf-line-chunk: shorter", (t) => {
@@ -103,8 +112,8 @@ test("turf-line-chunk: Prevent input mutation", (t) => {
103112
* @param {FeatureCollection|Feature<any>} geojson Feature or FeatureCollection
104113
* @returns {FeatureCollection<any>} colorized FeatureCollection
105114
*/
106-
function colorize(geojson) {
107-
const results = [];
115+
function colorize(geojson: any) {
116+
const results: Feature[] = [];
108117
featureEach(geojson, (feature, index) => {
109118
const r = index % 2 === 0 ? "F" : "0";
110119
const g = index % 2 === 0 ? "0" : "0";

0 commit comments

Comments
 (0)