diff options
author | Morris Jobke <hey@morrisjobke.de> | 2015-07-10 17:07:19 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-07-10 17:07:19 +0200 |
commit | ee7ccc9c3d3724e0081442711ff8ab8490e02c1d (patch) | |
tree | 1fe966d8c3be6719fc10971a56e4aa885e10fdaf /core | |
parent | a7c7d394ec73ccc50cb119a110eb38ac91d9182b (diff) | |
parent | fbc03b43b900d8657a865e447a471687342c4ce7 (diff) | |
download | nextcloud-server-ee7ccc9c3d3724e0081442711ff8ab8490e02c1d.tar.gz nextcloud-server-ee7ccc9c3d3724e0081442711ff8ab8490e02c1d.zip |
Merge pull request #17559 from owncloud/js-joinpathsutility
Add OC.joinPaths for convenient path joining
Diffstat (limited to 'core')
-rw-r--r-- | core/js/js.js | 42 | ||||
-rw-r--r-- | core/js/tests/specs/coreSpec.js | 41 |
2 files changed, 83 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js index e0adc3591ac..ff52c8f1f39 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -326,6 +326,48 @@ var OC={ }, /** + * Join path sections + * + * @param {...String} path sections + * + * @return {String} joined path, any leading or trailing slash + * will be kept + * + * @since 8.2 + */ + joinPaths: function() { + if (arguments.length < 1) { + return ''; + } + var path = ''; + var lastArg = arguments[arguments.length - 1]; + var leadingSlash = arguments[0].charAt(0) === '/'; + var trailingSlash = lastArg.charAt(lastArg.length - 1) === '/'; + var sections = []; + var i; + for (i = 0; i < arguments.length; i++) { + sections = sections.concat(arguments[i].split('/')); + } + var first = !leadingSlash; + for (i = 0; i < sections.length; i++) { + if (sections[i] !== '') { + if (first) { + first = false; + } else { + path += '/'; + } + path += sections[i]; + } + } + + if (trailingSlash) { + // add it back + path += '/'; + } + return path; + }, + + /** * Do a search query and display the results * @param {string} query the search query */ diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 6f7a34d21c8..7a09f362ee5 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -134,6 +134,47 @@ describe('Core base tests', function() { expect(escapeHTML('This is a good string without HTML.')).toEqual('This is a good string without HTML.'); }); }); + describe('joinPaths', function() { + it('returns empty string with no or empty arguments', function() { + expect(OC.joinPaths()).toEqual(''); + expect(OC.joinPaths('')).toEqual(''); + expect(OC.joinPaths('', '')).toEqual(''); + }); + it('returns joined path sections', function() { + expect(OC.joinPaths('abc')).toEqual('abc'); + expect(OC.joinPaths('abc', 'def')).toEqual('abc/def'); + expect(OC.joinPaths('abc', 'def', 'ghi')).toEqual('abc/def/ghi'); + }); + it('keeps leading slashes', function() { + expect(OC.joinPaths('/abc')).toEqual('/abc'); + expect(OC.joinPaths('/abc', 'def')).toEqual('/abc/def'); + expect(OC.joinPaths('/abc', 'def', 'ghi')).toEqual('/abc/def/ghi'); + }); + it('keeps trailing slashes', function() { + expect(OC.joinPaths('abc/')).toEqual('abc/'); + expect(OC.joinPaths('abc', 'def/')).toEqual('abc/def/'); + expect(OC.joinPaths('abc', 'def', 'ghi/')).toEqual('abc/def/ghi/'); + }); + it('splits paths in specified strings and discards extra slashes', function() { + expect(OC.joinPaths('//abc//')).toEqual('/abc/'); + expect(OC.joinPaths('//abc//def//')).toEqual('/abc/def/'); + expect(OC.joinPaths('//abc//', '//def//')).toEqual('/abc/def/'); + expect(OC.joinPaths('//abc//', '//def//', '//ghi//')).toEqual('/abc/def/ghi/'); + expect(OC.joinPaths('//abc//def//', '//ghi//jkl/mno/', '//pqr//')) + .toEqual('/abc/def/ghi/jkl/mno/pqr/'); + expect(OC.joinPaths('/abc', '/def')).toEqual('/abc/def'); + expect(OC.joinPaths('/abc/', '/def')).toEqual('/abc/def'); + expect(OC.joinPaths('/abc/', 'def')).toEqual('/abc/def'); + }); + it('discards empty sections', function() { + expect(OC.joinPaths('abc', '', 'def')).toEqual('abc/def'); + }); + it('returns root if only slashes', function() { + expect(OC.joinPaths('//')).toEqual('/'); + expect(OC.joinPaths('/', '/')).toEqual('/'); + expect(OC.joinPaths('/', '//', '/')).toEqual('/'); + }); + }); describe('filePath', function() { beforeEach(function() { OC.webroot = 'http://localhost'; |