@trackunit/shared-utils 0.0.83 → 0.0.84

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/index.cjs.js CHANGED
@@ -589,6 +589,31 @@ const geoJsonRectangularBoxPolygonSchema = zod.z
589
589
  });
590
590
 
591
591
  const EARTH_RADIUS = 6378137; // Earth’s mean radius in meters
592
+ const isDoubleNestedCoords = (coords) => Array.isArray(coords) &&
593
+ Array.isArray(coords[0]) &&
594
+ Array.isArray(coords[0][0]) &&
595
+ typeof coords[0][0][0] === "number";
596
+ const isSingleCoords = (coords) => typeof coords[0] === "number";
597
+ /**
598
+ * @description Returns coordinates in consistent format
599
+ * @param inconsistentCoordinates Single point, array of points or nested array of points
600
+ * @returns GeoPoint[]
601
+ */
602
+ const coordinatesToStandardFormat = (inconsistentCoordinates) => {
603
+ if (!inconsistentCoordinates) {
604
+ return [];
605
+ }
606
+ if (isSingleCoords(inconsistentCoordinates)) {
607
+ return [inconsistentCoordinates];
608
+ }
609
+ if (isDoubleNestedCoords(inconsistentCoordinates)) {
610
+ return inconsistentCoordinates[0] || [];
611
+ }
612
+ if (inconsistentCoordinates[0] && typeof inconsistentCoordinates[0][0] === "number") {
613
+ return inconsistentCoordinates;
614
+ }
615
+ return [];
616
+ };
592
617
  /**
593
618
  * @description Extracts a point coordinate from a GeoJSON object.
594
619
  * @param geoObject A GeoJSON object.
@@ -605,7 +630,13 @@ const getPointCoordinateFromGeoJsonObject = (geoObject) => {
605
630
  Array.isArray(geoObject.coordinates) &&
606
631
  typeof geoObject.coordinates[0] === "number" &&
607
632
  typeof geoObject.coordinates[1] === "number") {
608
- return { longitude: geoObject.coordinates[0], latitude: geoObject.coordinates[1] };
633
+ const [point] = coordinatesToStandardFormat(geoObject.coordinates);
634
+ if (point) {
635
+ return { latitude: point[1], longitude: point[0] };
636
+ }
637
+ else {
638
+ throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
639
+ }
609
640
  }
610
641
  else {
611
642
  throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
@@ -624,12 +655,8 @@ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
624
655
  else if ("geometry" in geoObject) {
625
656
  return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
626
657
  }
627
- else if ("coordinates" in geoObject &&
628
- Array.isArray(geoObject.coordinates) &&
629
- Array.isArray(geoObject.coordinates[0])) {
630
- // @ts-ignore - suppressImplicitAnyIndexErrors
631
- // We would not be here if element was not an array.
632
- return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
658
+ else if ("coordinates" in geoObject) {
659
+ return coordinatesToStandardFormat(geoObject.coordinates).map(([longitude, latitude]) => ({ longitude, latitude }));
633
660
  }
634
661
  else {
635
662
  throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
@@ -1380,6 +1407,7 @@ exports.capitalize = capitalize;
1380
1407
  exports.convertBlobToBase64 = convertBlobToBase64;
1381
1408
  exports.convertMetersToYards = convertMetersToYards;
1382
1409
  exports.convertYardsToMeters = convertYardsToMeters;
1410
+ exports.coordinatesToStandardFormat = coordinatesToStandardFormat;
1383
1411
  exports.dateCompare = dateCompare;
1384
1412
  exports.deleteUndefinedKeys = deleteUndefinedKeys;
1385
1413
  exports.difference = difference;
package/index.esm.js CHANGED
@@ -587,6 +587,31 @@ const geoJsonRectangularBoxPolygonSchema = z
587
587
  });
588
588
 
589
589
  const EARTH_RADIUS = 6378137; // Earth’s mean radius in meters
590
+ const isDoubleNestedCoords = (coords) => Array.isArray(coords) &&
591
+ Array.isArray(coords[0]) &&
592
+ Array.isArray(coords[0][0]) &&
593
+ typeof coords[0][0][0] === "number";
594
+ const isSingleCoords = (coords) => typeof coords[0] === "number";
595
+ /**
596
+ * @description Returns coordinates in consistent format
597
+ * @param inconsistentCoordinates Single point, array of points or nested array of points
598
+ * @returns GeoPoint[]
599
+ */
600
+ const coordinatesToStandardFormat = (inconsistentCoordinates) => {
601
+ if (!inconsistentCoordinates) {
602
+ return [];
603
+ }
604
+ if (isSingleCoords(inconsistentCoordinates)) {
605
+ return [inconsistentCoordinates];
606
+ }
607
+ if (isDoubleNestedCoords(inconsistentCoordinates)) {
608
+ return inconsistentCoordinates[0] || [];
609
+ }
610
+ if (inconsistentCoordinates[0] && typeof inconsistentCoordinates[0][0] === "number") {
611
+ return inconsistentCoordinates;
612
+ }
613
+ return [];
614
+ };
590
615
  /**
591
616
  * @description Extracts a point coordinate from a GeoJSON object.
592
617
  * @param geoObject A GeoJSON object.
@@ -603,7 +628,13 @@ const getPointCoordinateFromGeoJsonObject = (geoObject) => {
603
628
  Array.isArray(geoObject.coordinates) &&
604
629
  typeof geoObject.coordinates[0] === "number" &&
605
630
  typeof geoObject.coordinates[1] === "number") {
606
- return { longitude: geoObject.coordinates[0], latitude: geoObject.coordinates[1] };
631
+ const [point] = coordinatesToStandardFormat(geoObject.coordinates);
632
+ if (point) {
633
+ return { latitude: point[1], longitude: point[0] };
634
+ }
635
+ else {
636
+ throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
637
+ }
607
638
  }
608
639
  else {
609
640
  throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
@@ -622,12 +653,8 @@ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
622
653
  else if ("geometry" in geoObject) {
623
654
  return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
624
655
  }
625
- else if ("coordinates" in geoObject &&
626
- Array.isArray(geoObject.coordinates) &&
627
- Array.isArray(geoObject.coordinates[0])) {
628
- // @ts-ignore - suppressImplicitAnyIndexErrors
629
- // We would not be here if element was not an array.
630
- return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
656
+ else if ("coordinates" in geoObject) {
657
+ return coordinatesToStandardFormat(geoObject.coordinates).map(([longitude, latitude]) => ({ longitude, latitude }));
631
658
  }
632
659
  else {
633
660
  throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
@@ -1367,4 +1394,4 @@ const uuidv4 = () => {
1367
1394
  */
1368
1395
  const uuidv5 = (name, namespace) => v5(name, namespace);
1369
1396
 
1370
- export { DateTimeFormat, EARTH_RADIUS, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, geoJsonBboxSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonNoHolesSchema, geoJsonPolygonSchema, geoJsonPositionSchema, geoJsonRectangularBoxPolygonSchema, getBoundingBoxFromGeoJsonPolygon, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getGeoJsonPolygonFromBoundingBox, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, titleCase, toID, toIDs, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
1397
+ export { DateTimeFormat, EARTH_RADIUS, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, coordinatesToStandardFormat, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, geoJsonBboxSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonNoHolesSchema, geoJsonPolygonSchema, geoJsonPositionSchema, geoJsonRectangularBoxPolygonSchema, getBoundingBoxFromGeoJsonPolygon, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getGeoJsonPolygonFromBoundingBox, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, titleCase, toID, toIDs, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/shared-utils",
3
- "version": "0.0.83",
3
+ "version": "0.0.84",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -8,14 +8,21 @@ interface BoundingBox {
8
8
  nw: PointCoordinate;
9
9
  se: PointCoordinate;
10
10
  }
11
+ export type GeoPoint = [number, number];
11
12
  interface GeoJSONGeometry {
12
13
  type?: unknown;
13
- coordinates?: number[] | number[][] | number[][][] | null;
14
+ coordinates?: GeoPoint | GeoPoint[] | GeoPoint[][] | null;
14
15
  }
15
16
  interface GeoJsonFeature {
16
17
  type?: unknown;
17
18
  geometry?: GeoJSONGeometry | null;
18
19
  }
20
+ /**
21
+ * @description Returns coordinates in consistent format
22
+ * @param inconsistentCoordinates Single point, array of points or nested array of points
23
+ * @returns GeoPoint[]
24
+ */
25
+ export declare const coordinatesToStandardFormat: (inconsistentCoordinates: GeoPoint | GeoPoint[] | GeoPoint[][] | null | undefined) => GeoPoint[];
19
26
  /**
20
27
  * @description Extracts a point coordinate from a GeoJSON object.
21
28
  * @param geoObject A GeoJSON object.