<commit_before>var util = require('util');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('dozer-model:object-proxy');
var q = require('q');
var bson = require('bson');
var ImmutableDate = require('bloody-immutable-date');
var ArrayProxy = require('./array-proxy');

function ObjectProxy(obj) {
  Object.defineProperty(this, '__obj', {
    enumerable: false,
    configurable: true,
    writable: false,
    value: {}
  });
  Object.defineProperty(this, '__changeListeners', {
    enumerable: false,
    configurable: true,
    writable: false,
    value: {}
  });
  if (_.isObject(obj)) {
    for (var p in this.__definition.properties) {
      if ('undefined' !== typeof obj[p]) {
        this[p] = obj[p];
      }
    }
  }
  var init = this.__definition.init || this.__definition.initialize;
  if (init && typeof init === 'function') {
    init.call(this);
  }
}

util.inherits(ObjectProxy, EventEmitter);

ObjectProxy.prototype.set = function (path /* or data */, value) {
  if (_.isObject(path) && !Array.isArray(path)) {
    for (var p in path) {
      this[p] = path[p];
    }
  } else {
    if (util.isString(path)) {
      path = path.split('.');
    } else if (!Array.isArray(path)) {
      path = [path];
    }
    var propertyDef = this.__definition.properties[path[0]];
    var type = propertyDef && propertyDef.type ? propertyDef.type : 'undefined';
    if (path.length === 1) {
      this[path[0]] = value;
    } else {
      if ('undefined' === typeof this[path[0]]) {
        if (type === 'array') {
          this[path[0]] = [];
        } else {
          this[path[0]] = {};
        }
      }
      if (path.length === 2) {
        this[path[0]][path[1]] = value;
      } else {
        var child = this[path.shift()];
        child.set(path, value);
      }
    }
  }
  return this;
};

ObjectProxy.prototype.get = function (path) {
  if (util.isString(path)) {
    path = path.split('.');
  } else if (!util.isArray(path)) {
    path = [path];
  }
  if (path.length === 1) {
    return this[path[0]];
  } else if (path.length === 2) {
    return this[path[0]][path[1]];
  } else {
    return this[path.shift()].get(path);
  }
};

ObjectProxy.prototype.has = function (path) {
  if (!util.isArray(path)) {
    path = path.split('.');
  }
  if (path.length === 1) {
    return path[0] in this;
  }
  return this.get(path.shift()).has(path);
};

ObjectProxy.prototype.toObject = function (options) {
  options = options || {};
  _.defaults(options, {
    virtuals: true
  });
  function objectify (val) {
    var r;
    if (val instanceof ArrayProxy) {
      r = val.toArray(options);
    } else if (val instanceof ObjectProxy) {
      r = val.toObject(options);
    } else if (val instanceof ImmutableDate) {
      // HACK ImmutableDate doesn't give us access to the underlying date, so
      // we have to refer to the private var
      r = val._date;
    } else {
      r = val;
    }
    return r;
  }
  var obj = {};
  for (var p in this.__definition.properties) {
    var propertyDef = this.__definition.properties[p];
    if (!options.virtuals && propertyDef.type === 'virtual') {
      continue;
    }
    var o = objectify(this[p]);
    if (typeof o !== 'undefined') {
      obj[p] = o;
    }
  }
  return obj;
};

ObjectProxy.prototype.toJSON = function (options) {
  options = options || {};
  _.defaults(options, {
    virtuals: true
  });
  function jsonify (val) {
    var r;
    if (val instanceof ImmutableDate) {
      r = { $date: val.toISOString() };
    } else  if (_.isRegExp(val)) {
      r = { $regex: val.toString() };
    } else if (val instanceof bson.ObjectID) {
      r = { $oid: val.toString() };
    } else if ('undefined' !== typeof val && _.isFunction(val.toJSON)) {
      r = val.toJSON(options);
    } else {
      r = val;
    }
    return r;
  }
  var obj = {};
  for (var p in this.__definition.properties) {
    var propertyDef = this.__definition.properties[p];
    if (!options.virtuals && propertyDef.type === 'virtual') {
      continue;
    }
    var o = jsonify(this[p]);<commit_msg>adds flag to turn on/off extended json<commit_after>var util = require('util');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('dozer-model:object-proxy');
var q = require('q');
var bson = require('bson');
var ImmutableDate = require('bloody-immutable-date');
var ArrayProxy = require('./array-proxy');

function ObjectProxy(obj) {
  Object.defineProperty(this, '__obj', {
    enumerable: false,
    configurable: true,
    writable: false,
    value: {}
  });
  Object.defineProperty(this, '__changeListeners', {
    enumerable: false,
    configurable: true,
    writable: false,
    value: {}
  });
  if (_.isObject(obj)) {
    for (var p in this.__definition.properties) {
      if ('undefined' !== typeof obj[p]) {
        this[p] = obj[p];
      }
    }
  }
  var init = this.__definition.init || this.__definition.initialize;
  if (init && typeof init === 'function') {
    init.call(this);
  }
}

util.inherits(ObjectProxy, EventEmitter);

ObjectProxy.prototype.set = function (path /* or data */, value) {
  if (_.isObject(path) && !Array.isArray(path)) {
    for (var p in path) {
      this[p] = path[p];
    }
  } else {
    if (util.isString(path)) {
      path = path.split('.');
    } else if (!Array.isArray(path)) {
      path = [path];
    }
    var propertyDef = this.__definition.properties[path[0]];
    var type = propertyDef && propertyDef.type ? propertyDef.type : 'undefined';
    if (path.length === 1) {
      this[path[0]] = value;
    } else {
      if ('undefined' === typeof this[path[0]]) {
        if (type === 'array') {
          this[path[0]] = [];
        } else {
          this[path[0]] = {};
        }
      }
      if (path.length === 2) {
        this[path[0]][path[1]] = value;
      } else {
        var child = this[path.shift()];
        child.set(path, value);
      }
    }
  }
  return this;
};

ObjectProxy.prototype.get = function (path) {
  if (util.isString(path)) {
    path = path.split('.');
  } else if (!util.isArray(path)) {
    path = [path];
  }
  if (path.length === 1) {
    return this[path[0]];
  } else if (path.length === 2) {
    return this[path[0]][path[1]];
  } else {
    return this[path.shift()].get(path);
  }
};

ObjectProxy.prototype.has = function (path) {
  if (!util.isArray(path)) {
    path = path.split('.');
  }
  if (path.length === 1) {
    return path[0] in this;
  }
  return this.get(path.shift()).has(path);
};

ObjectProxy.prototype.toObject = function (options) {
  options = options || {};
  _.defaults(options, {
    virtuals: true
  });
  function objectify (val) {
    var r;
    if (val instanceof ArrayProxy) {
      r = val.toArray(options);
    } else if (val instanceof ObjectProxy) {
      r = val.toObject(options);
    } else if (val instanceof ImmutableDate) {
      // HACK ImmutableDate doesn't give us access to the underlying date, so
      // we have to refer to the private var
      r = val._date;
    } else {
      r = val;
    }
    return r;
  }
  var obj = {};
  for (var p in this.__definition.properties) {
    var propertyDef = this.__definition.properties[p];
    if (!options.virtuals && propertyDef.type === 'virtual') {
      continue;
    }
    var o = objectify(this[p]);
    if (typeof o !== 'undefined') {
      obj[p] = o;
    }
  }
  return obj;
};

ObjectProxy.prototype.toJSON = function (options) {
  options = options || {};
  _.defaults(options, {
    virtuals: true,
    extended: true
  });
  function jsonify (val) {
    var r;
    if (val instanceof ImmutableDate) {
      r = options.extended ? { $date: val.toISOString() } : val.toISOString();
    } else  if (_.isRegExp(val)) {
      r = options.extended ? { $regex: val.toString() } : val.toString();
    } else if (val instanceof bson.ObjectID) {
      r = options.extended ? { $oid: val.toString() } : val.toString();
    } else if ('undefined' !== typeof val && _.isFunction(val.toJSON)) {
      r = val.toJSON(options);
    } else {
      r = val;
    }
    return r;
  }
  var obj = {};
  for (var p in this.__definition.properties) {
    var propertyDef = this.__definition.properties[p];
    if (!options.virtuals && propertyDef.type === 'virtual') {
      continue;
    }
    var o = jsonify(this[p]);
{'fraction_non_alphanumeric': 0.1698135755258126, 'fraction_numerical': 0.0038240917782026767, 'mean_word_length': 2.64821272885789, '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': '5152183', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2774, 'n_words': 657}