nupp.io.path
nupp.io.path returns an immutable path value whose joining, normalizing and splitting follow the platform's own rules. Reach for it when a program builds a filesystem name or takes one apart, rather than when it reads the bytes behind one.
const path = nupp.io.path
local source = path.newPath("src", "app", "..", "main.nupp"):normalize()
assert(source:toString() == "src" .. path.separator() .. "main.nupp")
assert(source:fileName() == "main.nupp")Every operation returns a new path, and two paths are equal when their native text is. tostring(path) and path:toString() return that same text.
Building a path#
path.newPath(first, parts...) joins its components using the current platform's rules. join appends more to a path already built, and both accept a string or another path. Path has no public constructor; creation goes through newPath, which interns recent paths in a bounded LRU cache.
const path = nupp.io.path
local native = path.newPath("out"):join("lib", "native")
assert(native:fileName() == "native")currentDirectory and separator are functions on the module beside the record, because neither builds a path out of components:
const path = nupp.io.path
local current, reason = path.currentDirectory()
assert(current, reason)
local log = current:join("var", "app.log"):withExtension("jsonl")
assert(log:extension() == "jsonl")Normalizing and resolving#
normalize removes lexical . and .. components without touching the filesystem, so it returns even for a path that does not exist:
const path = nupp.io.path
assert(path.newPath("src", "app", "..", "main.nupp"):normalize():stem() == "main")The operations that consult the process or filesystem can fail, as can a relative path whose base uses a different coordinate system. Each returns nil with a reason when it does.
absolute()resolves the path against the process's working directory. A drive-relative Windows path fails when that directory uses another drive.resolve(parts...)makes it absolute, appends the parts, and normalizes.canonicalize()asks the filesystem for the real path, following every symbolic link, which requires the path to exist.relativeTo(base)expresses the path against another one, when both share a coordinate system. A path expressed against itself returns., the path that stays where the base already is.
const path = nupp.io.path
local real, reason = path.newPath("."):canonicalize()
assert(real, reason)
assert(real:isAbsolute())Path components#
parent returns another path or nil. fileName, stem and extension return the final component, that component without its extension, and the extension without its dot:
const path = nupp.io.path
local source = path.newPath("src", "main.nupp")
assert(source:fileName() == "main.nupp")
assert(source:stem() == "main")
assert(source:extension() == "nupp")
assert(assert(source:parent()):toString() == "src")withFileName and withExtension replace one component. Each takes one path component and raises when handed text that is not one, so a separator smuggled into a file name is reported where it was written rather than carried into the path it would have produced:
const path = nupp.io.path
local report = path.newPath("out", "report.tmp"):withExtension("json")
assert(report:fileName() == "report.json")isAbsolute and isRelative classify a path without accessing the filesystem.
Submodules
| Module | Description |
|---|---|
nupp.io.path.spi | Implementation interfaces consumed during ordinary module initialization. |
Module contents
Types
| Type | Kind | Description |
|---|---|---|
Path | record | A filesystem path. |
Functions
| Function | Kind | Description |
|---|---|---|
currentDirectory | function | Reads the process's current working directory. |
newPath | function | Builds a path by joining one or more components using the platform's rules. |
separator | function | Returns the current platform's primary path separator. |
Types#
Pathrecord#
record Path ...A filesystem path.
Members
| Name | Kind | Description |
|---|---|---|
toString | method | Returns the native UTF-8 path text. |
join | method | Appends path components using the current platform's rules. |
normalize | method | Removes lexical . |
absolute | method | Resolves this path against the process's current working directory. |
resolve | method | Makes this path absolute, appends components, and normalizes the result. |
canonicalize | method | Resolves every symbolic link and .., which requires the path to exist. |
relativeTo | method | Expresses this path relative to another. |
parent | method | The path without its final component. |
fileName | method | The final component, extension included. |
stem | method | The final component without its extension. |
extension | method | The final component's extension, without the dot. |
withFileName | method | The path with its final component replaced. |
withExtension | method | The path with its extension replaced. |
isAbsolute | method | Whether the path is anchored at a root. |
isRelative | method | Whether the path is interpreted against a working directory. |
__tostring | ||
__eq |
toStringmethod#
toString: function toString(self): stringReturns the native UTF-8 path text.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
string | the path text |
joinmethod#
Appends path components using the current platform's rules.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
... | string | Path | the components to append |
Returns
| Type | Description |
|---|---|
Path | the joined path |
normalizemethod#
normalize: function normalize(self): PathRemoves lexical . and .. components without accessing the filesystem.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
Path | the normalized path |
absolutemethod#
absolute: function absolute(self): Path?, string?Resolves this path against the process's current working directory.
A drive-relative Windows path can be resolved only when the current directory is on that drive.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
Path? | the absolute path, or nil on failure |
string? | a failure reason, when unsuccessful |
resolvemethod#
Makes this path absolute, appends components, and normalizes the result.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
... | string | Path | the components to append |
Returns
| Type | Description |
|---|---|
Path? | the resolved path, or nil on failure |
string? | a failure reason, when unsuccessful |
canonicalizemethod#
canonicalize: function canonicalize(self): Path?, string?Resolves every symbolic link and .., which requires the path to exist.
This reads the filesystem, where normalize works on the text alone, so it fails on a path that is not there.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
Path? | the canonical path, or nil on failure |
string? | a failure reason, when unsuccessful |
relativeTomethod#
Expresses this path relative to another. A path expressed against itself returns ., the path that stays where the base already is.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
base | string | Path | the path to express this one against |
Returns
| Type | Description |
|---|---|
Path? | the relative path, or nil when there is no relative form |
string? | a failure reason, when unsuccessful |
parentmethod#
parent: function parent(self): Path?The path without its final component.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
Path? | the parent, or nil when there is none |
fileNamemethod#
fileName: function fileName(self): string?The final component, extension included.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
string? | the file name, or nil when the path ends in a directory component |
stemmethod#
stem: function stem(self): string?The final component without its extension.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
string? | the stem, or nil when there is no file name |
extensionmethod#
extension: function extension(self): string?The final component's extension, without the dot.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
string? | the extension, or nil when there is none |
withFileNamemethod#
The path with its final component replaced.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
name | string | the file name to use |
Returns
| Type | Description |
|---|---|
Path | the new path |
Raises
| Type | Condition |
|---|---|
string | when name is not one component: empty, |
withExtensionmethod#
withExtension: function withExtension(self, extension: string): PathThe path with its extension replaced.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
extension | string | the extension to use, without the dot |
Returns
| Type | Description |
|---|---|
Path | the new path |
Raises
| Type | Condition |
|---|---|
string | when extension holds a separator or NUL byte (or, on Windows, a colon) |
isAbsolutemethod#
isAbsolute: function isAbsolute(self): booleanWhether the path is anchored at a root.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
boolean | whether the path is absolute |
isRelativemethod#
isRelative: function isRelative(self): booleanWhether the path is interpreted against a working directory.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | this path |
Returns
| Type | Description |
|---|---|
boolean | whether the path is relative |
__tostring#
__tostring: function(self): stringArguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
string |
__eq#
__eq: function(self, any): booleanArguments
| Name | Type | Description |
|---|---|---|
? | self | |
? | any |
Returns
| Type | Description |
|---|---|
boolean |
Functions#
currentDirectoryfunction#
function currentDirectory(): Path?, string?Reads the process's current working directory.
Returns
| Type | Description |
|---|---|
Path? | the current directory, or nil on failure |
string? | a failure reason, when unsuccessful |
newPathfunction#
Builds a path by joining one or more components using the platform's rules.
Repeatedly building the same path reuses its immutable value from a bounded LRU.
Examples#
Arguments
| Name | Type | Description |
|---|---|---|
first | string | Path | the first path component |
... | string | Path | the remaining path components |
Returns
| Type | Description |
|---|---|
Path | the joined path |
separatorfunction#
function separator(): stringReturns the current platform's primary path separator.
Returns
| Type | Description |
|---|---|
string | the path separator |