<commit_before>/*! Parker v0.0.0 - MIT license */

'use strict';

var _ = require('underscore'),
    CssStylesheet = require('./CssStylesheet.js'),
    CssRule = require('./CssRule.js'),
    CssSelector = require('./CssSelector.js'),
    CssDeclaration = require('./CssDeclaration.js');

var VALID_METRIC_TYPE = /stylesheet|rule|selector|identifier|declaration|property|value/g;

function Parker(metrics) {
    this.metrics = metrics;
}

Parker.prototype.run = function () {
    var args = arguments;

    if (arguments.length === 1 && _.isArray(arguments[0])) {
        args = arguments[0];
    }

    args = _.filter(args, function (argument) {return _.isString(argument); });
    var metrics = _.filter(this.metrics, function (metric) {return metric.type.match(VALID_METRIC_TYPE); });

    if (args.length > 0) {
        return runMetrics(metrics, args);
    }

    throw {'message': 'No valid stylesheet data supplied', 'name': 'DataTypeException'};
};

var runMetrics = function (metrics, stylesheets) {
    var data = {};

    _.each(stylesheets, function (rawStylesheet) {
        collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'stylesheet'), rawStylesheet));
  
        var stylesheet = new CssStylesheet(rawStylesheet);
        _.each(stylesheet.getRules(), function (rawRule) {
            collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'rule'), rawRule));

            var rule = new CssRule(rawRule);
            _.each(rule.getSelectors(), function (rawSelector) {
                var selector = new CssSelector(rawSelector);
                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'selector'), rawSelector));

                _.each(selector.getIdentifiers(), function (rawIdentifier) {
                    collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'identifier'), rawIdentifier));
                });
            });

            _.each(rule.getDeclarations(), function (rawDeclaration) {
                var declaration = new CssDeclaration(rawDeclaration);

                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'declaration'), rawDeclaration));
                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'property'), declaration.getProperty()));
                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'value'), declaration.getValue()));
            });
        });
    });

    data = aggregateData(data, metrics);

    return data;
};

var runMetricsOnNode = function (metrics, node) {
    var data = {};
    _.each(metrics, function (metric) {
        data[metric.id] = metric.measure(node);
    });

    return data;
};

var aggregateData = function (data, metrics) {
    _.each(metrics, function (metric) {
        if (metric.aggregate === 'sum') {
            data[metric.id] = sum(data[metric.id]);
        }
        else if (metric.aggregate === 'mean') {
            data[metric.id] = mean(data[metric.id]);
        }
        else if (metric.aggregate === 'max') {
            if (_.isUndefined(metric.iterator)) {
                data[metric.id] = _.max(data[metric.id]);
            } else {
                data[metric.id] = _.max(data[metric.id], metric.iterator);
            }
        }
        else if (metric.aggregate === 'list') {
            if (!_.isUndefined(metric.filter)) {
                data[metric.id] = data[metric.id].filter(metric.filter);
            }

            data[metric.id] = _.compact(data[metric.id]);
        }
        else if (metric.aggregate === 'length') {
            if (!_.isUndefined(metric.filter)) {
                data[metric.id] = data[metric.id].filter(metric.filter);
            }

            data[metric.id] = _.compact(data[metric.id]).length;
        }
    });

    return data;
};

var collateArrayAttributes = function () {
    var sources = _.toArray(arguments),
        target = sources.shift();

    _.each(sources, function (source) {
        _.each(source, function (attribute, attributeName) {
            if  (!_.has(target, attributeName)) {
                target[attributeName] = [];
            }

            if (!_.isUndefined(attribute)) {
                if (_.isString(attribute)) {
                    attribute = [attribute];
                }

                target[attributeName] = target[attributeName].concat(attribute);
            }
        });
    });

    return target;
};

var sum = function (values) {
    return values.reduce(function (previous, current) {return previous + current; });
};

var mean = function (values) {
    var valuesSum = sum(values);

    return valuesSum / values.length;
};

var filterMetricsByType = function (metrics, type) {
    return metrics.filter(function (metric) {return metric.type === type; })
}

module.exports = Parker;<commit_msg>Refactored aggregation of data into functions<commit_after>/*! Parker v0.0.0 - MIT license */

'use strict';

var _ = require('underscore'),
    CssStylesheet = require('./CssStylesheet.js'),
    CssRule = require('./CssRule.js'),
    CssSelector = require('./CssSelector.js'),
    CssDeclaration = require('./CssDeclaration.js');

var VALID_METRIC_TYPE = /stylesheet|rule|selector|identifier|declaration|property|value/g;

function Parker(metrics) {
    this.metrics = metrics;
}

Parker.prototype.run = function () {
    var args = arguments;

    if (arguments.length === 1 && _.isArray(arguments[0])) {
        args = arguments[0];
    }

    args = _.filter(args, function (argument) {return _.isString(argument); });
    var metrics = _.filter(this.metrics, function (metric) {return metric.type.match(VALID_METRIC_TYPE); });

    if (args.length > 0) {
        return runMetrics(metrics, args);
    }

    throw {'message': 'No valid stylesheet data supplied', 'name': 'DataTypeException'};
};

var runMetrics = function (metrics, stylesheets) {
    var data = {};

    _.each(stylesheets, function (rawStylesheet) {
        collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'stylesheet'), rawStylesheet));
  
        var stylesheet = new CssStylesheet(rawStylesheet);
        _.each(stylesheet.getRules(), function (rawRule) {
            collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'rule'), rawRule));

            var rule = new CssRule(rawRule);
            _.each(rule.getSelectors(), function (rawSelector) {
                var selector = new CssSelector(rawSelector);
                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'selector'), rawSelector));

                _.each(selector.getIdentifiers(), function (rawIdentifier) {
                    collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'identifier'), rawIdentifier));
                });
            });

            _.each(rule.getDeclarations(), function (rawDeclaration) {
                var declaration = new CssDeclaration(rawDeclaration);

                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'declaration'), rawDeclaration));
                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'property'), declaration.getProperty()));
                collateArrayAttributes(data, runMetricsOnNode(filterMetricsByType(metrics, 'value'), declaration.getValue()));
            });
        });
    });

    data = aggregateData(data, metrics);

    return data;
};

var runMetricsOnNode = function (metrics, node) {
    var data = {};
    _.each(metrics, function (metric) {
        data[metric.id] = metric.measure(node);
    });

    return data;
};

var aggregateData = function (data, metrics) {
    _.each(metrics, function (metric) {
        switch (metric.aggregate) {
            case 'sum':
                data[metric.id] = aggregateSum(data[metric.id]);
                break;
            case 'mean':
                data[metric.id] = aggregateMean(data[metric.id]);
                break;
            case 'max':
                data[metric.id] = aggregateMax(data[metric.id], metric.iterator);
                break;
            case 'list':
                data[metric.id] = aggregateList(data[metric.id], metric.filter);
                break;
            case 'length':
                data[metric.id] = aggregateLength(data[metric.id], metric.filter);
                break;
        }
    });

    return data;
};

var aggregateSum = function (data) {
    return sum(data);
};

var aggregateMean = function (data) {
    return mean(data);
};

var aggregateMax = function (data, iterator) {
    if (_.isUndefined(iterator)) {
        return _.max(data);
    }

    return _.max(data, iterator);
};

var aggregateList = function (data, filter) {
    if (!_.isUndefined(filter)) {
        return _.compact(data.filter(filter));
    }

    return _.compact(data);
};

var aggregateLength = function (data, filter) {
    if (!_.isUndefined(filter)) {
        return data.filter(filter).length;
    }

    return _.compact(data).length;
};

var collateArrayAttributes = function () {
    var sources = _.toArray(arguments),
        target = sources.shift();

    _.each(sources, function (source) {
        _.each(source, function (attribute, attributeName) {
            if  (!_.has(target, attributeName)) {
                target[attributeName] = [];
            }

            if (!_.isUndefined(attribute)) {
                if (_.isString(attribute)) {
                    attribute = [attribute];
                }

                target[attributeName] = target[attributeName].concat(attribute);
            }
        });
    });

    return target;
};

var sum = function (values) {
    return values.reduce(function (previous, current) {return previous + current; });
};

var mean = function (values) {
    var valuesSum = sum(values);

    return valuesSum / values.length;
};

var filterMetricsByType = function (metrics, type) {
    return metrics.filter(function (metric) {return metric.type === type; });
};

module.exports = Parker;
{'fraction_non_alphanumeric': 0.13504442250740376, 'fraction_numerical': 0.0013820335636722605, 'mean_word_length': 2.629881762809029, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3545395', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2732, 'n_words': 565}