CREATE FUNCTION _api.trim(
	_text TEXT
)
RETURNS TEXT
LANGUAGE plpgsql VOLATILE
AS $BODY$
DECLARE
	_new TEXT;
BEGIN

	IF _text IS NULL THEN
		RETURN NULL;
	END IF;

	_new = _text;
	_new = TRIM(_new);
	_new = REGEXP_REPLACE(_new, '^(?: |\r|\n)*', '');
	_new = REGEXP_REPLACE(_new, '(?: |\r|\n)*$', '');

	RETURN _new;
END
$BODY$;

GRANT EXECUTE ON FUNCTION _api.trim(TEXT)
	TO rest_anon, rest_user;
