<commit_before>import {covenance, CovenantBroken} from 'covenance'
import frosty from 'frosty'
import _ from 'underscore'

const DEFAULTS = {
  chunk: 1,
  delay: 0,
  scope: null
};

const ALIASES = {
  chunk: ['chunksize'],
  delay: [
    'yield',
    'yieldtime',
    'delaytime'
  ],
  scope: []
};

const SCHEMA = Object.getOwnPropertyNames(DEFAULTS);

class ChunkifyOptions {

  get covenance() {
    return covenance.of(
      {
        attribute: 'chunk',
        validator: (chunk) => {
          return _.isNumber(chunk) && chunk > 0;
        },
        excstring: "'chunk' should be a positive number"
      },
      {
        attribute: 'delay',
        validator: (delay) => {
          return _.isNumber(delay) && delay >= 0;
        },
        excstring: "'delay' should be a non-negative number"
      },
      {
        attribute: 'scope',
        validator: (scope) => {
          if (scope === undefined) {
            return false
          } else if (_.isBoolean(scope)) {
            return false
          } else if (_.isNumber(scope)) {
            return false
          }
          return true
        },
        excstring: "'scope' should be a defined non-boolean and non-number, or null"
      }
    )
  }

  constructor(options) {
    _.extend(this, this.constructor._parse_options(options));
    this.check_covenants();
    return this;
  }

  static _parse_options(options) {
    let parsed = {};
    let setkey = (key) => {
      if (options[key] === undefined) {
        return false;
      }
      parsed[key] = options[key];
      return true
    };
    for (let key of SCHEMA) {
      if (!setkey(key)) {
        for (let alias of ALIASES[key]) {
          if (setkey(alias)) {
            break
          }
        }
      }
    }
    return _.defaults(parsed, DEFAULTS)
  }

  static of(options) {
    let throw_error = () => {
      throw new TypeError(`Expected options object, got ${typeof options}`)
    };
    if (!_.isObject(options)) {
      throw_error()
    } else if (Array.isArray(options)) {
      throw_error()
    } else if (_.isFunction(options)) {
      throw_error()
    }
    return new this(options)
  }

}

frosty.freeze(ChunkifyOptions.prototype, ...SCHEMA);
covenance.covenant(ChunkifyOptions);

export default {
  of() {
    return ChunkifyOptions.of(...arguments)
  },

  is(thing) {
    return (thing instanceof ChunkifyOptions)
  }
}<commit_msg>Set original key from alias; don't set alias key<commit_after>import {covenance, CovenantBroken} from 'covenance'
import frosty from 'frosty'
import _ from 'underscore'

const DEFAULTS = {
  chunk: 1,
  delay: 0,
  scope: null
};

const ALIASES = {
  chunk: ['chunksize'],
  delay: [
    'yield',
    'yieldtime',
    'delaytime'
  ],
  scope: []
};

const SCHEMA = Object.getOwnPropertyNames(DEFAULTS);

class ChunkifyOptions {

  get covenance() {
    return covenance.of(
      {
        attribute: 'chunk',
        validator: (chunk) => {
          return _.isNumber(chunk) && chunk > 0;
        },
        excstring: "'chunk' should be a positive number"
      },
      {
        attribute: 'delay',
        validator: (delay) => {
          return _.isNumber(delay) && delay >= 0;
        },
        excstring: "'delay' should be a non-negative number"
      },
      {
        attribute: 'scope',
        validator: (scope) => {
          if (scope === undefined) {
            return false
          } else if (_.isBoolean(scope)) {
            return false
          } else if (_.isNumber(scope)) {
            return false
          }
          return true
        },
        excstring: "'scope' should be a defined non-boolean and non-number, or null"
      }
    )
  }

  constructor(options) {
    _.extend(this, this.constructor._parse_options(options));
    this.check_covenants();
    return this;
  }

  static _parse_options(options) {
    let parsed = {};
    let setkey = (key, alias) => {
      if (options[alias] === undefined) {
        return false;
      }
      parsed[key] = options[alias];
      return true
    };
    for (let key of SCHEMA) {
      if (!setkey(key, key)) {
        for (let alias of ALIASES[key]) {
          if (setkey(key, alias)) {
            break
          }
        }
      }
    }
    return _.defaults(parsed, DEFAULTS)
  }

  static of(options) {
    let throw_error = () => {
      throw new TypeError(`Expected options object, got ${typeof options}`)
    };
    if (!_.isObject(options)) {
      throw_error()
    } else if (Array.isArray(options)) {
      throw_error()
    } else if (_.isFunction(options)) {
      throw_error()
    }
    return new this(options)
  }

}

frosty.freeze(ChunkifyOptions.prototype, ...SCHEMA);
covenance.covenant(ChunkifyOptions);

export default {
  of() {
    return ChunkifyOptions.of(...arguments)
  },

  is(thing) {
    return (thing instanceof ChunkifyOptions)
  }
}
{'fraction_non_alphanumeric': 0.13151415675010258, 'fraction_numerical': 0.0016413623307345096, 'mean_word_length': 2.0854430379746836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4773840', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1533, 'n_words': 369}