Skip to content

Commit 14232e9

Browse files
authored
Merge pull request #16 from uber/upgrade-deps-types
* Adds TS type definitions * Cleans up tests for h3-js@3.7.1 * Upgrades dev dependencies in yarn.lock
2 parents b4f8cc9 + c630dde commit 14232e9

File tree

7 files changed

+955
-661
lines changed

7 files changed

+955
-661
lines changed

.travis.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
language: node_js
22
node_js:
3-
- '6'
3+
- '10'
4+
- '12'
5+
- '14'
46
before_script:
57
- yarn run dist-test
68
script:
7-
- yarn lint
8-
- yarn run test-es6
9-
- yarn run test-dist
10-
- yarn run check-prettier
11-
- yarn run check-docs
9+
- yarn test-ci
1210
- yarn cover
1311
after_success:
1412
- npm install -g coveralls

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ in a separate `Polygon` feature with optional properties.
142142
| Param | Type | Description |
143143
| --- | --- | --- |
144144
| hexagons | <code>Array.&lt;String&gt;</code> | Hexagon addresses |
145-
| [getProperties] | <code>function</code> | Optional function returning properties for a hexagon: f(hexAddress) => Object |
145+
| [getProperties] | <code>function</code> | Optional function returning properties for a hexagon: f(h3Index) => Object |
146146

147147

148148
* * *

package.json

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@
1515
},
1616
"main": "index.js",
1717
"es2015": "lib/geojson2h3.js",
18+
"types": "dist/types.d.ts",
1819
"dependencies": {
1920
"h3-js": "^3.6.1"
2021
},
2122
"devDependencies": {
23+
"@types/geojson": "^7946.0.7",
2224
"benchmark": "^2.1.4",
2325
"buble": "^0.19.3",
2426
"eslint": "^4.19.1",
2527
"eslint-config-prettier": "^2.9.0",
2628
"eslint-config-uber-es2015": "^3.1.2",
2729
"eslint-plugin-prettier": "^2.6.0",
28-
"faucet": "0.0.1",
30+
"faucet": "^0.0.1",
2931
"istanbul": "^0.4.3",
30-
"jsdoc-to-markdown": "^4.0.1",
32+
"jsdoc": "^3.6.6",
33+
"jsdoc-to-markdown": "^6.0.1",
3134
"prettier": "^1.12.1",
32-
"tape": "^4.8.0"
35+
"tape": "^4.8.0",
36+
"typescript": "^4.1.5"
3337
},
3438
"scripts": {
3539
"lint": "eslint src test",
@@ -38,19 +42,25 @@
3842
"test-raw": "node test/index.js",
3943
"test-es6": "yarn test-raw | faucet",
4044
"test-dist": "node dist/test/index.js | faucet",
45+
"test-ci": "yarn lint && yarn run test-es6 && yarn run test-dist && yarn run check-prettier && yarn run check-docs && yarn run check-types",
4146
"check-prettier": "yarn prettier && git diff --exit-code",
47+
"check-types": "tsc --strict --noEmit types.d.ts",
4248
"check-docs": "yarn build-docs && git diff --exit-code",
43-
"build-docs": "jsdoc2md --global-index-format grouped --separators --template doc-files/README.md.tmpl src/geojson2h3.js > README.md",
44-
"dist": "yarn dist-clean && buble -i src -o dist/src",
49+
"build-docs": "jsdoc2md --no-cache --global-index-format grouped --separators --template doc-files/README.md.tmpl src/geojson2h3.js > README.md",
50+
"dist": "yarn dist-clean && buble -i src -o dist/src && cp types.d.ts dist",
4551
"dist-clean": "rm -rf dist && mkdir dist",
4652
"dist-test": "yarn dist && buble -i test -o dist/test",
4753
"benchmarks": "yarn dist-test && node dist/test/benchmarks.js",
4854
"prepublish": "yarn dist",
49-
"prettier": "prettier --write --single-quote --no-bracket-spacing --print-width=100 'src/**/*.js' 'test/**/*.js'"
55+
"prettier": "prettier --write --single-quote --no-bracket-spacing --print-width=100 'src/**/*.js' 'test/**/*.js' '*.d.ts'"
5056
},
5157
"engines": {
5258
"node": ">=4",
5359
"npm": ">=3",
5460
"yarn": ">=1.3.0"
61+
},
62+
"volta": {
63+
"node": "12.19.0",
64+
"yarn": "1.22.10"
5565
}
5666
}

src/geojson2h3.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ function featureToH3Set(feature, resolution) {
109109
* @param {Object} [properties] Optional feature properties
110110
* @return {Feature} GeoJSON Feature object
111111
*/
112-
function h3ToFeature(hexAddress, properties = {}) {
112+
function h3ToFeature(h3Index, properties = {}) {
113113
// Wrap in an array for a single-loop polygon
114-
const coordinates = [h3.h3ToGeoBoundary(hexAddress, true)];
114+
const coordinates = [h3.h3ToGeoBoundary(h3Index, true)];
115115
return {
116116
type: FEATURE,
117-
id: hexAddress,
117+
id: h3Index,
118118
properties,
119119
geometry: {
120120
type: POLYGON,
@@ -162,9 +162,9 @@ function h3SetToFeature(hexagons, properties = {}) {
162162
* @return {Feature} GeoJSON Feature object
163163
*/
164164
function h3SetToMultiPolygonFeature(hexagons, properties = {}) {
165-
const coordinates = hexagons.map(hexAddress =>
165+
const coordinates = hexagons.map(h3Index =>
166166
// Wrap in an array for a single-loop polygon
167-
[h3.h3ToGeoBoundary(hexAddress, {geoJson: true})]
167+
[h3.h3ToGeoBoundary(h3Index, {geoJson: true})]
168168
);
169169
return {
170170
type: FEATURE,
@@ -184,15 +184,15 @@ function h3SetToMultiPolygonFeature(hexagons, properties = {}) {
184184
* @static
185185
* @param {String[]} hexagons Hexagon addresses
186186
* @param {Function} [getProperties] Optional function returning properties
187-
* for a hexagon: f(hexAddress) => Object
187+
* for a hexagon: f(h3Index) => Object
188188
* @return {FeatureCollection} GeoJSON FeatureCollection object
189189
*/
190190
function h3SetToFeatureCollection(hexagons, getProperties) {
191191
const features = [];
192192
for (let i = 0; i < hexagons.length; i++) {
193-
const hexAddress = hexagons[i];
194-
const properties = getProperties ? getProperties(hexAddress) : {};
195-
features.push(h3ToFeature(hexAddress, properties));
193+
const h3Index = hexagons[i];
194+
const properties = getProperties ? getProperties(h3Index) : {};
195+
features.push(h3ToFeature(h3Index, properties));
196196
}
197197
return {
198198
type: FEATURE_COLLECTION,

test/geojson2h3.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function assertSymmetrical(assert, feature, hexagons) {
134134
);
135135
assertEqualFeatures(
136136
assert,
137-
h3SetToFeature(featureToH3Set(feature, DEFAULT_RES)),
137+
h3SetToFeature(featureToH3Set(feature, DEFAULT_RES).sort()),
138138
feature,
139139
'h3SetToFeature round-trip matches expected'
140140
);
@@ -274,8 +274,8 @@ test('featureToH3Set - MultiPolygon, contiguous', assert => {
274274
];
275275

276276
assert.deepEqual(
277-
featureToH3Set(feature, DEFAULT_RES),
278-
hexagons,
277+
featureToH3Set(feature, DEFAULT_RES).sort(),
278+
hexagons.sort(),
279279
'featureToH3Set matches expected'
280280
);
281281

@@ -301,8 +301,8 @@ test('featureToH3Set - MultiPolygon, non-contiguous', assert => {
301301
];
302302

303303
assert.deepEqual(
304-
featureToH3Set(feature, DEFAULT_RES),
305-
hexagons,
304+
featureToH3Set(feature, DEFAULT_RES).sort(),
305+
hexagons.sort(),
306306
'featureToH3Set matches expected'
307307
);
308308

@@ -341,8 +341,8 @@ test('featureToH3Set - FeatureCollection', assert => {
341341
];
342342

343343
assert.deepEqual(
344-
featureToH3Set(feature, DEFAULT_RES),
345-
hexagons,
344+
featureToH3Set(feature, DEFAULT_RES).sort(),
345+
hexagons.sort(),
346346
'featureToH3Set matches expected'
347347
);
348348

types.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
declare module 'geojson2h3' {
2+
import {Feature, FeatureCollection, GeoJsonProperties} from 'geojson';
3+
4+
/**
5+
* Convert a GeoJSON feature to a set of hexagons. Only hexagons whose centers
6+
* fall within the feature will be included. Note that conversion from GeoJSON
7+
* is lossy; the resulting hexagon set only approximately describes the original
8+
* shape, at a level of precision determined by the hexagon resolution.
9+
*/
10+
export function featureToH3Set(feature: Feature, resolution: number): string[];
11+
12+
/**
13+
* Convert a single H3 hexagon to a GeoJSON `Polygon` feature
14+
*/
15+
export function h3ToFeature(h3Index: string, properties: GeoJsonProperties): Feature;
16+
17+
/**
18+
* Convert a set of hexagons to a GeoJSON `Feature` with the set outline(s). The
19+
* feature's geometry type will be either `Polygon` or `MultiPolygon` depending on
20+
* the number of outlines required for the set.
21+
*/
22+
export function h3SetToFeature(hexagons: string[], properties: GeoJsonProperties): Feature;
23+
24+
/**
25+
* Convert a set of hexagons to a GeoJSON `MultiPolygon` feature with the
26+
* outlines of each individual hexagon.
27+
*/
28+
export function h3SetToMultiPolygonFeature(
29+
hexagons: string[],
30+
properties: GeoJsonProperties
31+
): Feature;
32+
33+
/**
34+
* Convert a set of hexagons to a GeoJSON `FeatureCollection` with each hexagon
35+
* in a separate `Polygon` feature with optional properties.
36+
*/
37+
export function h3SetToFeatureCollection(
38+
hexagons: string[],
39+
getProperties: (h3Index: string) => GeoJsonProperties
40+
): FeatureCollection;
41+
}

0 commit comments

Comments
 (0)