<commit_before>"use strict";

var _ = require('underscore');

var TYPE_DELIMETER = ':'
	, PATH_REGEX = /^{[A-Za-z0-9]+}$/
	, DEFAULT_VAL_DELIMETER = '|='
	, extraOption;

//type:keyName  || type:{keyName}
function getRequiredKeyInfo(param_expression) {
	var keyInfo = {}
		, type_tokenized = param_expression.split(TYPE_DELIMETER)
		, len1 = type_tokenized.length
		, pathKey
		, keyName
		, err;

	if (len1 === 1) {
		keyInfo.type = 'string';

		pathKey = type_tokenized[0].match(PATH_REGEX) === null ? false : true;
		keyName = pathKey ? type_tokenized[0].replace('{', '').replace('}', '') : type_tokenized[0];
	} else if (len1 === 2) {
		keyInfo.type = type_tokenized[0];

		pathKey = type_tokenized[1].match(PATH_REGEX) === null ? false : true;
		keyName = pathKey ? type_tokenized[1].replace('{', '').replace('}', '') : type_tokenized[1];
	} else {
		err = new Error('Invalid required parameter expression. Your Input: ' + param_expression);
		err.code = 400;

		throw err;
	}

	keyInfo.pathKey = pathKey;
	keyInfo.keyName = keyName;

	return keyInfo;
}

function getOptionalKeyInfo(param_expression) {
	var keyInfo = {}
		, type_tokenized = param_expression.split(TYPE_DELIMETER)
		, len1 = type_tokenized.length
		, len2
		, val_tokenized
		, err;

	if (len1 === 1) { //no type, set type as string
		keyInfo.type = 'string';
		val_tokenized = type_tokenized[0].split(DEFAULT_VAL_DELIMETER);
	} else if (len1 === 2) {
		keyInfo.type = type_tokenized[0];
		val_tokenized = type_tokenized[1].split(DEFAULT_VAL_DELIMETER);
	} else {
		err = new Error('Invalid parameter expression. Your Input: ' + param_expression);
		err.code = 400;

		throw err;
	}

	len2 = val_tokenized.length;

	keyInfo.keyName = val_tokenized[0];
	if (len2 === 2) keyInfo.defaultVal = val_tokenized[1];

	if (len2 > 2) {
		err = new Error('Invalid parameter expression. expression have to include \''
			+ DEFAULT_VAL_DELIMETER + '\' delimeter one or zero. Your Input: ' + param_expression);
		err.code = 400;

		throw err;
	}

	return keyInfo;
}

//type:key_name|=default_value
function getOptionalParams(req, option_expressions) {
	var options = {}
    , getValueFn
		, err;

	//possible expression : {type}:keyName|={defaultValue}
	for (var i = 0, li = option_expressions.length; i < li; i++) {
		try {
			var keyInfo = getOptionalKeyInfo(option_expressions[i]);
		} catch (e) {
			err = e;
		}

    getValueFn = function(keyName) {
      if (req.params && req.params[keyName]) {
        return req.params[keyName];
      } else if (req.body && req.body[keyName]) {
        return req.body[keyName];
      } else if (req.query && req.query[keyName]) {
        return req.query[keyName];
      } else {
        return null;
      }
    };


    var parameterValue = getValueFn(keyInfo.keyName);

		if (keyInfo.type === 'number') {
      options[keyInfo.keyName] = parameterValue ? parseInt(parameterValue, 10) : parseInt(keyInfo.defaultVal, 10);
		} else {
      options[keyInfo.keyName] = parameterValue ? parameterValue : keyInfo.defaultVal;
		}

	}

	return {
		err: err,
		params: options
	};
}

function getDefaultRequestInfo(req, extraOption) {
	var options = {};

	for (var key in extraOption) {
		if (extraOption.hasOwnProperty(key)) {
			options[key] = req[key];
		}
	}

	return {
		params: options
	}
}

function requiredParameter(req, required_expressions) {
	var options = {}
		, getFunc
		, err;
<commit_msg>fix: modify fetch value error<commit_after>"use strict";

var _ = require('underscore');

var TYPE_DELIMETER = ':'
	, PATH_REGEX = /^{[A-Za-z0-9]+}$/
	, DEFAULT_VAL_DELIMETER = '|='
	, extraOption;

//type:keyName  || type:{keyName}
function getRequiredKeyInfo(param_expression) {
	var keyInfo = {}
		, type_tokenized = param_expression.split(TYPE_DELIMETER)
		, len1 = type_tokenized.length
		, pathKey
		, keyName
		, err;

	if (len1 === 1) {
		keyInfo.type = 'string';

		pathKey = type_tokenized[0].match(PATH_REGEX) === null ? false : true;
		keyName = pathKey ? type_tokenized[0].replace('{', '').replace('}', '') : type_tokenized[0];
	} else if (len1 === 2) {
		keyInfo.type = type_tokenized[0];

		pathKey = type_tokenized[1].match(PATH_REGEX) === null ? false : true;
		keyName = pathKey ? type_tokenized[1].replace('{', '').replace('}', '') : type_tokenized[1];
	} else {
		err = new Error('Invalid required parameter expression. Your Input: ' + param_expression);
		err.code = 400;

		throw err;
	}

	keyInfo.pathKey = pathKey;
	keyInfo.keyName = keyName;

	return keyInfo;
}

function getOptionalKeyInfo(param_expression) {
	var keyInfo = {}
		, type_tokenized = param_expression.split(TYPE_DELIMETER)
		, len1 = type_tokenized.length
		, len2
		, val_tokenized
		, err;

	if (len1 === 1) { //no type, set type as string
		keyInfo.type = 'string';
		val_tokenized = type_tokenized[0].split(DEFAULT_VAL_DELIMETER);
	} else if (len1 === 2) {
		keyInfo.type = type_tokenized[0];
		val_tokenized = type_tokenized[1].split(DEFAULT_VAL_DELIMETER);
	} else {
		err = new Error('Invalid parameter expression. Your Input: ' + param_expression);
		err.code = 400;

		throw err;
	}

	len2 = val_tokenized.length;

	keyInfo.keyName = val_tokenized[0];
	if (len2 === 2) keyInfo.defaultVal = val_tokenized[1];

	if (len2 > 2) {
		err = new Error('Invalid parameter expression. expression have to include \''
			+ DEFAULT_VAL_DELIMETER + '\' delimeter one or zero. Your Input: ' + param_expression);
		err.code = 400;

		throw err;
	}

	return keyInfo;
}

//type:key_name|=default_value
function getOptionalParams(req, option_expressions) {
	var options = {}
    , getValueFn
		, err;

	//possible expression : {type}:keyName|={defaultValue}
	for (var i = 0, li = option_expressions.length; i < li; i++) {
		try {
			var keyInfo = getOptionalKeyInfo(option_expressions[i]);
		} catch (e) {
			err = e;
		}

    getValueFn = function(keyName) {
      if (req.params && req.params[keyName]) {
        return req.params[keyName];
      } else if (req.body && req.body[keyName]) {
        return req.body[keyName];
      } else if (req.query && req.query[keyName]) {
        return req.query[keyName];
      } else {
        return null;
      }
    };


    var parameterValue = getValueFn(keyInfo.keyName);

		if (keyInfo.type === 'number') {
      options[keyInfo.keyName] = parameterValue ? parseInt(parameterValue, 10) : parseInt(keyInfo.defaultVal, 10);
		} else {
      options[keyInfo.keyName] = parameterValue ? parameterValue : keyInfo.defaultVal;
		}

	}

	return {
		err: err,
		params: options
	};
}

function getDefaultRequestInfo(req, extraOption) {
	var options = {};

	for (var key in extraOption) {
		if (extraOption.hasOwnProperty(key)) {
			options[key] = req[extraOption[key]];
		}
	}

	return {
		params: options
	}
}

function requiredParameter(req, required_expressions) {
	var options = {}
		, getFunc
		, err;

{'fraction_non_alphanumeric': 0.1457021525845658, 'fraction_numerical': 0.012886220530092253, 'mean_word_length': 3.726643598615917, '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': '7105495', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2396, 'n_words': 504}