<commit_before>var _ = require("lodash/core");

module.exports = function () {

	var DEFAULT_STATE = false;
	var COLOR_MODES = [
		{type: "same", range: [d3.rgb("#36C")]},
		{type: "gradient", range: [d3.rgb("#36C"), d3.rgb("#EE2867")]} // taken from Linked Data VOWL
	];

	var filter = {},
		nodes,
		properties,
		enabled = DEFAULT_STATE,
		filteredNodes,
		filteredProperties,
		colorModeType = "same";


	filter.filter = function (untouchedNodes, untouchedProperties) {
		nodes = untouchedNodes;
		properties = untouchedProperties;

		var externalElements = filterExternalElements(nodes.concat(properties));

		if (enabled) {
			setColorsForExternals(externalElements);
		} else {
			resetBackgroundColors(externalElements);
		}

		filteredNodes = nodes;
		filteredProperties = properties;
	};

	function filterExternalElements(nodes) {
		return nodes.filter(function (node) {
			return node.indications().indexOf("external") >= 0;
		});
	}

	function setColorsForExternals(elements) {
		var iriMap = mapExternalsToBaseUri(elements);
		var entries = iriMap.entries();

		var colorScale = d3.scale.linear()
			.domain([0, entries.length - 1])
			.range(_.find(COLOR_MODES, {type: colorModeType}).range)
			.interpolate(d3.interpolateHsl);

		for (var i = 0; i < entries.length; i++) {
			var groupedElements = entries[i].value;
			setBackgroundColorForNodes(groupedElements, colorScale(i));
		}
	}

	function mapExternalsToBaseUri(elements) {
		var map = d3.map();

		elements.forEach(function (element) {
			var baseIri = element.baseIri();

			if (!map.has(baseIri)) {
				map.set(baseIri, []);
			}
			map.get(baseIri).push(element);
		});

		return map;
	}

	function setBackgroundColorForNodes(elements, backgroundColor) {
		elements.forEach(function (element) {
			element.backgroundColor(backgroundColor);
		});
	}

	function resetBackgroundColors(elements) {
		elements.forEach(function (element) {
			element.backgroundColor(null);
		});
	}

	filter.colorModeType = function (p) {
		if (!arguments.length) return colorModeType;
		colorModeType = p;
		return filter;
	};

	filter.enabled = function (p) {
		if (!arguments.length) return enabled;
		enabled = p;
		return filter;
	};

	filter.reset = function () {
		enabled = DEFAULT_STATE;
	};


	// Functions a filter must have
	filter.filteredNodes = function () {
		return filteredNodes;
	};

	filter.filteredProperties = function () {
		return filteredProperties;
	};


	return filter;
};
<commit_msg>Fix colorscale returning black on multiple entries<commit_after>var _ = require("lodash/core");

module.exports = function () {

	var DEFAULT_STATE = false;
	var COLOR_MODES = [
		{type: "same", range: [d3.rgb("#36C"), d3.rgb("#36C")]},
		{type: "gradient", range: [d3.rgb("#36C"), d3.rgb("#EE2867")]} // taken from Linked Data VOWL
	];

	var filter = {},
		nodes,
		properties,
		enabled = DEFAULT_STATE,
		filteredNodes,
		filteredProperties,
		colorModeType = "same";


	filter.filter = function (untouchedNodes, untouchedProperties) {
		nodes = untouchedNodes;
		properties = untouchedProperties;

		var externalElements = filterExternalElements(nodes.concat(properties));

		if (enabled) {
			setColorsForExternals(externalElements);
		} else {
			resetBackgroundColors(externalElements);
		}

		filteredNodes = nodes;
		filteredProperties = properties;
	};

	function filterExternalElements(nodes) {
		return nodes.filter(function (node) {
			return node.indications().indexOf("external") >= 0;
		});
	}

	function setColorsForExternals(elements) {
		var iriMap = mapExternalsToBaseUri(elements);
		var entries = iriMap.entries();

		var colorScale = d3.scale.linear()
			.domain([0, entries.length - 1])
			.range(_.find(COLOR_MODES, {type: colorModeType}).range)
			.interpolate(d3.interpolateHsl);

		for (var i = 0; i < entries.length; i++) {
			var groupedElements = entries[i].value;
			setBackgroundColorForNodes(groupedElements, colorScale(i));
		}
	}

	function mapExternalsToBaseUri(elements) {
		var map = d3.map();

		elements.forEach(function (element) {
			var baseIri = element.baseIri();

			if (!map.has(baseIri)) {
				map.set(baseIri, []);
			}
			map.get(baseIri).push(element);
		});

		return map;
	}

	function setBackgroundColorForNodes(elements, backgroundColor) {
		elements.forEach(function (element) {
			element.backgroundColor(backgroundColor);
		});
	}

	function resetBackgroundColors(elements) {
		elements.forEach(function (element) {
			element.backgroundColor(null);
		});
	}

	filter.colorModeType = function (p) {
		if (!arguments.length) return colorModeType;
		colorModeType = p;
		return filter;
	};

	filter.enabled = function (p) {
		if (!arguments.length) return enabled;
		enabled = p;
		return filter;
	};

	filter.reset = function () {
		enabled = DEFAULT_STATE;
	};


	// Functions a filter must have
	filter.filteredNodes = function () {
		return filteredNodes;
	};

	filter.filteredProperties = function () {
		return filteredProperties;
	};


	return filter;
};

{'fraction_non_alphanumeric': 0.1358074222668004, 'fraction_numerical': 0.0078234704112337, 'mean_word_length': 4.298618490967057, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3130012', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1579, 'n_words': 325}