<commit_before>module.exports = function (template) {
    var operators = ['+', '#', '.', '/', ';', '?', '&'];

    function encodeReserved(str) {
        var parts = str.split(/(%[0-9A-Fa-f]{2})/g);

        for (var i = 0; i < parts.length; i += 1) {
            if (!/%[0-9A-Fa-f]/.test(parts[i])) {
                parts[i] = encodeURI(parts[i]);
            }
        }
        return parts.join('');
    }

    function encodeValue(operator, value, key) {
        value = (operator === '+' || operator === '#') ? encodeReserved(value) : encodeURIComponent(value);

        if (key) {
            return encodeURIComponent(key) + '=' + value;
        } else {
            return value;
        }
    }

    function isDefined(value) {
        return value !== undefined && value !== null;
    }

    function isKeyOperator(operator) {
        return operator === ';' || operator === '&' || operator === '?';
    }

    function getValues(context, operator, key, modifier) {
        var value = context[key],
            result = [];

        if (isDefined(value) && value !== '') {
            if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
                value = value.toString();

                if (modifier && modifier !== '*') {
                    value = value.substring(0, parseInt(modifier, 10));
                }

                result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
            } else {
                if (modifier === '*') {
                    if (Array.isArray(value)) {
                        for (var j = 0; j < value.length; j += 1) {
                            if (isDefined(value[j])) {
                                result.push(encodeValue(operator, value[j], isKeyOperator(operator) ? key : null));
                            }
                        }
                    } else {
                        Object.keys(value).forEach(function (k) {
                            if (isDefined(value[k])) {
                                result.push(encodeValue(operator, value[k], k));
                            }
                        });
                    }
                } else {
                    var tmp = [];

                    if (Array.isArray(value)) {
                        for (var j = 0; j < value.length; j += 1) {
                            if (isDefined(value[j])) {
                                tmp.push(encodeValue(operator, value[j]));
                            }
                        }
                    } else {
                        Object.keys(value).forEach(function (k) {
                            if (isDefined(value[k])) {
                                tmp.push(encodeURIComponent(k));
                                tmp.push(encodeValue(operator, value[k].toString()));
                            }
                        });
                    }

                    if (isKeyOperator(operator)) {
                        result.push(encodeURIComponent(key) + '=' + tmp.join(','));
                    } else if (tmp.length !== 0) {
                        result.push(tmp.join(','));
                    }
                }
            }
        } else {
            if (operator === ';') {
                result.push(encodeURIComponent(key));
            } else if (operator === '&' || operator === '?') {
                result.push(encodeURIComponent(key) + '=');
            } else if (value === '') {
                result.push('');
            }
        }
        return result;
    }

    return function (context) {
        return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) {
            if (expression) {
                var operator = null,
                    variables = [],
                    values = [];

                if (operators.indexOf(expression.charAt(0)) !== -1) {
                    operator = expression.charAt(0);
                    expression = expression.substr(1);
                }

                variables = expression.split(/,/g);

                for (var i = 0; i < variables.length; i += 1) {
                    var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variables[i]);

                    values.push.apply(values, getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
                }

                if (operator && operator !== '+') {
                    var separator = ',';

                    if (operator === '?') {
                        separator = '&';
                    } else if (operator !== '#') {
                        separator = operator;
                    }
                    return (values.length !== 0 ? operator : '') + values.join(separator);
                } else {
                    return values.join(',');
                }
            } else {
                return encodeReserved(literal);
            }
        });
    };
};<commit_msg>Refactored for loops to use ES5 constructs. Use an ES5 shim if you need support for older browsers.<commit_after>module.exports = function (template) {
    var operators = ['+', '#', '.', '/', ';', '?', '&'];

    function encodeReserved(str) {
        return str.split(/(%[0-9A-Fa-f]{2})/g).map(function (part) {
            if (!/%[0-9A-Fa-f]/.test(part)) {
                part = encodeURI(part);
            }
            return part;
        }).join('');
    }

    function encodeValue(operator, value, key) {
        value = (operator === '+' || operator === '#') ? encodeReserved(value) : encodeURIComponent(value);

        if (key) {
            return encodeURIComponent(key) + '=' + value;
        } else {
            return value;
        }
    }

    function isDefined(value) {
        return value !== undefined && value !== null;
    }

    function isKeyOperator(operator) {
        return operator === ';' || operator === '&' || operator === '?';
    }

    function getValues(context, operator, key, modifier) {
        var value = context[key],
            result = [];

        if (isDefined(value) && value !== '') {
            if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
                value = value.toString();

                if (modifier && modifier !== '*') {
                    value = value.substring(0, parseInt(modifier, 10));
                }

                result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
            } else {
                if (modifier === '*') {
                    if (Array.isArray(value)) {
                        value.filter(isDefined).forEach(function (value) {
                            result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
                        });
                    } else {
                        Object.keys(value).forEach(function (k) {
                            if (isDefined(value[k])) {
                                result.push(encodeValue(operator, value[k], k));
                            }
                        });
                    }
                } else {
                    var tmp = [];

                    if (Array.isArray(value)) {
                        value.filter(isDefined).forEach(function (value) {
                            tmp.push(encodeValue(operator, value));
                        });
                    } else {
                        Object.keys(value).forEach(function (k) {
                            if (isDefined(value[k])) {
                                tmp.push(encodeURIComponent(k));
                                tmp.push(encodeValue(operator, value[k].toString()));
                            }
                        });
                    }

                    if (isKeyOperator(operator)) {
                        result.push(encodeURIComponent(key) + '=' + tmp.join(','));
                    } else if (tmp.length !== 0) {
                        result.push(tmp.join(','));
                    }
                }
            }
        } else {
            if (operator === ';') {
                result.push(encodeURIComponent(key));
            } else if (operator === '&' || operator === '?') {
                result.push(encodeURIComponent(key) + '=');
            } else if (value === '') {
                result.push('');
            }
        }
        return result;
    }

    return function (context) {
        return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) {
            if (expression) {
                var operator = null,
                    values = [];

                if (operators.indexOf(expression.charAt(0)) !== -1) {
                    operator = expression.charAt(0);
                    expression = expression.substr(1);
                }

                expression.split(/,/g).forEach(function (variable) {
                    var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable);
                    values.push.apply(values, getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
                });

                if (operator && operator !== '+') {
                    var separator = ',';

                    if (operator === '?') {
                        separator = '&';
                    } else if (operator !== '#') {
                        separator = operator;
                    }
                    return (values.length !== 0 ? operator : '') + values.join(separator);
                } else {
                    return values.join(',');
                }
            } else {
                return encodeReserved(literal);
            }
        });
    };
};
{'fraction_non_alphanumeric': 0.1546232347180703, 'fraction_numerical': 0.004535614885063395, 'mean_word_length': 1.1455108359133126, 'pattern_counts': {'":': 0, '<': 7, '<?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': '190838', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2379, 'n_words': 449}