@hastehaul/common 2.9.0 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,4 +7,6 @@ export declare function colorize(text: string, colorCode: number): string;
7
7
  export declare function formatDatetimeToHumanReadable(datetime: Date, format?: string): string;
8
8
  export declare function generateIdentifier(messageContent: string): string;
9
9
  export declare function getValueFromSettings(settings: Setting[], key: string, envVar?: string, defaultValue?: any): any;
10
+ export declare function loadAsset(filename: string): Promise<string | null>;
11
+ export declare function isPointInGeoJSON(latLng: number[], fileName: string): Promise<any>;
10
12
  export {};
@@ -1,4 +1,36 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
2
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
36
  };
@@ -8,7 +40,13 @@ exports.colorize = colorize;
8
40
  exports.formatDatetimeToHumanReadable = formatDatetimeToHumanReadable;
9
41
  exports.generateIdentifier = generateIdentifier;
10
42
  exports.getValueFromSettings = getValueFromSettings;
43
+ exports.loadAsset = loadAsset;
44
+ exports.isPointInGeoJSON = isPointInGeoJSON;
45
+ const boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
46
+ const helpers_1 = __importDefault(require("@turf/helpers"));
11
47
  const moment_1 = __importDefault(require("moment"));
48
+ const fs_1 = require("fs");
49
+ const path = __importStar(require("path"));
12
50
  function hasElapsed(expirationDate) {
13
51
  const currentDate = (0, moment_1.default)();
14
52
  const targetDate = (0, moment_1.default)(expirationDate);
@@ -33,3 +71,42 @@ function getValueFromSettings(settings, key, envVar, defaultValue) {
33
71
  }
34
72
  return process.env[envVar] || defaultValue;
35
73
  }
74
+ function loadAsset(filename) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ try {
77
+ const file = path.join(__dirname, '..', 'geojson', `${filename}.json`);
78
+ const fileContent = yield fs_1.promises.readFile(file, 'utf-8');
79
+ return fileContent;
80
+ }
81
+ catch (error) {
82
+ console.error(`Error loading asset: ${error.message}`);
83
+ return null; // or handle the error in a way that makes sense for your application
84
+ }
85
+ });
86
+ }
87
+ function isPointInGeoJSON(latLng, fileName) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ try {
90
+ if (latLng.length === 0)
91
+ return false;
92
+ const geoJsonData = yield loadAsset(fileName);
93
+ const geojson = JSON.parse(geoJsonData);
94
+ const point = helpers_1.default.point([latLng[1], latLng[0]]);
95
+ const isInside = geojson.features.some((feature) => {
96
+ const geometry = feature.geometry;
97
+ if (geometry.type === 'Polygon') {
98
+ return (0, boolean_point_in_polygon_1.default)(point, helpers_1.default.polygon(geometry.coordinates));
99
+ }
100
+ else if (geometry.type === 'MultiPolygon') {
101
+ return geometry.coordinates.some((polygon) => (0, boolean_point_in_polygon_1.default)(point, helpers_1.default.polygon(polygon)));
102
+ }
103
+ return false;
104
+ });
105
+ return isInside;
106
+ }
107
+ catch (error) {
108
+ console.error(`Error checking point in GeoJSON: ${error}`);
109
+ return null;
110
+ }
111
+ });
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.9.0",
3
+ "version": "2.10.0",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -32,6 +32,10 @@
32
32
  "jsonwebtoken": "^9.0.1",
33
33
  "moment": "^2.29.4",
34
34
  "nats": "^2.17.0",
35
- "socket.io": "^4.7.1"
35
+ "socket.io": "^4.7.1",
36
+ "@turf/boolean-point-in-polygon": "^6.5.0",
37
+ "@turf/helpers": "^6.5.0",
38
+ "@turf/meta": "^6.5.0",
39
+ "@turf/points-within-polygon": "^6.5.0"
36
40
  }
37
41
  }