Browse Source

Remove keepBlanks flag from path functions

v0.7.4-release
Bert Belder 14 years ago
committed by Ryan Dahl
parent
commit
9ddfcfecca
  1. 35
      lib/path.js
  2. 16
      test/simple/test-path.js

35
lib/path.js

@ -1,40 +1,33 @@
function validPathPart(p, keepBlanks) { function validPathPart(p) {
return typeof p === 'string' && (p || keepBlanks); return typeof p === 'string' && p;
} }
exports.join = function() { exports.join = function() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
// edge case flag to switch into url-resolve-mode return exports.normalizeArray(args).join('/');
var keepBlanks = false;
if (args[args.length - 1] === true) {
keepBlanks = args.pop();
}
// return exports.split(args.join("/"), keepBlanks).join("/");
var joined = exports.normalizeArray(args, keepBlanks).join('/');
return joined;
}; };
exports.split = function(path, keepBlanks) { exports.split = function(path) {
// split based on / and \, but only if that / is not at the start or end. // split based on / and \, but only if that / is not at the start or end.
return exports.normalizeArray(path.split(/^|[\\\/](?!$)/), keepBlanks); return exports.normalizeArray(path.split(/^|[\\\/](?!$)/));
}; };
function cleanArray(parts, keepBlanks) { function cleanArray(parts) {
var i = 0; var i = 0;
var l = parts.length - 1; var l = parts.length - 1;
var stripped = false; var stripped = false;
// strip leading empty args // strip leading empty args
while (i < l && !validPathPart(parts[i], keepBlanks)) { while (i < l && !validPathPart(parts[i])) {
stripped = true; stripped = true;
i++; i++;
} }
// strip tailing empty args // strip tailing empty args
while (l >= i && !validPathPart(parts[l], keepBlanks)) { while (l >= i && !validPathPart(parts[l])) {
stripped = true; stripped = true;
l--; l--;
} }
@ -44,14 +37,14 @@ function cleanArray(parts, keepBlanks) {
parts = Array.prototype.slice.call(parts, i, l + 1); parts = Array.prototype.slice.call(parts, i, l + 1);
} }
return parts.filter(function(p) { return validPathPart(p, keepBlanks) }) return parts.filter(function(p) { return validPathPart(p) })
.join('/') .join('/')
.split(/^|[\\\/](?!$)/); .split(/^|[\\\/](?!$)/);
} }
exports.normalizeArray = function(original, keepBlanks) { exports.normalizeArray = function(original) {
var parts = cleanArray(original, keepBlanks); var parts = cleanArray(original);
if (!parts.length || (parts.length === 1 && !parts[0])) return ['.']; if (!parts.length || (parts.length === 1 && !parts[0])) return ['.'];
// now we're fully ready to rock. // now we're fully ready to rock.
@ -69,7 +62,7 @@ exports.normalizeArray = function(original, keepBlanks) {
var directory = parts[i]; var directory = parts[i];
// if it's blank, and we're not keeping blanks, then skip it. // if it's blank, and we're not keeping blanks, then skip it.
if (directory === '' && !keepBlanks) continue; if (directory === '') continue;
// if it's a dot, then skip it // if it's a dot, then skip it
if (directory === '.' && (directories.length || if (directory === '.' && (directories.length ||
@ -106,8 +99,8 @@ exports.normalizeArray = function(original, keepBlanks) {
}; };
exports.normalize = function(path, keepBlanks) { exports.normalize = function(path) {
return exports.join(path, keepBlanks || false); return exports.join(path);
}; };

16
test/simple/test-path.js

@ -82,13 +82,6 @@ var joinTests =
[[' ', '.'], ' '], [[' ', '.'], ' '],
[[' ', '/'], ' /'], [[' ', '/'], ' /'],
[[' ', ''], ' '], [[' ', ''], ' '],
// preserving empty path parts, for url resolution case
// pass boolean true as LAST argument.
[['', '', true], '/'],
[['foo', '', true], 'foo/'],
[['foo', '', 'bar', true], 'foo//bar'],
[['foo/', '', 'bar', true], 'foo///bar'],
[['', true], '.'],
// filtration of non-strings. // filtration of non-strings.
[['x', true, 7, 'y', null, {}], 'x/y'] [['x', true, 7, 'y', null, {}], 'x/y']
]; ];
@ -106,20 +99,11 @@ assert.equal(failures.length, 0, failures.join(''));
assert.equal(path.normalize('./fixtures///b/../b/c.js'), assert.equal(path.normalize('./fixtures///b/../b/c.js'),
'fixtures/b/c.js'); 'fixtures/b/c.js');
assert.equal(path.normalize('./fixtures///b/../b/c.js', true),
'fixtures///b/c.js');
assert.equal(path.normalize('/foo/../../../bar'), '/bar'); assert.equal(path.normalize('/foo/../../../bar'), '/bar');
assert.deepEqual(path.normalizeArray(['fixtures', 'b', '', '..', 'b', 'c.js']), assert.deepEqual(path.normalizeArray(['fixtures', 'b', '', '..', 'b', 'c.js']),
['fixtures', 'b', 'c.js']); ['fixtures', 'b', 'c.js']);
assert.deepEqual(path.normalizeArray(['fixtures', '', 'b', '..', 'b', 'c.js'],
true), ['fixtures', '', 'b', 'c.js']);
assert.equal(path.normalize('a//b//../b', true), 'a//b/b');
assert.equal(path.normalize('a//b//../b'), 'a/b'); assert.equal(path.normalize('a//b//../b'), 'a/b');
assert.equal(path.normalize('a//b//./c', true), 'a//b//c');
assert.equal(path.normalize('a//b//./c'), 'a/b/c'); assert.equal(path.normalize('a//b//./c'), 'a/b/c');
assert.equal(path.normalize('a//b//.', true), 'a//b/');
assert.equal(path.normalize('a//b//.'), 'a/b'); assert.equal(path.normalize('a//b//.'), 'a/b');

Loading…
Cancel
Save