<commit_before>var Backbone = require('backbone');

/**
 * Network Interceptors
 *
 * Tool to easily intercept network responses in order
 * to override or add behaviours on top of the usual ones
 */

function NetworkResponseInterceptor () {
  this._originalSync = Backbone.sync;
  this._successInterceptors = [];
  this._errorInterceptors = [];
}

NetworkResponseInterceptor.prototype.patchBackboneSync = function () {
  Backbone.sync = this._onSyncStart.bind(this);
};

NetworkResponseInterceptor.prototype.addSuccessInterceptor = function (interceptorFn) {
  this._successInterceptors.push(interceptorFn);
};

NetworkResponseInterceptor.prototype.addErrorInterceptor = function (interceptorFn) {
  this._errorInterceptors.push(interceptorFn);
};

NetworkResponseInterceptor.prototype._onSyncStart = function (method, model, options) {
  const errorCallback = options.error;
  const successCallback = options.success;

  const interceptErrorFn = function () {
    this._applyErrorInterceptors.apply(this, arguments);
    errorCallback.apply(this, arguments);
  };

  const interceptSuccessFn = function () {
    this._applySuccessInterceptors.apply(this, arguments);
    successCallback.apply(this, arguments);
  };

  options.error = interceptErrorFn.bind(this);
  options.success = interceptSuccessFn.bind(this);

  return this._originalSync.apply(this, arguments);
};

NetworkResponseInterceptor.prototype._applyErrorInterceptors = function (xhr, textStatus, errorThrown) {
  if (!this._errorInterceptors.length) {
    return;
  }

  this._errorInterceptors.map(function (interceptorFunction) {
    interceptorFunction(xhr, textStatus, errorThrown);
  });
};

NetworkResponseInterceptor.prototype._applySuccessInterceptors = function (response, textStatus, xhr) {
  if (!this._successInterceptors.length) {
    return;
  }

  this._successInterceptors.map(function (interceptorFunction) {
    interceptorFunction(response, textStatus, xhr);
  });
};

module.exports = new NetworkResponseInterceptor();<commit_msg>Add URL pattern filter to interceptor<commit_after>var _ = require('underscore');
var Backbone = require('backbone');

/**
 * Network Interceptors
 *
 * Tool to easily intercept network responses in order
 * to override or add behaviours on top of the usual ones
 */

function NetworkResponseInterceptor () {
  this._originalSync = Backbone.sync;
  this._successInterceptors = [];
  this._errorInterceptors = [];
  this._urlPatterns = [];
}

NetworkResponseInterceptor.prototype.patchBackboneSync = function () {
  Backbone.sync = this._onSyncStart.bind(this);
};

NetworkResponseInterceptor.prototype.addSuccessInterceptor = function (urlPattern, interceptorFn) {
  this._successInterceptors.push(interceptorFn);
  this._addPattern(urlPattern);
};

NetworkResponseInterceptor.prototype.addErrorInterceptor = function (urlPattern, interceptorFn) {
  this._errorInterceptors.push(interceptorFn);
  this._addPattern(urlPattern);
};

NetworkResponseInterceptor.prototype._onSyncStart = function (method, model, options) {
  var requestURL = model.url();

  if (!this._shouldListenToRequest(requestURL)) {
    return this._originalSync.apply(this, arguments);
  }

  const errorCallback = options.error;
  const successCallback = options.success;

  const interceptErrorFn = function () {
    this._applyErrorInterceptors.apply(this, arguments);
    errorCallback.apply(this, arguments);
  };

  const interceptSuccessFn = function () {
    this._applySuccessInterceptors.apply(this, arguments);
    successCallback.apply(this, arguments);
  };

  options.error = interceptErrorFn.bind(this);
  options.success = interceptSuccessFn.bind(this);

  return this._originalSync.apply(this, arguments);
};

NetworkResponseInterceptor.prototype._addPattern = function (pattern) {
  this._urlPatterns.push(pattern);
};

NetworkResponseInterceptor.prototype._applyErrorInterceptors = function (xhr, textStatus, errorThrown) {
  if (!this._errorInterceptors.length) {
    return;
  }

  this._errorInterceptors.map(function (interceptorFunction) {
    interceptorFunction(xhr, textStatus, errorThrown);
  });
};

NetworkResponseInterceptor.prototype._applySuccessInterceptors = function (response, textStatus, xhr) {
  if (!this._successInterceptors.length) {
    return;
  }

  this._successInterceptors.map(function (interceptorFunction) {
    interceptorFunction(response, textStatus, xhr);
  });
};

NetworkResponseInterceptor.prototype._shouldListenToRequest = function (url) {
  var patternChecks = this._urlPatterns.map(function (pattern) {
    return url.indexOf(pattern) > -1;
  });

  return _.some(patternChecks);
};

module.exports = new NetworkResponseInterceptor();
{'fraction_non_alphanumeric': 0.10869101003630152, 'fraction_numerical': 0.00021353833013025838, 'mean_word_length': 5.5694249649368865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '833229', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1280, 'n_words': 241}