Page MenuHomeSealhub

coordinates.ts
No OneTemporary

Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None

coordinates.ts

import { hasShape, predicates } from "@sealcode/ts-predicates";
import { Context, Field, type ValidationResult } from "../../../main.js";
import { OpenApiTypes } from "../../../schemas/open-api-types.js";
import { CoordsValue } from "./coords-value.js";
export type CoordinatesFieldInputType =
| string
| number[]
| { lat: number; lon: number };
type GeoJSONPoint = { type: "Point"; coordinates: number[] };
/** A field for coordinates, to handle values in diffrent form and store them in normalized GeoJSON format.
*
* **Accepted values**: strings (i.g. "12.12421,65.21312") tuple (i.g. `[12.12421, 65.21312]`) or object (i.g. `{lat: 12.12421, lon: 65.21312})`
*/
export default class Coordinates extends Field<
CoordsValue,
CoordinatesFieldInputType,
GeoJSONPoint
> {
typeName = "coordinates";
open_api_type = OpenApiTypes.STR;
async isProperValue(
ctx: Context,
value: CoordinatesFieldInputType
): Promise<ValidationResult> {
if (value == null) {
return Field.valid();
}
if (typeof value === "string" && value.split(",").length === 2) {
return Field.valid();
} else if (Array.isArray(value) && value.length === 2) {
return Field.valid();
} else if (
typeof value === "object" &&
"lat" in value &&
"lon" in value
) {
return Field.valid();
} else {
return Field.invalid(ctx.i18n`Invalid coordinates`);
}
}
async encode(
_: Context,
value: CoordinatesFieldInputType
): Promise<GeoJSONPoint | null> {
if (value == null) {
return null;
}
if (typeof value === "string") {
const coords = value.split(",").map(parseFloat);
const [lat, lon] = [coords[0], coords[1]];
if (lat === undefined || lon === undefined) {
throw new Error("coords is missing");
}
return {
type: "Point",
coordinates: [lat, lon],
};
} else if (Array.isArray(value) && value.length === 2) {
return {
type: "Point",
coordinates: value,
};
} else if (
typeof value === "object" &&
"lat" in value &&
"lon" in value
) {
const { lat, lon } = value as { lat: number; lon: number };
return {
type: "Point",
coordinates: [lat, lon],
};
} else {
throw new Error("Invalid coordinates value");
}
}
async decode(_: Context, db_value: unknown): Promise<CoordsValue | null> {
if (db_value === null || db_value === undefined) {
return null;
}
if (
!hasShape(
{
type: predicates.string,
coordinates: predicates.array(predicates.number),
},
db_value
)
) {
throw new Error("db value has incorrect shape");
}
const dbValueCasted = db_value as GeoJSONPoint;
if (
dbValueCasted?.type !== "Point" ||
!Array.isArray(dbValueCasted?.coordinates) ||
dbValueCasted?.coordinates.length !== 2
) {
throw new Error("db value is not vnvalid GeoJSON object");
}
const coordinates = dbValueCasted.coordinates as [number, number];
return new CoordsValue(coordinates);
}
getPostgreSqlFieldDefinitions(): string[] {
return [`"${this.name}" GEOGRAPHY`];
}
}

File Metadata

Mime Type
application/javascript
Expires
Sat, Nov 28, 03:25 (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1212300
Default Alt Text
coordinates.ts (2 KB)

Event Timeline