<commit_before>/**
 * @author vstross
 */

// Load the [underscore] library (try for both test & studio environments)
try {var _ = require('com.leftlanelab.firebase.underscore'); }
catch (err) {var _ = require('modules/com.leftlanelab.firebase/1.3/platform/iphone/com.leftlanelab.firebase.underscore');}

var _instances = {'Firebase':0, 'FirebaseQuery':0},
	_firebase = false,
	_forge = false;

/**
 * Public API Endpoint for getting a [Firebase] reference or module proxy
 *
 ******************************************************************************/
exports.new = function (url)
{
	// Only execute on the first Firebase...
	if (_instances.Firebase === 0)
	{
        _instances.Firebase++;

		// Set the global [firebase] handle
		_firebase = this;

		// Local Disk Persistence
		if (Ti.App.Properties.getBool('com.leftlanelab.firebase.persistence', false))
		{
			_firebase.persistence();
		}

		// Try to set the [forge] from App Properties
		_forge = Ti.App.Properties.getString('com.leftlanelab.firebase.forge', false);
	}

	// Return a new Firebase API Controller
	return new Firebase(url);
};

/**
 * Public API Endpoint for ServerValue Class
 *
 ******************************************************************************/
exports.ServerValue =
{
	'TIMESTAMP' : {'.sv':'timestamp'}
};

/*
===============================================================================>
	Firebase
===============================================================================>
*/
/*
 * Firebase API Controller
 *
 ******************************************************************************/
var Firebase = function (url)
{
	// Global Variables
	this.listeners = {};

	// Safety Net
	if (! _forge && (_.isUndefined(url) || _.isEmpty(url))) {return false;}

	// Set the [url] (allows for absolute &| empty [forge]), then strip trailing "/" for the sloppy types...
	this.url = (! _.isUndefined(url) && url.indexOf('https://') === 0 ? url : (_forge || '') + (url || '')).replace(/^https\:\/\/([\S]+[^\/])[\/]?/i, "https://$1");

	// Return the new [Firebase] pseudo-reference
	return this;
};

/*
 * Quick & Easy Class detection hack
 *
 ******************************************************************************/
Firebase.prototype.id = 'com.leftlanelab.firebase';
Firebase.prototype.version = '1.3';

Firebase.prototype.testy = function()
{
    _firebase.testy();
};

/*
 * Authenticates a Firebase client
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.auth = function (authToken, onComplete, onCancel)
{
	// Safety Net
	if (! _.isString(authToken)) {return false;}

	// Kick the [firebase]
	_firebase.auth(this.url, authToken,
		(_.isFunction(onComplete) ? onComplete : null),
		(_.isFunction(onCancel) ? onCancel : null)
	);

	return this;
};

/*
 * De-Authenticates a Firebase client
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.unauth = function (onComplete)
{
	// Kick the [firebase]
	_firebase.unauth(this.url, (_.isFunction(onComplete) ? onComplete : null));

	return this;
};

/*
 * Returns a new Firebase reference
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.child = function (path)
{
	// Safety Net
	if (! _.isString(path)) {return false;}

	// Create the [child] reference
	return _firebase.new(this.url + '/' + (path.indexOf('/') === 0 ? path.substring(1) : path));
};

/*
 * Returns a new Firebase reference
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.parent = function ()
{
	// Simple Safety Net (already at the top)
	if (! this.url.match(/^https\:\/\/([\S]*[^\/])\/[\S][^\/]*/i)) {return null;}

	// Pop the [child] off and the new reference is the [parent]
	return _firebase.new(this.url.replace(/^https\:\/\/([\S]*[^\/])\/[\S][^\/]*/i, "https://$1"));
};

/*
 * Returns a new Firebase reference
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.root = function ()
{
	// Pop the [children] off and the new reference is the [root]
	return _firebase.new(this.url.replace(/^https\:\/\/([^\/?#]+)[\/]?([\S]+)?/i, "https://$1"));
};

/*
 * Returns a the last token of this location as a string
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.name = function ()
{
	// Simple Safety Net (already at the top)
	if (! this.url.match(/^https\:\/\/([\S]*[^\/])\/[\S][^\/]*/i)) {return null;}

	// Pop the [child] off and you have the [name]
	return this.url.replace(/^https\:\/\/[\S]+\/([^\/]+)[\/]?/i, "$1");
};

/*
 * Returns the current full [url] property as a string
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.toString = function ()
{
	// You can't handle the truth!
	return this.url;
};

/*<commit_msg>Renamed name method to key to reflect API changes<commit_after>/**
 * @author vstross
 */

// Load the [underscore] library (try for both test & studio environments)
try {var _ = require('com.leftlanelab.firebase.underscore'); }
catch (err) {var _ = require('modules/com.leftlanelab.firebase/1.3/platform/iphone/com.leftlanelab.firebase.underscore');}

var _instances = {'Firebase':0, 'FirebaseQuery':0},
	_firebase = false,
	_forge = false;

/**
 * Public API Endpoint for getting a [Firebase] reference or module proxy
 *
 ******************************************************************************/
exports.new = function (url)
{
	// Only execute on the first Firebase...
	if (_instances.Firebase === 0)
	{
        _instances.Firebase++;

		// Set the global [firebase] handle
		_firebase = this;

		// Local Disk Persistence
		if (Ti.App.Properties.getBool('com.leftlanelab.firebase.persistence', false))
		{
			_firebase.persistence();
		}

		// Try to set the [forge] from App Properties
		_forge = Ti.App.Properties.getString('com.leftlanelab.firebase.forge', false);
	}

	// Return a new Firebase API Controller
	return new Firebase(url);
};

/**
 * Public API Endpoint for ServerValue Class
 *
 ******************************************************************************/
exports.ServerValue =
{
	'TIMESTAMP' : {'.sv':'timestamp'}
};

/*
===============================================================================>
	Firebase
===============================================================================>
*/
/*
 * Firebase API Controller
 *
 ******************************************************************************/
var Firebase = function (url)
{
	// Global Variables
	this.listeners = {};

	// Safety Net
	if (! _forge && (_.isUndefined(url) || _.isEmpty(url))) {return false;}

	// Set the [url] (allows for absolute &| empty [forge]), then strip trailing "/" for the sloppy types...
	this.url = (! _.isUndefined(url) && url.indexOf('https://') === 0 ? url : (_forge || '') + (url || '')).replace(/^https\:\/\/([\S]+[^\/])[\/]?/i, "https://$1");

	// Return the new [Firebase] pseudo-reference
	return this;
};

/*
 * Quick & Easy Class detection hack
 *
 ******************************************************************************/
Firebase.prototype.id = 'com.leftlanelab.firebase';
Firebase.prototype.version = '1.3';

Firebase.prototype.testy = function()
{
    _firebase.testy();
};

/*
 * Authenticates a Firebase client
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.auth = function (authToken, onComplete, onCancel)
{
	// Safety Net
	if (! _.isString(authToken)) {return false;}

	// Kick the [firebase]
	_firebase.auth(this.url, authToken,
		(_.isFunction(onComplete) ? onComplete : null),
		(_.isFunction(onCancel) ? onCancel : null)
	);

	return this;
};

/*
 * De-Authenticates a Firebase client
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.unauth = function (onComplete)
{
	// Kick the [firebase]
	_firebase.unauth(this.url, (_.isFunction(onComplete) ? onComplete : null));

	return this;
};

/*
 * Returns a new Firebase reference
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.child = function (path)
{
	// Safety Net
	if (! _.isString(path)) {return false;}

	// Create the [child] reference
	return _firebase.new(this.url + '/' + (path.indexOf('/') === 0 ? path.substring(1) : path));
};

/*
 * Returns a new Firebase reference
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.parent = function ()
{
	// Simple Safety Net (already at the top)
	if (! this.url.match(/^https\:\/\/([\S]*[^\/])\/[\S][^\/]*/i)) {return null;}

	// Pop the [child] off and the new reference is the [parent]
	return _firebase.new(this.url.replace(/^https\:\/\/([\S]*[^\/])\/[\S][^\/]*/i, "https://$1"));
};

/*
 * Returns a new Firebase reference
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.root = function ()
{
	// Pop the [children] off and the new reference is the [root]
	return _firebase.new(this.url.replace(/^https\:\/\/([^\/?#]+)[\/]?([\S]+)?/i, "https://$1"));
};

/*
 * Returns a the last token of this location as a string
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.key = function ()
{
	// Simple Safety Net (already at the top)
	if (! this.url.match(/^https\:\/\/([\S]*[^\/])\/[\S][^\/]*/i)) {return null;}

	// Pop the [child] off and you have the [key]
	return this.url.replace(/^https\:\/\/[\S]+\/([^\/]+)[\/]?/i, "$1");
};

/*
 * Returns the current full [url] property as a string
 *
 * 	- Matches Firebase Library (VS: 2014-07-23)
 ******************************************************************************/
Firebase.prototype.toString = function ()
{
	// You can't handle the truth!
	return this.url;
};

/*
{'fraction_non_alphanumeric': 0.34952561669829224, 'fraction_numerical': 0.013282732447817837, 'mean_word_length': 4.358922216573462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4103527', 'n_tokens_mistral': 3351, 'n_tokens_neox': 3079, 'n_words': 742}