<commit_before>var fs = require('fs')
  , path = require('path')
  , bogart = require('bogart')
  , util = require('util')
  , q = bogart.q
  , formWarden = require('form-warden');

function createValidationBeforeRenderListener(req, validationResult) {
  return function (viewEngine, opts) {
    opts = opts || {};
    opts.locals = opts.locals || {};

    if (!validationResult.validForm) {
      opts.locals.errors = validationResult.fields;
    }
  };
}

function createInitializationBeforeRenderListener(req, selector, validationOptions) {
  return function (viewEngine, opts) {
    opts.locals.formwardenScript = 
      opts.locals.formwardenScript +
      '\n\n' +
      util.format('<script type="text/javascript">'+
        '(function ($) { $("%s").validation(%s); })(jQuery);'+
        '</script>',
        selector, JSON.stringify(validationOptions))
  };
}

function Form(validationOptions) {
  var selector = 'form'
    , viewEngine;

  var form = function (next) {
    var handler = function (req) {
      var res
        , initializationListener = createInitializationBeforeRenderListener(req,
                                      form.selector(), validationOptions);
      
      form.viewEngine().on('beforeRender', initializationListener);

      if (bogart.isPost(req) || bogart.isPut(req) || bogart.isDelete(req)) {
        var validationResult = formWarden.validateForm(req.params, validationOptions)
          , beforeRenderListener = createValidationBeforeRenderListener(req, validationResult);
      
        form.viewEngine().on('beforeRender', beforeRenderListener);

        res = next(req)
          .then(function (res) {
            return res;
          })
          .fin(function (res) {
            form.viewEngine().removeListener('beforeRender', beforeRenderListener);

            return res;
          });
      } else {
        res = next(req);
      }

      return res.fin(function (res) {
        form.viewEngine().removeListener('beforeRender', initializationListener);
        return res;
      });
    };

    return handler;
  };

  form.viewEngine = function (val) {
    if (val) {
      viewEngine = viewEnginePlugin(val);
      return this;
    }

    if (viewEngine === undefined) {
      viewEngine = ViewEngine('mustache');
    }

    return viewEngine;
  };

  form.selector = function (val) {
    if (val) {
      selector = val;
      return this;
    }

    return selector;
  };

  return form;
}<commit_msg>use bogart.viewEngine where ViewEngine was still in use<commit_after>var fs = require('fs')
  , path = require('path')
  , bogart = require('bogart')
  , util = require('util')
  , q = bogart.q
  , formWarden = require('form-warden');

function createValidationBeforeRenderListener(req, validationResult) {
  return function (viewEngine, opts) {
    opts = opts || {};
    opts.locals = opts.locals || {};

    if (!validationResult.validForm) {
      opts.locals.errors = validationResult.fields;
    }
  };
}

function createInitializationBeforeRenderListener(req, selector, validationOptions) {
  return function (viewEngine, opts) {
    opts.locals.formwardenScript = 
      opts.locals.formwardenScript +
      '\n\n' +
      util.format('<script type="text/javascript">'+
        '(function ($) { $("%s").validation(%s); })(jQuery);'+
        '</script>',
        selector, JSON.stringify(validationOptions))
  };
}

function Form(validationOptions) {
  var selector = 'form'
    , viewEngine;

  var form = function (next) {
    var handler = function (req) {
      var res
        , initializationListener = createInitializationBeforeRenderListener(req,
                                      form.selector(), validationOptions);
      
      form.viewEngine().on('beforeRender', initializationListener);

      if (bogart.isPost(req) || bogart.isPut(req) || bogart.isDelete(req)) {
        var validationResult = formWarden.validateForm(req.params, validationOptions)
          , beforeRenderListener = createValidationBeforeRenderListener(req, validationResult);
      
        form.viewEngine().on('beforeRender', beforeRenderListener);

        res = next(req)
          .then(function (res) {
            return res;
          })
          .fin(function (res) {
            form.viewEngine().removeListener('beforeRender', beforeRenderListener);

            return res;
          });
      } else {
        res = next(req);
      }

      return res.fin(function (res) {
        form.viewEngine().removeListener('beforeRender', initializationListener);
        return res;
      });
    };

    return handler;
  };

  form.viewEngine = function (val) {
    if (val) {
      viewEngine = viewEnginePlugin(val);
      return this;
    }

    if (viewEngine === undefined) {
      viewEngine = bogart.viewEngine('mustache');
    }

    return viewEngine;
  };

  form.selector = function (val) {
    if (val) {
      selector = val;
      return this;
    }

    return selector;
  };

  return form;
}
{'fraction_non_alphanumeric': 0.1306007635121559, 'fraction_numerical': 0.0, 'mean_word_length': 2.6177325581395348, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1485879', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1382, 'n_words': 277}