<commit_before>/*
 * Auxiliary functions
 */

/**
 * Convert topic to regex
 *
 * @param topic
 * @returns {*}
 */
export function topicToRegex(topic) {

    // deep copy
    let regex = (' ' + topic).slice(1);

    // Parse topic
    if (regex.indexOf('+') >= 0) {
        regex = regex.replace(/\+/g, "[^/]+");
    }
    if (regex.indexOf('#') === regex.length - 1) {
        regex = regex.replace("#", "[a-zA-Z0-9_/ ]+");
    }
    // Invalid topic
    if (regex.indexOf('#') >= 0) {
        throw new Error("Invalid topic '" + topic + "'");
    }

    // compile regex?
    if (topic !== regex)
        return new RegExp('^' + regex + '$');
    else
        return topic;

}

function matchStringOrRegExp(regex, topic) {
    if (regex.length > 1 && regex[0] === '/' && regex[regex.length - 1] === '/')
        return topic.match(regex);
    else
        return topic === regex;
}

/**
 * Matches topic to all possible entries in array of regexps.
 *
 * @param array the array of regexps
 * @param topic the topic to be matched
 * @returns {Array}
 */
export function matchAllTopics(array, topic) {

    const topics = [];
    for(let i = 0; i < array.length; i++) {
        const regex = array[i]
        if (matchStringOrRegExp(regex, topic))
            topics.push(regex);
    }
    return topics;

}

/**<commit_msg>TopicObserver pattern.<commit_after>/*
 * Auxiliary functions
 */

/**
 * Convert topic to regex
 *
 * @param topic
 * @returns {*}
 */
export function topicToRegex(topic) {

    // deep copy
    let regex = (' ' + topic).slice(1);

    // Parse topic
    if (regex.indexOf('+') >= 0) {
        regex = regex.replace(/\+/g, "[^/]+");
    }
    if (regex.indexOf('#') === regex.length - 1) {
        regex = regex.replace("#", "[a-zA-Z0-9_/ ]+");
    }
    // Invalid topic
    if (regex.indexOf('#') >= 0) {
        throw new Error("Invalid topic '" + topic + "'");
    }

    // compile regex?
    if (topic !== regex)
        return new RegExp('^' + regex + '$');
    else
        return topic;

}

function matchStringOrRegExp(regex, topic) {
    console.log(regex);
    console.log(topic);
    if (regex.length > 1 && regex[0] === '/' && regex[regex.length - 1] === '/') {
        console.log('> regex');
        return topic.match(regex);
    } else {
        console.log('> string');
        return topic === regex;
    }
}

/**
 * Matches topic to all possible entries in array of regexps.
 *
 * @param array the array of regexps
 * @param topic the topic to be matched
 * @returns {Array}
 */
export function matchAllTopics(array, topic) {

    const topics = [];
    for(let i = 0; i < array.length; i++) {
        const regex = array[i]
        if (matchStringOrRegExp(regex, topic))
            topics.push(regex);
    }
    return topics;

}

/**
{'fraction_non_alphanumeric': 0.18080517613227895, 'fraction_numerical': 0.007189072609633357, 'mean_word_length': 2.2473745624270713, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2616676', 'n_tokens_mistral': 909, 'n_tokens_neox': 886, 'n_words': 237}