Skip to content

Commit 9236810

Browse files
authored
Merge pull request #113 from isaacbrodsky/h3getresolution-accept-ints
Accept integers to h3GetResolution
2 parents 5e19510 + 8e978f1 commit 9236810

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

lib/bindings.js

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default [
5555
['h3IsPentagon', BOOLEAN, [H3_LOWER, H3_UPPER]],
5656
['h3IsResClassIII', BOOLEAN, [H3_LOWER, H3_UPPER]],
5757
['h3GetBaseCell', NUMBER, [H3_LOWER, H3_UPPER]],
58+
['h3GetResolution', NUMBER, [H3_LOWER, H3_UPPER]],
5859
['maxFaceCount', NUMBER, [H3_LOWER, H3_UPPER]],
5960
['h3GetFaces', null, [H3_LOWER, H3_UPPER, POINTER]],
6061
['h3ToParent', H3_LOWER, [H3_LOWER, H3_UPPER, RESOLUTION]],

lib/h3core.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const INVALID_HEXIDECIMAL_CHAR = /[^0-9a-fA-F]/;
117117
* @param {H3IndexInput} h3Index H3 index to check
118118
* @return {number[]} A two-element array with 32 lower bits and 32 upper bits
119119
*/
120-
function h3IndexToSplitLong(h3Index) {
120+
export function h3IndexToSplitLong(h3Index) {
121121
if (
122122
Array.isArray(h3Index) &&
123123
h3Index.length === 2 &&
@@ -160,7 +160,7 @@ function hexFrom32Bit(num) {
160160
* @param {number} upper Upper 32 bits
161161
* @return {H3Index} H3 index
162162
*/
163-
function splitLongToh3Index(lower, upper) {
163+
export function splitLongToh3Index(lower, upper) {
164164
return hexFrom32Bit(upper) + zeroPad(8, hexFrom32Bit(lower));
165165
}
166166

@@ -580,10 +580,12 @@ export function h3GetFaces(h3Index) {
580580
* @return {number} The number (0-15) resolution, or -1 if invalid
581581
*/
582582
export function h3GetResolution(h3Index) {
583-
if (typeof h3Index !== 'string') {
583+
const [lower, upper] = h3IndexToSplitLong(h3Index);
584+
if (!H3.h3IsValid(lower, upper)) {
585+
// Compatability with stated API
584586
return -1;
585587
}
586-
return parseInt(h3Index.charAt(1), BASE_16);
588+
return H3.h3GetResolution(lower, upper);
587589
}
588590

589591
/**

out/libh3.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/h3core.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ test('h3GetResolution', assert => {
145145
assert.end();
146146
});
147147

148+
test('h3GetResolution - integers', assert => {
149+
for (let res = 0; res < 16; res++) {
150+
// Same as in h3GetResolution above
151+
const h3Index = h3.geoToH3(37.3615593, -122.0553238, res);
152+
const h3IndexInt = h3.h3IndexToSplitLong(h3Index);
153+
assert.equal(
154+
h3.h3GetResolution(h3IndexInt),
155+
res,
156+
'Got the expected resolution back for int'
157+
);
158+
}
159+
assert.end();
160+
});
161+
148162
test('h3ToGeo', assert => {
149163
const latlng = h3.h3ToGeo('85283473fffffff');
150164
assert.deepEqual(

0 commit comments

Comments
 (0)