dir-tools
An extended version of Node's path class
Overview
This module provides extends the path object with some additional functionality, to provide easier parsing of file-system objects.
Installation
Use npm to install the module:
npm install pixl-xml
Usage
Use require()
to load it in your code:
path = ; // Returns an FsObject
Constructor
The object can be intialized with a string or an InitOptions object.
InitOptions
Regardless of how its intialized, an InitOptions object is built internally which defines the options for the FsObject.
Property | Type | Description |
---|---|---|
path | string | The path string. |
usePosix | boolean | Whether to use Posix functions and path separators. Default value is true. |
Initialization with a string
A new object can be intialized with just a string representing a path.
var myDirPath = "c:\\"; // An FsObject for the directoryconsole; /* { "root": "c:/", "dir": "c:/", "base": "", "ext": "", "name": "", "usePosix": true, "fullPath": "c:/", "isSet": true, "exists": true, "isDirectory": true, "isFile": false } */ var myFilePath = "c:/sampleFile.txt"; // An FsObject for the fileconsole; /* { "root": "c:/", "dir": "c:/", "base": "sampleFile.txt", "ext": ".txt", "name": "sampleFile", "usePosix": true, "fullPath": "c:/sampleFile.txt", "isSet": true, "exists": false, "isDirectory": false, "isFile": false } */
Initialization with an object
By default, the FsObject will transform all backslashes into forward slashes (UNIX-style). To change this default, use an InitOptions object for initialization.
var myWin32Path = path: "c:/" usePosix: false; // myWin32Path.fullPath returns 'D:\' var myUNIXPath = "c:\\"; // myUNIXPath.fullPath returns 'D:/'
Properties
The result of Node's path.parse function is the base object, and those properties are included.
console; // C:/
Property | Type | Description |
---|---|---|
usePosix | boolean | If true, then backslashes will be used, and posix functions will be used. If false, then forward slashes will be used, and win32 functions will be used. Note that incoming slashes will be converted as needed. |
isSet | boolean | Returns true if the object is set |
fullPath | string | Returns the full path if the object is set, or an empty string if it is not set. This works regardless of whether the object exists on disk or not. |
exists | boolean | Returns true if the object is set, exists on the disk, and we can access it |
isDirectory | boolean | Returns true if the object is set, exists on the disk, and is a directory |
isFile | boolean | Returns true if the object is set, exists on the disk, and is a file |
getChildren | FsObject[] | If the FsObject is a directory, returns an array of FsObjects for each child. If there are no children, an empty array is returned. |
getParent | FsObject | Returns an FsObject representing the parent object. |
Determine whether the path is a file or directory
console; // trueconsole; // falseconsole // falseconsole // true
Get the path's child objects
var dirChildren = myDirPath; // Array of FsObjectsvar fileChildren = myFilePath; // null object
Additional functions
var scriptPath = ; // Is auto-set to the script's pathconsole; // Same as path.format(scriptPath)