James Beard e0bdd0add8
Converting packages to type: module by default (#2565)
Updating Turf packags to be ESM modules rather than a CommonJS project. The build still generates and packages both ESM and CJS variants of implementation and types. From a client perspective, nothing should change about the way Turf modules are loaded or called.


* Marking all packages as "ESM first", with additional CJS files for compatibility. Flow on changes from that include requiring a js extension for relative includes, and workarounds for a couple of older third party libraries that were no longer importing smoothly. Builds successfully. Updating tests next.

* Adding js extension to more local imports in tests, and removing references to __dirname. Added some re-exporting wrapper files for older packages like rbush and sweepline-intersections. See turf-line-intersects for an example.

* Disabling some type tests in packages we were having export / import issues in post type: module. Maybe someone in future can exercise some typescript jujitsu to re-enable them.

* Reverting dist/ generated JS to .js extension where possible. Was identified that the .cjs extension was causing problems for some consumers. Would have like to use .d.ts rather .d.cts for CJS type defs, but there's a but in tsup at the moment that won't allow that to be customised.

* Reintroducing __dirname references, though need to add implementation as not supplied automatically by node any more. Updating benchmark tests. Adding some missing bench and tape @types libraries.

* Added a few __dirname references that should have been included, and removed a few that had inadvertently been added.

* Explicitly using cjs extension for CommonJS distributed source files. Got disabled types tests working again by adjusting tsc command (needed to include node16 module and moduleResolution switched on command line). Also needed to refer to "index", etc as "index.js", etc from test.ts files to avoid "Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16'" errors.

* Missed a .js -> .cjs in turf/turf last-checks target.

* Removing hacky cjs/esm export/import wrappers in favour of defaultImport library which takes care of finding the correct default to import from CJS packages.

* Revert "Removing hacky cjs/esm export/import wrappers in favour of defaultImport library which takes care of finding the correct default to import from CJS packages."

This reverts commit f8184c3fb124369b36fb9afa4e1aa83543417e42.

* Found a few more spots to add a js extension to satisfy newer node16 module resolution.
2024-03-12 10:41:26 +11:00

77 lines
1.8 KiB
TypeScript

import { glob } from "glob";
import path from "path";
import { fileURLToPath } from "url";
import test from "tape";
import { loadJsonFileSync } from "load-json-file";
import { lineString } from "@turf/helpers";
import { booleanClockwise as isClockwise } from "./index.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test("isClockwise#fixtures", (t) => {
// True Fixtures
glob
.sync(path.join(__dirname, "test", "true", "*.geojson"))
.forEach((filepath) => {
const name = path.parse(filepath).name;
const geojson = loadJsonFileSync(filepath);
const feature = geojson.features[0];
t.true(isClockwise(feature), "[true] " + name);
});
// False Fixtures
glob
.sync(path.join(__dirname, "test", "false", "*.geojson"))
.forEach((filepath) => {
const name = path.parse(filepath).name;
const geojson = loadJsonFileSync(filepath);
const feature = geojson.features[0];
t.false(isClockwise(feature), "[false] " + name);
});
t.end();
});
test("isClockwise", (t) => {
const cwArray = [
[0, 0],
[1, 1],
[1, 0],
[0, 0],
];
const ccwArray = [
[0, 0],
[1, 0],
[1, 1],
[0, 0],
];
t.equal(isClockwise(cwArray), true, "[true] clockwise array input");
t.equal(
isClockwise(ccwArray),
false,
"[false] counter-clockwise array input"
);
t.end();
});
test("isClockwise -- Geometry types", (t) => {
const line = lineString([
[0, 0],
[1, 1],
[1, 0],
[0, 0],
]);
t.equal(isClockwise(line), true, "Feature");
t.equal(isClockwise(line.geometry), true, "Geometry Object");
t.end();
});
// test('isClockwise -- throws', t => {
// const pt = point([-10, -33]);
// t.throws(() => isClockwise(pt), 'feature geometry not supported');
// t.end();
// });