Miscellaneous utility functions.

Static methods

static cloneObject<T> (v:T):T

Makes an object copy recursively. Taken from Haxe mailing list. This version does not support any special classes like maps and lists. Arrays are supported.

Parameters:

v

Object to make a copy of.

Returns:

A copy of an object given.

static getIntValueJSON (jsonString:String, key:String):Int

Useful JSON hack. Returns a numerical value from a JSON encoded string by unique key. This function works much faster than parsing whole JSON encoded string into an anonymous object.

Example usage:

var json = '{..., "myUniqueKey":20, ...}'; // proper JSON encoded string (abbreviated for brevity)
var val = Util.getValueJSON(json, "myUniqueKey"); // val = 20

var json = '{..., "test":{"myUniqueKey":30}, ... }'; // another JSON string (abbreviated for brevity)
var val = Util.getValueJSON(json, "myUniqueKey"); // val = 30

Note: Remember that non-unique key will just return the first value found.

Parameters:

jsonString

JSON encoded string.

key

Key to get value of.

Returns:

Parameter value, or 0 if key not found.

static getMedian (q:String, func:String ‑> PostgresResultSet):Float

Returns the median value for the list of values returned by query. The query must use "val" field and be ordered from min to max, for example "SELECT ID AS Val FROM Users ORDER BY Time".

Parameters:

q

Query string.

func

Query function.

Returns:

Median value.

static round (number:Float, precision:Int):Float

Round float number to a specified number of decimal places.

Parameters:

number

Number to round.

precision

Number of decimal places to round to.

Returns:

Rounded number.

static urlRequest (url:String, method:String, params:Dynamic, log:String ‑> Void, logURL:Bool):String

Sends HTTP request with a given URL and returns the result.

Note: To enable HTTPS support on Neko 2.0, you will need to install hxssl library with "haxelib git hxssl https://github.com/tong/hxssl" command and insert the "-lib hxssl" line into the compilation HXML file.

Parameters:

url

HTTP URL.

method

Request method, "get" or "post".

params

Request parameters object. If the object contains field "_postData" (String), it will be used to set POST request data. If the object contains field "_headers" (Map), it will be used to set request headers. If the object contains field "_ignoreErrors" (Bool) and it is set to true, HTTP error codes will be ignored and callback will run. Connection errors will still be considered fatal.

log

Logging function.

logURL

If enabled, request URL will be logged.

Returns:

Webserver response. If the connection could not be established, returns "#CONNECT" string. If the request failed due to an exception and "_ignoreErrors" is false, returns "#ERROR" string. If the request failed due to an exception and "_ignoreErrors" is true, returns the webserver response.

static urlRequestExt (url:String, method:String, params:Dynamic, log:String ‑> Void, logURL:Bool, retries:Int, pause:Float):String

Extended version of HTTP request. Sends HTTP request with a given URL and returns the result. Supports automatic request retry on error.

Note: To enable HTTPS support, you will need to install hxssl library with "haxelib git hxssl https://github.com/tong/hxssl" command and insert the "-lib hxssl" line into the compilation HXML file.

Note: By default, sets the content type header to 'text/html; charset=utf-8'.

Parameters:

url

HTTP URL.

method

Request method, "get" or "post".

params

Request parameters object. If the object contains field "_postData" (String), it will be used to set POST request data. If the object contains field "_headers" (Map), it will be used to set request headers. If the object contains field "_ignoreErrors" (Bool) and it is set to true, HTTP error codes will be ignored and callback will run. Connection errors will still be considered fatal.

log

Logging function.

logURL

If enabled, request URL will be logged.

retries

Number of attempts to send the request. If it equals 0, will try indefinitely.

pause

Time in seconds to pause between attempts.

Returns:

Webserver response. If all attempts to make the request failed, returns "#ERROR" string.

static utf8length (s:String):Int

Returns UTF8 string length in UTF8 characters.

Parameters:

s

String.

Returns:

String length.