b9j.path - UNIX-style path parsing, manipulation, and generation
var path = new b9j.path.Path("/a/b/c")
// /a/b/c/xyzzy
var child = path.child("xyzzy")
// /a/b
var parent = path.parent()
This package provides a way to parse, manipulate, and generate UNIX-style paths.
Returns a new object representing the empty path
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/
Returns a clone of path
Set the path to a cleaned-up version of the arguments
path.set( "////a", "b/c" ) // /a/b/c
Return a string representing path
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
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
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
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
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
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"
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
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
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
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
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
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
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
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
Returns the first part of path, not including any slashes
new b9j.path.Path("/a/b/c/").first() // a
Returns the last part of path, not including any slashes
new b9j.path.Path("/a/b/c/").last() // c
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
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
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
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" ]
b9j - A JavaScript toolkit
Robert Krimen <robertkrimen at gmail.com> http://bravo9.com
Available as part of b9j: b9j-latest.zip
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 2008 Robert Krimen
Code licensed under the BSD License: http://appengine.bravo9.com/b9j/documentation/license.txt