_Partial_ fix for backslash path separator support in path.js
authorBert Belder <bertbelder@gmail.com>
Tue, 30 Nov 2010 09:36:25 +0000 (10:36 +0100)
committerBert Belder <bertbelder@gmail.com>
Mon, 20 Dec 2010 22:51:27 +0000 (23:51 +0100)
Needs review & tests

lib/path.js

index fc7f4c3..43461cd 100644 (file)
@@ -17,8 +17,8 @@ exports.join = function() {
 
 
 exports.split = function(path, keepBlanks) {
-  // split based on /, but only if that / is not at the start or end.
-  return exports.normalizeArray(path.split(/^|\/(?!$)/), keepBlanks);
+  // split based on / and \, but only if that / is not at the start or end.
+  return exports.normalizeArray(path.split(/^|[\\\/](?!$)/), keepBlanks);
 };
 
 
@@ -46,7 +46,7 @@ function cleanArray(parts, keepBlanks) {
 
   return parts.filter(function(p) { return validPathPart(p, keepBlanks) })
               .join('/')
-              .split(/^|\/(?!$)/);
+              .split(/^|[\\\/](?!$)/);
 }
 
 
@@ -112,10 +112,10 @@ exports.normalize = function(path, keepBlanks) {
 
 
 exports.dirname = function(path) {
-  if (path.length > 1 && '/' === path[path.length - 1]) {
+  if (path.length > 1 && '\\/'.indexOf(path[path.length-1]) != -1) {
     path = path.replace(/\/+$/, '');
   }
-  var lastSlash = path.lastIndexOf('/');
+  var lastSlash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
   switch (lastSlash) {
     case -1:
       return '.';
@@ -138,7 +138,7 @@ exports.basename = function(path, ext) {
 
 exports.extname = function(path) {
   var dot = path.lastIndexOf('.'),
-      slash = path.lastIndexOf('/');
+      slash = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
   // The last dot must be in the last path component, and it (the last dot) must
   // not start the last path component (i.e. be a dot that signifies a hidden
   // file in UNIX).