I wanted to drop in google's open location code javascript implementation into PostgreSQL (using the plv8 extension) and make it available to encode/decode from PostGIS geometry/geography data types.
While I was successful, I wasn't able to work out how to only create a single function for the https://github.com/google/open-location-code/blob/master/js/src/openlocationcode.js file and I ended up putting a copy of that function into each function where I needed to encode/decode plus codes. When I attempted to pull it out into its own function, I could either get a string containing javascript or a string of [Object],[object] rather than a callable function.
Is this possible with the plv8 extension in PostgreSQL?
Incomplete code snippet example (full version here):
DROP FUNCTION IF EXISTS olc.encode(float,float,integer);
CREATE OR REPLACE FUNCTION olc.encode(
p_latitude double precision,
p_longitude double precision,
p_code_length integer DEFAULT 10
)
RETURNS text AS
$BODY$
var f = function () {
var OpenLocationCode = {};
/**
* Provides a normal precision code, approximately 14x14 meters.
* @const {number}
*/
OpenLocationCode.CODE_PRECISION_NORMAL = 10;
/**
* Provides an extra precision code, approximately 2x3 meters.
* @const {number}
*/
OpenLocationCode.CODE_PRECISION_EXTRA = 11;
// A separator used to break the code into two parts to aid memorability.
var SEPARATOR_ = '+';
// The number of characters to place before the separator.
var SEPARATOR_POSITION_ = 8;
// The character used to pad codes.
var PADDING_CHARACTER_ = '0';