<commit_before>'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _lodash = require('lodash');

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var createGetterAndSetter = function createGetterAndSetter(instance, field) {
  return {
    set: function set(value) {
      if (instance.data[field] !== value) {
        instance.data[field] = value;
        return instance._validate();
      }
    },
    get: function get() {
      return instance.data[field];
    },
    enumerable: true
  };
};

var Speck = function () {
  function Speck(data) {
    var _this = this;

    _classCallCheck(this, Speck);

    Object.defineProperty(this, 'schema', {
      value: this.constructor.SCHEMA,
      enumerable: false
    });

    Object.defineProperty(this, 'contexts', {
      value: this.constructor.CONTEXTS,
      enumerable: false
    });

    Object.defineProperty(this, 'childrenEntities', {
      value: Object.keys(this.constructor.SCHEMA).filter(function (field) {
        return !!_this.constructor.SCHEMA[field].type;
      }),
      enumerable: false
    });

    this.errors = {};
    Object.defineProperty(this, 'data', {
      value: this._mergeDefault(data || {}),
      enumerable: false
    });

    this._validate();
  }

  _createClass(Speck, [{
    key: '_mergeDefault',
    value: function _mergeDefault(data) {
      var newData = {};
      var field = void 0;
      for (field in this.schema) {

        newData[field] = (0, _lodash.isNil)(data[field]) ? this.schema[field].defaultValue : data[field];

        if (this.schema[field].type) {
          newData[field] = this.applyEntityConstructor(this.schema[field], newData[field]);
        }

        Object.defineProperty(this, field, createGetterAndSetter(this, field));
      }
      return newData;
    }
  }, {
    key: '_fetchChild',
    value: function _fetchChild(fieldValue) {
      if (Array.isArray(fieldValue)) {
        return fieldValue.map(this._fetchChild);
      }

      if (fieldValue && typeof fieldValue.toJSON === 'function') {
        return fieldValue.toJSON();
      }

      return fieldValue;
    }
  }, {
    key: '__validateField',
    value: function __validateField(field) {
      var validator = typeof this.schema[field] === 'function' ? this.schema[field] : this.schema[field].validator;

      var error = validator(this.data, field, this.constructor.name + 'Entity');

      if (error) {
        this.errors[field] = { errors: [error.message || error] };
      }
    }
  }, {
    key: '_validate',
    value: function _validate() {
      this.errors = {};

      var field = void 0;
      for (field in this.schema) {
        this.__validateField(field);
      }
      this.valid = Object.keys(this.errors).length === 0;

      if (!this.valid) {
        return this.errors;
      }
    }
  }, {
    key: '_includeChildErrors',
    value: function _includeChildErrors(field, errors, entity, index) {
      if (!entity.valid) {
        if (errors[field] === undefined) {
          errors[field] = {};
        }

        errors[field][index] = entity.getErrors();
      }
      return errors;
    }
  }, {
    key: '_getChildrenErrors',
    value: function _getChildrenErrors(errors, field) {
      var children = Array.isArray(this[field]) ? this[field] : [this[field]];

      return children.reduce(this._includeChildErrors.bind(this, field), errors);
    }
  }, {
    key: 'applyEntityConstructor',
    value: function applyEntityConstructor(field, data) {
      var Type = field.type;

      if (field.builder) {
        return field.builder(data, Type);
      }

      if (!data) return;

      if (Array.isArray(data)) {
        return data.map(function (instance) {
          return new Type(instance);
        });
      }

      return new Type(data);
    }<commit_msg>building the lib<commit_after>'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _lodash = require('lodash');

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var createGetterAndSetter = function createGetterAndSetter(instance, field) {
  return {
    set: function set(value) {
      if (instance.data[field] !== value) {
        instance.data[field] = value;
        return instance._validate();
      }
    },
    get: function get() {
      return instance.data[field];
    },
    enumerable: true
  };
};

var Speck = function () {
  function Speck(data) {
    var _this = this;

    _classCallCheck(this, Speck);

    Object.defineProperty(this, 'schema', {
      value: this.constructor.SCHEMA,
      enumerable: false
    });

    Object.defineProperty(this, 'contexts', {
      value: this.constructor.CONTEXTS,
      enumerable: false
    });

    Object.defineProperty(this, 'childrenEntities', {
      value: Object.keys(this.constructor.SCHEMA).filter(function (field) {
        return !!_this.constructor.SCHEMA[field].type;
      }),
      enumerable: false
    });

    this.errors = {};
    Object.defineProperty(this, 'data', {
      value: this._mergeDefault(data || {}),
      enumerable: false
    });

    this._validate();
  }

  _createClass(Speck, [{
    key: '_mergeDefault',
    value: function _mergeDefault(data) {
      var newData = {};
      var field = void 0;
      for (field in this.schema) {

        newData[field] = (0, _lodash.isNil)(data[field]) ? this.schema[field].defaultValue : data[field];

        if (this.schema[field].type || this.schema[field].builder) {
          newData[field] = this.applyEntityConstructor(this.schema[field], newData[field]);
        }

        Object.defineProperty(this, field, createGetterAndSetter(this, field));
      }
      return newData;
    }
  }, {
    key: '_fetchChild',
    value: function _fetchChild(fieldValue) {
      if (Array.isArray(fieldValue)) {
        return fieldValue.map(this._fetchChild);
      }

      if (fieldValue && typeof fieldValue.toJSON === 'function') {
        return fieldValue.toJSON();
      }

      return fieldValue;
    }
  }, {
    key: '__validateField',
    value: function __validateField(field) {
      var validator = typeof this.schema[field] === 'function' ? this.schema[field] : this.schema[field].validator;

      var error = validator(this.data, field, this.constructor.name + 'Entity');

      if (error) {
        this.errors[field] = { errors: [error.message || error] };
      }
    }
  }, {
    key: '_validate',
    value: function _validate() {
      this.errors = {};

      var field = void 0;
      for (field in this.schema) {
        this.__validateField(field);
      }
      this.valid = Object.keys(this.errors).length === 0;

      if (!this.valid) {
        return this.errors;
      }
    }
  }, {
    key: '_includeChildErrors',
    value: function _includeChildErrors(field, errors, entity, index) {
      if (!entity.valid) {
        if (errors[field] === undefined) {
          errors[field] = {};
        }

        errors[field][index] = entity.getErrors();
      }
      return errors;
    }
  }, {
    key: '_getChildrenErrors',
    value: function _getChildrenErrors(errors, field) {
      var children = Array.isArray(this[field]) ? this[field] : [this[field]];

      return children.reduce(this._includeChildErrors.bind(this, field), errors);
    }
  }, {
    key: 'applyEntityConstructor',
    value: function applyEntityConstructor(field, data) {
      var Type = field.type;

      if ((0, _lodash.isFunction)(field.builder)) {
        return field.builder(data, Type);
      }

      if (!data) return;

      if (Array.isArray(data)) {
        return data.map(function (instance) {
          return new Type(instance);
        });
      }

      return new Type(data);
    }
{'fraction_non_alphanumeric': 0.1413108839446783, 'fraction_numerical': 0.0013028663058729205, 'mean_word_length': 3.038445973290166, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7337685', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2912, 'n_words': 682}