UNPKG

postgrejs

Version:

Professional PostgreSQL client NodeJS

43 lines (42 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayPointType = exports.PointType = void 0; const constants_js_1 = require("../constants.js"); const POINT_PATTERN1 = /^\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\)$/; const POINT_PATTERN2 = /^(-?\d+\.?\d*) *, *(-?\d+\.?\d*)$/; exports.PointType = { name: 'point', oid: constants_js_1.DataTypeOIDs.point, jsType: 'object', parseBinary(v) { return { x: v.readDoubleBE(0), y: v.readDoubleBE(8), }; }, encodeBinary(buf, v) { buf.writeDoubleBE(v.x); buf.writeDoubleBE(v.y); }, parseText(v) { const m = v.match(POINT_PATTERN1) || v.match(POINT_PATTERN2); if (!m) return undefined; return { x: parseFloat(m[1]), y: parseFloat(m[2]), }; }, isType(v) { return (typeof v === 'object' && Object.keys(v).length === 2 && typeof v.x === 'number' && typeof v.y === 'number'); }, }; exports.ArrayPointType = { ...exports.PointType, name: '_point', oid: constants_js_1.DataTypeOIDs._point, elementsOID: constants_js_1.DataTypeOIDs.point, };