<commit_before>/**
 * http-backend-proxy - https://github.com/kbaltrinic/http-backend-proxy
 * (c) 2014 <NAME>. http://www.baltrinic.com
 * License: MIT
 */
'use strict';

module.exports = function(browser, options){

  var DEFAULT_CONTEXT_FIELD_NAME = 'context';

  options || (options = {});
  options.buffer || (options.buffer = false);

  //This is for backward compatibility since we changed the API from 1.1.1 to 1.2
  if(options.contextField === false){
    console.warn("Setting contextField: false is deprected.  Set contextAutoSync: false instead.");
    options.contextAutoSync = false;
  }

  options.contextAutoSync = typeof(options.contextAutoSync) === 'undefined' || !!options.contextAutoSync;
  options.contextField || (options.contextField = DEFAULT_CONTEXT_FIELD_NAME);

  var proxy = this;
  var buffer = [];

  this[options.contextField] = {};

  createMethods(this, 'when', buildWhenFunction);

  function createMethods(proxy, prefix, functionBuilder) {
    ['', 'GET', 'PUT', 'HEAD', 'POST', 'DELETE', 'PATCH', 'JSONP'].forEach(function(method) {
     proxy[prefix + method] = functionBuilder(prefix + method);
    });
  }

  function buildWhenFunction(funcName){

    return function(){

      var whenJS = 'window.$httpBackend.' + funcName + '(' + stringifyArgs(arguments) + ')';

      return {
        respond: function() {

          var fullJS = whenJS +'.respond(' + stringifyArgs(arguments) + ');'

          return executeOrBuffer(fullJS);
        },
        passThrough: function() {
          return executeOrBuffer(whenJS +'.passThrough(' + stringifyArgs(arguments) + ');');
        }
      };

    };

  };

  function executeOrBuffer(script){
    if(options.buffer){
      buffer.push(script);
    } else {
      script = getContextDefinitionScript() + script;
      return browser.executeScript(script);
    }
  }

  this.flush = function(){
    if(buffer.length > 0){
      var script = getContextDefinitionScript() + buffer.join('\n');
      buffer = [];
      return browser.executeScript(script);
    } else {
      var deferred = protractor.promise.defer();
      deferred.promise.complete();
      return deferred.promise;
    }
  }

  this.syncContext = function(context){

    if(typeof(context) !== 'undefined'){

      //If and only if both are simple objects, merge them
      if(Object.prototype.toString.call(proxy[options.contextField]) === '[object Object]'
        && Object.prototype.toString.call(context) === '[object Object]'){

        for (var key in context) {
          if (context.hasOwnProperty(key)) {
            proxy[options.contextField][key] = context[key];
          }
        }

        context = proxy[options.contextField];

      } else {

        proxy[options.contextField] = context;

      }

    } else {

      if(typeof(proxy[options.contextField]) === 'undefined'){
        proxy[options.contextField] = {};
      }

      context =  proxy[options.contextField];
    }

    return browser.executeScript(getContextDefinitionScript(context));
  }

  function stringifyArgs(args){
    var i, s = [];
    for(i = 0; i < args.length; i++){
      s.push(stringifyObject(args[i]));
    }
    return s.join(', ');
  }

  function getContextDefinitionScript(context){

    if(options.contextAutoSync){
      context = context || proxy[options.contextField];
    }

    if(typeof(context) !== 'undefined'){
      return 'window.$httpBackend.' + options.contextField + '=' + stringifyObject(context) + ';';
    } else {
      return '';
    }
  }

  function stringifyObject(obj){

    if(obj === null)
      return 'null';

    if(typeof obj === 'function' || obj instanceof RegExp )
      return obj.toString();

    if(typeof(obj) === 'object' && !(obj instanceof Array)){

      var fields = [];
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          fields.push('"'+ key + '":' + stringifyObject(obj[key]));
        }
      }

      return '{' + fields.join(',') + '}';
    }

    return JSON.stringify(obj);

  }

};<commit_msg>Update to make reuse of the proxy class easier.<commit_after>/**
 * http-backend-proxy - https://github.com/kbaltrinic/http-backend-proxy
 * (c) 2014 <NAME>. http://www.baltrinic.com
 * License: MIT
 */
'use strict';

var Proxy = function(browser, options){

  var DEFAULT_CONTEXT_FIELD_NAME = 'context';

  options || (options = {});
  options.buffer || (options.buffer = false);

  //This is for backward compatibility since we changed the API from 1.1.1 to 1.2
  if(options.contextField === false){
    console.warn("Setting contextField: false is deprected.  Set contextAutoSync: false instead.");
    options.contextAutoSync = false;
  }

  options.contextAutoSync = typeof(options.contextAutoSync) === 'undefined' || !!options.contextAutoSync;
  options.contextField || (options.contextField = DEFAULT_CONTEXT_FIELD_NAME);

  var proxy = this;
  var buffer = [];

  this[options.contextField] = {};

  createMethods(this, 'when', buildWhenFunction);

  function createMethods(proxy, prefix, functionBuilder) {
    ['', 'GET', 'PUT', 'HEAD', 'POST', 'DELETE', 'PATCH', 'JSONP'].forEach(function(method) {
     proxy[prefix + method] = functionBuilder(prefix + method);
    });
  }

  function buildWhenFunction(funcName){

    return function(){

      var whenJS = 'window.$httpBackend.' + funcName + '(' + stringifyArgs(arguments) + ')';

      return {
        respond: function() {

          var fullJS = whenJS +'.respond(' + stringifyArgs(arguments) + ');'

          return executeOrBuffer(fullJS);
        },
        passThrough: function() {
          return executeOrBuffer(whenJS +'.passThrough(' + stringifyArgs(arguments) + ');');
        }
      };

    };

  };

  function executeOrBuffer(script){
    if(options.buffer){
      buffer.push(script);
    } else {
      script = getContextDefinitionScript() + script;
      return browser.executeScript(script);
    }
  }

  this.flush = function(){
    if(buffer.length > 0){
      var script = getContextDefinitionScript() + buffer.join('\n');
      buffer = [];
      return browser.executeScript(script);
    } else {
      var deferred = protractor.promise.defer();
      deferred.promise.complete();
      return deferred.promise;
    }
  }

  this.syncContext = function(context){

    if(typeof(context) !== 'undefined'){

      //If and only if both are simple objects, merge them
      if(Object.prototype.toString.call(proxy[options.contextField]) === '[object Object]'
        && Object.prototype.toString.call(context) === '[object Object]'){

        for (var key in context) {
          if (context.hasOwnProperty(key)) {
            proxy[options.contextField][key] = context[key];
          }
        }

        context = proxy[options.contextField];

      } else {

        proxy[options.contextField] = context;

      }

    } else {

      if(typeof(proxy[options.contextField]) === 'undefined'){
        proxy[options.contextField] = {};
      }

      context =  proxy[options.contextField];
    }

    return browser.executeScript(getContextDefinitionScript(context));
  }

  function stringifyArgs(args){
    var i, s = [];
    for(i = 0; i < args.length; i++){
      s.push(stringifyObject(args[i]));
    }
    return s.join(', ');
  }

  function getContextDefinitionScript(context){

    if(options.contextAutoSync){
      context = context || proxy[options.contextField];
    }

    if(typeof(context) !== 'undefined'){
      return 'window.$httpBackend.' + options.contextField + '=' + stringifyObject(context) + ';';
    } else {
      return '';
    }
  }

  function stringifyObject(obj){

    if(obj === null)
      return 'null';

    if(typeof obj === 'function' || obj instanceof RegExp )
      return obj.toString();

    if(typeof(obj) === 'object' && !(obj instanceof Array)){

      var fields = [];
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          fields.push('"'+ key + '":' + stringifyObject(obj[key]));
        }
      }

      return '{' + fields.join(',') + '}';
    }

    return JSON.stringify(obj);

  }

};

module.exports = Proxy;
{'fraction_non_alphanumeric': 0.15450061652281136, 'fraction_numerical': 0.0027127003699136867, 'mean_word_length': 2.8939030244839175, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2206213', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2349, 'n_words': 485}