// mongoose-keywordize

module.exports = exports = function keywordize (schema, options) {
  if (!Array.isArray(options.fields)) options.fields = [options.fields];

  var fields = options.fields.slice()
    , fn = 'function' == typeof options.fn && options.fn
    , upper = !! options.upper // if true, keywords will not be lowercased
    , index = options.index

  schema.add({ keywords: [String] });

  if (index) {
    schema.path('keywords').index(index);
  }

  /**
   * Keywordize.
   *
   * Breaks apart field values into separate keywords.
   * @return {MongooseArray}
   * @api public
   */

  schema.methods.keywordize = function () {
    var self = this;

    var values = fields.map(function (field) {
      return self.get(field);
    });

    if (fn) {
      var res = fn.call(self);
      if (undefined !== res) {
        if (!Array.isArray(res)) res = [res];
        values = values.concat(res);
      }
    }

    this.set('keywords', []);
    var keywords = this.keywords;
    var i = values.length;

    while (i--) {
      var arr = Array.isArray(values[i])
        ? values[i]
        : (values[i] || '').split(/\s/);

      arr.forEach(function (word) {
        if (word = word.trim()) {
          if (upper)
            keywords.addToSet(word);
          else
            keywords.addToSet(word.toLowerCase());
        }
      });
    }

    return keywords;
  }

  /**
   * Update the keywords if any field changed.
   */

  schema.pre('save', function (next) {
    var self = this;

    var changed = this.isNew || fields.some(function (field) {
      return self.isModified(field);
    });

    if (changed) this.keywordize();
    next();
  });
}

/**
 * Expose version.
 */

exports.version = JSON.parse(
    require('fs').readFileSync(__dirname + '/../package.json')
).version;

@@ -253,16 +253,94 @@
 ions.fn%0A
+%09%09, keywordField = options.keywordField %7C%7C 'keywords'%0A%09%09, newSchemaField = %7B%7D%0A
     , up
@@ -439,43 +439,78 @@
 ex%0A%0A
-  schema.add(%7B keywords: %5BString%5D %7D
+%09newSchemaField%5BkeywordField%5D = %5BString%5D;%0A%0A  schema.add(newSchemaField
 );%0A%0A
@@ -540,26 +540,28 @@
 ma.path(
-'
 keyword
-s'
+Field
 ).index(
@@ -1048,26 +1048,28 @@
 his.set(
-'
 keyword
-s'
+Field
 , %5B%5D);%0A 
@@ -1094,25 +1094,29 @@
 this.keyword
-s
+Field
 ;%0A    var i 

Update lib/index.js