Skip to content

Commit 76b869c

Browse files
Implement max code length behavior for JavaScript
1 parent 55e1884 commit 76b869c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

js/closure/openlocationcode.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ var GRID_SIZE_DEGREES = 0.000125;
141141
*/
142142
var MIN_TRIMMABLE_CODE_LEN = 6;
143143

144+
/**
145+
* Maximum length of a code.
146+
*/
147+
var MAX_CODE_LEN = 15;
148+
144149
/**
145150
* Returns the characters used to produce the codes.
146151
* @return {string} the OLC alphabet.
@@ -311,6 +316,7 @@ function encode(latitude, longitude, opt_length) {
311316
(opt_length < PAIR_CODE_LENGTH && opt_length % 2 == 1)) {
312317
throw 'IllegalArgumentException: Invalid Open Location Code length';
313318
}
319+
opt_length = Math.min(opt_length, MAX_CODE_LEN);
314320
// Ensure that latitude and longitude are valid.
315321
latitude = clipLatitude(latitude);
316322
longitude = normalizeLongitude(longitude);
@@ -351,6 +357,10 @@ function decode(code) {
351357
code = code.replace(SEPARATOR, '');
352358
code = code.replace(new RegExp(PADDING_CHARACTER + '+'), '');
353359
code = code.toUpperCase();
360+
if (code.length > MAX_CODE_LEN) {
361+
code = code.substring(0, MAX_CODE_LEN);
362+
}
363+
354364
var /** @type {number} */ precision = ENCODING_BASE;
355365
var latitude = 0.0;
356366
var longitude = 0.0;

0 commit comments

Comments
 (0)