<commit_before>/* OWID standalone utility functions */

;(function() {
	"use strict";

	var owid = {};

	owid.displayYear = function(year) {
		year = parseInt(year);
		if (isNaN(year)) {
			console.error("Invalid year '" + year + "'");
			return null;
		}

		if (year < 0)
			return Math.abs(year) + " BCE";
		else
			return year.toString();
	};

	owid.tryParseFloat = function(val) {
		var asFloat = parseFloat(val);
		if (isNaN(asFloat))
			return val;
		else
			return asFloat;
	};

	owid.timeRangesToString = function(timeRanges) {
		var timeRangeStrs = [];

		_.each(timeRanges, function(timeRange) {
			if (timeRange.year) 
				timeRangeStrs.push(timeRange.year.toString());
			else {
				var s = timeRange.startYear + " to " + timeRange.endYear;
				if (timeRange.interval) s += " every " + timeRange.interval;
				timeRangeStrs.push(s);
			}
		});

		return timeRangeStrs.join("; ");
	};

	owid.timeRangesToYears = function(timeRanges, first, last) {
		if (_.isEmpty(timeRanges)) {
			timeRanges = [{ startYear: 'first', endYear: 'last' }];
		}

		var outputYears = [];

		var parseYear = function(year) {
			var result;

			if (year == "first") result = parseInt(first);
			else if (year == "last") result = parseInt(last);
			else result = parseInt(year);

			if (isNaN(result))
				throw new TypeError("Couldn't parse year: " + result);

			return result;
		};

		_.each(timeRanges, function(timeRange) {
			if (timeRange.year)
				outputYears.push(parseYear(timeRange.year));
			else {
				var startYear = parseYear(timeRange.startYear);
				var endYear = parseYear(timeRange.endYear);
				var interval = timeRange.interval || 1;

				if (startYear > endYear) {
					var tmp = endYear;
					endYear = startYear;
					startYear = tmp;
				}

				for (var i = startYear; i <= endYear; i += interval) {
					outputYears.push(i);
				}
			}
		});

		return _.uniq(_.sortBy(outputYears), true);
	};

	owid.timeRangesFromString = function(timeRangesStr) {
		if (!timeRangesStr)
			return [];
		
		var timeRanges = [];
		var rangeStrs = timeRangesStr.split(';');

		var validateYear = function(yearStr) {
			if (yearStr == "first" || yearStr == "last") 
				return yearStr;
			else {
				var year = parseInt(yearStr);
				if (!year) {
					throw new RangeError("Invalid year " + yearStr);
				} else {
					return year;
				}
			}
		};

		_.each(rangeStrs, function(rangeStr) {
			var timeRange = {};
			rangeStr = $.trim(rangeStr);

			var range = rangeStr.match(/^([0-9-]+|first|last|) to ([0-9-]+|first|last)(?: every ([0-9-]+))?$/);
			if (range) {
				var startYear = validateYear(range[1]);
				var endYear = validateYear(range[2]);
				var interval = range[3] ? parseInt(range[3]) : null;

				timeRange.startYear = startYear;
				timeRange.endYear = endYear;
				if (interval) timeRange.interval = interval;
			} else if (rangeStr.match(/^([0-9-]+|first|last)$/)) {
				var year = validateYear(rangeStr);
				timeRange.year = year;
			} else {
				throw RangeError("Invalid range " + rangeStr);
			}

			timeRanges.push(timeRange);
		});

		return timeRanges;
	};

	owid.scatterPlotContentGenerator = function(data) {
		var unitsString = App.ChartModel.get("units"),
			units = !_.isEmpty(unitsString) ? $.parseJSON(unitsString) : {},
			outputHtml = "";

		var times = data.point.time; // e.g. { x: 1990 }
		var heading = data.series[0].key; // e.g. "United Arab Emirates"
		outputHtml += "<h3>" + heading + "</h3><p>";
		_.each(data.point, function(value, key) {
			if (key == "time" || key == "series" || key == "color") return;

			var unit = _.findWhere(units, { property: key }),
				isHidden = ( unit && unit.hasOwnProperty( "visible" ) && !unit.visible )? true: false;

			if (isHidden) return;
			
			value = App.Utils.formatNumeric(unit, value);

			var unitSetting = (unit && unit.unit)||"";
			var titleSetting = (unit && unit.title)||"";

			var valueString = (_.isEmpty(titleSetting) ? "" : titleSetting + ": ") + value + " " + unitSetting;<commit_msg>Fix scatterplot mouseover issue<commit_after>/* OWID standalone utility functions */

;(function() {
	"use strict";

	var owid = {};

	owid.displayYear = function(year) {
		year = parseInt(year);
		if (isNaN(year)) {
			console.error("Invalid year '" + year + "'");
			return null;
		}

		if (year < 0)
			return Math.abs(year) + " BCE";
		else
			return year.toString();
	};

	owid.tryParseFloat = function(val) {
		var asFloat = parseFloat(val);
		if (isNaN(asFloat))
			return val;
		else
			return asFloat;
	};

	owid.timeRangesToString = function(timeRanges) {
		var timeRangeStrs = [];

		_.each(timeRanges, function(timeRange) {
			if (timeRange.year) 
				timeRangeStrs.push(timeRange.year.toString());
			else {
				var s = timeRange.startYear + " to " + timeRange.endYear;
				if (timeRange.interval) s += " every " + timeRange.interval;
				timeRangeStrs.push(s);
			}
		});

		return timeRangeStrs.join("; ");
	};

	owid.timeRangesToYears = function(timeRanges, first, last) {
		if (_.isEmpty(timeRanges)) {
			timeRanges = [{ startYear: 'first', endYear: 'last' }];
		}

		var outputYears = [];

		var parseYear = function(year) {
			var result;

			if (year == "first") result = parseInt(first);
			else if (year == "last") result = parseInt(last);
			else result = parseInt(year);

			if (isNaN(result))
				throw new TypeError("Couldn't parse year: " + result);

			return result;
		};

		_.each(timeRanges, function(timeRange) {
			if (timeRange.year)
				outputYears.push(parseYear(timeRange.year));
			else {
				var startYear = parseYear(timeRange.startYear);
				var endYear = parseYear(timeRange.endYear);
				var interval = timeRange.interval || 1;

				if (startYear > endYear) {
					var tmp = endYear;
					endYear = startYear;
					startYear = tmp;
				}

				for (var i = startYear; i <= endYear; i += interval) {
					outputYears.push(i);
				}
			}
		});

		return _.uniq(_.sortBy(outputYears), true);
	};

	owid.timeRangesFromString = function(timeRangesStr) {
		if (!timeRangesStr)
			return [];
		
		var timeRanges = [];
		var rangeStrs = timeRangesStr.split(';');

		var validateYear = function(yearStr) {
			if (yearStr == "first" || yearStr == "last") 
				return yearStr;
			else {
				var year = parseInt(yearStr);
				if (!year) {
					throw new RangeError("Invalid year " + yearStr);
				} else {
					return year;
				}
			}
		};

		_.each(rangeStrs, function(rangeStr) {
			var timeRange = {};
			rangeStr = $.trim(rangeStr);

			var range = rangeStr.match(/^([0-9-]+|first|last|) to ([0-9-]+|first|last)(?: every ([0-9-]+))?$/);
			if (range) {
				var startYear = validateYear(range[1]);
				var endYear = validateYear(range[2]);
				var interval = range[3] ? parseInt(range[3]) : null;

				timeRange.startYear = startYear;
				timeRange.endYear = endYear;
				if (interval) timeRange.interval = interval;
			} else if (rangeStr.match(/^([0-9-]+|first|last)$/)) {
				var year = validateYear(rangeStr);
				timeRange.year = year;
			} else {
				throw RangeError("Invalid range " + rangeStr);
			}

			timeRanges.push(timeRange);
		});

		return timeRanges;
	};

	owid.scatterPlotTooltipGenerator = function(data) {
		var unitsString = App.ChartModel.get("units"),
			units = !_.isEmpty(unitsString) ? $.parseJSON(unitsString) : {},
			outputHtml = "";

		var times = data.point.time; // e.g. { x: 1990 }
		var heading = data.series[0].key; // e.g. "United Arab Emirates"
		outputHtml += "<h3>" + heading + "</h3><p>";
		_.each(data.point, function(value, key) {
			if (key == "time" || key == "series" || key == "color") return;

			var unit = _.findWhere(units, { property: key }),
				isHidden = ( unit && unit.hasOwnProperty( "visible" ) && !unit.visible )? true: false;

			if (isHidden) return;
			
			value = App.Utils.formatNumeric(unit, value);

			var unitSetting = (unit && unit.unit)||"";
			var titleSetting = (unit && unit.title)||"";

			var valueString = (_.isEmpty(titleSetting) ? "" : titleSetting + ": ") + value + " " + unitSetting;
{'fraction_non_alphanumeric': 0.16649949849548645, 'fraction_numerical': 0.00526579739217653, 'mean_word_length': 3.25667022411953, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, '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': '5295725', 'n_tokens_mistral': 3256, 'n_tokens_neox': 2721, 'n_words': 598}