/**!
 * urlencode - lib/urlencode.js
 *
 * Copyright(c) 2012 - 2014
 * MIT Licensed
 *
 * Authors:
 *   fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
 */

"use strict";

/**
 * Module dependencies.
 */

var iconv = require('iconv-lite');
var QueryString = require('querystring');
var utility = require('utility');

function isUTF8(charset) {
  if (!charset) {
    return true;
  }
  charset = charset.toLowerCase();
  return charset === 'utf8' || charset === 'utf-8';
}

function encode(str, charset) {
  if (isUTF8(charset)) {
    return encodeURIComponent(str);
  }

  var buf = iconv.encode(str, charset);
  var encodeStr = '';
  for (var i = 0; i < buf.length; i++) {
    encodeStr += '%' + buf[i].toString('16').toUpperCase();
  }
  return encodeStr;
}

function decode(str, charset) {
  if (isUTF8(charset)) {
    return decodeURIComponent(str);
  }

  var bytes = [];
  for (var i = 0; i < str.length; ) {
    if (str[i] === '%') {
      i++;
      bytes.push(parseInt(str.substring(i, i + 2), 16));
      i += 2;
    } else {
      bytes.push(str.charCodeAt(i));
      i++;
    }
  }
  var buf = new Buffer(bytes);
  return iconv.decode(buf, charset);
}

function parse(qs, sep, eq, options) {
  if (typeof sep === 'object') {
    // parse(qs, options)
    options = sep;
    sep = null;
  }

  sep = sep || '&';
  eq = eq || '=';
  var obj = {};

  if (typeof qs !== 'string' || qs.length === 0) {
    return obj;
  }

  var regexp = /\+/g;
  qs = qs.split(sep);

  var maxKeys = 1000;
  var charset = null;
  if (options) {
    if (typeof options.maxKeys === 'number') {
      maxKeys = options.maxKeys;
    }
    if (typeof options.charset === 'string') {
      charset = options.charset;
    }
  }

  var len = qs.length;
  // maxKeys <= 0 means that we should not limit keys count
  if (maxKeys > 0 && len > maxKeys) {
    len = maxKeys;
  }

  for (var i = 0; i < len; ++i) {
    var x = qs[i].replace(regexp, '%20');
    var idx = x.indexOf(eq);
    var kstr, vstr, k, v;

    if (idx >= 0) {
      kstr = x.substr(0, idx);
      vstr = x.substr(idx + 1);
    } else {
      kstr = x;
      vstr = '';
    }

    if (kstr && kstr.indexOf('%') >= 0) {
      try {
        k = decode(kstr, charset);
      } catch (e) {
        k = kstr;
      }
    } else {
      k = kstr;
    }

    if (vstr && vstr.indexOf('%') >= 0) {
      try {
        v = decode(vstr, charset);
      } catch (e) {
        v = vstr;
      }
    } else {
      v = vstr;
    }

    if (!utility.hasOwnProperty(obj, k)) {
      obj[k] = v;
    } else if (Array.isArray(obj[k])) {
      obj[k].push(v);
    } else {
      obj[k] = [obj[k], v];
    }
  }

  return obj;
}

module.exports = encode;
module.exports.encode = encode;
module.exports.decode = decode;
module.exports.parse = parse;

@@ -245,50 +245,8 @@
 ');%0A
-var QueryString = require('querystring');%0A
 var 
@@ -677,16 +677,45 @@
 ng('16')
+;%0A  %7D%0A  encodeStr = encodeStr
 .toUpper
@@ -722,20 +722,16 @@
 Case();%0A
-  %7D%0A
   return

remove unused variable QueryString