<commit_before>/**
 * Module dependencies
 */

var request = require('hyper-path');
var websafe = require('websafe-base64');
var type = require('type');
var has = Object.prototype.hasOwnProperty;

/**
 * Watch the value at a path
 */

exports.$watchPath = function(path, fn) {
  var $scope = this;

  // Make the request to the api
  var req = request(path);

  // we're not starting at the root of the api so we need to watch
  // the first property, or `req.index`, in the path
  if (!req.isRoot) $scope.$watch(req.index, function(parent, old) {
    req._root = req._root || {};
    req._root[req.index] = parent;
    req.scope(req._root);
  }, true);

  // listen to any updates from the api
  req.on(function(err, value) {
    $safeApply.call($scope, function() {
      fn(err, value, req);
    });
  });

  // stop listening when our directive goes away
  $scope.$on('$destroy', function() {
    req.off();
  });
};

/**
 * Safely call apply
 *
 * @param {Function} fn
 * @api private
 */

exports.$safeApply = $safeApply;
function $safeApply(fn) {
  var phase = this.$root.$$phase;
  if (phase === '$apply' || phase === '$digest') return fn();
  this.$apply(fn);
}

exports.encode = function(str) {
  return websafe.encode(str);
};

exports.decode = function(str) {
  return websafe.decode(str);
};

/**
 * Expose merge
 */

exports.merge = merge;

/**
 * Merge `b` into `a`.
 *
 * @param {Object} a
 * @param {Object} b
 * @return {Object} a
 * @api public
 */

function merge (a, b) {
  var atype = type(a);
  var btype = type(b);
  // set simple types
  if (btype !== 'object' && btype !== 'array') return b;

  // if they're different types just return b
  if (atype !== btype) return b;

  for (var key in b) {
    if (has.call(b, key) && b[key] != null) {
      if ('object' === type(b[key])) {
        a[key] = merge(a[key], b[key]);
      } else {
        a[key] = b[key];
      }
    }
  }
  return a;
}<commit_msg>improve the merge function<commit_after>/**
 * Module dependencies
 */

var request = require('hyper-path');
var websafe = require('websafe-base64');
var type = require('type');

/**
 * Watch the value at a path
 */

exports.$watchPath = function(path, fn) {
  var $scope = this;

  // Make the request to the api
  var req = request(path);

  // we're not starting at the root of the api so we need to watch
  // the first property, or `req.index`, in the path
  if (!req.isRoot) $scope.$watch(req.index, function(parent, old) {
    req._root = req._root || {};
    req._root[req.index] = parent;
    req.scope(req._root);
  }, true);

  // listen to any updates from the api
  req.on(function(err, value) {
    $safeApply.call($scope, function() {
      fn(err, value, req);
    });
  });

  // stop listening when our directive goes away
  $scope.$on('$destroy', function() {
    req.off();
  });
};

/**
 * Safely call apply
 *
 * @param {Function} fn
 * @api private
 */

exports.$safeApply = $safeApply;
function $safeApply(fn) {
  var phase = this.$root.$$phase;
  if (phase === '$apply' || phase === '$digest') return fn();
  this.$apply(fn);
}

exports.encode = function(str) {
  return websafe.encode(str);
};

exports.decode = function(str) {
  return websafe.decode(str);
};

/**
 * Expose merge
 */

exports.merge = merge;

/**
 * Merge `b` into `a`.
 *
 * @param {Object} a
 * @param {Object} b
 * @return {Object} a
 * @api public
 */

function merge (a, b) {
  var t = type(b);
  // set simple types
  if (t !== 'object' && t !== 'array') return b;

  // if they're different types just return b
  if (type(a) !== t) return b;

  // make the arrays the same length
  if (t === 'array') a.splice(b.length);

  // remove any old properties
  // TODO is there a way to improve the O(2N)?
  if (t === 'object') {
    for (var key in a) {
      // don't delete angular properties
      if (key.charAt && key.charAt(0) === '$') continue;
      if (!b[key]) delete a[key];
    }
  }

  for (var key in b) {
    a[key] = merge(a[key], b[key]);
  }
  return a;
}
{'fraction_non_alphanumeric': 0.18079519879969994, 'fraction_numerical': 0.0015003750937734434, 'mean_word_length': 2.883495145631068, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1141579', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1370, 'n_words': 421}