<commit_before>"use strict";

module.exports = {
    escapeJavaScript: function(string) {
        return JSON.stringify(string);
    },

    urlEncode: function(obj) {
        return encodeURIComponent(obj);
    },

    urlDecode: function(string) {
        return decodeURIComponent(string);
    },

    base64Encode: function(obj) {
        return new Buffer(obj).toString('base64');
    },

    base64Decode: function(string) {
        return new Buffer(string, 'base64').toString('utf8');
    },

    // Mapping template
    parseMappingTemplate: function(template, input, context) {
        // Use a crazy-ass regex to try and capture any variables that we need to evaluate
        // Three rules:
        // 1. Within double quotes (value ends up being a string)
        // 2. Within single quotes (value ends up being a string)
        // 3. As a "word" (value ends up being an object or variable reference)
        const regexParts = [
            '(?:\"(\$(input|context|util)\.((?:[\w\.\(\)\$]|\'|\\\")*))\")', // 1
            '(?:\'(\$(input|context|util)\.((?:[\w\.\(\)\$]|\\\'|\")*))\')', // 2
            '(\$(input|context|util)\.((?:[\w\.\(\)\$]|\'|\")*))'            // 3
        ];
        const regex = new RegExp(regexParts.join('|'), 'g');
        const variableSubstitution = /\$(input|context|util)/g;

        return template.replace(regex, function(m, match) {
            // Replace $util, $input and $context
            if (!match) match = m;

            const adjustedMatch = match.replace(variableSubstitution, function(mi, variable) {
                return variable;
            });

            // Try to evaluate the match
            let result = match;
            try {
                const func = new Function('input', 'context', 'util', 'return ' + adjustedMatch);
                result = func(input, context, this);
            } catch (e) {
                // Ignore
            }

            if (m.charAt(0) == '\'' || m.charAt(0) == '\"')
                return '\"' + result + '\"';

            return result;
        });
    }
};<commit_msg>Fix the regex escaping<commit_after>"use strict";

module.exports = {
    escapeJavaScript: function(string) {
        return JSON.stringify(string);
    },

    urlEncode: function(obj) {
        return encodeURIComponent(obj);
    },

    urlDecode: function(string) {
        return decodeURIComponent(string);
    },

    base64Encode: function(obj) {
        return new Buffer(obj).toString('base64');
    },

    base64Decode: function(string) {
        return new Buffer(string, 'base64').toString('utf8');
    },

    // Mapping template
    parseMappingTemplate: function(template, input, context) {
        // Use a crazy-ass regex to try and capture any variables that we need to evaluate
        // Three rules:
        // 1. Within double quotes (value ends up being a string)
        // 2. Within single quotes (value ends up being a string)
        // 3. As a "word" (value ends up being an object or variable reference)
        const regexParts = [
            '(?:"(\\$(input|context|util).((?:[\\w\\.\\(\\)\\$]|\'|")*))")', // 1
            '(?:\'(\\$(input|context|util).((?:[\\w\\.\\(\\)\\$]|\'|")*))\')', // 2
            '(\\$(input|context|util)\\.((?:[\\w\\.\\(\\)\\$]|\'|")*))'            // 3
        ];
        const regex = new RegExp(regexParts.join('|'), 'g');
        const variableSubstitution = /\$(input|context|util)/g;

        return template.replace(regex, function(m, match) {
            // Replace $util, $input and $context
            if (!match) match = m;

            const adjustedMatch = match.replace(variableSubstitution, function(mi, variable) {
                return variable;
            });

            // Try to evaluate the match
            let result = match;
            try {
                const func = new Function('input', 'context', 'util', 'return ' + adjustedMatch);
                result = func(input, context, this);
            } catch (e) {
                // Ignore
            }

            if (m.charAt(0) == '\'' || m.charAt(0) == '\"')
                return '\"' + result + '\"';

            return result;
        });
    }
};
{'fraction_non_alphanumeric': 0.17614853606284217, 'fraction_numerical': 0.008093311116400857, 'mean_word_length': 1.8201342281879194, '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': '2739415', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1176, 'n_words': 301}