<commit_before>/*
==================================================================================================
  Core - JavaScript Foundation
  Copyright 2010-2012 <NAME>
==================================================================================================
*/

(function(global) {
	
	// defineProperty exists in IE8 but will error when trying to define a property on
	// native objects. IE8 does not have defineProperies, however, so this check saves a try/catch block.
	if(Object.defineProperty && Object.defineProperties)
	{
		var add = function(target, name, method) 
		{
			Object.defineProperty(target, name, 
			{
				value: method, 
				configurable: true, 
				enumerable: false, 
				writeable: true 
			});
		};
	}
	else 
	{
		var add = function(target, name, method) {
			target[name] = method;
		};
	};
	
	
	/** {Map} Stores and maps namespaces */
	var cache = {};


	/**
	 * Add @members {Map} to the object found under the given @name {String|Object}.
	 */
	function addStatics(name, members) 
	{
		var object = typeof name == "string" ? global[name] || cache[name] : name;
		var prefix = name + ".";
		for (var memberName in members) 
		{
			var item = members[memberName];
			if (item instanceof Function) {
				item.displayName = prefix + name;
			}

			add(object, name, item);
		}
	}


	/**
	 * Add @members {Map} to the prototype of the object found under the given @name {String|Object}.
	 */
	function addMembers(name, members) 
	{
		var object = typeof name == "string" ? global[name] || cache[name] : name;
		var proto = object.prototype;
		var prefix = name + ".prototype.";
		for (var memberName in members) 
		{
			var item = members[memberName];
			if (item instanceof Function) {
				item.displayName = prefix + name;
			}

			add(proto, name, item);
		}
	}
	
	
	/**
	 * Declares the given name and stores the given object onto it.
	 *
	 * @param name {String} Namespace/Package e.g. foo.bar.baz
	 * @param object {Object|Function} Any object or function
	 * @return {Object} Returns the given object
	 */
	function declareNamespace(name, object, duplicate)
	{
		var splits = name.split(".");
		var current = global;
		var length = splits.length-1;
		var segment;
		var i = 0;

		while(i<length)
		{
			segment = splits[i++];
			if (current[segment] == null) {
				current = current[segment] = {};
			} else {
				current = current[segment];
			}
		}

		return cache[name] = current[splits[i]] = object;
	}
	
	
	/**
	 * {Array} Returns all registers names (modules, interfaces, classes, etc.)
	 */
	function getNamespaces() {
		return Object.keys(cache);
	}


	/**
	 * {Boolean} Clears the object under the given @name {String} (including name cache) and returns if that was successful.
	 */
	function clearNamespace(name)
	{
		if (name in cache)
		{
			delete cache[name];

			var current = global;
			var splitted = name.split(".");
			for (var i=0, l=splitted.length-1; i<l; i++) {
				current = current[splitted[i]];
			}

			// Delete might not work when global object is affected
			try{
				delete current[splitted[i]];
			} catch(ex) {
				current[splitted[i]] = undef;
			}

			return true;
		}

		return false;
	}


	/**
	 * {Object|Function|Array} Resolves a given @name {String} into the item stored unter it.
	 */
	function resolveNamespace(name)
	{
		var current = cache[name];
		if (!current)
		{
			current = global;
			if (name)
			{
				var splitted = name.split(".");
				for (var i=0, l=splitted.length; i<l; i++)
				{
					current = current[splitted[i]];
					if (!current)
					{
						current = null;
						break;
					}
				}
			}
		}

		return current;
	}
	

	/**
	 * {Boolean} Whether the given @object {Object} or the @property {String?} inside the given object is a native host type.
	 */
	function isHostType(object, property) 
	{
		var type = object != null ? typeof object[property] : 'number';
		return !/^(?:boolean|number|string|undefined)$/.test(type) && (type == 'object' ? !!object[property] : true);
	}


	// Prefill cache for `isClassOf`
	var toStringMap = {};
	"Array Function RegExp Object Date Number String".replace(/\w+/g, function(cls) {
		toStringMap[cls] = "[object " + cls + "]";
	});
	
	
	/**
	 * {Boolean} Whether the given value is an instance of the given native class @name.
	 *
	 * Supports these classes:
	 *
	 * - `Array`
	 * - `Function`
	 * - `RegExp`
	 * - `Object`
	 * - `Date`
	 * - `Number`
	 * - `String`
	 */
	function isClassOf(value, name) {
		return value != null && toStringMap[toString.call(value)] == name;
	}
	
	
	/**
	 * {Boolean} Whether the given value is of the given type
	 *
	 * Supports all class checks support by `isClassOf` but also:
	 * 
	 * - `Map`
	 * - `Integer`
	 * - `Primitive`
	 */
	function isTypeOf(value, type) 
	{
		
		var result;
		
		if (toStringMap[type]) 
		{
			result = isClassOf(value, type);
		}
		else if (type == "Map") 
		{
			result = isClassOf(value, "Object") && value.constructor === Object;
		}
		else if (type == "Integer") 
		{
			result = isClassOf(value, "Number") && (~~value) == value;
		} 
		else if (type == "Primitive") 
		{
			var type = typeof value;
			result = value == null || type == "boolean" || type == "number" || type == "string";
		}
		
		return result;
	}
	
	
	// Temporary hack to make next statement workable
	Object.prototype.addStatics = addStatics;
	
	/**
	 * Useful root methods to add members to objects
	 */
	Object.prototype.addStatics("Object", 
	{
		declareNamespace : declareNamespace,
		getNamespaces: getNamespaces,
		clearNamespace: clearNamespace,
		resolveNamespace: resolveNamespace,

		addStatics : addStatics,
		addMembers : addMembers,

		isHostType : isHostType,
		isClassOf : isClassOf,
		isTypeOf : isTypeOf
	});
	
})(this);<commit_msg>Numerous bug fixes. Added new validateKeys method. Support host type checks in isTypeOf.<commit_after>/*
==================================================================================================
  Core - JavaScript Foundation
  Copyright 2010-2012 <NAME>
==================================================================================================
*/

(function(global, toString) {
	
	// defineProperty exists in IE8 but will error when trying to define a property on
	// native objects. IE8 does not have defineProperies, however, so this check saves a try/catch block.
	if(Object.defineProperty && Object.defineProperties)
	{
		var add = function(target, name, method) 
		{
			Object.defineProperty(target, name, 
			{
				value: method, 
				configurable: true, 
				enumerable: false, 
				writeable: true 
			});
		};
	}
	else 
	{
		var add = function(target, name, method) {
			target[name] = method;
		};
	};
	
	
	/** {Map} Stores and maps namespaces */
	var cache = {};


	/**
	 * Add @members {Map} to the object found under the given @name {String|Object}.
	 */
	function addStatics(name, members) 
	{
		var object = typeof name == "string" ? global[name] || cache[name] : name;
		var prefix = name + ".";
		for (var memberName in members) 
		{
			var item = members[memberName];
			if (item instanceof Function) {
				item.displayName = prefix + name;
			}

			add(object, memberName, item);
		}
	}


	/**
	 * Add @members {Map} to the prototype of the object found under the given @name {String|Object}.
	 */
	function addMembers(name, members) 
	{
		var object = typeof name == "string" ? global[name] || cache[name] : name;
		var proto = object.prototype;
		var prefix = name + ".prototype.";
		for (var memberName in members) 
		{
			var item = members[memberName];
			if (item instanceof Function) {
				item.displayName = prefix + name;
			}

			add(proto, memberName, item);
		}
	}
	
	
	/**
	 * Declares the given name and stores the given object onto it.
	 *
	 * @param name {String} Namespace/Package e.g. foo.bar.baz
	 * @param object {Object|Function} Any object or function
	 * @return {Object} Returns the given object
	 */
	function declareNamespace(name, object, duplicate)
	{
		var splits = name.split(".");
		var current = global;
		var length = splits.length-1;
		var segment;
		var i = 0;

		while(i<length)
		{
			segment = splits[i++];
			if (current[segment] == null) {
				current = current[segment] = {};
			} else {
				current = current[segment];
			}
		}

		return cache[name] = current[splits[i]] = object;
	}
	
	
	/**
	 * {Array} Returns all registers names (modules, interfaces, classes, etc.)
	 */
	function getNamespaces() {
		return Object.keys(cache);
	}


	/**
	 * {Boolean} Clears the object under the given @name {String} (including name cache) and returns if that was successful.
	 */
	function clearNamespace(name)
	{
		if (name in cache)
		{
			delete cache[name];

			var current = global;
			var splitted = name.split(".");
			for (var i=0, l=splitted.length-1; i<l; i++) {
				current = current[splitted[i]];
			}

			// Delete might not work when global object is affected
			try{
				delete current[splitted[i]];
			} catch(ex) {
				current[splitted[i]] = undef;
			}

			return true;
		}

		return false;
	}


	/**
	 * {Object|Function|Array} Resolves a given @name {String} into the item stored unter it.
	 */
	function resolveNamespace(name)
	{
		var current = cache[name];
		if (!current)
		{
			current = global;
			if (name)
			{
				var splitted = name.split(".");
				for (var i=0, l=splitted.length; i<l; i++)
				{
					current = current[splitted[i]];
					if (!current)
					{
						current = null;
						break;
					}
				}
			}
		}

		return current;
	}
	

	/**
	 * {Boolean} Whether the given @object {Object} or the @property {String?} inside the given object is a native host type.
	 */
	function isHostType(object, property) 
	{
		var type = object != null ? typeof object[property] : 'number';
		return !/^(?:boolean|number|string|undefined)$/.test(type) && (type == 'object' ? !!object[property] : true);
	}


	// Prefill cache for `isClassOf`
	var toStringMap = {};
	"Array Function RegExp Object Date Number String Boolean".replace(/\w+/g, function(cls) {
		toStringMap[cls] = "[object " + cls + "]";
	});
	
	
	/**
	 * {Boolean} Whether the given value is an instance of the given native class @name.
	 *
	 * Supports these classes:
	 *
	 * - `Array`
	 * - `Function`
	 * - `RegExp`
	 * - `Object`
	 * - `Date`
	 * - `Number`
	 * - `String`
	 * - `Boolean`
	 */
	function isClassOf(value, name) {
		return value != null && toString.call(value) == toStringMap[name];
	}
	
	
	/**
	 * {Boolean} Whether the given value is of the given type
	 *
	 * Supports all class checks support by `isClassOf` but also:
	 * 
	 * - `Native`
	 * - `Map`
	 * - `Integer`
	 * - `Primitive`
	 */
	function isTypeOf(value, type) 
	{
		var result;
		
		if (toStringMap[type]) 
		{
			result = isClassOf(value, type);
		}
		else if (type == "Native")
		{
			result = isHostType(value);
		}
		else if (type == "Map") 
		{
			result = isClassOf(value, "Object") && value.constructor === Object;
		}
		else if (type == "Integer") 
		{
			result = isClassOf(value, "Number") && (~~value) == value;
		} 
		else if (type == "Primitive") 
		{
			var type = typeof value;
			result = value == null || type == "boolean" || type == "number" || type == "string";
		}
		else if (global.console)
		{
			console.warn("Unsupported type call to Object.isTypeOf(): " + type);
		}
		
		return result;
	}
	
	
	/**
	 * {String} Validates the @object {Map} to don't hold other keys than the ones defined by @allowed {Array}. 
	 * Returns first non matching key which was found or `undefined` if all keys are valid.
	 */
	function validateKeys(object, allowed) 
	{
		// Build lookup table
		var set = {};
		for (var i=0, l=allowed.length; i<l; i++) {
			set[allowed[i]] = true;
		}
		
		// Collect used keys
		var list = Object.keys(object);
		
		// Validate keys
		var invalid = [];
		for (var i=0, l=list.length; i<l; i++) 
		{
			var current = list[i];
			if (!set[current]) {
				invalid.push(current);
			}
		}
		
		return invalid;
	}	
	
	
	// Temporary hack to make next statement workable
	Object.addStatics = addStatics;
	
	/**
	 * Useful root methods to add members to objects
	 */
	Object.addStatics("Object", 
	{
		declareNamespace : declareNamespace,
		getNamespaces: getNamespaces,
		clearNamespace: clearNamespace,
		resolveNamespace: resolveNamespace,

		addStatics : addStatics,
		addMembers : addMembers,
		
		validateKeys : validateKeys,

		isHostType : isHostType,
		isClassOf : isClassOf,
		isTypeOf : isTypeOf
	});
	
})(this, Object.prototype.toString);
{'fraction_non_alphanumeric': 0.16765650080256822, 'fraction_numerical': 0.002568218298555377, 'mean_word_length': 3.07355344883949, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '717258', 'n_tokens_mistral': 4615, 'n_tokens_neox': 3878, 'n_words': 1165}