tspath

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ExtensionTs          = ".ts"
	ExtensionTsx         = ".tsx"
	ExtensionDts         = ".d.ts"
	ExtensionJs          = ".js"
	ExtensionJsx         = ".jsx"
	ExtensionJson        = ".json"
	ExtensionTsBuildInfo = ".tsbuildinfo"
	ExtensionMjs         = ".mjs"
	ExtensionMts         = ".mts"
	ExtensionDmts        = ".d.mts"
	ExtensionCjs         = ".cjs"
	ExtensionCts         = ".cts"
	ExtensionDcts        = ".d.cts"
)
View Source
const (
	DirectorySeparator = '/'
)

Internally, we represent paths as strings with '/' as the directory separator. When we make system calls (eg: LanguageServiceHost.getDirectory()), we expect the host to correctly handle paths in our specified format.

Variables

Functions

func ChangeExtension

func ChangeExtension(path string, newExtension string) string

func CombinePaths

func CombinePaths(firstPath string, paths ...string) string

Combines paths. If a path is absolute, it replaces any previous path. Relative paths are not simplified.

```
// Non-rooted
CombinePaths("path", "to", "file.ext") === "path/to/file.ext"
CombinePaths("path", "dir", "..", "to", "file.ext") === "path/dir/../to/file.ext"
// POSIX
CombinePaths("/path", "to", "file.ext") === "/path/to/file.ext"
CombinePaths("/path", "/to", "file.ext") === "/to/file.ext"
// DOS
CombinePaths("c:/path", "to", "file.ext") === "c:/path/to/file.ext"
CombinePaths("c:/path", "c:/to", "file.ext") === "c:/to/file.ext"
// URL
CombinePaths("file:///path", "to", "file.ext") === "file:///path/to/file.ext"
CombinePaths("file:///path", "file:///to", "file.ext") === "file:///to/file.ext"
```

func ComparePaths

func ComparePaths(a string, b string, options ComparePathsOptions) int

func ComparePathsCaseInsensitive

func ComparePathsCaseInsensitive(a string, b string, currentDirectory string) int

func ComparePathsCaseSensitive

func ComparePathsCaseSensitive(a string, b string, currentDirectory string) int

func ContainsPath

func ContainsPath(parent string, child string, options ComparePathsOptions) bool

func ConvertToRelativePath

func ConvertToRelativePath(absoluteOrRelativePath string, options ComparePathsOptions) string

func EnsureTrailingDirectorySeparator

func EnsureTrailingDirectorySeparator(path string) string

func ExtensionIsTs

func ExtensionIsTs(ext string) bool

func FileExtensionIs

func FileExtensionIs(path string, extension string) bool

func FileExtensionIsOneOf

func FileExtensionIsOneOf(path string, extensions []string) bool

func ForEachAncestorDirectory

func ForEachAncestorDirectory[T any](directory string, callback func(directory string) (result T, stop bool)) (result T, ok bool)

func ForEachAncestorDirectoryPath

func ForEachAncestorDirectoryPath[T any](directory Path, callback func(directory Path) (result T, stop bool)) (result T, ok bool)

func GetAnyExtensionFromPath

func GetAnyExtensionFromPath(path string, extensions []string, ignoreCase bool) string

Gets the file extension for a path. If extensions are provided, gets the file extension for a path, provided it is one of the provided extensions.

GetAnyExtensionFromPath("/path/to/file.ext", nil, false) == ".ext"
GetAnyExtensionFromPath("/path/to/file.ext/", nil, false) == ".ext"
GetAnyExtensionFromPath("/path/to/file", nil, false) == ""
GetAnyExtensionFromPath("/path/to.ext/file", nil, false) == ""
GetAnyExtensionFromPath("/path/to/file.ext", ".ext", true) === ".ext"
GetAnyExtensionFromPath("/path/to/file.js", ".ext", true) === ""
GetAnyExtensionFromPath("/path/to/file.js", [".ext", ".js"], true) === ".js"
GetAnyExtensionFromPath("/path/to/file.ext", ".EXT", false) === ""

func GetBaseFileName

func GetBaseFileName(path string) string

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"
GetBaseFileName("/path/to/") == "to"
GetBaseFileName("/") == ""
// DOS
GetBaseFileName("c:/path/to/file.ext") == "file.ext"
GetBaseFileName("c:/path/to/") == "to"
GetBaseFileName("c:/") == ""
GetBaseFileName("c:") == ""
// URL
GetBaseFileName("http://typescriptlang.org/path/to/file.ext") == "file.ext"
GetBaseFileName("http://typescriptlang.org/path/to/") == "to"
GetBaseFileName("http://typescriptlang.org/") == ""
GetBaseFileName("http://typescriptlang.org") == ""
GetBaseFileName("file://server/path/to/file.ext") == "file.ext"
GetBaseFileName("file://server/path/to/") == "to"
GetBaseFileName("file://server/") == ""
GetBaseFileName("file://server") == ""
GetBaseFileName("file:///path/to/file.ext") == "file.ext"
GetBaseFileName("file:///path/to/") == "to"
GetBaseFileName("file:///") == ""
GetBaseFileName("file://") == ""

func GetCanonicalFileName

func GetCanonicalFileName(fileName string, useCaseSensitiveFileNames bool) string

func GetDeclarationFileExtension

func GetDeclarationFileExtension(fileName string) string

func GetDirectoryPath

func GetDirectoryPath(path string) string

func GetEncodedRootLength

func GetEncodedRootLength(path string) int

func GetNormalizedAbsolutePath

func GetNormalizedAbsolutePath(fileName string, currentDirectory string) string

func GetNormalizedPathComponents

func GetNormalizedPathComponents(path string, currentDirectory string) []string

func GetPathComponents

func GetPathComponents(path string, currentDirectory string) []string

func GetPathComponentsRelativeTo

func GetPathComponentsRelativeTo(from string, to string, options ComparePathsOptions) []string

func GetPathFromPathComponents

func GetPathFromPathComponents(pathComponents []string) string

func GetRelativePathFromDirectory

func GetRelativePathFromDirectory(fromDirectory string, to string, options ComparePathsOptions) string

func GetRelativePathToDirectoryOrUrl

func GetRelativePathToDirectoryOrUrl(directoryPathOrUrl string, relativeOrAbsolutePath string, isAbsolutePathAnUrl bool, options ComparePathsOptions) string

func GetRootLength

func GetRootLength(path string) int

func HasExtension

func HasExtension(fileName string) bool

func HasImplementationTSFileExtension

func HasImplementationTSFileExtension(path string) bool

func HasTSFileExtension

func HasTSFileExtension(path string) bool

func HasTrailingDirectorySeparator

func HasTrailingDirectorySeparator(path string) bool

func IsDeclarationFileName

func IsDeclarationFileName(fileName string) bool

func IsDiskPathRoot

func IsDiskPathRoot(path string) bool

Determines whether a path consists only of a path root.

func IsExternalModuleNameRelative

func IsExternalModuleNameRelative(moduleName string) bool

func IsRootedDiskPath

func IsRootedDiskPath(path string) bool

Determines whether a path is an absolute disk path (e.g. starts with `/`, or a dos path like `c:`, `c:\` or `c:/`).

func IsUrl

func IsUrl(path string) bool

Determines whether a path starts with a URL scheme (e.g. starts with `http://`, `ftp://`, `file://`, etc.).

func NormalizePath

func NormalizePath(path string) string

func NormalizeSlashes

func NormalizeSlashes(path string) string

func PathIsAbsolute

func PathIsAbsolute(path string) bool

Determines whether a path starts with an absolute path component (i.e. `/`, `c:/`, `file://`, etc.).

```
// POSIX
PathIsAbsolute("/path/to/file.ext") === true
// DOS
PathIsAbsolute("c:/path/to/file.ext") === true
// URL
PathIsAbsolute("file:///path/to/file.ext") === true
// Non-absolute
PathIsAbsolute("path/to/file.ext") === false
PathIsAbsolute("./path/to/file.ext") === false
```

func PathIsRelative

func PathIsRelative(path string) bool

func RemoveExtension

func RemoveExtension(path string, extension string) string

func RemoveFileExtension

func RemoveFileExtension(path string) string

func RemoveTrailingDirectorySeparator

func RemoveTrailingDirectorySeparator(path string) string

func RemoveTrailingDirectorySeparators

func RemoveTrailingDirectorySeparators(path string) string

func ResolvePath

func ResolvePath(path string, paths ...string) string

Combines and resolves paths. If a path is absolute, it replaces any previous path. Any `.` and `..` path components are resolved. Trailing directory separators are preserved.

```go resolvePath("/path", "to", "file.ext") == "path/to/file.ext" resolvePath("/path", "to", "file.ext/") == "path/to/file.ext/" resolvePath("/path", "dir", "..", "to", "file.ext") == "path/to/file.ext" ```

func ToFileNameLowerCase

func ToFileNameLowerCase(fileName string) string

func TryExtractTSExtension

func TryExtractTSExtension(fileName string) string

func TryGetExtensionFromPath

func TryGetExtensionFromPath(p string) string

Types

type ComparePathsOptions

type ComparePathsOptions struct {
	UseCaseSensitiveFileNames bool
	CurrentDirectory          string
}

func (ComparePathsOptions) GetComparer

func (o ComparePathsOptions) GetComparer() func(a, b string) int

type Path

type Path string

func ToPath

func ToPath(fileName string, basePath string, useCaseSensitiveFileNames bool) Path

func (Path) EnsureTrailingDirectorySeparator

func (p Path) EnsureTrailingDirectorySeparator() Path

func (Path) GetDirectoryPath

func (p Path) GetDirectoryPath() Path

func (Path) RemoveTrailingDirectorySeparator

func (p Path) RemoveTrailingDirectorySeparator() Path

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL