Skip to content

Commit

Permalink
fix-for-normalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPilot committed Sep 29, 2015
1 parent 72cbfc6 commit 5058884
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
32 changes: 19 additions & 13 deletions lib/services/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ var _ = require('lodash'),
/**
* Normalize path to always exist, always start with a slash, and never end with a slash.
*
* @param {string} path
* @param {string} urlPath
* @returns {string}
*/
function normalizePath(path) {
if (!path) {
path = '/';
} else if (_.first(path) !== '/') {
path = '/' + path;
} else if (path.length > 1 && _.last(path) === '/') {
path = path.substring(0, path.length - 2);
function normalizePath(urlPath) {
if (!urlPath) {
urlPath = '/';
} else if (_.first(urlPath) !== '/') {
urlPath = '/' + urlPath;
}
return path;

if (urlPath.length > 1 && _.last(urlPath) === '/') {
urlPath = urlPath.substring(0, urlPath.length - 1);
}

return urlPath;
}

/**
Expand All @@ -26,13 +29,16 @@ function normalizePath(path) {
* @returns {string}
*/
function normalizeDirectory(dir) {
if (!dir) {
if (!dir || dir === path.sep) {
dir = '.';
} else if (_.first(dir) === '/') {
} else if (_.first(dir) === path.sep) {
dir = dir.substr(1);
} else if (dir.length > 1 && _.last(dir) === '/') {
dir = dir.substring(0, dir.length - 2);
}

if (dir.length > 1 && _.last(dir) === path.sep) {
dir = dir.substring(0, dir.length - 1);
}

return dir;
}

Expand Down
38 changes: 18 additions & 20 deletions lib/services/sites.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,17 @@ describe(_.startCase(filename), function () {
expect(fn()).to.equal('/');
});

it('always begins with /', function () {
expect(_.first(fn('some/path'))).to.equal('/');
expect(_.first(fn('some/path/'))).to.equal('/');
expect(_.first(fn('/some/path'))).to.equal('/');
expect(_.first(fn('/some/path/'))).to.equal('/');
it('allows single slash', function () {
expect(fn('/')).to.equal('/');
});

it('never ends with /', function () {
expect(_.last(fn('some/path'))).to.not.equal('/');
expect(_.last(fn('some/path/'))).to.not.equal('/');
expect(_.last(fn('/some/path'))).to.not.equal('/');
expect(_.last(fn('/some/path/'))).to.not.equal('/');
it('always begins with and never ends with /', function () {
var normalized = '/some/path';

expect(fn('some/path')).to.equal(normalized);
expect(fn('some/path/')).to.equal(normalized);
expect(fn('/some/path')).to.equal(normalized);
expect(fn('/some/path/')).to.equal(normalized);
});
});

Expand All @@ -147,18 +146,17 @@ describe(_.startCase(filename), function () {
expect(fn()).to.equal('.');
});

it('never begins with /', function () {
expect(_.first(fn('some/path'))).to.not.equal('/');
expect(_.first(fn('some/path/'))).to.not.equal('/');
expect(_.first(fn('/some/path'))).to.not.equal('/');
expect(_.first(fn('/some/path/'))).to.not.equal('/');
it('does not allow single slash', function () {
expect(fn(path.sep)).to.equal('.');
});

it('never ends with /', function () {
expect(_.last(fn('some/path'))).to.not.equal('/');
expect(_.last(fn('some/path/'))).to.not.equal('/');
expect(_.last(fn('/some/path'))).to.not.equal('/');
expect(_.last(fn('/some/path/'))).to.not.equal('/');
it('never begins with and never ends with /', function () {
var normalized = ['some', path.sep, 'path'].join('');

expect(fn(['some', path.sep, 'path'].join(''))).to.equal(normalized);
expect(fn(['some', path.sep, 'path'].join('') + path.sep)).to.equal(normalized);
expect(fn(path.sep + ['some', path.sep, 'path'].join(''))).to.equal(normalized);
expect(fn(path.sep + ['some', path.sep, 'path'].join('') + path.sep)).to.equal(normalized);
});
});
});

0 comments on commit 5058884

Please sign in to comment.