path
File path API.
Summary
Static Properties
delimiter | The character that separates path segments. |
Static Methods
extractPath | Return the path without file name. |
getBasename | Return the basename of the path. |
getDirectory | Get the directory name from the path. |
getExtension | Return the extension of the path. |
isRelativePath | Check if a string s is relative path. |
join | Join two or more sections of file path together, inserting a delimiter if needed. |
normalize | Normalize the path by removing '. |
split | Split the pathname path into a pair [head, tail] where tail is the final part of the path after the last delimiter and head is everything leading up to that. |
Details
Static Properties
delimiter | The character that separates path segments. |
Static Methods
extractPath(pathname)
Return the path without file name. If path is relative path, start with period.
pc.path.extractPath("path/to/file.txt"); // returns "./path/to"
pc.path.extractPath("./path/to/file.txt"); // returns "./path/to"
pc.path.extractPath("../path/to/file.txt"); // returns "../path/to"
pc.path.extractPath("/path/to/file.txt"); // returns "/path/to"
Parameters
pathname | string | The full path to process. |
Returns
stringThe path without a last element from list split by slash.
getBasename(pathname)
Return the basename of the path. That is the second element of the pair returned by passing path into path.split.
pc.path.getBasename("/path/to/file.txt"); // returns "file.txt"
pc.path.getBasename("/path/to/dir"); // returns "dir"
Parameters
pathname | string | The path to process. |
Returns
stringThe basename.
getDirectory(pathname)
Get the directory name from the path. This is everything up to the final instance of path.delimiter.
Parameters
pathname | string | The path to get the directory from. |
Returns
stringThe directory part of the path.
getExtension(pathname)
Return the extension of the path. Pop the last value of a list after path is split by question mark and comma.
pc.path.getExtension("/path/to/file.txt"); // returns ".txt"
pc.path.getExtension("/path/to/file.jpg"); // returns ".jpg"
pc.path.getExtension("/path/to/file.txt?function=getExtension"); // returns ".txt"
Parameters
pathname | string | The path to process. |
Returns
stringThe extension.
isRelativePath(pathname)
Check if a string s is relative path.
pc.path.isRelativePath("file.txt"); // returns true
pc.path.isRelativePath("path/to/file.txt"); // returns true
pc.path.isRelativePath("./path/to/file.txt"); // returns true
pc.path.isRelativePath("../path/to/file.jpg"); // returns true
pc.path.isRelativePath("/path/to/file.jpg"); // returns false
pc.path.isRelativePath("http://path/to/file.jpg"); // returns false
Parameters
pathname | string | The path to process. |
Returns
booleanTrue if s doesn't start with slash and doesn't include colon and double slash.
join(section)
Join two or more sections of file path together, inserting a delimiter if needed.
const path = pc.path.join('foo', 'bar');
console.log(path); // Prints 'foo/bar'
const path = pc.path.join('alpha', 'beta', 'gamma');
console.log(path); // Prints 'alpha/beta/gamma'
Parameters
section | string | Section of path to join. 2 or more can be provided as parameters. |
Returns
stringThe joined file path.
normalize(pathname)
Normalize the path by removing '.' and '..' instances.
Parameters
pathname | string | The path to normalize. |
Returns
stringThe normalized path.
split(pathname)
Split the pathname path into a pair [head, tail] where tail is the final part of the path after the last delimiter and head is everything leading up to that. tail will never contain a slash.
Parameters
pathname | string | The path to split. |
Returns
string[]The split path which is an array of two strings, the path and the filename.