<commit_before>var global = this || window;

function identity(x) { return x; }

// String.trim polyfill
if (!String.prototype.trim) {
	String.prototype.trim = function () {
		return this.replace(/^\s+|\s+$/g, '');
	};
}

/**
 * Activates typeahead behavior for given element.
 * @param element (required) The DOM element to infect.
 * @param source (optional) The custom data source.
 */
Meteor.typeahead = function(element, source) {
	var $e = $(element);
	var datasets = resolve_datasets($e, source);

	$e.typeahead('destroy');

	var options = $e.data('options') || {};
	if (typeof options != 'object') {
		options = {};
	}

	function get_bool(name, defval) {
		var val = $e.data(name);
		return val === undefined ? defval : !!val;
	}

	// other known options passed via data attributes
	var highlight = get_bool('highlight', false);
	var hint = get_bool('hint', true);
	var autoselect = get_bool('autoselect', false);
	var minLength = get_min_length($e);

	options = $.extend(options, {
		highlight: highlight,
		hint: hint,
		minLength: minLength,
		autoselect: autoselect
	});

	var instance;
	if (Array.isArray(datasets)) {
		instance = $e.typeahead.apply($e, [options].concat(datasets));
	} else {
		var dataset = datasets;

		// TODO remove this when typeahead.js will support minLength = 0
		if (minLength === 0) {
			// based on @snekse suggestion (see https://github.com/twitter/typeahead.js/pull/719)
			var altSource = dataset.source;
			dataset.source = function(query, cb) {
				return query ? altSource(query, cb) : cb(dataset.local());
			};
		}

		instance = $e.typeahead(options, dataset);
	}

	// bind event handlers
	[
		"opened",
		"closed",
		"selected",
		"autocompleted",
	].forEach(function(name) {
		var fn = resolve_template_function($e[0], $e.data(name));
		if ($.isFunction(fn)) {
			instance.on('typeahead:' + name, fn);
		}
	});

	// fix to apply bootstrap form-control to tt-hint
	// TODO support other classes if needed
	if ($e.hasClass('form-control')) {
		$e.parent('.twitter-typeahead').find('.tt-hint').addClass('form-control');
	}

	// TODO remove this when typeahead.js will support minLength = 0
	if (minLength === 0) {
		$e.on('focus', function() {
			if ($e.val() === '') {
				$e.data('ttTypeahead').input.trigger('queryChanged', '');
			}
		});
	}

	return instance;
};

/**
 * Activates all typeahead elements.
 * @param selector (optional) selector to find typeahead elements to be activated
 */
Meteor.typeahead.inject = function(selector) {
	if (!selector) {
		selector = '.typeahead';
	}
	$(selector).each(function(i,e) {
		try {
			Meteor.typeahead(e);
		} catch (err) {
			console.log(err);
		}
	});
};

function resolve_datasets($e, source) {
	var element = $e[0];
	var datasets = $e.data('sources') || $e.data('sets');
	if (datasets) {
		if (typeof datasets == 'string') {
			datasets = resolve_template_function(element, datasets);
		}
		if ($.isFunction(datasets)) {
			datasets = datasets() || [];
		}
		return datasets.map(function(ds) {
			return make_bloodhound(ds);
		});
	}

	var name = normalize_dataset_name($e.attr('name') || $e.attr('id') || 'dataset');
	var limit = $e.data('limit');
	var templateName = $e.data('template'); // specifies name of custom template
	var templates = $e.data('templates'); // specifies custom templates
	var valueKey = $e.data('value-key') || 'value';
	var minLength = get_min_length($e);

	if (!source) {
		source = $e.data('source') || [];
	}

	var dataset = {<commit_msg>Fixes multi-inject bug<commit_after>var global = this || window;

function identity(x) { return x; }

// String.trim polyfill
if (!String.prototype.trim) {
	String.prototype.trim = function () {
		return this.replace(/^\s+|\s+$/g, '');
	};
}

/**
 * Activates typeahead behavior for given element.
 * @param element (required) The DOM element to infect.
 * @param source (optional) The custom data source.
 */
Meteor.typeahead = function(element, source) {
	var $e = $(element);
	var datasets = resolve_datasets($e, source);

	$e.typeahead('destroy');

	var options = $e.data('options') || {};
	if (typeof options != 'object') {
		options = {};
	}

	function get_bool(name, defval) {
		var val = $e.data(name);
		return val === undefined ? defval : !!val;
	}

	// other known options passed via data attributes
	var highlight = get_bool('highlight', false);
	var hint = get_bool('hint', true);
	var autoselect = get_bool('autoselect', false);
	var minLength = get_min_length($e);

	options = $.extend(options, {
		highlight: highlight,
		hint: hint,
		minLength: minLength,
		autoselect: autoselect
	});

	var instance;
	if (Array.isArray(datasets)) {
		instance = $e.typeahead.apply($e, [options].concat(datasets));
	} else {
		var dataset = datasets;

		// TODO remove this when typeahead.js will support minLength = 0
		if (minLength === 0) {
			// based on @snekse suggestion (see https://github.com/twitter/typeahead.js/pull/719)
			var altSource = dataset.source;
			dataset.source = function(query, cb) {
				return query ? altSource(query, cb) : cb(dataset.local());
			};
		}

		instance = $e.typeahead(options, dataset);
	}

	// bind event handlers
	[
		"opened",
		"closed",
		"selected",
		"autocompleted",
	].forEach(function(name) {
		var fn = resolve_template_function($e[0], $e.data(name));
		if ($.isFunction(fn)) {
			instance.on('typeahead:' + name, fn);
		}
	});

	// fix to apply bootstrap form-control to tt-hint
	// TODO support other classes if needed
	if ($e.hasClass('form-control')) {
		$e.parent('.twitter-typeahead').find('.tt-hint').addClass('form-control');
	}

	// TODO remove this when typeahead.js will support minLength = 0
	if (minLength === 0) {
		$e.on('focus', function() {
			if ($e.val() === '') {
				$e.data('ttTypeahead').input.trigger('queryChanged', '');
			}
		});
	}

	return instance;
};

/**
 * Activates all typeahead elements.
 * @param selector (optional) selector to find typeahead elements to be activated
 */
Meteor.typeahead.inject = function(selector) {
	if (!selector) {
		selector = '.typeahead';
	}

	// See if we have a template instance to reference
	var template = Template.instance();
	if (!template) {
		// If we don't, just init on the entire DOM
		$(selector).each(init_typeahead);
	} else {
		// Otherwise just init this template's typeaheads
		template.$(selector).each(init_typeahead);
	}
};

function init_typeahead(index, element) {
	try {
		if (element.data('ttTypeahead') == null) {
			Meteor.typeahead(element);
		}
	} catch (err) {
		console.log(err);
		return;
	}
}

function resolve_datasets($e, source) {
	var element = $e[0];
	var datasets = $e.data('sources') || $e.data('sets');
	if (datasets) {
		if (typeof datasets == 'string') {
			datasets = resolve_template_function(element, datasets);
		}
		if ($.isFunction(datasets)) {
			datasets = datasets() || [];
		}
		return datasets.map(function(ds) {
			return make_bloodhound(ds);
		});
	}

	var name = normalize_dataset_name($e.attr('name') || $e.attr('id') || 'dataset');
	var limit = $e.data('limit');
	var templateName = $e.data('template'); // specifies name of custom template
	var templates = $e.data('templates'); // specifies custom templates
	var valueKey = $e.data('value-key') || 'value';
	var minLength = get_min_length($e);

	if (!source) {
		source = $e.data('source') || [];
	}

	var dataset = {
{'fraction_non_alphanumeric': 0.17359212684527064, 'fraction_numerical': 0.0024603608529250955, 'mean_word_length': 3.878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1280964', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2426, 'n_words': 606}