diff --git a/packages/turf-line-chunk/index.d.ts b/packages/turf-line-chunk/index.d.ts deleted file mode 100644 index b0ee9595a1..0000000000 --- a/packages/turf-line-chunk/index.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { - LineString, - MultiLineString, - GeometryCollection, - Feature, - FeatureCollection, -} from "geojson"; -import { Units } from "@turf/helpers"; - -/** - * http://turfjs.org/docs/#lineChunk - */ -declare function lineChunk( - geojson: - | Feature - | FeatureCollection - | T - | GeometryCollection - | Feature, - segmentLength: number, - options?: { - units?: Units; - reverse?: boolean; - } -): FeatureCollection; - -export { lineChunk }; -export default lineChunk; diff --git a/packages/turf-line-chunk/index.js b/packages/turf-line-chunk/index.ts similarity index 68% rename from packages/turf-line-chunk/index.js rename to packages/turf-line-chunk/index.ts index 28074c5f11..34d8e3da69 100644 --- a/packages/turf-line-chunk/index.js +++ b/packages/turf-line-chunk/index.ts @@ -1,7 +1,14 @@ import { length } from "@turf/length"; import { lineSliceAlong } from "@turf/line-slice-along"; import { flattenEach } from "@turf/meta"; -import { featureCollection, isObject } from "@turf/helpers"; +import { featureCollection, isObject, Units } from "@turf/helpers"; +import { + Feature, + FeatureCollection, + GeometryCollection, + LineString, + MultiLineString, +} from "geojson"; /** * Divides a {@link LineString} into chunks of a specified length. @@ -22,30 +29,47 @@ import { featureCollection, isObject } from "@turf/helpers"; * //addToMap * var addToMap = [chunk]; */ -function lineChunk(geojson, segmentLength, options) { +function lineChunk( + geojson: + | Feature + | FeatureCollection + | T + | GeometryCollection + | Feature, + segmentLength: number, + options: { + units?: Units; + reverse?: boolean; + } = {} +): FeatureCollection { // Optional parameters - options = options || {}; if (!isObject(options)) throw new Error("options is invalid"); - var units = options.units; - var reverse = options.reverse; + const { units = "kilometers", reverse = false } = options; // Validation if (!geojson) throw new Error("geojson is required"); - if (segmentLength <= 0) + if (segmentLength <= 0) { throw new Error("segmentLength must be greater than 0"); + } // Container - var results = []; + const results: Feature[] = []; // Flatten each feature to simple LineString - flattenEach(geojson, function (feature) { + flattenEach(geojson, (feature: Feature) => { // reverses coordinates to start the first chunked segment at the end - if (reverse) + if (reverse) { feature.geometry.coordinates = feature.geometry.coordinates.reverse(); + } - sliceLineSegments(feature, segmentLength, units, function (segment) { - results.push(segment); - }); + sliceLineSegments( + feature as Feature, + segmentLength, + units, + (segment) => { + results.push(segment); + } + ); }); return featureCollection(results); } @@ -60,11 +84,18 @@ function lineChunk(geojson, segmentLength, options) { * @param {Function} callback iterate over sliced line segments * @returns {void} */ -function sliceLineSegments(line, segmentLength, units, callback) { +function sliceLineSegments( + line: Feature, + segmentLength: number, + units: Units, + callback: (feature: Feature) => void +): void { var lineLength = length(line, { units: units }); // If the line is shorter than the segment length then the orginal line is returned. - if (lineLength <= segmentLength) return callback(line); + if (lineLength <= segmentLength) { + return callback(line); + } var numberOfSegments = lineLength / segmentLength; @@ -80,7 +111,7 @@ function sliceLineSegments(line, segmentLength, units, callback) { segmentLength * (i + 1), { units: units } ); - callback(outline, i); + callback(outline); } } diff --git a/packages/turf-line-chunk/package.json b/packages/turf-line-chunk/package.json index 9039a274f9..0c2e4f21f8 100644 --- a/packages/turf-line-chunk/package.json +++ b/packages/turf-line-chunk/package.json @@ -67,6 +67,7 @@ "tape": "^5.9.0", "tsup": "^8.4.0", "tsx": "^4.19.4", + "typescript": "^5.8.3", "write-json-file": "^6.0.0" }, "dependencies": { @@ -74,6 +75,7 @@ "@turf/length": "workspace:*", "@turf/line-slice-along": "workspace:*", "@turf/meta": "workspace:*", - "@types/geojson": "^7946.0.10" + "@types/geojson": "^7946.0.10", + "tslib": "^2.8.1" } } diff --git a/packages/turf-line-chunk/test.ts b/packages/turf-line-chunk/test.ts index 8ec0d013ae..45a61f8de4 100644 --- a/packages/turf-line-chunk/test.ts +++ b/packages/turf-line-chunk/test.ts @@ -6,8 +6,14 @@ import { loadJsonFileSync } from "load-json-file"; import { writeJsonFileSync } from "write-json-file"; import { truncate } from "@turf/truncate"; import { featureEach } from "@turf/meta"; -import { lineString, featureCollection } from "@turf/helpers"; +import { + lineString, + featureCollection, + point, + geometryCollection, +} from "@turf/helpers"; import { lineChunk } from "./index.js"; +import { Feature } from "geojson"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -17,7 +23,10 @@ const directories = { }; const fixtures = fs.readdirSync(directories.in).map((filename) => { - return { filename, geojson: loadJsonFileSync(directories.in + filename) }; + return { + filename, + geojson: loadJsonFileSync(directories.in + filename) as any, + }; }); test("turf-line-chunk: shorter", (t) => { @@ -103,8 +112,8 @@ test("turf-line-chunk: Prevent input mutation", (t) => { * @param {FeatureCollection|Feature} geojson Feature or FeatureCollection * @returns {FeatureCollection} colorized FeatureCollection */ -function colorize(geojson) { - const results = []; +function colorize(geojson: any) { + const results: Feature[] = []; featureEach(geojson, (feature, index) => { const r = index % 2 === 0 ? "F" : "0"; const g = index % 2 === 0 ? "0" : "0"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index be8b847c37..5c2f7fb676 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3625,6 +3625,9 @@ importers: '@types/geojson': specifier: ^7946.0.10 version: 7946.0.14 + tslib: + specifier: ^2.8.1 + version: 2.8.1 devDependencies: '@turf/truncate': specifier: workspace:* @@ -3650,6 +3653,9 @@ importers: tsx: specifier: ^4.19.4 version: 4.19.4 + typescript: + specifier: ^5.8.3 + version: 5.8.3 write-json-file: specifier: ^6.0.0 version: 6.0.0