<commit_before>/**
 * vSphere Connect
 * 
 * @author <NAME> <<EMAIL>>
 * @license MIT
 * 
 */
module.exports = function(env) {
	
	// modules
	var _     = env.lodash;
	var util  = env.util;

	/**
	 * recursive formatting function
	 */
	function formatValue(value) {
		
		var out;
		var type = util.sType(value);
		
		// if there is a type, check all sub items for types
		if (_.has(value, 'attributes.type') && _.has(value, '$value') && _.keys(value).length === 2) {
			out = util.moRef(value);
		}
		else if (type) {
			if (util.isArray(value)) {
				out = [];
				
				var subtype = type;
				
				if (subtype.match(/^ArrayOf/)) {
					subtype = subtype.replace(/^ArrayOf/, '');

					if (!Array.isArray(_.get(value, subtype)) && value[subtype]) {
						value[subtype] = [value[subtype]];
					}
				}
				
				_.forEach(value[_.keys(value)[1]], function(obj) {
					out.push(formatValue(obj));
				});
			}
			else if (util.isBoolean(value)) {
				out = Boolean(value.$value);
			}
			else if (_.has(value, '$value')) {
				out = value.$value;
			}
			else {
				out = formatValue(_.omit(value, 'attributes'));
			}
			
		}
		else if (Array.isArray(value)) {
			out = [];
			_.forEach(value, function(val) {
				if (util.sType(val) || util.isArray(val) || util.hasKeys(val)) {
					out.push(formatValue(val));
				}
				else {
					out.push(val);
				}
			});
		}
		else if (util.hasKeys(value)) {
			out = {};
			_.forEach(value, function(val, key) {
				if (util.sType(val) || util.isArray(val) || util.hasKeys(val)) {
					out[key] = formatValue(val);
				}
				else {
					out[key] = val;
				}
			});
		}
		else {
			out = value;
		}
		return out;
	}
	
	
	// format each object
	function formatProp(obj) {

		var out      = {};
		var props    = _.get(obj, 'propSet');
		var moRef    = _.has(obj, 'obj') ? util.moRef(obj.obj) : util.moRef(obj);
		out.id       = moRef.id;
		out.type     = moRef.type;

		// loop through the propSet
		if (Array.isArray(props)) {
			_.forEach(props, function(prop) {
				out[prop.name] = formatValue(prop.val);
			});
		}
		else if (_.has(props, 'name')) {
			out[props.name] = formatValue(props.val);
		}

		return out;
	}
	
	// since filters with dot notation return field names with dots, expand those fields
	// and remove the dot named field
	function expandFields(obj) {
		var out = {};
		
		_.forEach(obj, function(val, key) {
			if (_.includes(key, '.')) {
				_.set(out, key, val);
			}
			else {
				out[key] = val;
			}
		});

		return out;
	}
	
	
	// main formatting function
	function format(response) {

		var newObj = [];
		var objects = _.get(response, 'objects') || response;
		
		if (objects) {

			// check if objects is an array
			if (Array.isArray(objects)) {
				_.forEach(objects, function(obj) {
					newObj.push(expandFields(formatProp(obj)));
				});
			}
			else {
				newObj = expandFields(formatProp(objects));
			}
			return newObj;
		}

		// if no objects return the raw response
		return response;
	}
	
	
	// return format function
	return format;<commit_msg>fixed boolean typecast<commit_after>/**
 * vSphere Connect
 * 
 * @author <NAME> <<EMAIL>>
 * @license MIT
 * 
 */
module.exports = function(env) {
	
	// modules
	var _     = env.lodash;
	var util  = env.util;

	/**
	 * recursive formatting function
	 */
	function formatValue(value) {
		
		var out;
		var type = util.sType(value);
		
		// if there is a type, check all sub items for types
		if (_.has(value, 'attributes.type') && _.has(value, '$value') && _.keys(value).length === 2) {
			out = util.moRef(value);
		}
		else if (type) {
			if (util.isArray(value)) {
				out = [];
				
				var subtype = type;
				
				if (subtype.match(/^ArrayOf/)) {
					subtype = subtype.replace(/^ArrayOf/, '');

					if (!Array.isArray(_.get(value, subtype)) && value[subtype]) {
						value[subtype] = [value[subtype]];
					}
				}
				
				_.forEach(value[_.keys(value)[1]], function(obj) {
					out.push(formatValue(obj));
				});
			}
			else if (util.isBoolean(value)) {
				
				// check for valid true values, otherwise false
				if (_.includes(['1', 'true'], _.lowerCase(value.$value))) {
					out = true;
				}
				else {
					out = false;
				}
			}
			else if (_.has(value, '$value')) {
				out = value.$value;
			}
			else {
				out = formatValue(_.omit(value, 'attributes'));
			}
			
		}
		else if (Array.isArray(value)) {
			out = [];
			_.forEach(value, function(val) {
				if (util.sType(val) || util.isArray(val) || util.hasKeys(val)) {
					out.push(formatValue(val));
				}
				else {
					out.push(val);
				}
			});
		}
		else if (util.hasKeys(value)) {
			out = {};
			_.forEach(value, function(val, key) {
				if (util.sType(val) || util.isArray(val) || util.hasKeys(val)) {
					out[key] = formatValue(val);
				}
				else {
					out[key] = val;
				}
			});
		}
		else {
			out = value;
		}
		return out;
	}
	
	
	// format each object
	function formatProp(obj) {

		var out      = {};
		var props    = _.get(obj, 'propSet');
		var moRef    = _.has(obj, 'obj') ? util.moRef(obj.obj) : util.moRef(obj);
		out.id       = moRef.id;
		out.type     = moRef.type;

		// loop through the propSet
		if (Array.isArray(props)) {
			_.forEach(props, function(prop) {
				out[prop.name] = formatValue(prop.val);
			});
		}
		else if (_.has(props, 'name')) {
			out[props.name] = formatValue(props.val);
		}

		return out;
	}
	
	// since filters with dot notation return field names with dots, expand those fields
	// and remove the dot named field
	function expandFields(obj) {
		var out = {};
		
		_.forEach(obj, function(val, key) {
			if (_.includes(key, '.')) {
				_.set(out, key, val);
			}
			else {
				out[key] = val;
			}
		});

		return out;
	}
	
	
	// main formatting function
	function format(response) {

		var newObj = [];
		var objects = _.get(response, 'objects') || response;
		
		if (objects) {

			console.log(JSON.stringify(objects, null, '  '));
			
			// check if objects is an array
			if (Array.isArray(objects)) {
				_.forEach(objects, function(obj) {
					newObj.push(expandFields(formatProp(obj)));
				});
			}
			else {
				newObj = expandFields(formatProp(objects));
			}
			return newObj;
		}

		// if no objects return the raw response
		return response;
	}
	
	
	// return format function
	return format;
{'fraction_non_alphanumeric': 0.16964285714285715, 'fraction_numerical': 0.0007971938775510204, 'mean_word_length': 2.473421926910299, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '4758093', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2185, 'n_words': 489}