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

Convert tests to Typescript, change require to import, and add named exports to modules #2550

Merged
merged 10 commits into from
Dec 5, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
30 changes: 4 additions & 26 deletions .monorepolint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,11 @@ module.exports = {
{
options: {
scripts: {
"test:tape": "tsx test.js",
bench: "tsx bench.ts",
"test:tape": "tsx test.ts",
},
},
includePackages: JS_TAPE_PACKAGES,
},
{
options: {
scripts: {
"test:tape": "tsx test.js",
},
},
includePackages: TS_TAPE_PACKAGES,
},
{
options: {
scripts: {
bench: "tsx bench.js",
},
},
includePackages: JS_TAPE_PACKAGES,
},
{
options: {
scripts: {
bench: "tsx bench.js",
},
},
includePackages: TS_TAPE_PACKAGES,
includePackages: [...TS_TAPE_PACKAGES, ...JS_TAPE_PACKAGES],
},
{
options: {
Expand Down Expand Up @@ -263,6 +240,7 @@ module.exports = {
tslib: "^2.6.2",
},
devDependencies: {
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
typescript: "^5.2.2",
},
Expand Down
21 changes: 11 additions & 10 deletions packages/turf-along/bench.js → packages/turf-along/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require("fs");
const Benchmark = require("benchmark");
const along = require("./index").default;
import fs from "fs";
import Benchmark from "benchmark";
import { along } from "./index";
import { Feature, LineString } from "geojson";

const line = {
const line: Feature<LineString> = {
type: "Feature",
properties: {},
geometry: {
Expand All @@ -19,25 +20,25 @@ const line = {
};

const route = JSON.parse(
fs.readFileSync(__dirname + "/test/fixtures/route.geojson")
fs.readFileSync(__dirname + "/test/fixtures/route.geojson").toString()
);

const suite = new Benchmark.Suite("turf-along");
suite
.add("turf-along", function () {
along(line, 1, "miles");
along(line, 1, { units: "miles" });
})
.add("turf-along#route 1 mile", function () {
along(route, 1, "miles");
along(route, 1, { units: "miles" });
})
.add("turf-along#route 10 miles", function () {
along(route, 10, "miles");
along(route, 10, { units: "miles" });
})
.add("turf-along#route 50 miles", function () {
along(route, 50, "miles");
along(route, 50, { units: "miles" });
})
.add("turf-along#route 100 miles", function () {
along(route, 100, "miles");
along(route, 100, { units: "miles" });
})
.on("cycle", function (event) {
console.log(String(event.target));
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-along/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getGeom } from "@turf/invariant";
* //addToMap
* var addToMap = [along, line]
*/
export default function along(
function along(
line: Feature<LineString> | LineString,
distance: number,
options: { units?: Units } = {}
Expand Down Expand Up @@ -55,3 +55,6 @@ export default function along(
}
return point(coords[coords.length - 1]);
}

export { along };
export default along;
5 changes: 3 additions & 2 deletions packages/turf-along/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@
"dist"
],
"scripts": {
"bench": "tsx bench.js",
"bench": "tsx bench.ts",
"build": "npm-run-all --npm-path npm build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "tsx ../../scripts/generate-readmes.ts",
"test": "npm-run-all --npm-path npm test:*",
"test:tape": "tsx test.js"
"test:tape": "tsx test.ts"
},
"devDependencies": {
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"load-json-file": "^7.0.1",
Expand Down
15 changes: 8 additions & 7 deletions packages/turf-along/test.js → packages/turf-along/test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const path = require("path");
const test = require("tape");
const { loadJsonFileSync } = require("load-json-file");
const { featureCollection } = require("@turf/helpers");
const along = require("./index").default;
import path from "path";
import test from "tape";
import { loadJsonFileSync } from "load-json-file";
import { Units, featureCollection } from "@turf/helpers";
import { along } from "./index";
import { Feature, LineString } from "geojson";

const line = loadJsonFileSync(
const line: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "dc-line.geojson")
);

test("turf-along", (t) => {
const options = { units: "miles" };
const options: { units: Units } = { units: "miles" };
const pt1 = along(line, 1, options);
const pt2 = along(line.geometry, 1.2, options);
const pt3 = along(line, 1.4, options);
Expand Down
6 changes: 3 additions & 3 deletions packages/turf-angle/bench.js → packages/turf-angle/bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Benchmark = require("benchmark");
const angle = require("./index").default;
import Benchmark from "benchmark";
import { angle } from "./index";

/**
* Benchmark Results
Expand All @@ -9,7 +9,7 @@ const angle = require("./index").default;
*/
new Benchmark.Suite("turf-angle")
.add("angle", () => angle([5, 5], [5, 6], [3, 4]))
.add("angle -- meractor", () =>
.add("angle -- mercator", () =>
angle([5, 5], [5, 6], [3, 4], { mercator: true })
)
.on("cycle", (e) => console.log(String(e.target)))
Expand Down
1 change: 1 addition & 0 deletions packages/turf-angle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ function angle(
return angleAO;
}

export { angle };
export default angle;
5 changes: 3 additions & 2 deletions packages/turf-angle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@
"dist"
],
"scripts": {
"bench": "tsx bench.js",
"bench": "tsx bench.ts",
"build": "npm-run-all --npm-path npm build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "tsx ../../scripts/generate-readmes.ts",
"test": "npm-run-all --npm-path npm test:*",
"test:tape": "tsx test.js"
"test:tape": "tsx test.ts"
},
"devDependencies": {
"@turf/distance": "^7.0.0-alpha.2",
"@turf/sector": "^7.0.0-alpha.2",
"@turf/truncate": "^7.0.0-alpha.2",
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"glob": "^10.3.10",
Expand Down
27 changes: 11 additions & 16 deletions packages/turf-angle/test.js → packages/turf-angle/test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const test = require("tape");
const path = require("path");
const { glob } = require("glob");
const { loadJsonFileSync } = require("load-json-file");
const { writeJsonFileSync } = require("write-json-file");
const sector = require("@turf/sector").default;
const bearing = require("@turf/bearing").default;
const truncate = require("@turf/truncate").default;
const distance = require("@turf/distance").default;
const {
point,
round,
lineString,
featureCollection,
} = require("@turf/helpers");
const angle = require("./index").default;
import test from "tape";
import path from "path";
import { glob } from "glob";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import sector from "@turf/sector";
import { bearing } from "@turf/bearing";
import { truncate } from "@turf/truncate";
import { distance } from "@turf/distance";
import { point, round, lineString, featureCollection } from "@turf/helpers";
import { angle } from "./index";

test("turf-angle", (t) => {
glob
Expand Down
10 changes: 5 additions & 5 deletions packages/turf-area/bench.js → packages/turf-area/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require("fs");
const path = require("path");
const { loadJsonFileSync } = require("load-json-file");
const Benchmark = require("benchmark");
const area = require("./index").default;
import fs from "fs";
import path from "path";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { area } from "./index";

// Define fixtures
const directory = path.join(__dirname, "test", "in") + path.sep;
Expand Down
7 changes: 4 additions & 3 deletions packages/turf-area/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import { geomReduce } from "@turf/meta";
* var addToMap = [polygon]
* polygon.properties.area = area
*/
export default function area(
geojson: Feature<any> | FeatureCollection<any> | Geometry
) {
function area(geojson: Feature<any> | FeatureCollection<any> | Geometry) {
return geomReduce(
geojson,
(value, geom) => {
Expand Down Expand Up @@ -122,3 +120,6 @@ function ringArea(coords: number[][]): number {

return total * FACTOR;
}

export { area };
export default area;
5 changes: 3 additions & 2 deletions packages/turf-area/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@
"dist"
],
"scripts": {
"bench": "tsx bench.js",
"bench": "tsx bench.ts",
"build": "npm-run-all --npm-path npm build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "tsx ../../scripts/generate-readmes.ts",
"test": "npm-run-all --npm-path npm test:*",
"test:tape": "tsx test.js"
"test:tape": "tsx test.ts"
},
"devDependencies": {
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"load-json-file": "^7.0.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-area/test.js → packages/turf-area/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require("fs");
const test = require("tape");
const path = require("path");
const { loadJsonFileSync } = require("load-json-file");
const { writeJsonFileSync } = require("write-json-file");
const area = require("./index").default;
import fs from "fs";
import test from "tape";
import path from "path";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { area } from "./index";

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require("fs");
const path = require("path");
const { loadJsonFileSync } = require("load-json-file");
const Benchmark = require("benchmark");
const bbox = require("@turf/bbox").default;
const bboxClip = require("./index").default;
import fs from "fs";
import path from "path";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { bbox } from "@turf/bbox";
import { bboxClip } from "./index";

const directory = path.join(__dirname, "test", "in") + path.sep;
const fixtures = fs.readdirSync(directory).map((filename) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { lineclip, polygonclip } from "./lib/lineclip";
* //addToMap
* var addToMap = [bbox, poly, clipped]
*/
export default function bboxClip<
function bboxClip<
G extends Polygon | MultiPolygon | LineString | MultiLineString,
P extends GeoJsonProperties = GeoJsonProperties,
>(feature: Feature<G, P> | G, bbox: BBox) {
Expand Down Expand Up @@ -91,3 +91,6 @@ function clipPolygon(rings: number[][][], bbox: BBox) {
}
return outRings;
}

export { bboxClip };
export default bboxClip;
5 changes: 3 additions & 2 deletions packages/turf-bbox-clip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@
"dist"
],
"scripts": {
"bench": "tsx bench.js",
"bench": "tsx bench.ts",
"build": "npm-run-all --npm-path npm build:*",
"build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
"build:js": "tsc",
"docs": "tsx ../../scripts/generate-readmes.ts",
"test": "npm-run-all --npm-path npm test:*",
"test:tape": "tsx test.js"
"test:tape": "tsx test.ts"
},
"devDependencies": {
"@turf/bbox": "^7.0.0-alpha.2",
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"load-json-file": "^7.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require("fs");
const test = require("tape");
const path = require("path");
const { loadJsonFileSync } = require("load-json-file");
const { writeJsonFileSync } = require("write-json-file");
const { point, feature, featureCollection } = require("@turf/helpers");
const turfBBox = require("@turf/bbox").default;
const bboxClip = require("./index").default;
import fs from "fs";
import test from "tape";
import path from "path";
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";

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Benchmark = require("benchmark");
const bboxpolygon = require("./index").default;
import Benchmark from "benchmark";
import { bboxPolygon } from "./index";

/**
* Benchmark Results
*
* turf-bbox-polygon x 3,885,828 ops/sec ±1.20% (86 runs sampled)
*/
new Benchmark.Suite("turf-bbox-polygon")
.add("turf-bbox-polygon", () => bboxpolygon([0, 0, 10, 10]))
.add("turf-bbox-polygon", () => bboxPolygon([0, 0, 10, 10]))
.on("cycle", (e) => console.log(String(e.target)))
.run();
7 changes: 4 additions & 3 deletions packages/turf-bbox-polygon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import { polygon, Id } from "@turf/helpers";
* //addToMap
* var addToMap = [poly]
*/
export default function bboxPolygon<
P extends GeoJsonProperties = GeoJsonProperties,
>(
function bboxPolygon<P extends GeoJsonProperties = GeoJsonProperties>(
bbox: BBox,
options: {
properties?: P;
Expand Down Expand Up @@ -52,3 +50,6 @@ export default function bboxPolygon<
{ bbox, id: options.id }
);
}

export { bboxPolygon };
export default bboxPolygon;
Loading