Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .monorepolint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export default {
options: {
scripts: {
bench: "node bench.ts",
"test:node": "node --test",
"test:node": "node --test test.ts",
"test:tape": REMOVE,
},
},
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"@monorepolint/config": "0.6.0-alpha.6",
"@monorepolint/core": "0.6.0-alpha.6",
"@monorepolint/rules": "0.6.0-alpha.6",
"@types/benchmark": "^2.1.5",
"@types/node": "22.15.3",
"@types/benchmark": "catalog:",
"@types/node": "catalog:",
"acorn": "^8.14.1",
"benchmark": "^2.1.4",
"benchmark": "catalog:",
"camelcase": "^8.0.0",
"d3-queue": "*",
"decamelize": "^6.0.0",
Expand All @@ -59,8 +59,8 @@
"meow": "^13.2.0",
"prettier": "^3.5.3",
"progress": "^2.0.3",
"tsup": "^8.4.0",
"typescript": "^5.8.3",
"tsup": "catalog:",
"typescript": "catalog:",
"typescript-eslint": "^8.38.0",
"yaml": "^2.8.2"
}
Expand Down
35 changes: 5 additions & 30 deletions packages/turf-area/bench.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { area } from "./index.js";
import { area } from "./index.ts";
import { benchFixtures } from "../../support/benchFixtures.mts";

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

// Define fixtures
const directory = path.join(__dirname, "test", "in") + path.sep;
const fixtures = fs.readdirSync(directory).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directory + filename),
};
});

/**
* Benmark Results
*
* polygon x 8,510,024 ops/sec ±0.28% (96 runs sampled)
*/

// Define benchmark
const suite = new Benchmark.Suite("turf-area");
for (const { name, geojson } of fixtures) {
suite.add(name, () => area(geojson));
}
suite.on("cycle", (e) => console.log(String(e.target))).run();
// Benchmark Results
// polygon.geojson x 22,944,912 ops/sec ±0.28% (98 runs sampled)
await benchFixtures("turf-area", (input) => area(input));
2 changes: 1 addition & 1 deletion packages/turf-area/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature, FeatureCollection, Geometry } from "geojson";
import type { Feature, FeatureCollection, Geometry } from "geojson";
import { earthRadius } from "@turf/helpers";
import { geomReduce } from "@turf/meta";

Expand Down
13 changes: 3 additions & 10 deletions packages/turf-area/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,14 @@
"dist"
],
"scripts": {
"bench": "tsx bench.ts",
"bench": "node bench.ts",
"build": "tsup --config ../../tsup.config.ts",
"test": "pnpm run /test:.*/",
"test:tape": "tsx test.ts"
"test:node": "node --test test.ts"
},
"devDependencies": {
"@types/benchmark": "catalog:",
"@types/tape": "catalog:",
"benchmark": "catalog:",
"load-json-file": "^7.0.1",
"tape": "catalog:",
"tsup": "catalog:",
"tsx": "catalog:",
"typescript": "catalog:",
"write-json-file": "^6.0.0"
"typescript": "catalog:"
},
"dependencies": {
"@turf/helpers": "workspace:*",
Expand Down
52 changes: 13 additions & 39 deletions packages/turf-area/test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import fs from "fs";
import test from "tape";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { area } from "./index.js";
import test from "node:test";
import { area } from "./index.ts";
import { polygon } from "@turf/helpers";
import { Polygon } from "geojson";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
import type { Polygon } from "geojson";
import { testFixtures } from "../../support/testFixtures.mts";
import assert from "assert";

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
out: path.join(__dirname, "test", "out") + path.sep,
};

const fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directories.in + filename),
};
});

test("turf-area", (t) => {
for (const fixture of fixtures) {
const name = fixture.name;
const geojson = fixture.geojson;
const results = Math.round(area(geojson));
if (process.env.REGEN)
writeJsonFileSync(directories.out + name + ".json", results);
t.equal(results, loadJsonFileSync(directories.out + name + ".json"), name);
}
t.end();
await test("area fixtures", async (t) => {
await testFixtures(t, (geojson) => {
return Math.round(area(geojson));
});
});

test("turf-area-length-check", (t) => {
test("turf-area -- length-check", () => {
const invalidPoly: Polygon = {
type: "Polygon",
coordinates: [
Expand All @@ -46,12 +23,10 @@ test("turf-area-length-check", (t) => {
],
};
const result = area(invalidPoly);
t.equal(result, 0);

t.end();
assert.strictEqual(result, 0);
});

test("turf-area-rotation-consistency", (t) => {
test("turf-area rotation-consistency", () => {
const rotatingPoly = polygon([
[
[28.321755510202507, 16.35627490376781],
Expand Down Expand Up @@ -84,7 +59,6 @@ test("turf-area-rotation-consistency", (t) => {
rotatingPoly.geometry.coordinates[0][i];

const curResult = area(changingPoly);
t.equal(result, curResult);
assert.strictEqual(result, curResult);
}
t.end();
});
50 changes: 14 additions & 36 deletions packages/turf-bbox-clip/bench.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { bbox } from "@turf/bbox";
import { bboxClip } from "./index.js";
import { bboxClip } from "./index.ts";
import { benchFixtures } from "../../support/benchFixtures.mts";

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

const directory = path.join(__dirname, "test", "in") + path.sep;
const fixtures = fs.readdirSync(directory).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directory + filename),
};
});

/**
* Benchmark Results
*
* linestring-single-line x 1,065,073 ops/sec ±1.11% (90 runs sampled)
* linestring x 56,599 ops/sec ±1.17% (90 runs sampled)
* multi-linestring x 859,048 ops/sec ±1.01% (91 runs sampled)
* multi-polygon x 26,991 ops/sec ±0.87% (94 runs sampled)
* polygon-crossing-hole x 25,277 ops/sec ±0.72% (92 runs sampled)
* polygon-holes x 27,233 ops/sec ±0.89% (91 runs sampled)
* polygon x 21,339 ops/sec ±1.19% (89 runs sampled)
*/
const suite = new Benchmark.Suite("turf-bbox-clip");
for (const { name, geojson } of fixtures) {
suite.add(name, () =>
bboxClip(geojson.features[0], bbox(geojson.features[1]))
);
}

suite.on("cycle", (e) => console.log(String(e.target))).run();
// Benchmark Results
// linestring-single-line.geojson x 15,871,930 ops/sec ±0.24% (98 runs sampled)
// linestring.geojson x 264,962 ops/sec ±1.14% (96 runs sampled)
// multi-linestring.geojson x 7,892,547 ops/sec ±0.35% (95 runs sampled)
// multi-polygon.geojson x 138,989 ops/sec ±0.31% (98 runs sampled)
// polygon-crossing-hole.geojson x 151,793 ops/sec ±0.35% (95 runs sampled)
// polygon-holes.geojson x 142,335 ops/sec ±0.30% (96 runs sampled)
// polygon-point-intersection.geojson x 8,580,181 ops/sec ±0.73% (94 runs sampled)
// polygon.geojson x 117,120 ops/sec ±0.18% (99 runs sampled)
await benchFixtures("turf-bbox-clip", (input) =>
bboxClip(input.features[0], bbox(input.features[1]))
);
4 changes: 2 additions & 2 deletions packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
BBox,
Feature,
LineString,
Expand All @@ -15,7 +15,7 @@ import {
polygon,
} from "@turf/helpers";
import { getGeom } from "@turf/invariant";
import { lineclip, polygonclip } from "./lib/lineclip.js";
import { lineclip, polygonclip } from "./lib/lineclip.ts";

/**
* Takes a {@link Feature} and a bbox and clips the feature to the bbox using
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-bbox-clip/lib/lineclip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Cohen-Sutherland line clipping algorithm, adapted to efficiently
// handle polylines rather than just segments
import { BBox } from "geojson";
import type { BBox } from "geojson";

export function lineclip(
points: number[][],
Expand Down
13 changes: 3 additions & 10 deletions packages/turf-bbox-clip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,15 @@
"dist"
],
"scripts": {
"bench": "tsx bench.ts",
"bench": "node bench.ts",
"build": "tsup --config ../../tsup.config.ts",
"test": "pnpm run /test:.*/",
"test:tape": "tsx test.ts"
"test:node": "node --test test.ts"
},
"devDependencies": {
"@turf/bbox": "workspace:*",
"@types/benchmark": "catalog:",
"@types/tape": "catalog:",
"benchmark": "catalog:",
"load-json-file": "^7.0.1",
"tape": "catalog:",
"tsup": "catalog:",
"tsx": "catalog:",
"typescript": "catalog:",
"write-json-file": "^6.0.0"
"typescript": "catalog:"
},
"dependencies": {
"@turf/helpers": "workspace:*",
Expand Down
67 changes: 18 additions & 49 deletions packages/turf-bbox-clip/test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,37 @@
import fs from "fs";
import test from "tape";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { point, feature, featureCollection } from "@turf/helpers";
import { bbox as turfBBox } from "@turf/bbox";
import { bboxClip } from "./index.js";
import { bboxClip } from "./index.ts";
import { testFixtures } from "../../support/testFixtures.mts";
import assert from "assert";
import test from "node:test";

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

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
out: path.join(__dirname, "test", "out") + path.sep,
};

const fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directories.in + filename),
};
});

test("turf-bbox-clip", (t) => {
for (const fixture of fixtures) {
const filename = fixture.filename;
const name = fixture.name;
const geojson = fixture.geojson;
await test("bboxClip features", async (t) => {
await testFixtures(t, (geojson) => {
const feature = geojson.features[0];
const bbox = turfBBox(geojson.features[1]);
const clipped = bboxClip(feature, bbox);
const results = featureCollection([
return featureCollection([
colorize(feature, "#080"),
colorize(clipped, "#F00"),
colorize(geojson.features[1], "#00F", 3),
]);
});
});

if (process.env.REGEN)
writeJsonFileSync(directories.out + filename, results);
t.deepEquals(results, loadJsonFileSync(directories.out + filename), name);
}
t.end();
test("turf-bbox-clip -- throws", () => {
assert.throws(() => {
bboxClip(point([5, 10]) as any, [-180, -90, 180, 90]);
}, /geometry Point not supported/);
});

test("turf-bbox-clip -- throws", (t) => {
t.throws(
() => bboxClip(point([5, 10]), [-180, -90, 180, 90]),
/geometry Point not supported/
test("turf-bbox-clip -- null geometries", () => {
assert.throws(
() => bboxClip(feature(null as any), [-180, -90, 180, 90]),
"coords must be GeoJSON Feature, Geometry Object or an Array"
);
t.end();
});

function colorize(feature, color, width) {
color = color || "#F00";
width = width || 6;
function colorize(feature: any, color = "#F00", width = 6) {
feature.properties = {
stroke: color,
fill: color,
Expand All @@ -63,11 +40,3 @@ function colorize(feature, color, width) {
};
return feature;
}

test("turf-bbox-clip -- null geometries", (t) => {
t.throws(
() => bboxClip(feature(null), [-180, -90, 180, 90]),
"coords must be GeoJSON Feature, Geometry Object or an Array"
);
t.end();
});
Loading
Loading