Resource Path
ResourcePath represents asset identifiers in a consistent structure and exposes helpers for parsing, manipulating, and generating resource paths for files, URLs, and relative locations.
Properties
ResourcePath provides a set of properties to access the path parts. It doesn't allow modifying the path parts directly via these properties. Instead, ResourcePath provides a set of methods to rebuild or create a new instance with modified parts.
schema– Returns the schema such ashttp,https, orfilewhen present.hostname– Returns the hostname that follows the schema.volume– Returns the volume segment likeC:for local file systems.params– Returns the query parameters as a mutableURLSearchParamsinstance ornull.relative– Indicates that the current path is relative.absolute– Indicates that the current path is absolute.fullPath– Returns all path parts and appends serialized query parameters when available.schemaPath– Returns schema and hostname with trailing separators for absolute URLs.volumePath– Returns the volume segment including the trailing separator.relativePath– Returns the normalized parent path with the file or folder name.parentPath– Returns the root path including schema or volume and returns the path to the parent folder.path– Returns the full path without query parameters.pathname– Returns the normalized parent path without the final element.extension– Returns the lowercase file or folder extension without the dot.name– Returns the file or folder name stored in the path.
Creating and Parsing Example
// Create a ResourcePath from a URL
const urlPath = new ResourcePath('https://example.com/assets/images/sprite.png?v=1');
// Access components of the path
console.log(urlPath.schema); // https
console.log(urlPath.hostname); // example.com
console.log(urlPath.path); // https://example.com/assets/images/sprite.png
console.log(urlPath.params); // URLSearchParams { 'v' => '1' }
console.log(urlPath.name); // sprite.png
// Create a ResourcePath from a file path
const filePath = new ResourcePath('C:/games/assets/images/sprite.png');
// Access components of the path
console.log(filePath.volume); // C
console.log(filePath.path); // C:/games/assets/images/sprite.png
console.log(filePath.name); // sprite.png
rebuild
Rebuilds the current object using the provided content. This is useful when you want to reuse an existing instance while pointing it to another location.
rebuild(content:string, unknown:boolean = true):void
const resource = new ResourcePath('db://reels/config.json');
console.log(resource.fullPath);
// Output: 'db://reels/config.json'
resource.rebuild('db://reels/paytable.json');
console.log(resource.fullPath);
// Output: 'db://reels/paytable.json'
cloneWith
Creates a new ResourcePath with selected parts overridden. Missing values fall back to the current instance, and the result is normalized before being returned.
cloneWith(parts:ResourcePathParts):ResourcePath
const sourcePath = new ResourcePath('https://example.com/images/ui/button.png?v=2');
const resultPath = sourcePath.cloneWith({ params: new URLSearchParams('locale=en'), name: 'button_en.png' });
console.log(resultPath.fullPath);
// Output: https://example.com/images/ui/button_en.png?locale=en
clone
Creates a deep copy of the current instance including query parameters.
clone():ResourcePath
const sourcePath = new ResourcePath('assets/maps/bonus.json');
const resultPath = sourcePath.clone();
console.log(resultPath.fullPath);
// Output: assets/maps/bonus.json