<commit_before>// Licence: PUBLIC DOMAIN <http://unlicense.org/>
// Author: <NAME> <http://github.com/Acubed>

function MediaType(s, p){
	this.type = '';
	this.params = {};
	if(typeof s=='string'){
		var c = splitQuotedString(s);
		this.type = c.shift();
		for(var i=0; i<c.length; i++){
			this.parseParameter(c[i]);
		}
	}else if(s instanceof MediaType){
		this.type = s.type;
		this.q = s.q;
		for(var n in s.params) this.params[n]=s.params[n];
	}
	if(typeof p=='string'){	 
		var c = splitQuotedString(p);
		for(var i=0; i<c.length; i++){
			this.parseParameter(c[i]);
		}
	}else if(typeof p=='object'){
		for(var n in p) this.params[n]=p[n];
	}
}
MediaType.prototype.parseParameter = function parseParameter(s){
	var param = s.split('=',1);
	var name = param[0].trim();
	var value = s.substr(param[0].length+1).trim();
	if(!value || !name) return;
	if(name=='q' && this.q===undefined){
		this.q=parseFloat(value);
	}else{
		if(value[0]=='"' && value[value.length-1]=='"'){
			value = value.substr(1, value.length-2);
			value = value.replace(/\\(.)/g, function(a,b){return b;});
		}
		this.params[name]=value;
	}
}
MediaType.prototype.toString = function toString(){
	var str = this.type + ';q='+this.q;
	for(var n in this.params){
		str += ';'+n+'=';
		if(this.params[n].match(/["=;<>\[\]\(\) ,\-]/)){
			str += '"' + this.params[n].replace(/["\\]/g, function(a){return '\\'+a;}) + '"';
		}else{
			str += this.params[n];
		}
	}
	return str;
}
exports.MediaType = MediaType;

// Split a string by character, but ignore quoted parts and backslash-escaped characters
function splitQuotedString(str, delim, quote){
	delim = delim || ';';
	quote = quote || '"';
	var res = [];
	var start = 0;
	var offset = 0;
	function findNextChar(v, c, i, a){<commit_msg>Don't include an undefined q-value and order parameters constistently<commit_after>// Licence: PUBLIC DOMAIN <http://unlicense.org/>
// Author: <NAME> <http://github.com/Acubed>

function MediaType(s, p){
	this.type = '';
	this.params = {};
	if(typeof s=='string'){
		var c = splitQuotedString(s);
		this.type = c.shift();
		for(var i=0; i<c.length; i++){
			this.parseParameter(c[i]);
		}
	}else if(s instanceof MediaType){
		this.type = s.type;
		this.q = s.q;
		for(var n in s.params) this.params[n]=s.params[n];
	}
	if(typeof p=='string'){	 
		var c = splitQuotedString(p);
		for(var i=0; i<c.length; i++){
			this.parseParameter(c[i]);
		}
	}else if(typeof p=='object'){
		for(var n in p) this.params[n]=p[n];
	}
}
MediaType.prototype.parseParameter = function parseParameter(s){
	var param = s.split('=',1);
	var name = param[0].trim();
	var value = s.substr(param[0].length+1).trim();
	if(!value || !name) return;
	if(name=='q' && this.q===undefined){
		this.q=parseFloat(value);
	}else{
		if(value[0]=='"' && value[value.length-1]=='"'){
			value = value.substr(1, value.length-2);
			value = value.replace(/\\(.)/g, function(a,b){return b;});
		}
		this.params[name]=value;
	}
}
MediaType.prototype.toString = function toString(){
	var str = this.type;
	if(this.q!==undefined) str += ';q='+this.q;
	var params = Object.keys(this.params).sort();
	for(var i=0; i<params.length; i++){
		var n = params[i];
		str += ';'+n+'=';
		if(this.params[n].match(/["=;<>\[\]\(\) ,\-]/)){
			str += '"' + this.params[n].replace(/["\\]/g, function(a){return '\\'+a;}) + '"';
		}else{
			str += this.params[n];
		}
	}
	return str;
}
exports.MediaType = MediaType;

// Split a string by character, but ignore quoted parts and backslash-escaped characters
function splitQuotedString(str, delim, quote){
	delim = delim || ';';
	quote = quote || '"';
	var res = [];
	var start = 0;
	var offset = 0;
	function findNextChar(v, c, i, a){
{'fraction_non_alphanumeric': 0.2295571855474056, 'fraction_numerical': 0.006791632708503124, 'mean_word_length': 3.9622641509433962, 'pattern_counts': {'":': 0, '<': 16, '<?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': '5061117', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1440, 'n_words': 301}