Skip to content

Commit

Permalink
Converted turf-square to Typescript (#2648)
Browse files Browse the repository at this point in the history
* Converted turf-square to Typescript.
  • Loading branch information
smallsaucepan authored Jul 9, 2024
1 parent c9c0a4c commit 5940110
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
6 changes: 3 additions & 3 deletions packages/turf-square/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ would contain the input.
### Examples

```javascript
var bbox = [-20, -20, -15, 0];
var squared = turf.square(bbox);
const bbox = [-20, -20, -15, 0];
const squared = turf.square(bbox);

//addToMap
var addToMap = [turf.bboxPolygon(bbox), turf.bboxPolygon(squared)]
const addToMap = [turf.bboxPolygon(bbox), turf.bboxPolygon(squared)]
```

Returns **[BBox][1]** a square surrounding `bbox`
Expand Down
9 changes: 5 additions & 4 deletions packages/turf-square/bench.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Benchmark from "benchmark";
import { BBox } from "geojson";
import Benchmark, { Event } from "benchmark";
import { square } from "./index.js";

var bbox = [0, 0, 5, 10];
const bbox: BBox = [0, 0, 5, 10];

var suite = new Benchmark.Suite("turf-square");
const suite = new Benchmark.Suite("turf-square");
suite
.add("turf-square", function () {
square(bbox);
})
.on("cycle", function (event) {
.on("cycle", function (event: Event) {
console.log(String(event.target));
})
.run();
9 changes: 0 additions & 9 deletions packages/turf-square/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { distance } from "@turf/distance";
import { BBox } from "geojson";

/**
* Takes a bounding box and calculates the minimum square bounding box that
Expand All @@ -8,13 +9,13 @@ import { distance } from "@turf/distance";
* @param {BBox} bbox extent in [west, south, east, north] order
* @returns {BBox} a square surrounding `bbox`
* @example
* var bbox = [-20, -20, -15, 0];
* var squared = turf.square(bbox);
* const bbox = [-20, -20, -15, 0];
* const squared = turf.square(bbox);
*
* //addToMap
* var addToMap = [turf.bboxPolygon(bbox), turf.bboxPolygon(squared)]
* const addToMap = [turf.bboxPolygon(bbox), turf.bboxPolygon(squared)]
*/
function square(bbox) {
function square(bbox: BBox): BBox {
var west = bbox[0];
var south = bbox[1];
var east = bbox[2];
Expand Down
9 changes: 7 additions & 2 deletions packages/turf-square/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@
"test:tape": "tsx test.ts"
},
"devDependencies": {
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.2.32",
"benchmark": "^2.1.4",
"npm-run-all": "^4.1.5",
"tape": "^5.7.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2"
"tsx": "^4.6.2",
"typescript": "^5.2.2"
},
"dependencies": {
"@turf/distance": "workspace:^",
"@turf/helpers": "workspace:^"
"@turf/helpers": "workspace:^",
"@types/geojson": "7946.0.8",
"tslib": "^2.6.2"
}
}
9 changes: 5 additions & 4 deletions packages/turf-square/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BBox } from "geojson";
import test from "tape";
import { square } from "./index.js";

test("square", function (t) {
var bbox1 = [0, 0, 5, 10];
var bbox2 = [0, 0, 10, 5];
const bbox1: BBox = [0, 0, 5, 10];
const bbox2: BBox = [0, 0, 10, 5];

var sq1 = square(bbox1);
var sq2 = square(bbox2);
const sq1 = square(bbox1);
const sq2 = square(bbox2);

t.deepEqual(sq1, [-2.5, 0, 7.5, 10]);
t.deepEqual(sq2, [0, -2.5, 10, 7.5]);
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 5940110

Please sign in to comment.