Helper (functions)

Get Tuple

GetTuple( obj )

The GetTuple function recurses down through arrays containing arrays until an object is located. This is useful for when database results have been processed into hierarchical array structures, and you want to receive a specific value that you know is common to a specific branch.

function GetTuple( obj )
{
	var tuple = null;

	if ( obj instanceof Array )
	{
		for ( const index in obj )
		{
			tuple = GetTuple( obj[index] );
			break;
		}
	}
	else
	{
		tuple = obj;
	}

	return tuple;
}

Limit Offset Parameters

LimitOffsetParameters( json )

The LimitOffsetParameters function simple returns an object with the two attributes 'limit' and 'offset', which are calculated from the integer values of the passed object decoded from JSON. Before being returned, the 'offset' attribute is incremented by 'limit'.

For example,

function Handler( responseText )
{
    var json = JSON.parse( responseText );

    if ( json.status = "OK" )
    {
        var parameters = LimitOffsetParameters( json );
        ...
    }
}

This is used when an AJAX call has retrieve a subset of possible results, and the parameters object needs to be modified to request the next set of results.

function LimitOffsetParameters( json )
{
	var parameters = null;
	var offset     = ("" != json.offset) ? parseInt( json.offset ) : 0;
	var limit      = ("" != json.limit ) ? parseInt( json.limit  ) : 0;
	
	if ( limit )
	{
		parameters        = new Object();
		parameters.limit  = limit;
		parameters.offset = limit + offset;
	}
	
	return parameters;
}

Object Get

ObjectGet( object, member )

If the member exists, returns the URI decoded value of objectmember], otherwise returns an empty string.

function Object_Get( $object, $member )
{
	if ( $object.hasOwnProperty( $member ) )
	{
		return decodeURI( $object[$member] );
	}
	else
	{
		return "";
	}
}

Replace

Replace( text, dictionary, recode = false )

For each key of the dictionary, searches and replaces any instances of '%key%' in the text with the value from the dictionary. If 'recode' is true, will decode html entities and re-encode using as URI componet.

function Replace( text, array, recode = false )
{
	for ( var member in array )
	{
		var key   = "%" + member + "%";
		var value = recode ? Recode( array[member] ) : array[member];

		while ( -1 != text.indexOf( key ) )
		{
			text = text.replace( key, value );
		}
	}
	return text;
}

Recode

Recode( html_entity_encoded_text )

function Recode( html_entity_encoded_text )
{
	var decoded = DecodeHTMLEntity( html_entity_encoded_text );
	var encoded = encodeURIComponent( decoded );

	return encoded;
}

DecodeHTMLEntity

DecodeHTMLEntity( html_entity_encoded_text )

function DecodeHTMLEntity( html_entity_encoded_text )
{
	var ret  = html_entity_encoded_text;
	var type = typeof html_entity_encoded_text;

	if ( "string" == type )
	{
		//	Based on:
		//	https://stackoverflow.com/a/29824550
		//
		ret = html_entity_encoded_text.replace(/&#(\d+);/g, function( match, dec ) { return String.fromCharCode(dec); } );
	}
	return ret;
}