File.Basename.js (from JSAN)


/ Published in: JavaScript
Save to your folder(s)

File.Basename - A library that provides unix-like tools 'dirname' and 'basename'


Copy this code and paste it in your HTML
  1. /*
  2.  Author: Lee Carmichael [email protected]
  3.  Copyright: Copyright (c) 2005 Lee Carmichael. All rights reserved.
  4.  License: Artistic License
  5.  Source: http://www.openjsan.org
  6.  
  7.  Description:
  8.  
  9.   File.Basename - A library that provides unix-like tools 'dirname' and 'basename'
  10.   (Grabbed from JSAN, modified syntax.)
  11.  
  12.  Example:
  13.  
  14.   File.basename('/javascripts/bin.js',2) // => bin.js
  15.   File.dirname('/javascripts/bin.js',2) // => /javascripts
  16.   File.path('/javascripts/bin.js',1) // => /javascripts/bin.js
  17.   File.platform('/javascripts/bin.js',2) // => 2
  18.   File.platformString('/javascripts/bin.js',2) // => Macintosh
  19.   File.platformString('/javascripts/bin.js',1) // => Windows
  20.   File.platformString('/javascripts/bin.js',2) // => Macintosh
  21.   File.platformString('/javascripts/bin.js',3) // => UNIX
  22.   File.platformString('/javascripts/bin.js',4) // => undefined
  23.  
  24. */
  25.  
  26. var File = (function() {
  27. var TEMP = {};
  28. // it would be nice to turn this into a class
  29. function _chop(str) {
  30. // if not defined return empty string
  31. if (!str)
  32. return "";
  33.  
  34. // remove last character from string
  35. return str.substr(0, (str.length - 1));
  36. }
  37. var File = {
  38. CONSTANTS: {
  39. WIN: 1,
  40. MAC: 2,
  41. UNIX: 3,
  42. platformStrs: new Array("N/A", "Windows", "Macintosh", "UNIX")
  43. },
  44.  
  45. analyze_path: function(path, platform) {
  46. // should we error here? or just return?
  47. if (!path) return;
  48. // setup the path
  49. TEMP._path = path;
  50. // need to check this value
  51. // grab passed platform
  52. if (platform) TEMP._platform = platform;
  53. // figure out client type
  54. if (typeof(TEMP._platform) == "undefined") {
  55. // need to check for navigator and platform
  56. // just in case if not in browser env...
  57. if (navigator.platform.indexOf("Win") >= 0) {
  58. TEMP._platform = File.CONSTANTS.WIN;
  59. } else if (navigator.platform.indexOf("Mac") >= 0) {
  60. TEMP._platform = File.CONSTANTS.MAC;
  61. } else {
  62. TEMP._platform = File.CONSTANTS.UNIX;
  63. }
  64. };
  65. // set path pattern (current for Win we leave the <Drive Letter>)
  66. if (TEMP._platform == File.CONSTANTS.WIN)
  67. TEMP._pattern = /^(.*\\)?(.*)/;
  68. // this will not match drive letter...
  69. else
  70. TEMP._pattern = /^(.*\/)?(.*)/;
  71.  
  72. // match string
  73. var rc = TEMP._path.match(TEMP._pattern);
  74.  
  75. // should we check values here?
  76. // * skip rc[0] since it returns our string *
  77. // setup basename
  78. TEMP._basename = rc[2];
  79.  
  80. if (!TEMP._basename) TEMP._basename = "";
  81.  
  82. // setup dirname
  83. // and remove trailing slash
  84. TEMP._dirname = _chop(rc[1]);
  85.  
  86. return {
  87. basename: TEMP._basename,
  88. dirname: TEMP._dirname,
  89. path: TEMP._path,
  90. platform: TEMP._platform,
  91. platformString: File.CONSTANTS.platformStrs[TEMP._platform]
  92. };
  93. },
  94. basename: function(path, platform) {
  95. return File.analyze_path(path, platform).basename
  96. },
  97. dirname: function(path, platform) {
  98. return File.analyze_path(path, platform).dirname
  99. },
  100. path: function(path, platform) {
  101. return File.analyze_path(path, platform).path
  102. },
  103. platform: function(path, platform) {
  104. return File.analyze_path(path, platform).platform
  105. },
  106. platformString: function(path, platform) {
  107. return File.analyze_path(path, platform).platformString
  108. }
  109.  
  110.  
  111. }
  112. return File;
  113. })()
  114. // File.Basename -

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.