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

Converted turf-transform-translate to Typescript #2653

Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/turf-transform-translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ on the provided direction angle.

### Parameters

* `geojson` **[GeoJSON][1]** object to be translated
* `distance` **[number][2]** length of the motion; negative values determine motion in opposite direction
* `direction` **[number][2]** of the motion; angle from North in decimal degrees, positive clockwise
* `options` **[Object][3]** Optional parameters (optional, default `{}`)
* `geojson` **([GeoJSON][1] | [GeometryCollection][2])** object to be translated
* `distance` **[number][3]** length of the motion; negative values determine motion in opposite direction
* `direction` **[number][3]** of the motion; angle from North in decimal degrees, positive clockwise
* `options` **[Object][4]** Optional parameters (optional, default `{}`)

* `options.units` **[string][4]** in which `distance` will be express; miles, kilometers, degrees, or radians (optional, default `'kilometers'`)
* `options.zTranslation` **[number][2]** length of the vertical motion, same unit of distance (optional, default `0`)
* `options.units` **Units** in which `distance` will be express; miles, kilometers, degrees, or radians (optional, default `'kilometers'`)
* `options.zTranslation` **[number][3]** length of the vertical motion, same unit of distance (optional, default `0`)
* `options.mutate` **[boolean][5]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`)

### Examples
Expand All @@ -29,15 +29,15 @@ var addToMap = [poly, translatedPoly];
translatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
```

Returns **[GeoJSON][1]** the translated GeoJSON object
Returns **([GeoJSON][1] | [GeometryCollection][2])** the translated GeoJSON object

[1]: https://tools.ietf.org/html/rfc7946#section-3

[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.8

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

Expand Down
7 changes: 4 additions & 3 deletions packages/turf-transform-translate/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Feature } from "geojson";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import Benchmark, { Event } from "benchmark";
import { transformTranslate as translate } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand All @@ -11,7 +12,7 @@ const directory = path.join(__dirname, "test", "in") + path.sep;
const fixtures = fs.readdirSync(directory).map((filename) => {
return {
name: path.parse(filename).name,
geojson: loadJsonFileSync(directory + filename),
geojson: loadJsonFileSync(directory + filename) as Feature,
};
});

Expand Down Expand Up @@ -60,4 +61,4 @@ for (const { name, geojson } of fixtures) {
);
}

suite.on("cycle", (e) => console.log(String(e.target))).run();
suite.on("cycle", (e: Event) => console.log(String(e.target))).run();
18 changes: 0 additions & 18 deletions packages/turf-transform-translate/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GeoJSON, GeometryCollection } from "geojson";
import { coordEach } from "@turf/meta";
import { isObject } from "@turf/helpers";
import { isObject, Units } from "@turf/helpers";
import { getCoords } from "@turf/invariant";
import { clone } from "@turf/clone";
import { rhumbDestination } from "@turf/rhumb-destination";
Expand All @@ -9,14 +10,14 @@ import { rhumbDestination } from "@turf/rhumb-destination";
* on the provided direction angle.
*
* @name transformTranslate
* @param {GeoJSON} geojson object to be translated
* @param {GeoJSON|GeometryCollection} geojson object to be translated
* @param {number} distance length of the motion; negative values determine motion in opposite direction
* @param {number} direction of the motion; angle from North in decimal degrees, positive clockwise
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] in which `distance` will be express; miles, kilometers, degrees, or radians
* @param {Units} [options.units='kilometers'] in which `distance` will be express; miles, kilometers, degrees, or radians
* @param {number} [options.zTranslation=0] length of the vertical motion, same unit of distance
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
* @returns {GeoJSON} the translated GeoJSON object
* @returns {GeoJSON|GeometryCollection} the translated GeoJSON object
* @example
* var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
* var translatedPoly = turf.transformTranslate(poly, 100, 35);
Expand All @@ -25,7 +26,16 @@ import { rhumbDestination } from "@turf/rhumb-destination";
* var addToMap = [poly, translatedPoly];
* translatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
*/
function transformTranslate(geojson, distance, direction, options) {
function transformTranslate<T extends GeoJSON | GeometryCollection>(
geojson: T,
distance: number,
direction: number,
options?: {
units?: Units;
zTranslation?: number;
mutate?: boolean;
}
): T {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-transform-translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@
"tape": "^5.7.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"typescript": "^5.2.2",
"write-json-file": "^5.0.0"
},
"dependencies": {
"@turf/clone": "workspace:^",
"@turf/helpers": "workspace:^",
"@turf/invariant": "workspace:^",
"@turf/meta": "workspace:^",
"@turf/rhumb-destination": "workspace:^"
"@turf/rhumb-destination": "workspace:^",
"@types/geojson": "7946.0.8",
"tslib": "^2.6.2"
}
}
15 changes: 12 additions & 3 deletions packages/turf-transform-translate/test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Feature } from "geojson";
import fs from "fs";
import test from "tape";
import path from "path";
Expand All @@ -24,7 +25,7 @@ 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 Feature,
};
});

Expand All @@ -37,7 +38,7 @@ test("translate", (t) => {
zTranslation,
});
const result = featureCollection([
colorize(truncate(translated, { precision: 6, coordiantes: 3 })),
colorize(truncate(translated, { precision: 6, coordinates: 3 })),
geojson,
]);

Expand All @@ -56,11 +57,16 @@ test("translate", (t) => {
test("translate -- throws", (t) => {
const pt = point([-70.823364, -33.553984]);

// @ts-expect-error intentional invalid parameter
t.throws(() => translate(null, 100, -29), "missing geojson");
// @ts-expect-error intentional invalid parameter
t.throws(() => translate(pt, null, 98), "missing distance");
// @ts-expect-error intentional invalid parameter
t.throws(() => translate(pt, 23, null), "missing direction");
// @ts-expect-error intentional invalid parameter
t.throws(() => translate(pt, 56, 57, { units: "foo" }), "invalid units");
t.throws(
// @ts-expect-error intentional invalid parameter
() => translate(pt, 56, 57, { zTranslation: "foo" }),
"invalid zTranslation"
);
Expand Down Expand Up @@ -130,7 +136,10 @@ test("rotate -- geometry support", (t) => {
});

// style result
function colorize(geojson) {
function colorize(geojson: Feature) {
// We are going to add some properties, so make sure properties attribute is
// present.
geojson.properties = geojson.properties ?? {};
if (
geojson.geometry.type === "Point" ||
geojson.geometry.type === "MultiPoint"
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading