<commit_before>'use strict';

var escallmatch = require('escallmatch'),
    isPhantom = typeof window !== 'undefined' && typeof window.callPhantom === 'function';


function enhance (target, formatter, config) {
    var eagerEvaluation = !(config.modifyMessageOnFail || config.saveContextOnFail),
        doPowerAssert = function (baseAssert, args, message, context) {
            var f;
            if (eagerEvaluation) {
                args.push(buildPowerAssertText(message, context));
                return baseAssert.apply(target, args);
            }
            try {
                args.push(message);
                return baseAssert.apply(target, args);
            } catch (e) {
                if (e.name !== 'AssertionError') {
                    throw e;
                }
                if (typeof target.AssertionError !== 'function') {
                    throw e;
                }
                if (isPhantom) {
                    f = new target.AssertionError({
                        actual: e.actual,
                        expected: e.expected,
                        operator: e.operator,
                        message: e.message
                    });
                } else {
                    f = e;
                }
                if (config.modifyMessageOnFail) {
                    f.message = buildPowerAssertText(message, context);
                    if (typeof e.generatedMessage !== 'undefined') {
                        f.generatedMessage = false;
                    }
                }
                if (config.saveContextOnFail) {
                    f.powerAssertContext = context;
                }
                throw f;
            }
        },
        enhancement = (typeof target === 'function') ? decorateOneArg(target, target, doPowerAssert) : {},
        events = [];

    function buildPowerAssertText (message, context) {
        var powerAssertText = formatter(context);
        return message ? message + ' ' + powerAssertText : powerAssertText;
    }

    function _capt (value, espath) {
        events.push({value: value, espath: espath});
        return value;
    }

    function _expr (value, args) {
        var captured = events;
        events = [];
        return { powerAssertContext: {value: value, events: captured}, source: {content: args.content, filepath: args.filepath, line: args.line} };
    }

    var matchers = config.patterns.map(function (pt) { return escallmatch(pt); });
    matchers.forEach(function (matcher) {
        var args = matcher.argumentSignitures();
        var len = args.length,
            lastArg;
        if (0 < len) {
            lastArg = args[len - 1];
            if (lastArg.name === 'message' && lastArg.kind === 'optional') {
                len -= 1;
            }
        }
        var methodName = detectMethodName(matcher.calleeAst());
        if (methodName && typeof target[methodName] === 'function') {
            switch (len) {
            case 1:
                enhancement[methodName] = decorateOneArg(target, target[methodName], doPowerAssert);
                break;
            case 2:
                enhancement[methodName] = decorateTwoArgs(target, target[methodName], doPowerAssert);
                break;
            default:
                throw new Error('not supported');
            }
        }
    });

    enhancement._capt = _capt;
    enhancement._expr = _expr;
    return enhancement;
}


function detectMethodName (node) {
    if (node.type === 'MemberExpression') {
        return node.property.name;
    }
    return null;
}


function isEspoweredValue (value) {
    return (typeof value === 'object') && (value !== null) && (typeof value.powerAssertContext !== 'undefined');
}


function decorateOneArg (target, baseAssert, doPowerAssert) {
    return function (arg1, message) {
        var context, val1;
        if (! isEspoweredValue(arg1)) {
            return baseAssert.apply(target, [arg1, message]);
        }
        val1 = arg1.powerAssertContext.value;
        context = {
            source: arg1.source,
            args: []
        };
        context.args.push({
            value: val1,
            events: arg1.powerAssertContext.events
        });
        return doPowerAssert(baseAssert, [val1], message, context);
    };
}


function decorateTwoArgs (target, baseAssert, doPowerAssert) {
    return function (arg1, arg2, message) {
        var context, val1, val2;
        if (!(isEspoweredValue(arg1) || isEspoweredValue(arg2))) {
            return baseAssert.apply(target, [arg1, arg2, message]);
        }

        if (isEspoweredValue(arg1)) {
            context = {
                source: arg1.source,
                args: []
            };
            context.args.push({
                value: arg1.powerAssertContext.value,
                events: arg1.powerAssertContext.events
            });
            val1 = arg1.powerAssertContext.value;
        } else {
            val1 = arg1;
        }

        if (isEspoweredValue(arg2)) {
            if (!isEspoweredValue(arg1)) {
                context = {
                    source: arg2.source,
                    args: []
                };
            }
            context.args.push({
                value: arg2.powerAssertContext.value,
                events: arg2.powerAssertContext.events
            });
            val2 = arg2.powerAssertContext.value;
        } else {
            val2 = arg2;
        }

        return doPowerAssert(baseAssert, [val1, val2], message, context);
    };
}

module.exports = enhance;
<commit_msg>refactor(empower): integrate decorateOneArg and decorateTwoArgs into decorateArgs function<commit_after>'use strict';

var escallmatch = require('escallmatch'),
    slice = Array.prototype.slice,
    isPhantom = typeof window !== 'undefined' && typeof window.callPhantom === 'function';


function enhance (target, formatter, config) {
    var eagerEvaluation = !(config.modifyMessageOnFail || config.saveContextOnFail),
        doPowerAssert = function (baseAssert, args, message, context) {
            var f;
            if (eagerEvaluation) {
                args.push(buildPowerAssertText(message, context));
                return baseAssert.apply(target, args);
            }
            try {
                args.push(message);
                return baseAssert.apply(target, args);
            } catch (e) {
                if (e.name !== 'AssertionError') {
                    throw e;
                }
                if (typeof target.AssertionError !== 'function') {
                    throw e;
                }
                if (isPhantom) {
                    f = new target.AssertionError({
                        actual: e.actual,
                        expected: e.expected,
                        operator: e.operator,
                        message: e.message
                    });
                } else {
                    f = e;
                }
                if (config.modifyMessageOnFail) {
                    f.message = buildPowerAssertText(message, context);
                    if (typeof e.generatedMessage !== 'undefined') {
                        f.generatedMessage = false;
                    }
                }
                if (config.saveContextOnFail) {
                    f.powerAssertContext = context;
                }
                throw f;
            }
        },
        // check function signiture
        enhancement = (typeof target === 'function') ? decorateArgs(1, target, target, doPowerAssert) : {},
        events = [];

    function buildPowerAssertText (message, context) {
        var powerAssertText = formatter(context);
        return message ? message + ' ' + powerAssertText : powerAssertText;
    }

    function _capt (value, espath) {
        events.push({value: value, espath: espath});
        return value;
    }

    function _expr (value, args) {
        var captured = events;
        events = [];
        return { powerAssertContext: {value: value, events: captured}, source: {content: args.content, filepath: args.filepath, line: args.line} };
    }

    var matchers = config.patterns.map(function (pt) { return escallmatch(pt); });
    matchers.forEach(function (matcher) {
        var args = matcher.argumentSignitures();
        var len = args.length,
            lastArg;
        if (0 < len) {
            lastArg = args[len - 1];
            if (lastArg.name === 'message' && lastArg.kind === 'optional') {
                len -= 1;
            }
        }
        var methodName = detectMethodName(matcher.calleeAst());
        if (methodName && typeof target[methodName] === 'function') {
            switch (len) {
            case 1:
                enhancement[methodName] = decorateArgs(len, target, target[methodName], doPowerAssert);
                break;
            case 2:
                enhancement[methodName] = decorateArgs(len, target, target[methodName], doPowerAssert);
                break;
            default:
                throw new Error('not supported');
            }
        }
    });

    enhancement._capt = _capt;
    enhancement._expr = _expr;
    return enhancement;
}


function detectMethodName (node) {
    if (node.type === 'MemberExpression') {
        return node.property.name;
    }
    return null;
}


function isEspoweredValue (value) {
    return (typeof value === 'object') && (value !== null) && (typeof value.powerAssertContext !== 'undefined');
}


function decorateArgs (numNonMessageArgs, target, baseAssert, doPowerAssert) {
    return function () {
        var context, message, args = slice.apply(arguments);

        if (args.every(function (arg) { return !isEspoweredValue(arg); })) {
            return baseAssert.apply(target, args);
        }

        var values = args.slice(0, numNonMessageArgs).map(function (arg, idx) {
            if (!isEspoweredValue(arg)) {
                return arg;
            }
            if (!context) {
                context = {
                    source: arg.source,
                    args: []
                };
            }
            context.args.push({
                value: arg.powerAssertContext.value,
                events: arg.powerAssertContext.events
            });
            return arg.powerAssertContext.value;
        });

        if (numNonMessageArgs === (args.length - 1)) {
            message = args[args.length - 1];
        }

        return doPowerAssert(baseAssert, values, message, context);
    };
}


module.exports = enhance;

{'fraction_non_alphanumeric': 0.11000568504832291, 'fraction_numerical': 0.004832291074474133, 'mean_word_length': 1.6029593094944512, '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': '4461348', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2679, 'n_words': 617}