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 | 139x 139x 1x 144x 3x 2x 2x 3x 3x 13x 7x 8x 5x 5x 5x 5x 4x 1x 77x 77x 174x 174x 1280x 1280x 973x 973x 973x | import { AbstractDataType } from './abstract-data-type.ts';
import { GeometricText, type GeoPoint } from './geometric-text.ts';
/** Keys of the geometric data types. */
export type GeometricKey = 'POINT' | 'LINE' | 'LSEG' | 'BOX' | 'PATH' | 'POLYGON' | 'CIRCLE';
/** `path` value: `closed` paths are written `( … )`, open ones `[ … ]`. */
export type GeoPath = Readonly<{
/** Vertices in order. */
points: readonly GeoPoint[];
/** True for a closed path. */
closed: boolean;
}>;
/** `line` value: the line `a·x + b·y + c = 0`. */
export type GeoLine = Readonly<{
/** Coefficient of `x`. */
a: number;
/** Coefficient of `y`. */
b: number;
/** Constant term. */
c: number;
}>;
/** `circle` value: center and radius. */
export type GeoCircle = Readonly<{
/** Center x. */
x: number;
/** Center y. */
y: number;
/** Radius. */
radius: number;
}>;
type Codec = Readonly<{
parse: (numbers: number[], text: string) => unknown;
format: (value: unknown) => string;
}>;
const text = new GeometricText();
const CODECS: Readonly<Record<GeometricKey, Codec>> = {
POINT: {
parse: (n) => text.points(n)[0],
format: (value) => text.point(value),
},
LINE: {
parse: ([a = 0, b = 0, c = 0]) => ({ a, b, c }),
format: (value) => {
const line = text.fields(value);
return `{${text.number(line['a'])},${text.number(line['b'])},${text.number(line['c'])}}`;
},
},
LSEG: {
parse: (n) => text.points(n),
format: (value) => `[${text.pair(value)}]`,
},
BOX: {
parse: (n) => text.points(n),
format: (value) => text.pair(value),
},
PATH: {
parse: (n, literal) => ({ points: text.points(n), closed: !literal.startsWith('[') }),
format: (value) => {
const path = text.fields(value);
const points = text.list(path['points']);
return path['closed'] === false ? `[${points}]` : `(${points})`;
},
},
POLYGON: {
parse: (n) => text.points(n),
format: (value) => `(${text.list(value)})`,
},
CIRCLE: {
parse: ([x = 0, y = 0, radius = 0]) => ({ x, y, radius }),
format: (value) => {
const circle = text.fields(value);
return `<${text.point(circle)},${text.number(circle['radius'])}>`;
},
},
};
/**
* Geometric types (`DataTypes.POINT`, `LINE`, `LSEG`, `BOX`, `PATH`, `POLYGON`, `CIRCLE`); values
* are plain objects and arrays of points.
*
* @internal
*/
export class GeometricType extends AbstractDataType {
/** Converts a driver literal into the value shape; non-text values are kept. */
readonly parse = (value: unknown): unknown =>
typeof value === 'string' ? CODECS[this.#key].parse(text.numbers(value), value.trim()) : value;
/** Converts a value into a literal bound as one parameter; text and `null` are kept. */
readonly serialize = (value: unknown): unknown =>
value === null || value === undefined || typeof value === 'string'
? value
: CODECS[this.#key].format(value);
readonly #key: GeometricKey;
/**
* @param key - Geometric type key; also the SQL type name.
*/
constructor(key: GeometricKey) {
super(key, key);
this.#key = key;
Object.freeze(this);
}
}
|