<commit_before>var express   = require('express');
	app       = express(),
	polyfills = require('./index'),
	useragent = require('useragent'),
	uglify    = require('uglify-js'),
	AliasResolver = require('./aliases');

// Load additional useragent features: primarily to use: agent.satisfies to
// test a browser version against a semver string
require('useragent/features');

var aliasResolver = new AliasResolver([
		function(polyfill) {

			var aliases = polyfills.aliases[polyfill.name];

			// If aliases exist, expand them adding aliasOf information to
			// each and tranferring the flags from the alias
			if (aliases) {
				return aliases.map(function(alias) {
					return {
						name: alias,
						flags: polyfill.flags,
						aliasOf: polyfill.aliasOf
					};
				});
			}

			return [polyfill];
		}
	]);

app.get(/^\/polyfill(\.\w+)(\.\w+)?/, function(req, res) {
	var ua = useragent.lookup(req.header('user-agent')),
		requestedPolyfills = parseRequestedPolyfills(req),
		firstParameter = req.params[0].toLowerCase(),
		minified =  firstParameter === '.min',
		extension = minified ? req.params[1].toLowerCase() : firstParameter;


	// Holds the strings that will be built into the explainer comment that is
	// placed before the polyfill code.
	var explainerComment = [
		req.originalUrl,
		'Detected ' + ua.toAgent()
	];

	// Holds the source code for each polyfill
	var currentPolyfills = {};

	if (extension === '.js') {
		res.set('Content-Type', 'application/javascript');
	} else {
		res.set('Conent-Type', 'text/css');
	}


	function includePolyfill(polyfill) {
		var polyfillSource = polyfills.sources[polyfill.name];
		if (!polyfillSource) {
			explainerComment.push(polyfillInfo.name + ' does not match any polyfills');
			return;
		}

		explainerComment.push(polyfill.name + ' - ' + polyfill.aliasOf + ' (LICENSE TODO)');
		currentPolyfills[polyfill.name] = polyfillSource.file;
	}

	requestedPolyfills.defaultPolyfills.forEach(includePolyfill);

	requestedPolyfills.maybePolyfills.forEach(function(polyfill) {
		var polyfillSource = polyfills.sources[polyfill.name];

		if (!polyfillSource) {
			explainerComment.push(polyfill.name + ' does not match any polyfills');
			return;
		}

		var polyfillConfig = polyfills.sources[polyfill.name].config;

		if (!(polyfillConfig && polyfillConfig.browsers)) {
			return;
		}

		var browserVersion = polyfillConfig.browsers[ua.family];

		if (browserVersion) {
			var uaMatchesBrowser = ua.satisfies(browserVersion);

			if (uaMatchesBrowser) {
				includePolyfill(polyfill);
			}
		}
	});


	var builtExplainerComment = '/* ' + explainerComment.join('\n * ') + '\n */\n';
	var builtPolyfillString = Object.keys(currentPolyfills).map(function(polyfillName) { return currentPolyfills[polyfillName]; }).join('\n');

	if (minified) {
		builtPolyfillString = uglify.minify(builtPolyfillString, {fromString: true}).code;
	}

	res.send(builtExplainerComment + builtPolyfillString);
});

app.listen(3000);

function parseRequestedPolyfills(req) {
	var maybeQuery       = req.query.maybe   ? req.query.maybe.split(',')   : [],
		defaultQuery     = req.query.default ? req.query.default.split(',') : [],
		maybePolyfills   = maybeQuery.map(parsePolyfillInfo),
		defaultPolyfills = defaultQuery.map(parsePolyfillInfo);

	return {
		maybePolyfills: aliasResolver.resolve(maybePolyfills),
		defaultPolyfills: aliasResolver.resolve(defaultPolyfills)
	};
}

function parsePolyfillInfo(name) {
	var nameAndFlags = name.split('|');
	return {
		flags:   nameAndFlags.slice(1),
		name:    nameAndFlags[0],
		aliasOf: nameAndFlags[0]
	};
}
<commit_msg>Use AliasResolver.createDefault, use uaFamily<commit_after>var express   = require('express');
	app       = express(),
	polyfills = require('./index'),
	useragent = require('useragent'),
	uglify    = require('uglify-js'),
	AliasResolver = require('./aliases'),
	lookupAgent = require('./agent');

// Load additional useragent features: primarily to use: agent.satisfies to
// test a browser version against a semver string
require('useragent/features');

var aliasResolver = AliasResolver.createDefault(polyfills);

app.get(/^\/polyfill(\.\w+)(\.\w+)?/, function(req, res) {
	var ua = useragent.lookup(req.header('user-agent')),
		requestedPolyfills = parseRequestedPolyfills(req),
		firstParameter = req.params[0].toLowerCase(),
		minified =  firstParameter === '.min',
		extension = minified ? req.params[1].toLowerCase() : firstParameter,
		uaFamily = lookupAgent(ua.family.toLowerCase());


	// Holds the strings that will be built into the explainer comment that is
	// placed before the polyfill code.
	var explainerComment = [
		req.originalUrl,
		'Detected ' + uaFamily + '/' + ua.toVersion()
	];

	// Holds the source code for each polyfill
	var currentPolyfills = {};

	if (extension === '.js') {
		res.set('Content-Type', 'application/javascript');
	} else {
		res.set('Conent-Type', 'text/css');
	}


	function includePolyfill(polyfill) {
		var polyfillSource = polyfills.sources[polyfill.name];
		if (!polyfillSource) {
			explainerComment.push(polyfillInfo.name + ' does not match any polyfills');
			return;
		}

		explainerComment.push(polyfill.name + ' - ' + polyfill.aliasOf + ' (LICENSE TODO)');
		currentPolyfills[polyfill.name] = polyfillSource.file;
	}

	requestedPolyfills.defaultPolyfills.forEach(includePolyfill);

	requestedPolyfills.maybePolyfills.forEach(function(polyfill) {
		var polyfillSource = polyfills.sources[polyfill.name];

		if (!polyfillSource) {
			explainerComment.push(polyfill.name + ' does not match any polyfills');
			return;
		}

		var polyfillConfig = polyfills.sources[polyfill.name].config;

		if (!(polyfillConfig && polyfillConfig.browsers)) {
			return;
		}

		var browserVersion = polyfillConfig.browsers[uaFamily];

		if (browserVersion) {
			var uaMatchesBrowser = ua.satisfies(browserVersion);

			if (uaMatchesBrowser) {
				includePolyfill(polyfill);
			}
		}
	});


	var builtExplainerComment = '/* ' + explainerComment.join('\n * ') + '\n */\n';
	var builtPolyfillString = Object.keys(currentPolyfills).map(function(polyfillName) { return currentPolyfills[polyfillName]; }).join('\n');

	if (minified) {
		builtPolyfillString = uglify.minify(builtPolyfillString, {fromString: true}).code;
	}

	res.send(builtExplainerComment + builtPolyfillString);
});

app.listen(3000);

function parseRequestedPolyfills(req) {
	var maybeQuery       = req.query.maybe   ? req.query.maybe.split(',')   : [],
		defaultQuery     = req.query.default ? req.query.default.split(',') : [],
		maybePolyfills   = maybeQuery.map(parsePolyfillInfo),
		defaultPolyfills = defaultQuery.map(parsePolyfillInfo);

	return {
		maybePolyfills: aliasResolver.resolve(maybePolyfills),
		defaultPolyfills: aliasResolver.resolve(defaultPolyfills)
	};
}

function parsePolyfillInfo(name) {
	var nameAndFlags = name.split('|');
	return {
		flags:   nameAndFlags.slice(1),
		name:    nameAndFlags[0],
		aliasOf: nameAndFlags[0]
	};
}

{'fraction_non_alphanumeric': 0.14016676250718804, 'fraction_numerical': 0.002587694077055779, 'mean_word_length': 4.414007782101168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '686894', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2198, 'n_words': 429}