Create a new RelativePath from a string or another RelativePath.
The path is normalized and guaranteed to be relative. Any trailing separator is removed.
Throws an error if the provided path is absolute
Returns the extension of the filename including the leading dot, as a string. If the filename has no extension, returns an empty string.
Note that if the filename starts with a dot (e.g. .gitignore
),
that dot is considered part of the stem. So .gitignore
has
extension ''
and .gitignore.bak
has extension .bak
Returns the stem of the filename, i.e. the part before the extension. If the filename has no extension, returns the entire filename
Note that if the filename starts with a dot (e.g. .gitignore
),
that dot is considered part of the stem. So .gitignore
and
.gitignore.bak
both have stem .gitignore
Returns true if this path equals the other path or string
Join additional path segments to this path.
Accepts strings or path objects; null
and undefined
are ignored.
The resulting path is normalized.
A new path instance with the segments appended
const a1 = new AbsolutePath('/project/demo')
const a2 = a1.join('demo1/src', 'index.js') // '/project/demo/demo1/src/index.js'
a2 instanceof AbsolutePath // true
const r1 = new RelativePath('demo')
const r2 = r1.join('demo1/src', 'index.js') // 'demo/demo1/src/index.js'
r2 instanceof RelativePath // true
Protected
newProtected factory to construct a new instance of the current class, with the given path.
Used by all mutation-like methods to return a new instance of the same class, allowing derived classes that inherit those methods to return new instances of themselves without needing to override them.
The default implementation assumes the derived class's constructor takes a single string argument (the path). Derived classes with different constructor siguatures should override newSelf.
Replace the filename extension, keeping the stem the same. The passed can include or omit the leading dot; if omitted, it will be added.
New extension, e.g. json
or .json
A new path with the extension replaced.
Replace the filename (last segment).
A new path with the filename replaced.
Replace the parent directory while keeping the current filename.
Parent directory as string or another PathBase
.
A new path rooted at newParent
with the same filename.
Replace the filename stem, keeping the extension the same
New stem to use (extension is preserved).
A new path with the stem replaced.
Returns the path as string.
Static
isChecks whether a string is a relative path. (I.e., if it would be acceptable to the RelativePath constructor.)
The string to check.
True if the string is an absolute path, otherwise false.
Represents an relative filesystem path (i.e. a path that doesn't start at the root, i.e. doesn't have a leading separator) and is not, and provides methods for path manipulation and queries.
RelativePath instances are normalized and immutable.
RelativePath is similar to AbsolutePath but lacks methods that are only valid for absolute paths.
Example