Skip to content

Commit 0b148ee

Browse files
committed
added sort
1 parent 5dfdb7d commit 0b148ee

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Bounding boxes, or rectangular extents, are represented as an array of 4 numbers
2626
- [reproject](#reproject)
2727
- [scale](#scale)
2828
- [shift](#shift)
29+
- [sort](#sort)
2930
- [split](#split)
3031
- [union](#union)
3132
- [unwrap](#unwrap)
@@ -327,6 +328,18 @@ reproject(bbox, forwardAsync, { async: true })
327328
reproject(bbox, forward, { density: 99 })
328329
```
329330

331+
### sort
332+
```js
333+
import sort from "bbox-fns/sort.js";
334+
335+
const bboxes = [
336+
[175, -85, 180, 90],
337+
[-180, -85, -175, 90]
338+
];
339+
sort(bboxes)
340+
[[-180,-85,-175,90],[175,-85,180,90]]
341+
```
342+
330343
### union
331344
Combine all bounding boxes that intersect.
332345
This is different from merge, which will combine bounding boxes even if they don't intersect.

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const preciseReproject = require("./precise/reproject.js");
2121
const reproject = require("./reproject.js");
2222
const scale = require("./scale.js");
2323
const shift = require("./shift.js");
24+
const sort = require("./sort.js");
2425
const split = require("./split.js");
2526
const validate = require("./validate.js");
2627
const preciseValidate = require("./precise/validate.js");
@@ -52,6 +53,7 @@ const bboxfns = {
5253
split,
5354
validate,
5455
preciseValidate,
56+
sort,
5557
union,
5658
unwrap
5759
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"precise/reproject.js",
3030
"precise/validate.js",
3131
"shift.js",
32+
"sort.js",
3233
"split.js",
3334
"union.js",
3435
"unwrap.js",

sort.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function sort(bboxes) {
2+
return bboxes.sort((a, b) => {
3+
const [axmin, aymin, axmax, aymax] = a;
4+
const [bxmin, bymin, bxmax, bymax] = b;
5+
6+
if (axmin < bxmin) return -1;
7+
if (axmin > bxmin) return 1;
8+
if (aymin < bymin) return 1;
9+
if (aymin > bymin) return -1;
10+
11+
return 0;
12+
});
13+
}
14+
15+
module.exports = sort;
16+
module.exports.default = sort;

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
preciseReproject,
4646
scale,
4747
shift,
48+
sort,
4849
split,
4950
preciseDivide,
5051
validate,
@@ -711,6 +712,17 @@ test("preciseValidate", ({ eq }) => {
711712
eq(preciseValidate(["-45", "10", "-90", "20"]), false);
712713
});
713714

715+
test("sort", ({ eq }) => {
716+
const bboxes = [
717+
[175, -85, 180, 90],
718+
[-180, -85, -175, 90]
719+
];
720+
eq(sort(bboxes), [
721+
[-180, -85, -175, 90],
722+
[175, -85, 180, 90]
723+
]);
724+
});
725+
714726
test("union", ({ eq }) => {
715727
const wyoming = [-110.99, 40.97, -104.08, 45.03];
716728
const usa = [-125.1, 24.75, -66, 49.54];

unwrap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use_strict";
22

33
const shift = require("./shift.js");
4+
const sort = require("./sort.js");
45
const split = require("./split.js");
56
const union = require("./union.js");
67

@@ -39,6 +40,8 @@ function unwrap(bbox, container) {
3940
// combine bboxes if they overlap
4041
bboxes = union(bboxes);
4142

43+
bboxes = sort(bboxes);
44+
4245
return bboxes;
4346
}
4447

0 commit comments

Comments
 (0)