<commit_before>AstroPublish = class {
  constructor(name, collection){
    this._predicates = [];
    this._mongoRules = [];
    this._name = name;
    this._queries = [];
    this._collection = collection;
  }

  static defineMethod(options){
    if(AstroPublish.prototype[options.name]){
      throw new Error('Already defined');
    }

    if(!_.isObject(options)){
      throw new Error('Options must be an object');
    }

    if(!_.contains(['predicate', 'mongoRule', 'query'], options.type)){
      throw new Error(`Such method type ${type} doesn\'t exist`);
    }

    let methods = {
      predicate: '_predicates',
      mongoRule: '_mongoRules',
      query: '_queries'
    };

    AstroPublish.prototype[ options.name ] = function(...args){
      this[ methods[options.type] ].push({
        fn: options.fn,
        context: options.context,
        args
      });

      return this;
    };
  }

  apply(){
    let self = this;

    Meteor.publish(this._name, function(...args){
      let userId = this.userId;
      let query = compactMethods(self._queries, self, args, userId);
      let rules = compactMethods(self._mongoRules, self, args, userId);

      let allPredicatesPass = _.every(self._predicates, (predicate) => {
        return predicate.fn.call(this, ...predicate.args, userId) === true;
      });

      if(allPredicatesPass){
        return self._collection.find(query, rules);
      } else {
        this.ready();
      }
    });
  }
};

AP = AstroPublish;

function compactMethods(methods, AstroPublish, args, userId){
  return _.reduce(methods, function(rules, rule){
    let fn = _.isFunction(rule.args[0]) ? rule.args[0] : rule.fn;
    let fnArgs = rule.context === 'chain' ? rule.args : args;

    return _.extend(rules, fn.call(AstroPublish, ...fnArgs, userId));
  }, {});
}

Mongo.Collection.prototype.publish = function(name){
  if(!name){
    throw new Error('[AstroPublish] You have to specify the publish name');
  }

  return new AstroPublish(name, this);
};
<commit_msg>Minor typo<commit_after>AstroPublish = class {
  constructor(name, collection){
    this._predicates = [];
    this._mongoRules = [];
    this._name = name;
    this._queries = [];
    this._collection = collection;
  }

  static defineMethod(options){
    if(AstroPublish.prototype[options.name]){
      throw new Error('Already defined');
    }

    if(!_.isObject(options)){
      throw new Error('Options must be an object');
    }

    if(!_.contains(['predicate', 'mongoRule', 'query'], options.type)){
      throw new Error(`Such method type ${options.type} doesn\'t exist`);
    }

    let methods = {
      predicate: '_predicates',
      mongoRule: '_mongoRules',
      query: '_queries'
    };

    AstroPublish.prototype[ options.name ] = function(...args){
      this[ methods[options.type] ].push({
        fn: options.fn,
        context: options.context,
        args
      });

      return this;
    };
  }

  apply(){
    let self = this;

    Meteor.publish(this._name, function(...args){
      let userId = this.userId;
      let query = compactMethods(self._queries, self, args, userId);
      let rules = compactMethods(self._mongoRules, self, args, userId);

      let allPredicatesPass = _.every(self._predicates, (predicate) => {
        return predicate.fn.call(this, ...predicate.args, userId) === true;
      });

      if(allPredicatesPass){
        return self._collection.find(query, rules);
      } else {
        this.ready();
      }
    });
  }
};

AP = AstroPublish;

function compactMethods(methods, AstroPublish, args, userId){
  return _.reduce(methods, function(rules, rule){
    let fn = _.isFunction(rule.args[0]) ? rule.args[0] : rule.fn;
    let fnArgs = rule.context === 'chain' ? rule.args : args;

    return _.extend(rules, fn.call(AstroPublish, ...fnArgs, userId));
  }, {});
}

Mongo.Collection.prototype.publish = function(name){
  if(!name){
    throw new Error('[AstroPublish] You have to specify the publish name');
  }

  return new AstroPublish(name, this);
};

{'fraction_non_alphanumeric': 0.15157529149094517, 'fraction_numerical': 0.0009923096005953857, 'mean_word_length': 2.873198847262248, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3126879', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1253, 'n_words': 279}