Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 139x 34x 30x 30x 70x 30x 528x 150x 378x 261x 261x 18x 1x 40x 10x 1x 9x 345x 1x 344x 84x | /** A `point` value. */
export type GeoPoint = Readonly<{
/** Horizontal coordinate. */
x: number;
/** Vertical coordinate. */
y: number;
}>;
const NUMBER = /-?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?|-?Infinity|NaN/gi;
/**
* Reads and writes the text form of geometric values.
*
* @internal
*/
export class GeometricText {
/**
* Extracts the numbers of a geometric literal, e.g. `((1,2),(3,4))` → `[1, 2, 3, 4]`.
*
* @param text - Literal returned by PostgreSQL.
* @returns Numbers in order of appearance.
*/
numbers(text: string): number[] {
return (text.match(NUMBER) ?? []).map(Number);
}
/**
* Groups numbers into points.
*
* @param values - Flat coordinates.
* @returns One point per pair.
*/
points(values: readonly number[]): GeoPoint[] {
const result: GeoPoint[] = [];
for (let index = 0; index + 1 < values.length; index += 2) {
result.push({ x: values[index] ?? 0, y: values[index + 1] ?? 0 });
}
return result;
}
/**
* Validates a coordinate before it is formatted.
*
* @param value - Coordinate.
* @returns The value as text.
* @throws Error when the value is not a finite number.
*/
number(value: unknown): string {
if (typeof value !== 'number' || !Number.isFinite(value)) {
throw new Error(`Geometric coordinates must be finite numbers, got ${String(value)}`);
}
return String(value);
}
/**
* Formats a point as `(x,y)`.
*
* @param value - Point-like value.
* @returns Point text.
*/
point(value: unknown): string {
const point = this.#object(value);
return `(${this.number(point['x'])},${this.number(point['y'])})`;
}
/**
* Formats a list of points as `(x,y),(x,y)`.
*
* @param value - Array of points.
* @returns Comma-joined point text.
*/
list(value: unknown): string {
if (!Array.isArray(value)) {
throw new Error('Expected an array of points');
}
return value.map((item) => this.point(item)).join(',');
}
/**
* Formats a pair of points (`lseg`, `box`).
*
* @param value - Array of exactly two points.
* @returns Comma-joined point text.
*/
pair(value: unknown): string {
if (!Array.isArray(value) || value.length !== 2) {
throw new Error('Expected an array of two points');
}
return this.list(value);
}
#object(value: unknown): Record<string, unknown> {
if (typeof value !== 'object' || value === null) {
throw new Error(`Expected an object, got ${String(value)}`);
}
return value as Record<string, unknown>;
}
/**
* Narrows a value to an object for field access.
*
* @param value - Value to check.
* @returns The value as a record.
*/
fields(value: unknown): Record<string, unknown> {
return this.#object(value);
}
}
|