b9j.path UNIX-style path parsing, manipulation, and generation

NAME

b9j.path - UNIX-style path parsing, manipulation, and generation

SYNOPSIS

 var path = new b9j.path.Path("/a/b/c")

 // /a/b/c/xyzzy
 var child = path.child("xyzzy")

 // /a/b
 var parent = path.parent()

DESCRIPTION

This package provides a way to parse, manipulate, and generate UNIX-style paths.

METHODS

new b9j.path.Path( )

Returns a new object representing the empty path

new b9j.path.Path( $part1, [ $part2, ... ] )

Returns a new object representing a path formed by joining $partN together with '/'

     var path = b9j.path.Path( "a/b", "c", "d//e/f/" ) // a/b/c/d/e/f/

path.clone()

Returns a clone of path

path.set( $path )

path.set( $part1, $part2, ... )

Set the path to a cleaned-up version of the arguments

     path.set( "////a", "b/c" ) // /a/b/c

path.toString()

path.get()

Return a string representing path

path.pop( [ $count ] )

Pop $count parts (a part is what is between slashes) off the end of path

If $count is not specified, then it defaults to 1

Returns a b9j.path.Path object representing the popped parts

     var path = new b9j.path.Path( "a/b/c/d/e/f/g" )
     var popped_path = path.pop(3)
     // path is a/b/c/d
     // popped_path is e/f/g

path.up( [ $count ] )

Behaves similarly to path.pop, in that it takes off $count parts from the end of the path. However, path.up returns path, so you can use it for chaining:

     var path = new b9j.path.Path( "a/b/c/d/e/f/g" )
     path.up().up(2).toString() // a/b/c/d

path.parent()

Returns the parent path of path as a new, separate b9j.path.Path object

     var path = new b9j.path.Path( "a/b/c" )
     var parent_path = path.parent()
     // path is STILL a/b/c
     // parent_path is a/b

path.push( $part1, [ $part2 ], ... )

path.down( $part1, [ $part2 ], ... )

Modifies path by appending $part1, $part2, to path separated by slashes

Returns path so you can use it for chaining:

     var path = new b9j.path.Path( "a/b/c" )
     path.down( "d//e", "f" ).down( "g" ) // "a/b/c/d/e/f/g

path.child( $part1, [ $part2 ], ... )

Returns a child path of path as a new, separate b9j.path.Path object with $partN appended (separated by slashes)

     var path = new b9j.path.Path( "a/b/c" )
     var child_path = path.child( "d/e" )
     // path is STILL a/b/c
     // child_path is a/b/c/d/e

path.append( $part1, [ $part2 ], ... )

Modify path by appending $part1 WITHOUT separating it by a slash. Any, optional, following $part2, ..., will be separated by slashes as normal

     var path = new b9j.path.Path( "a/b/c" )
     path.append( "d", "ef/g", "h" ) // "a/b/cd/ef/g/h"

path.extension()

Returns the extension of path, including the leading the dot

Returns "" if path does not have an extension

     new b9j.path.Path( "a/b/c.html" ).extension() // .html
     new b9j.path.Path( "a/b/c" ).extension() // ""
     new b9j.path.Path( "a/b/c.tar.gz" ).extension() // .gz
     new b9j.path.Path( "a/b/c.tar.gz" ).extension({ match: "*" }) // .tar.gz

path.extension( $extension )

Modify path by changing the existing extension of path, if any, to $extension

     new b9j.path.Path( "a/b/c.html" ).extension( ".txt" ) // a/b/c.txt
     new b9j.path.Path( "a/b/c.html" ).extension( "zip" ) // a/b/c.zip
     new b9j.path.Path( "a/b/c.html" ).extension( "" ) // a/b/c

Returns path

path.isEmpty()

Returns true if path is the empty path ("")

     new b9j.path.Path().isEmpty()     // true
     new b9j.path.Path("").isEmpty()   // true
     new b9j.path.Path("/").isEmpty()  // false
     new b9j.path.Path("a").isEmpty()  // false

path.isRoot()

Returns true if path is the root path ("/")

     new b9j.path.Path("").isRoot()   // false
     new b9j.path.Path("/").isRoot()  // true
     new b9j.path.Path("a").isRoot()  // false

path.isTree()

Returns true if path begins with a slash

     new b9j.path.Path("").isTree()    // false
     new b9j.path.Path("/").isTree()   // true
     new b9j.path.Path("a").isTree()   // false
     new b9j.path.Path("/a").isTree()  // true

path.isBranch()

Returns true if path does NOT begin with a slash

     new b9j.path.Path("").isBranch()    // true
     new b9j.path.Path("/").isBranch()   // false
     new b9j.path.Path("a").isBranch()   // true
     new b9j.path.Path("/a").isBranch()  // false

path.toTree()

Modifies path by prepending a slash

     new b9j.path.Path("").toTree()    // /
     new b9j.path.Path("/").toTree()   // /
     new b9j.path.Path("a").toTree()   // /a
     new b9j.path.Path("/a").toTree()  // /a

path.toBranch()

Modifies path by removing the leading slash, if any

     new b9j.path.Path("").toBranch()    // ""
     new b9j.path.Path("/").toBranch()   // ""
     new b9j.path.Path("a").toBranch()   // a
     new b9j.path.Path("/a").toBranch()  // a

path.first()

Returns the first part of path, not including any slashes

     new b9j.path.Path("/a/b/c/").first()  // a

path.last()

Returns the last part of path, not including any slashes

     new b9j.path.Path("/a/b/c/").last()  // c

path.at( $index )

Returns the part of path at $index, not including any slashes You can use a negative $index to start from the end of path

     new b9j.path.Path("/a/b/c/").at(0)  // a (equivalent to path.first())
     new b9j.path.Path("/a/b/c/").at(-1) // c (equivalent to path.last())
     new b9j.path.Path("/a/b/c/").at(1)  // b

path.beginning()

Returns the first part of path, including the leading slash, if any

     new b9j.path.Path("/a/b/c/").beginning() // /a
     new b9j.path.Path("a/b/c/").beginning()  // a

path.ending()

Returns the last part of path, including the trailing slash, if any

     new b9j.path.Path("/a/b/c/").beginning() // c/
     new b9j.path.Path("a/b/c/").beginning()  // c

path.list()

Returns an array of the parts of path

     new b9j.path.Path().list()          // []
     new b9j.path.Path("/").list()       // []
     new b9j.path.Path("/a/b/c/").list() // [ "a", "b", "c" ]
     new b9j.path.Path("a/b/c/").list()  // [ "a", "b", "c" ]

SEE ALSO

b9j - A JavaScript toolkit

AUTHOR

Robert Krimen <robertkrimen at gmail.com> http://bravo9.com

DOWNLOAD

Available as part of b9j: b9j-latest.zip

SOURCE

You can contribute or fork this project via GitHub:

http://github.com/robertkrimen/b9j/tree/master

 git clone git://github.com/robertkrimen/b9j.git

COPYRIGHT & LICENSE

Copyright 2008 Robert Krimen

Code licensed under the BSD License: http://appengine.bravo9.com/b9j/documentation/license.txt