JS utility function to compare paths

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Vincent Petry 2015-12-21 13:10:03 +01:00 committed by Roeland Jago Douma
parent 59c5be1cc5
commit 25d9dce067
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
2 changed files with 87 additions and 0 deletions

View File

@ -422,6 +422,28 @@ var OC={
return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
},
/**
* Returns whether the given paths are the same, without
* leading, trailing or doubled slashes and also removing
* the dot sections.
*
* @param {String} path1 first path
* @param {String} path2 second path
* @return {bool} true if the paths are the same
*
* @since 9.0
*/
isSamePath: function(path1, path2) {
var filterDot = function(p) {
return p !== '.';
};
var pathSections1 = _.filter((path1 || '').split('/'), filterDot);
var pathSections2 = _.filter((path2 || '').split('/'), filterDot);
path1 = OC.joinPaths.apply(OC, pathSections1);
path2 = OC.joinPaths.apply(OC, pathSections2);
return path1 === path2;
},
/**
* Join path sections
*

View File

@ -188,6 +188,71 @@ describe('Core base tests', function() {
expect(OC.joinPaths('/', '//', '/')).toEqual('/');
});
});
describe('isSamePath', function() {
it('recognizes empty paths are equal', function() {
expect(OC.isSamePath('', '')).toEqual(true);
expect(OC.isSamePath('/', '')).toEqual(true);
expect(OC.isSamePath('//', '')).toEqual(true);
expect(OC.isSamePath('/', '/')).toEqual(true);
expect(OC.isSamePath('/', '//')).toEqual(true);
});
it('recognizes path with single sections as equal regardless of extra slashes', function() {
expect(OC.isSamePath('abc', 'abc')).toEqual(true);
expect(OC.isSamePath('/abc', 'abc')).toEqual(true);
expect(OC.isSamePath('//abc', 'abc')).toEqual(true);
expect(OC.isSamePath('abc', '/abc')).toEqual(true);
expect(OC.isSamePath('abc/', 'abc')).toEqual(true);
expect(OC.isSamePath('abc/', 'abc/')).toEqual(true);
expect(OC.isSamePath('/abc/', 'abc/')).toEqual(true);
expect(OC.isSamePath('/abc/', '/abc/')).toEqual(true);
expect(OC.isSamePath('//abc/', '/abc/')).toEqual(true);
expect(OC.isSamePath('//abc//', '/abc/')).toEqual(true);
expect(OC.isSamePath('abc', 'def')).toEqual(false);
expect(OC.isSamePath('/abc', 'def')).toEqual(false);
expect(OC.isSamePath('//abc', 'def')).toEqual(false);
expect(OC.isSamePath('abc', '/def')).toEqual(false);
expect(OC.isSamePath('abc/', 'def')).toEqual(false);
expect(OC.isSamePath('abc/', 'def/')).toEqual(false);
expect(OC.isSamePath('/abc/', 'def/')).toEqual(false);
expect(OC.isSamePath('/abc/', '/def/')).toEqual(false);
expect(OC.isSamePath('//abc/', '/def/')).toEqual(false);
expect(OC.isSamePath('//abc//', '/def/')).toEqual(false);
});
it('recognizes path with multiple sections as equal regardless of extra slashes', function() {
expect(OC.isSamePath('abc/def', 'abc/def')).toEqual(true);
expect(OC.isSamePath('/abc/def', 'abc/def')).toEqual(true);
expect(OC.isSamePath('abc/def', '/abc/def')).toEqual(true);
expect(OC.isSamePath('abc/def/', '/abc/def/')).toEqual(true);
expect(OC.isSamePath('/abc/def/', '/abc/def/')).toEqual(true);
expect(OC.isSamePath('/abc/def/', 'abc/def/')).toEqual(true);
expect(OC.isSamePath('//abc/def/', 'abc/def/')).toEqual(true);
expect(OC.isSamePath('//abc/def//', 'abc/def/')).toEqual(true);
expect(OC.isSamePath('abc/def', 'abc/ghi')).toEqual(false);
expect(OC.isSamePath('/abc/def', 'abc/ghi')).toEqual(false);
expect(OC.isSamePath('abc/def', '/abc/ghi')).toEqual(false);
expect(OC.isSamePath('abc/def/', '/abc/ghi/')).toEqual(false);
expect(OC.isSamePath('/abc/def/', '/abc/ghi/')).toEqual(false);
expect(OC.isSamePath('/abc/def/', 'abc/ghi/')).toEqual(false);
expect(OC.isSamePath('//abc/def/', 'abc/ghi/')).toEqual(false);
expect(OC.isSamePath('//abc/def//', 'abc/ghi/')).toEqual(false);
});
it('recognizes path entries with dot', function() {
expect(OC.isSamePath('.', '')).toEqual(true);
expect(OC.isSamePath('.', '.')).toEqual(true);
expect(OC.isSamePath('.', '/')).toEqual(true);
expect(OC.isSamePath('/.', '/')).toEqual(true);
expect(OC.isSamePath('/./', '/')).toEqual(true);
expect(OC.isSamePath('/./', '/.')).toEqual(true);
expect(OC.isSamePath('/./', '/./')).toEqual(true);
expect(OC.isSamePath('/./', '/./')).toEqual(true);
expect(OC.isSamePath('a/./b', 'a/b')).toEqual(true);
expect(OC.isSamePath('a/b/.', 'a/b')).toEqual(true);
expect(OC.isSamePath('./a/b', 'a/b')).toEqual(true);
});
});
describe('filePath', function() {
beforeEach(function() {
OC.webroot = 'http://localhost';