fspath (Path)
Immutable Path object replaces using strings for paths. It provides some functionality from both path
and fs
core modules.
Path avoids doing any internal work, except storing the string and its parts split by the path separator, until a function is called requesting the information.
Some of the helpful capabilities:
- all the
path
module's functions isAbsolute
andisRelative
- fs.stats related information
- fs.{write|append|read}File[Sync]
- create read/write streams
- piping between paths
- listing directory contents
- path startsWith, endsWith, equals
- subpath
- and much more
Many of Path's functions will operate asynchronously when a callback is provided; otherwise they operate synchronously.
Note: Although this document is incomplete the library is fully functional and there are many tests.
Install
npm install fspath --save
Examples
// get the buildervar buildPath = // create a path to the current working directoryvar dir = // create a path to specified relative pathdir = // create a new path at parent pathvar parentDir = dirparent// OR: parentDir = dir.to('..') // create a path beneath this one.// could be a file or a directoryvar child = dir // create a path relative to itvar sibling = child var result = dir// result.paths is an array with all the paths.// OR:dir // a new path we know is a filevar file = dir // write to itfile // or append to itfile // OR:file content = file// content = 'some data\nmore data' // create two pathsvar source = dirvar target = dir // pipe the first into the second.// this requires `source` to be a real file,// and requires `target` *not* be a directory.source// the above calls reader() on source,// and writer() on target, then pipes them. // or, provide an options object.// it accepts options for both reader/writer,// and for adding events to both streams.source // for example, listen for the writer's finish event:source
Immutable Path
The Path object is immutable. Functions which require a different internal state return a new Path object.
This allows passing a Path object around without worrying some operation is changing it.
It also allows that object to be the focus of managing streams it creates to the file for only one file path.
API
Accessible Properties
path
: the path as a stringparts
: path string split on delim into an array of stringsisRelative
: true when the path doesn't start with a slashisAbsolute
: true when the path starts with a slash
stats([callback])
// sync (no callback) call returns the stats object provided by node's fs modulestats = path // async call provides the stats or an error objectpath
isReal([callback])
// sync (no callback) call returns true if the file/directory existsisReal = path // async call provides true/false or an error objectpath
isFile([callback])
// sync (no callback) call returns true if it exists and is a fileisFile = path // async call provides true/false or an error objectpath
isDir([callback])
// sync (no callback) call returns true if it exists and is a directoryisDir = path // async call provides true/false or an error objectpath
isCanonical([callback])
// sync (no callback) call returns true if the path is normalized.var isCanonical = path // async call provides true/false or an error objectpath