<commit_before>var handlers = [];


function defaultMatcher() {
    return true;
}

function createMatcher(val) {
    if (val instanceof Function) {
        return val;
    } else if (val && val.jasmineMatches !== undefined) {
        return val.jasmineMatches.bind(val);
    } else {
        return function(arg) { return val === arg };
    }
}

function MatcherSet(matchers) {
    matchers = matchers || [];
    var responses = [];

    this.matches = function(args) {
        if (matchers.length !== args.length) return false;

        for (var i = 0; i < args.length; ++i) {
            // Quickly bail if we can't match
            if (!matchers[i](args[i])) {
                return false;
            }
        }

        return true;
    };

    this.apply = function(args) {
        var res = responses.length > 1 ? responses.shift() : responses[0];

        if (res) {
            return res.apply(res, args);
        }
    };

    this.addResponse = function(fn) {
        responses.push(fn);
    };
}

function captor(val) {
    var matcher = arguments.length === 0 ?  defaultMatcher : createMatcher(val);

    var values = [];

    var matcherProxy = function(arg) {
        if (matcher(arg)) {
            append(arg);
            return true;
        }

        return false;
    };

    var append = function(val) {
        matcherProxy.latest = val;
        values.push(val);
    };

    matcherProxy.values = function() {
        return values;
    };

    return matcherProxy;
}

function proxy(matcher) {
    return {
        then : function(fn) {
            matcher.addResponse(fn);
            return this;
        },
        thenReturn : function(val) {
            matcher.addResponse(function() {
                return val;
            });

            return this;
        },
        thenThrow : function(err) {
            matcher.addResponse(function() {
                throw err;
            });

            return this;
        }
    };
}

function CallHandler(spy) {
    var matcherSets = [];
    var defaultSet = new MatcherSet();

    spy.and.callFake(function() {
        var args = Array.prototype.slice.call(arguments, 0);

        for (var i = 0; i < matcherSets.length; ++i) {
            var set = matcherSets[i];
            
            if (set.matches(args)) {
                return set.apply(args);
            }
        }

        return defaultSet.apply(args);
    });

    this.isCalledWith = function() {
        var args  = Array.prototype.slice.call(arguments, 0);
        var matchers = [];

        for (var i = 0; i < args.length; ++i) {
            matchers.push(createMatcher(args[i]));
        }

        var matcherSet = new MatcherSet(matchers);
        matcherSets.push(matcherSet);

        return proxy(matcherSet);
    };

    this.isCalled = proxy(defaultSet);
    spy.and.callFake = this.isCalled.then;
}

function when(spy) {
    for (var i = 0; i < handlers.length; ++i) {
        if (handlers[i].spy === spy) {
            return handlers[i].handler;
        }
    }
    
    var handler = new CallHandler(spy);
    
    handlers.push({
        'spy' : spy,
        'handler' : handler
    });

    return handler;
}

when.is = function(val) {
    return function(arg) {
        return val == arg;
    };
};

when.captor = captor;

module.exports = when;<commit_msg>2 fixes: 1. Works with jasmine 2.2+ that renamed 'jasmineMatches' to 'asymmetricMatch'.  See https://github.com/jasmine/jasmine/blob/master/release_notes/2.2.0.md 2. Works in browser as well as nodejs.  See http://www.richardrodger.com/2013/09/27/how-to-make-simple-node-js-modules-work-in-the-browser<commit_after>'use strict';

(function() {
  var root = this;
  var previous_when = root.when;

var handlers = [];


function defaultMatcher() {
    return true;
}

function createMatcher(val) {
    if (val instanceof Function) {
        return val;
    } else if (val && val.jasmineMatches !== undefined) {
        return val.jasmineMatches.bind(val);
    } else if (val && val.asymmetricMatch !== undefined) {
        return val.asymmetricMatch.bind(val);
    } else {
        return function(arg) { return val === arg };
    }
}

function MatcherSet(matchers) {
    matchers = matchers || [];
    var responses = [];

    this.matches = function(args) {
        if (matchers.length !== args.length) return false;

        for (var i = 0; i < args.length; ++i) {
            // Quickly bail if we can't match
            if (!matchers[i](args[i])) {
                return false;
            }
        }

        return true;
    };

    this.apply = function(args) {
        var res = responses.length > 1 ? responses.shift() : responses[0];

        if (res) {
            return res.apply(res, args);
        }
    };

    this.addResponse = function(fn) {
        responses.push(fn);
    };
}

function captor(val) {
    var matcher = arguments.length === 0 ?  defaultMatcher : createMatcher(val);

    var values = [];

    var matcherProxy = function(arg) {
        if (matcher(arg)) {
            append(arg);
            return true;
        }

        return false;
    };

    var append = function(val) {
        matcherProxy.latest = val;
        values.push(val);
    };

    matcherProxy.values = function() {
        return values;
    };

    return matcherProxy;
}

function proxy(matcher) {
    return {
        then : function(fn) {
            matcher.addResponse(fn);
            return this;
        },
        thenReturn : function(val) {
            matcher.addResponse(function() {
                return val;
            });

            return this;
        },
        thenThrow : function(err) {
            matcher.addResponse(function() {
                throw err;
            });

            return this;
        }
    };
}

function CallHandler(spy) {
    var matcherSets = [];
    var defaultSet = new MatcherSet();

    spy.and.callFake(function() {
        var args = Array.prototype.slice.call(arguments, 0);

        for (var i = 0; i < matcherSets.length; ++i) {
            var set = matcherSets[i];
            
            if (set.matches(args)) {
                return set.apply(args);
            }
        }

        return defaultSet.apply(args);
    });

    this.isCalledWith = function() {
        var args  = Array.prototype.slice.call(arguments, 0);
        var matchers = [];

        for (var i = 0; i < args.length; ++i) {
            matchers.push(createMatcher(args[i]));
        }

        var matcherSet = new MatcherSet(matchers);
        matcherSets.push(matcherSet);

        return proxy(matcherSet);
    };

    this.isCalled = proxy(defaultSet);
    spy.and.callFake = this.isCalled.then;
}

function when(spy) {
    for (var i = 0; i < handlers.length; ++i) {
        if (handlers[i].spy === spy) {
            return handlers[i].handler;
        }
    }
    
    var handler = new CallHandler(spy);
    
    handlers.push({
        'spy' : spy,
        'handler' : handler
    });

    return handler;
}

when.is = function(val) {
    return function(arg) {
        return val == arg;
    };
};

when.captor = captor;

when.noConflict = function() {
  root.when = previous_when;
  return when;
}

if (typeof exports !== 'undefined' ) {
  if (typeof module !== 'undefined' && module.exports) {
    exports = module.exports = when;
  }
  exports.when = when;
}
else {
  root.when = when;
}
}).call(this);
{'fraction_non_alphanumeric': 0.13252361673414306, 'fraction_numerical': 0.004588394062078272, 'mean_word_length': 1.8286259541984733, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '5844832', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2235, 'n_words': 502}