<commit_before>/*
Copyright (C) 2014 Kano Computing Ltd.
License: http://opensource.org/licenses/MIT The MIT License (MIT)
*/

var swearwords = require('./swearwords.json'),
    util = require('./util');

var DEFAULT_REPLACEMENTS = [
        'bunnies',
        'butterfly',
        'kitten',
        'love',
        'gingerly',
        'flowers',
        'puppy',
        'joyful',
        'rainbows',
        'unicorn'
    ],
    DEFAULT_REGEX = getListRegex(swearwords);

function getListRegex (list) {
    return new RegExp('\\b(' + list.join('|') + ')\\b', 'gi');
}

function check (target, forbiddenList) {
    var targets = [],
        regex = forbiddenList ? getListRegex(forbiddenList) : DEFAULT_REGEX;

    if (typeof target === 'string') {
        targets.push(target);
    } else if (typeof target === 'object') {

        util.eachRecursive(target, function (val) {
            if (typeof val === 'string') {
                targets.push(val);
            }
        });
    }

    return targets.join(' ').match(regex) || [];
}

function purifyString (str, options) {
    options = options || {};

    var matches = [],
        purified,
        forbiddenList = options.forbiddenList || null,
        replacementsList = options.replacementsList || DEFAULT_REPLACEMENTS,
        regex = forbiddenList ? getListRegex(forbiddenList) : DEFAULT_REGEX,
        replace = options.replace || false,
        obscureSymbol = options.obscureSymbol || '*';

    purified = str.replace(regex, function (val) {
        matches.push(val);

        if (replace) {
            return replacementsList[Math.floor(Math.random() * replacementsList.length)];
        }

        var str = val.substr(0, 1);

        for (var i = 0; i < val.length - 2; i += 1) {
            str += obscureSymbol;
        }

        return str + val.substr(-1);
    });

    return [ purified, matches ];
}

function purify (target, options) {
    options = options || {};

    var matches = [],
        fields = options.fields || Object.keys(target),
        result;

    if (typeof target === 'string') {

        return purifyString(target, options);

    } else if (typeof target === 'object') {
        fields.forEach(function (field) {

            // TODO: Use better recursive checking, make DRYer
            if (typeof target[field] === 'string') {

                result = purifyString(target[field], options);
                target[field] = result[0];<commit_msg>fix for support of non ES6 browsers<commit_after>/*
Copyright (C) 2014 Kano Computing Ltd.
License: http://opensource.org/licenses/MIT The MIT License (MIT)
*/

var swearwords = require('./swearwords.json'),
    util = require('./util');

var DEFAULT_REPLACEMENTS = [
        'bunnies',
        'butterfly',
        'kitten',
        'love',
        'gingerly',
        'flowers',
        'puppy',
        'joyful',
        'rainbows',
        'unicorn'
    ],
    DEFAULT_REGEX = getListRegex(swearwords);

function getListRegex (list) {
    return new RegExp('\\b(' + list.join('|') + ')\\b', 'gi');
}

function check (target, forbiddenList) {
    var targets = [],
        regex = forbiddenList ? getListRegex(forbiddenList) : DEFAULT_REGEX;

    if (typeof target === 'string') {
        targets.push(target);
    } else if (typeof target === 'object') {

        util.eachRecursive(target, function (val) {
            if (typeof val === 'string') {
                targets.push(val);
            }
        });
    }

    return targets.join(' ').match(regex) || [];
}

function purifyString (str, options) {
    options = options || {};

    var matches = [],
        purified,
        forbiddenList = options.forbiddenList || null,
        replacementsList = options.replacementsList || DEFAULT_REPLACEMENTS,
        regex = forbiddenList ? getListRegex(forbiddenList) : DEFAULT_REGEX,
        replace = options.replace || false,
        obscureSymbol = options.obscureSymbol || '*';

    purified = str.replace(regex, function (val) {
        matches.push(val);

        if (replace) {
            return replacementsList[Math.floor(Math.random() * replacementsList.length)];
        }

        var str = val.substr(0, 1);

        for (var i = 0; i < val.length - 2; i += 1) {
            str += obscureSymbol;
        }

        return str + val.substr(-1);
    });

    return [ purified, matches ];
}

function purify (target, options) {
    options = options || {};

    var matches = [],
        fields = options.fields || ( target instanceof Object ? Object.keys(target) : [] ),
        result;

    if (typeof target === 'string') {

        return purifyString(target, options);

    } else if (typeof target === 'object') {
        fields.forEach(function (field) {

            // TODO: Use better recursive checking, make DRYer
            if (typeof target[field] === 'string') {

                result = purifyString(target[field], options);
                target[field] = result[0];
{'fraction_non_alphanumeric': 0.1440677966101695, 'fraction_numerical': 0.004640839386602098, 'mean_word_length': 2.2377531025473547, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6295840', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1425, 'n_words': 346}