commit 207382685f145a060a9273350bd201c2dadcc077
parent 916633c3fc76f478838d1f54ac57e3e846aef7d0
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Wed, 28 Aug 2024 09:42:19 +0000
utils: add geolocation functions, fix time_created_on() to use iso format
Diffstat:
6 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/utils/package.json b/utils/package.json
@@ -10,6 +10,7 @@
"dev": "tsc -w"
},
"devDependencies": {
+ "@types/ngeohash": "0.6.8",
"@types/uuid": "^10.0.0",
"typescript": "^5.3.3"
},
@@ -19,6 +20,7 @@
"dependencies": {
"@noble/hashes": "^1.4.0",
"jshashes": "^1.0.8",
+ "ngeohash": "0.6.3",
"nostr-tools": "^2.7.2",
"uuid": "^10.0.0"
}
diff --git a/utils/src/geolocation.ts b/utils/src/geolocation.ts
@@ -0,0 +1,29 @@
+import ngeohash from "ngeohash";
+import { LocationPoint } from "./types";
+
+export const geohash_encode = (opts: {
+ lat: string | number;
+ lng: string | number;
+}): string => {
+ const geohash = ngeohash.encode(opts.lat, opts.lng);
+ return geohash;
+};
+
+export const geohash_decode = (opts: {
+ geohash: string;
+}): LocationPoint => {
+ const { latitude: lat, longitude: lng, error: { latitude: lat_err, longitude: lng_err } } = ngeohash.decode(opts.geohash);
+ return {
+ lat,
+ lng,
+ error: {
+ lat: lat_err,
+ lng: lng_err
+ }
+ };
+};
+
+export const location_geohash = (lat: number, lng: number): string => {
+ const res = geohash_encode({ lat, lng });
+ return res;
+};
+\ No newline at end of file
diff --git a/utils/src/index.ts b/utils/src/index.ts
@@ -1,5 +1,6 @@
import "./global.d.ts";
+export * from "./geolocation"
export * from "./jshashes"
export * from "./nostr-tools"
export * from "./types"
diff --git a/utils/src/types.ts b/utils/src/types.ts
@@ -1,3 +1,13 @@
+
export type ErrorResponse = {
error: string;
-};
-\ No newline at end of file
+};
+
+export type LocationPoint = {
+ lat: number;
+ lng: number;
+ error: {
+ lat: number;
+ lng: number;
+ };
+}
+\ No newline at end of file
diff --git a/utils/src/utils.ts b/utils/src/utils.ts
@@ -1,5 +1,5 @@
import { v4 } from "uuid";
-import { ErrorResponse } from "./types";
+import type { ErrorResponse } from "./types";
export const regex: Record<string, RegExp> = {
word_only: /^[a-zA-Z]+$/,
@@ -16,7 +16,7 @@ export function time_now_ms(): number {
};
export function time_created_on(): string {
- return new Date().toUTCString();
+ return new Date().toISOString();
};
export function err_msg(e: unknown, append?: string): ErrorResponse {
diff --git a/utils/tsconfig.json b/utils/tsconfig.json
@@ -8,6 +8,7 @@
"outDir": "./dist",
"rootDir": "./src",
"noEmit": true,
+ "esModuleInterop": true
},
"include": [
"src"