Lee Carmichael - File.Basename-0.03
NAME
File.Basename - A library that provides unix-like tools 'dirname' and 'basename'
SYNOPSIS
<script type="text/javascript" src="myscripts/JSAN.js"></script>
JSAN.use('File.Basename');
// or
<script type="text/javascript" src="myscripts/File/Basename.js"></script>
// Class Example
var path = "/testing/path/file.ext";
var file = new File.Basename(path);
// basename = 'file.ext'
var basename = file.basename();
var dirname = file.dirname();
// tell user
document.writeln("basename is '" + basename + "'");
// dump platform
document.writeln("Parsed " + file.path() + " as platform " + file.platform_str());
DESCRIPTION
This library provides tools to take a filepath and pull the directory name and filename out of it. This is much like the perl module 'File::Basename' or the unix shell tools of 'basename' and 'dirname'.
This type of tool can be useful if you need to process full paths on the server in request but want to display only filenames to the user.
Constructor
var file = new File.Basename(path, platform);
Create a new File.Basename object.
- path
- platform optional
File path to process
A 'File.Basename' platform constant, this tells the object how to process the filepath it was given. If this is not passed the object will try to guess based upon the user agent string. If this is not available then it will default to UNIX.
For the platform constants see the section Constants below.
Class Properties
VERSION
File.Basename.VERSION
Current library version
Methods
basename()
file.basename();
Returns the basename of the filepath passed to the constructor. Returns empty string if no basename exists.
dirname()
file.dirname
Returns the directory name of the path.
Under Windows this can/will return the drive letter as well.
Constants
- File.Basename.WIN
- File.Basename.MAC
- File.Basename.UNIX
Indicates the desire for Windows pathname processing. (backslashes)
Indicates the desire for Mac (same as UNIX today) pathname processing. (forward slashes)
Indicates object is current processing pathnames for *NIX systems (forward slashes).
SEE ALSO
nothing at this time.
TODO
This will not work with several multibyte character sets like 'shift_jis'. It will work fine with UTF8.
Need to add a test for non-english UTF8 characters and such...
Need to add support to pull extension off
Need to add functional support. (basename(filepath) returns basename)
AUTHOR
Lee Carmichael <lecar_red@yahoo.com>
COPYRIGHT
Copyright (c) 2005 Lee Carmichael. All rights reserved.
This module is free software; you can redistribute it and/or modify it
under the terms of the Artistic license.
// File.Basename - Lee Carmichael <lecar_red[at]yahoo[dot]com
// Set up namepace(s)
if (typeof(File) == "undefined") File = {};
if (typeof(File.Basename) == "undefined") File.Basename = {};
// constructor
File.Basename = function (path, platform) {
// should we error here? or just return?
if (!path)
return;
// setup the path
this._path = path;
// need to check this value
// grab passed platform
if (platform)
this._platform = platform;
// figure out client type
if (typeof(this._platform) == "undefined") {
// need to check for navigator and platform
// just in case of not in browser env...
if (navigator.platform.indexOf("Win") >= 0) {
this._platform = File.Basename.WIN;
} else if (navigator.platform.indexOf("Mac") >= 0) {
this._platform = File.Basename.MAC;
} else {
this._platform = File.Basename.UNIX;
}
}
// set path pattern (current for Win we leave the <Drive Letter>)
if (this._platform == File.Basename.WIN)
this._pattern = /^(.*\\)?(.*)/; // this will not match drive letter...
else
this._pattern = /^(.*\/)?(.*)/;
// match string
var rc = this._path.match(this._pattern);
// should we check values here?
// * skip rc[0] since it returns our string *
// setup basename
this._basename = rc[2];
if (!this._basename) this._basename = "";
// setup dirname
// and remove trailing slash
this._dirname = _chop(rc[1]);
return;
}
// setup version
File.Basename.VERSION = "0.02";
// constants
File.Basename.WIN = 1;
File.Basename.MAC = 2; // does this really apply anymore?
File.Basename.UNIX = 3;
// platform string mapping
File.Basename.platformStrs = new Array("N/A", "Windows", "Macintosh", "UNIX");
// it would be nice to turn this into a class
function _chop(str) {
// if not defined return empty string
if (!str)
return "";
// remove last character from string
return str.substr(0, (str.length-1));
}
File.Basename.prototype.basename = function() {
return this._basename;
}
File.Basename.prototype.dirname = function() {
return this._dirname;
}
// not sure about this.
File.Basename.prototype.path = function() {
return this._path;
}
File.Basename.prototype.platform = function() {
return this._platform;
}
File.Basename.prototype.platform_str = function() {
return File.Basename.platformStrs[this._platform];
}
/*
=head1 NAME
File.Basename - A library that provides unix-like tools 'dirname' and 'basename'
=head1 SYNOPSIS
<script type="text/javascript" src="myscripts/JSAN.js"></script>
JSAN.use('File.Basename');
// or
<script type="text/javascript" src="myscripts/File/Basename.js"></script>
// Class Example
var path = "/testing/path/file.ext";
var file = new File.Basename(path);
// basename = 'file.ext'
var basename = file.basename();
var dirname = file.dirname();
// tell user
document.writeln("basename is '" + basename + "'");
// dump platform
document.writeln("Parsed " + file.path() + " as platform " + file.platform_str());
=head1 DESCRIPTION
This library provides tools to take a filepath and pull the directory name and
filename out of it. This is much like the perl module 'File::Basename' or the unix
shell tools of 'basename' and 'dirname'.
This type of tool can be useful if you need to process full paths on the server in
request but want to display only filenames to the user.
=head2 Constructor
var file = new File.Basename(path, platform);
Create a new C<File.Basename> object.
=over
=item * path
File path to process
=item * platform I<optional>
A 'File.Basename' platform constant, this tells the object how to process the filepath
it was given. If this is not passed the object will try to guess based upon the
user agent string. If this is not available then it will default to UNIX.
For the platform constants see the section L<Constants> below.
=back
=head2 Class Properties
=head3 VERSION
File.Basename.VERSION
Current library version
=head2 Methods
=head3 basename()
file.basename();
Returns the basename of the filepath passed to the constructor. Returns empty string
if no basename exists.
=head3 dirname()
file.dirname
Returns the directory name of the path.
Under Windows this can/will return the drive letter as well.
=head2 Constants
=over
=item * File.Basename.WIN
Indicates the desire for Windows pathname processing. (backslashes)
=item * File.Basename.MAC
Indicates the desire for Mac (same as UNIX today) pathname processing. (forward slashes)
=item * File.Basename.UNIX
Indicates object is current processing pathnames for *NIX systems (forward slashes).
=back
=head1 SEE ALSO
nothing at this time.
=head1 TODO
This will not work with several multibyte character sets like 'shift_jis'. It will work
fine with UTF8.
Need to add a test for non-english UTF8 characters and such...
Need to add support to pull extension off
Need to add functional support. (basename(filepath) returns basename)
=head1 AUTHOR
Lee Carmichael <F<lecar_red@yahoo.com>>
=head1 COPYRIGHT
Copyright (c) 2005 Lee Carmichael. All rights reserved.
This module is free software; you can redistribute it and/or modify it
under the terms of the Artistic license.
=cut
*/