This package has been deprecated

Author message:

This package is no longer maintained or even a good thing to use

dir-tools

0.0.4 • Public • Published

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.

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 = 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:/
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.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)

Readme

Keywords

Package Sidebar

Install

npm i dir-tools

Weekly Downloads

1

Version

0.0.4

License

MIT

Last publish

Collaborators

  • jobrad