From eb9de6486cdb245203569ca2cd9757ed5967a142 Mon Sep 17 00:00:00 2001 From: mfedderly <24275386+mfedderly@users.noreply.github.com> Date: Thu, 18 Dec 2025 07:52:43 -0500 Subject: [PATCH] @turf/line-slice to TypeScript (#2979) --- packages/turf-line-slice/bench.ts | 37 +++++++++++-------- packages/turf-line-slice/index.d.ts | 14 ------- .../turf-line-slice/{index.js => index.ts} | 31 +++++++++------- packages/turf-line-slice/package.json | 4 +- packages/turf-line-slice/test.ts | 13 +++++-- pnpm-lock.yaml | 6 +++ 6 files changed, 57 insertions(+), 48 deletions(-) delete mode 100644 packages/turf-line-slice/index.d.ts rename packages/turf-line-slice/{index.js => index.ts} (66%) diff --git a/packages/turf-line-slice/bench.ts b/packages/turf-line-slice/bench.ts index 133539d98..66bdf4c5e 100644 --- a/packages/turf-line-slice/bench.ts +++ b/packages/turf-line-slice/bench.ts @@ -1,35 +1,42 @@ -import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; import Benchmark from "benchmark"; import { point } from "@turf/helpers"; import { lineSlice } from "./index.js"; +import { loadJsonFileSync } from "load-json-file"; +import { Feature, FeatureCollection, LineString } from "geojson"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); -var route1 = JSON.parse(fs.readFileSync(__dirname + "/test/in/route1.geojson")); -var route2 = JSON.parse(fs.readFileSync(__dirname + "/test/in/route2.geojson")); -var line1 = JSON.parse(fs.readFileSync(__dirname + "/test/in/line1.geojson")); +const route1: FeatureCollection = loadJsonFileSync( + path.join(__dirname, "test", "in", "route1.geojson") +); +const route2: FeatureCollection = loadJsonFileSync( + path.join(__dirname, "test", "in", "route2.geojson") +); +const line1: FeatureCollection = loadJsonFileSync( + path.join(__dirname, "test", "in", "line1.geojson") +); -var start1 = point([-97.79617309570313, 22.254624939561698]); -var stop1 = point([-97.72750854492188, 22.057641623615734]); -var start2 = point([-79.0850830078125, 37.60117623656667]); -var stop2 = point([-77.7667236328125, 38.65119833229951]); -var start3 = point([-112.60660171508789, 45.96021963947196]); -var stop3 = point([-111.97265625, 48.84302835299516]); +const start1 = point([-97.79617309570313, 22.254624939561698]); +const stop1 = point([-97.72750854492188, 22.057641623615734]); +const start2 = point([-79.0850830078125, 37.60117623656667]); +const stop2 = point([-77.7667236328125, 38.65119833229951]); +const start3 = point([-112.60660171508789, 45.96021963947196]); +const stop3 = point([-111.97265625, 48.84302835299516]); -var suite = new Benchmark.Suite("turf-line-slice"); +const suite = new Benchmark.Suite("turf-line-slice"); suite .add("turf-line-slice#simple", function () { - lineSlice(start1, stop1, line1); + lineSlice(start1, stop1, line1.features[0] as Feature); }) .add("turf-line-slice#route1", function () { - lineSlice(start2, stop2, route1); + lineSlice(start2, stop2, route1.features[0] as Feature); }) .add("turf-line-slice#route2", function () { - lineSlice(start3, stop3, route2); + lineSlice(start3, stop3, route2.features[0] as Feature); }) - .on("cycle", function (event) { + .on("cycle", function (event: any) { console.log(String(event.target)); }) .run(); diff --git a/packages/turf-line-slice/index.d.ts b/packages/turf-line-slice/index.d.ts deleted file mode 100644 index f01ebd861..000000000 --- a/packages/turf-line-slice/index.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Feature, LineString } from "geojson"; -import { Coord } from "@turf/helpers"; - -/** - * http://turfjs.org/docs/#lineslice - */ -declare function lineSlice( - startPt: Coord, - stopPt: Coord, - line: Feature | LineString -): Feature; - -export { lineSlice }; -export default lineSlice; diff --git a/packages/turf-line-slice/index.js b/packages/turf-line-slice/index.ts similarity index 66% rename from packages/turf-line-slice/index.js rename to packages/turf-line-slice/index.ts index b4fdbc7b7..0c30b9673 100644 --- a/packages/turf-line-slice/index.js +++ b/packages/turf-line-slice/index.ts @@ -1,6 +1,7 @@ import { getCoords, getType } from "@turf/invariant"; -import { lineString as linestring } from "@turf/helpers"; +import { Coord, lineString as linestring } from "@turf/helpers"; import { nearestPointOnLine } from "@turf/nearest-point-on-line"; +import { Feature, LineString } from "geojson"; /** * Takes a {@link LineString|line}, a start {@link Point}, and a stop point @@ -31,30 +32,32 @@ import { nearestPointOnLine } from "@turf/nearest-point-on-line"; * //addToMap * var addToMap = [start, stop, line] */ -function lineSlice(startPt, stopPt, line) { +function lineSlice( + startPt: Coord, + stopPt: Coord, + line: Feature | LineString +): Feature { // Validation - var coords = getCoords(line); + const coords = getCoords(line); if (getType(line) !== "LineString") throw new Error("line must be a LineString"); - var startVertex = nearestPointOnLine(line, startPt); - var stopVertex = nearestPointOnLine(line, stopPt); - var ends; - if (startVertex.properties.index <= stopVertex.properties.index) { - ends = [startVertex, stopVertex]; - } else { - ends = [stopVertex, startVertex]; - } - var clipCoords = [ends[0].geometry.coordinates]; + const startVertex = nearestPointOnLine(line, startPt); + const stopVertex = nearestPointOnLine(line, stopPt); + const ends = + startVertex.properties.index <= stopVertex.properties.index + ? [startVertex, stopVertex] + : [stopVertex, startVertex]; + const clipCoords = [ends[0].geometry.coordinates]; for ( - var i = ends[0].properties.index + 1; + let i = ends[0].properties.index + 1; i < ends[1].properties.index + 1; i++ ) { clipCoords.push(coords[i]); } clipCoords.push(ends[1].geometry.coordinates); - return linestring(clipCoords, line.properties); + return linestring(clipCoords, line.type === "Feature" ? line.properties : {}); } export { lineSlice }; diff --git a/packages/turf-line-slice/package.json b/packages/turf-line-slice/package.json index ee1192828..fe328983a 100644 --- a/packages/turf-line-slice/package.json +++ b/packages/turf-line-slice/package.json @@ -62,12 +62,14 @@ "tape": "^5.9.0", "tsup": "^8.4.0", "tsx": "^4.19.4", + "typescript": "^5.8.3", "write-json-file": "^6.0.0" }, "dependencies": { "@turf/helpers": "workspace:*", "@turf/invariant": "workspace:*", "@turf/nearest-point-on-line": "workspace:*", - "@types/geojson": "^7946.0.10" + "@types/geojson": "^7946.0.10", + "tslib": "^2.8.1" } } diff --git a/packages/turf-line-slice/test.ts b/packages/turf-line-slice/test.ts index 07d10a148..7fbc1b50b 100644 --- a/packages/turf-line-slice/test.ts +++ b/packages/turf-line-slice/test.ts @@ -7,6 +7,7 @@ import { writeJsonFileSync } from "write-json-file"; import { truncate } from "@turf/truncate"; import { featureCollection, point, lineString } from "@turf/helpers"; import { lineSlice } from "./index.js"; +import { Feature, FeatureCollection, LineString, Point } from "geojson"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -19,16 +20,20 @@ const fixtures = fs.readdirSync(directories.in).map((filename) => { return { filename, name: path.parse(filename).name, - geojson: loadJsonFileSync(directories.in + filename), + geojson: loadJsonFileSync(directories.in + filename) as FeatureCollection, }; }); test("turf-line-slice", (t) => { for (const { filename, geojson, name } of fixtures) { - const [linestring, start, stop] = geojson.features; + const [linestring, start, stop] = geojson.features as [ + Feature, + Feature, + Feature, + ]; const sliced = truncate(lineSlice(start, stop, linestring)); - sliced.properties["stroke"] = "#f0f"; - sliced.properties["stroke-width"] = 6; + sliced.properties!["stroke"] = "#f0f"; + sliced.properties!["stroke-width"] = 6; const results = featureCollection(geojson.features); results.features.push(sliced); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 88bcdd7ea..5c55bab49 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3876,6 +3876,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:* @@ -3901,6 +3904,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