mapcode-ts 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/NOTICE ADDED
@@ -0,0 +1,8 @@
1
+ -------------------------------------------------------------------------------
2
+ MAPCODE TYPESCRIPT LIBRARY
3
+ -------------------------------------------------------------------------------
4
+
5
+ Original C library created by Pieter Geelen. Work on TypeScript version
6
+ of the Mapcode library by Rijn Buve.
7
+
8
+ Copyright (C) 2014-2026, Stichting Mapcode Foundation (http://www.mapcode.com)
package/README.md CHANGED
@@ -11,7 +11,7 @@ This is a faithful TypeScript port of the [mapcode-java](https://github.com/mapc
11
11
  (version 2.4.19).
12
12
 
13
13
  Licensed under the [Apache License, Version 2.0](LICENSE).
14
- Copyright (C) 2014-2024, Stichting Mapcode Foundation.
14
+ Copyright (C) 2014-2026, Stichting Mapcode Foundation.
15
15
 
16
16
  ---
17
17
 
@@ -101,8 +101,8 @@ console.log(t.getFullName()); // "Netherlands"
101
101
 
102
102
  // Request a mapcode rendered in a specific alphabet:
103
103
  const m = encodeToShortest(37.9715, 23.7263, Territory.GRC);
104
- console.log(m.getCode()); // Roman: "ΗΠ.Θ2" (decoded from Unicode input)
105
- console.log(m.getCode(Alphabet.GREEK)); // Greek script
104
+ console.log(m.getCode()); // Roman: "HJ.Q5"
105
+ console.log(m.getCode(Alphabet.GREEK)); // Greek script: "ΗΠ.485"
106
106
  ```
107
107
 
108
108
  ---
package/dist/index.cjs CHANGED
@@ -1294,16 +1294,14 @@ var _Mapcode = class _Mapcode {
1294
1294
  }
1295
1295
  getCode(precisionOrAlphabet, alphabet) {
1296
1296
  const { precision, alpha } = _Mapcode.resolveArgs(precisionOrAlphabet, alphabet);
1297
+ _Mapcode.checkPrecision(precision);
1297
1298
  if (precision === 0) {
1298
1299
  return convertStringToAlphabet(this.codePrecision8.substring(0, this.codePrecision8.length - 9), alpha);
1299
- } else if (precision <= 8) {
1300
- return convertStringToAlphabet(
1301
- this.codePrecision8.substring(0, this.codePrecision8.length - 8 + precision),
1302
- alpha
1303
- );
1304
- } else {
1305
- throw new IllegalArgumentError("getCodePrecision: precision must be in [0, 8]");
1306
1300
  }
1301
+ return convertStringToAlphabet(
1302
+ this.codePrecision8.substring(0, this.codePrecision8.length - 8 + precision),
1303
+ alpha
1304
+ );
1307
1305
  }
1308
1306
  getCodeWithTerritoryFullname(precisionOrAlphabet, alphabet) {
1309
1307
  const { precision, alpha } = _Mapcode.resolveArgs(precisionOrAlphabet, alphabet);
@@ -1332,6 +1330,11 @@ var _Mapcode = class _Mapcode {
1332
1330
  }
1333
1331
  return { precision: precisionOrAlphabet, alpha: alphabet ?? null };
1334
1332
  }
1333
+ static checkPrecision(precision) {
1334
+ if (!Number.isInteger(precision) || precision < 0 || precision > 8) {
1335
+ throw new IllegalArgumentError("precision must be an integer in [0, 8]");
1336
+ }
1337
+ }
1335
1338
  /**
1336
1339
  * Return the mapcode precision format, given a mapcode string. Throws if the format is invalid.
1337
1340
  *
@@ -1380,9 +1383,7 @@ var _Mapcode = class _Mapcode {
1380
1383
  * Get a safe maximum for the distance between a decoded mapcode and its original location.
1381
1384
  */
1382
1385
  static getSafeMaxOffsetInMeters(precision) {
1383
- if (precision < 0 || precision > 8) {
1384
- throw new IllegalArgumentError("precision must be in [0, 8]");
1385
- }
1386
+ _Mapcode.checkPrecision(precision);
1386
1387
  return _Mapcode.PRECISION_0_MAX_OFFSET_METERS[precision];
1387
1388
  }
1388
1389
  /**
@@ -3923,6 +3924,11 @@ var DATA_MODEL4 = DataModel.getInstance();
3923
3924
  var createBoundaryForTerritoryRecord2 = Boundary.createBoundaryForTerritoryRecord.bind(Boundary);
3924
3925
  var { X_SIDE, Y_SIDE, NC, xDivider, countCityCoordinatesForCountry, getFirstNamelessRecord } = Common;
3925
3926
  var ENCODE_CHARS2 = Data.ENCODE_CHARS;
3927
+ function checkFiniteCoordinate(param, value) {
3928
+ if (!Number.isFinite(value)) {
3929
+ throw new IllegalArgumentError(`Parameter ${param} must be a finite number`);
3930
+ }
3931
+ }
3926
3932
  function encodeExtension(pointToEncode, extrax4, extray, dividerx4, dividery, ydirection) {
3927
3933
  let extraDigits = 8;
3928
3934
  let factorx = Point.MAX_PRECISION_FACTOR * dividerx4;
@@ -4243,6 +4249,8 @@ var Encoder = class {
4243
4249
  constructor() {
4244
4250
  }
4245
4251
  static encode(latDeg, lonDeg, territory, limitToOneResult) {
4252
+ checkFiniteCoordinate("latDeg", latDeg);
4253
+ checkFiniteCoordinate("lonDeg", lonDeg);
4246
4254
  return encodeInternal(latDeg, lonDeg, territory, limitToOneResult, null);
4247
4255
  }
4248
4256
  };
@@ -4284,7 +4292,7 @@ function encode(arg0, arg1, arg2) {
4284
4292
  if (arg0 instanceof Point) {
4285
4293
  const point = arg0;
4286
4294
  const restrictToTerritory2 = arg1 ?? null;
4287
- checkNonnull("point", point);
4295
+ checkDefined("point", point);
4288
4296
  return encode(point.getLatDeg(), point.getLonDeg(), restrictToTerritory2);
4289
4297
  }
4290
4298
  const latDeg = arg0;
@@ -4398,6 +4406,11 @@ function decode2(mapcode, defaultTerritoryContext) {
4398
4406
  function decodeToRectangle(mapcode, defaultTerritoryContext) {
4399
4407
  checkNonnull("mapcode", mapcode);
4400
4408
  const mapcodeZone = decodeToMapcodeZone(mapcode, defaultTerritoryContext ?? null);
4409
+ if (mapcodeZone.isEmpty()) {
4410
+ throw new UnknownMapcodeError(
4411
+ "Unknown mapcode, mapcode=" + mapcode + ", territoryContext=" + defaultTerritoryContext
4412
+ );
4413
+ }
4401
4414
  const southWest = Point.fromLatLonFractions(mapcodeZone.getLatFractionMin(), mapcodeZone.getLonFractionMin());
4402
4415
  const northEast = Point.fromLatLonFractions(mapcodeZone.getLatFractionMax(), mapcodeZone.getLonFractionMax());
4403
4416
  const rectangle = new Rectangle(southWest, northEast);