function dirname(path) {
  assertPath(path);
  const len = path.length;
  if (len === 0) return ".";
  let rootEnd = -1;
  let end = -1;
  let matchedSlash = true;
  let offset = 0;
  const code = path.charCodeAt(0);
  // Try to match a root
  if (len > 1) {
    if (isPathSeparator(code)) {
      // Possible UNC root
      rootEnd = offset = 1;
      if (isPathSeparator(path.charCodeAt(1))) {
        // Matched double path separator at beginning
        let j = 2;
        let last = j;
        // Match 1 or more non-path separators
        for (; j < len; ++j) {
          if (isPathSeparator(path.charCodeAt(j))) break;
        }
        if (j < len && j !== last) {
          // Matched!
          last = j;
          // Match 1 or more path separators
          for (; j < len; ++j) {
            if (!isPathSeparator(path.charCodeAt(j))) break;
          }
          if (j < len && j !== last) {
            // Matched!
            last = j;
            // Match 1 or more non-path separators
            for (; j < len; ++j) {
              if (isPathSeparator(path.charCodeAt(j))) break;
            }
            if (j === len) {
              // We matched a UNC root only
              return path;
            }
            if (j !== last) {
              // We matched a UNC root with leftovers
              // Offset by 1 to include the separator after the UNC root to
              // treat it as a "normal root" on top of a (UNC) root
              rootEnd = offset = j + 1;
            }
          }
        }
      }
    } else if (isWindowsDeviceRoot(code)) {
      // Possible device root
      if (path.charCodeAt(1) === CHAR_COLON) {
        rootEnd = offset = 2;
        if (len > 2) {
          if (isPathSeparator(path.charCodeAt(2))) rootEnd = offset = 3;
        }
      }
    }
  } else if (isPathSeparator(code)) {
    // `path` contains just a path separator, exit early to avoid
    // unnecessary work
    return path;
  }
  for (let i = len - 1; i >= offset; --i) {
    if (isPathSeparator(path.charCodeAt(i))) {
      if (!matchedSlash) {
        end = i;
        break;
      }
    } else {
      // We saw the first non-path separator
      matchedSlash = false;
    }
  }
  if (end === -1) {
    if (rootEnd === -1) return ".";
    else end = rootEnd;
  }
  return path.slice(0, end);
}
/**
 * Return the directory name of a `path`.
 * @param path to determine name for
 */
Return the directory name of a `path`.
@param path to determine name for
Return the directory name of a ` path ` . @param path to determine name for
function dirname(path) {
  assertPath(path);
  const len = path.length;
  if (len === 0) return ".";
  let rootEnd = -1;
  let end = -1;
  let matchedSlash = true;
  let offset = 0;
  const code = path.charCodeAt(0);
  if (len > 1) {
    if (isPathSeparator(code)) {
      rootEnd = offset = 1;
      if (isPathSeparator(path.charCodeAt(1))) {
        let j = 2;
        let last = j;
        for (; j < len; ++j) {
          if (isPathSeparator(path.charCodeAt(j))) break;
        }
        if (j < len && j !== last) {
          last = j;
          for (; j < len; ++j) {
            if (!isPathSeparator(path.charCodeAt(j))) break;
          }
          if (j < len && j !== last) {
            last = j;
            for (; j < len; ++j) {
              if (isPathSeparator(path.charCodeAt(j))) break;
            }
            if (j === len) {
              return path;
            }
            if (j !== last) {
              rootEnd = offset = j + 1;
            }
          }
        }
      }
    } else if (isWindowsDeviceRoot(code)) {
      if (path.charCodeAt(1) === CHAR_COLON) {
        rootEnd = offset = 2;
        if (len > 2) {
          if (isPathSeparator(path.charCodeAt(2))) rootEnd = offset = 3;
        }
      }
    }
  } else if (isPathSeparator(code)) {
    return path;
  }
  for (let i = len - 1; i >= offset; --i) {
    if (isPathSeparator(path.charCodeAt(i))) {
      if (!matchedSlash) {
        end = i;
        break;
      }
    } else {
      matchedSlash = false;
    }
  }
  if (end === -1) {
    if (rootEnd === -1) return ".";
    else end = rootEnd;
  }
  return path.slice(0, end);
}
function dirname ( path ) { assertPath ( path ) ; const len = path . length ; if ( len === 0 ) return "." ; let rootEnd = - 1 ; let end = - 1 ; let matchedSlash = true ; let offset = 0 ; const code = path . charCodeAt ( 0 ) ; if ( len > 1 ) { if ( isPathSeparator ( code ) ) { rootEnd = offset = 1 ; if ( isPathSeparator ( path . charCodeAt ( 1 ) ) ) { let j = 2 ; let last = j ; for ( ; j < len ; ++ j ) { if ( isPathSeparator ( path . charCodeAt ( j ) ) ) break ; } if ( j < len && j !== last ) { last = j ; for ( ; j < len ; ++ j ) { if ( ! isPathSeparator ( path . charCodeAt ( j ) ) ) break ; } if ( j < len && j !== last ) { last = j ; for ( ; j < len ; ++ j ) { if ( isPathSeparator ( path . charCodeAt ( j ) ) ) break ; } if ( j === len ) { return path ; } if ( j !== last ) { rootEnd = offset = j + 1 ; } } } } } else if ( isWindowsDeviceRoot ( code ) ) { if ( path . charCodeAt ( 1 ) === CHAR_COLON ) { rootEnd = offset = 2 ; if ( len > 2 ) { if ( isPathSeparator ( path . charCodeAt ( 2 ) ) ) rootEnd = offset = 3 ; } } } } else if ( isPathSeparator ( code ) ) { return path ; } for ( let i = len - 1 ; i >= offset ; -- i ) { if ( isPathSeparator ( path . charCodeAt ( i ) ) ) { if ( ! matchedSlash ) { end = i ; break ; } } else { matchedSlash = false ; } } if ( end === - 1 ) { if ( rootEnd === - 1 ) return "." ; else end = rootEnd ; } return path . slice ( 0 , end ) ; }
Return the directory name of a `path`.
Return the directory name of a ` path ` .
// Try to match a root // Possible UNC root // Matched double path separator at beginning // Match 1 or more non-path separators // Matched! // Match 1 or more path separators // Matched! // Match 1 or more non-path separators // We matched a UNC root only // We matched a UNC root with leftovers // Offset by 1 to include the separator after the UNC root to // treat it as a "normal root" on top of a (UNC) root // Possible device root // `path` contains just a path separator, exit early to avoid // unnecessary work // We saw the first non-path separator