MCPcopy Create free account
hub / github.com/microsoft/typescript-go / GetBaseFileName

Function GetBaseFileName

internal/tspath/path.go:840–853  ·  view source on GitHub ↗

Gets the portion of a path following the last (non-terminal) separator (`/`). Semantics align with NodeJS's `path.basename` except that we support URL's as well. If the base name has any one of the provided extensions, it is removed. // POSIX GetBaseFileName("/path/to/file.ext") == "file.ext" Ge

(path string)

Source from the content-addressed store, hash-verified

838// GetBaseFileName("file:///") == ""
839// GetBaseFileName("file://") == ""
840func GetBaseFileName(path string) string {
841 path = NormalizeSlashes(path)
842
843 // if the path provided is itself the root, then it has no file name.
844 rootLength := GetRootLength(path)
845 if rootLength == len(path) {
846 return ""
847 }
848
849 // return the trailing portion of the path starting after the last (non-terminal) directory
850 // separator but not including any trailing directory separator.
851 path = RemoveTrailingDirectorySeparator(path)
852 return path[max(GetRootLength(path), strings.LastIndex(path, string(DirectorySeparator))+1):]
853}
854
855// Gets the file extension for a path.
856// If extensions are provided, gets the file extension for a path, provided it is one of the provided extensions.

Calls 6

NormalizeSlashesFunction · 0.85
GetRootLengthFunction · 0.85
lenFunction · 0.85
maxFunction · 0.85
stringEnum · 0.50

Tested by 2

runCompilerTestsFunction · 0.74
TestEmbeddedLibsFunction · 0.74