function removeDotSegments(path) {
    if (path === '/')
      return '/';

    var leadingSlash = path[0] === '/' ? '/' : '';
    var trailingSlash = path.slice(-1) === '/' ? '/' : '';
    var segments = path.split('/');

    var out = [];
    var up = 0;
    for (var pos = 0; pos < segments.length; pos++) {
      var segment = segments[pos];
      switch (segment) {
        case '':
        case '.':
          break;
        case '..':
          if (out.length)
            out.pop();
          else
            up++;
          break;
        default:
          out.push(segment);
      }
    }

    if (!leadingSlash) {
      while (up-- > 0) {
        out.unshift('..');
      }

      if (out.length === 0)
        out.push('.');
    }

    return leadingSlash + out.join('/') + trailingSlash;
  }
Removes dot segments in given path component, as described in
RFC 3986, section 5.2.4.

@param {string} path A non-empty path component.
@return {string} Path component with removed dot segments.