0.0.4 • Published 9 years ago

dir-tools v0.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

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 = require ('dir-tools');                                   // 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.

PropertyTypeDescription
pathstringThe path string.
usePosixbooleanWhether 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 = new path("c:\\");                               // An FsObject for the directory
console.log(JSON.stringify(myDirPath));                         /*  {
                                                                       "root": "c:/",
                                                                       "dir": "c:/",
                                                                       "base": "",
                                                                       "ext": "",
                                                                       "name": "",
                                                                       "usePosix": true,
                                                                       "fullPath": "c:/",
                                                                       "isSet": true,
                                                                       "exists": true,
                                                                       "isDirectory": true,
                                                                       "isFile": false
                                                                   } */

var myFilePath = new path("c:/sampleFile.txt");                 // An FsObject for the file
console.log(JSON.stringify(myFilePath));                        /*  {
                                                                       "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 = new path({path: "c:/", usePosix: false});     // myWin32Path.fullPath returns 'D:\' 
var myUNIXPath = new path("c:\\");                              // myUNIXPath.fullPath returns 'D:/'

Properties

The result of Node's path.parse function is the base object, and those properties are included.

console.log(myDirPath.root);                                    // C:/
PropertyTypeDescription
usePosixbooleanIf 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.
isSetbooleanReturns true if the object is set
fullPathstringReturns 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.
existsbooleanReturns true if the object is set, exists on the disk, and we can access it
isDirectorybooleanReturns true if the object is set, exists on the disk, and is a directory
isFilebooleanReturns true if the object is set, exists on the disk, and is a file
getChildrenFsObject[]If the FsObject is a directory, returns an array of FsObjects for each child. If there are no children, an empty array is returned.
getParentFsObjectReturns an FsObject representing the parent object.

Determine whether the path is a file or directory

console.log(myDirPath.isDirectory);                             // true
console.log(myDirPath.isFile);                                  // false
console.log(myFilePath.isDirectory)                             // false
console.log(myFilePath.isFile)                                  // true

Get the path's child objects

var dirChildren = myDirPath.getChildren();                      // Array of FsObjects
var fileChildren = myFilePath.getChildren();                    // null object

Additional functions

var scriptPath = new path();                                    // Is auto-set to the script's path
console.log(scriptPath.fullPath);                               // Same as path.format(scriptPath)
0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago