<commit_before>import Pegjs from "pegjs";

import Grammar from "./grammar.js";

const Parser = Pegjs.buildParser(Grammar);

class Compiler {

	static compile(graph) {
		return Compiler.merge(Compiler.flattenAll(Compiler.traverse(Parser.parse(graph))));
	}

	static flatten(path) {
		let flattened = [];
		for (const element of path) {
			if (Array.isArray(element)) {
				flattened = flattened.concat(Compiler.flatten(element));
			} else {
				flattened.push(element);
			}
		}
		return flattened;
	}

	static flattenAll(paths) {
		return paths.map((path) => {
			return Compiler.flatten(path);
		});
	}

	static merge(paths) {
		const merged = [];
		for (const path of paths) {
			const atom = path.pop();
			let index = -1;
			for (const checkPath in merged) {
				if (JSON.stringify(path) === JSON.stringify(merged[checkPath].slice(0, -1))) {
					index = checkPath;
					break;
				}
			}
			if (index >= 0) {
				merged[index][merged[index].length - 1].push(atom);
			} else {
				path.push([
					atom
				]);
				merged.push(path);
			}
		}
		for (const path of merged) {
			if (path[path.length - 1].length === 1) {
				path[path.length - 1] = path[path.length - 1][0];
			}
		}
		return merged;
	}

	static traverse(items) {
		const paths = [];
		for (const item of items) {
			const path = [];
			path.push(item.identifier);
			if (item.filters) {
				if (item.filters[0].key === "id" || item.filters[0].key === "index") {
					path.push(item.filters[0].value | 0);
				} else {
					path.push(item.filters.reduce((filters, filter) => {
						filters[filter.key] = filter.value | 0;
						return filters;
					}, {}));
				}
			}
			if (item.children) {
				const childrenPaths = Compiler.traverse(item.children);
				for (const childPath of childrenPaths) {
					const fullPath = [].concat(path);
					fullPath.push(childPath);
					paths.push(fullPath);
				}
			} else {
				paths.push(path);
			}
		}
		return paths;
	}

}

export default Compiler.compile;<commit_msg>Removed unnecessary class wrapper<commit_after>import Pegjs from "pegjs";

import Grammar from "./grammar.js";

const Parser = Pegjs.buildParser(Grammar);

const flatten = (path) => {
	let flattened = [];
	for (const element of path) {
		if (Array.isArray(element)) {
			flattened = flattened.concat(flatten(element));
		} else {
			flattened.push(element);
		}
	}
	return flattened;
};

const flattenAll = (paths) => {
	return paths.map((path) => {
		return flatten(path);
	});
};

const merge = (paths) => {
	const merged = [];
	for (const path of paths) {
		const atom = path.pop();
		let index = -1;
		for (const checkPath in merged) {
			if (JSON.stringify(path) === JSON.stringify(merged[checkPath].slice(0, -1))) {
				index = checkPath;
				break;
			}
		}
		if (index >= 0) {
			merged[index][merged[index].length - 1].push(atom);
		} else {
			path.push([atom]);
			merged.push(path);
		}
	}
	for (const path of merged) {
		if (path[path.length - 1].length === 1) {
			path[path.length - 1] = path[path.length - 1][0];
		}
	}
	return merged;
};

const traverse = (items) => {
	const paths = [];
	for (const item of items) {
		const path = [];
		path.push(item.identifier);
		if (item.filters) {
			if (item.filters[0].key === "id" || item.filters[0].key === "index") {
				path.push(item.filters[0].value | 0);
			} else {
				path.push(item.filters.reduce((filters, filter) => {
					filters[filter.key] = filter.value | 0;
					return filters;
				}, {}));
			}
		}
		if (item.children) {
			const childrenPaths = traverse(item.children);
			for (const childPath of childrenPaths) {
				const fullPath = [].concat(path);
				fullPath.push(childPath);
				paths.push(fullPath);
			}
		} else {
			paths.push(path);
		}
	}
	return paths;
};

export default (graph) => {
	return merge(flattenAll(traverse(Parser.parse(graph))));
};
{'fraction_non_alphanumeric': 0.16976196704159038, 'fraction_numerical': 0.007847240387130526, 'mean_word_length': 3.0422832980972516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '2797421', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1335, 'n_words': 285}