Cleans up sloppy URLs on the request object, like /foo////bar/// to /foo/bar. - Node.js String

Node.js examples for String:URL String

Description

Cleans up sloppy URLs on the request object, like /foo////bar/// to /foo/bar.

Demo Code


'use strict'//from w  ww  .  j  a v  a2  s .  c  o m

/* *
 * Cleans up sloppy URLs on the request object, like /foo////bar/// to /foo/bar.
 * @private
 * @function strip
 * @param    {Object} path a url path to clean up
 * @returns  {String}
 */
function strip (path) {
  var cur
  var next
  var str = ''

  for (var i = 0; i < path.length; i++) {
    cur = path.charAt(i)

    if (i !== path.length - 1) {
      next = path.charAt(i + 1)
    }

    if (cur === '/' && (next === '/' || (next === '?' && i > 0))) {
      continue
    }

    str += cur
  }

  return (str)
}

Related Tutorials