diff --git a/lib/helpers/combineURLs.js b/lib/helpers/combineURLs.js new file mode 100644 index 0000000..7145d10 --- /dev/null +++ b/lib/helpers/combineURLs.js @@ -0,0 +1,12 @@ +'use strict'; + +/** + * Creates a new URL by combining the specified URLs + * + * @param {string} baseURL The base URL + * @param {string} relativeURL The relative URL + * @returns {string} The combined URL + */ +module.exports = function combineURLs(baseURL, relativeURL) { + return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, ''); +}; diff --git a/test/specs/helpers/combineURLs.spec.js b/test/specs/helpers/combineURLs.spec.js new file mode 100644 index 0000000..e09b730 --- /dev/null +++ b/test/specs/helpers/combineURLs.spec.js @@ -0,0 +1,15 @@ +var combineURLs = require('../../../lib/helpers/combineURLs'); + +describe('helpers::combineURLs', function () { + it('should combine URLs', function () { + expect(combineURLs('https://api.github.com', '/users')).toBe('https://api.github.com/users'); + }); + + it('should remove duplicate slashes', function () { + expect(combineURLs('https://api.github.com/', '/users')).toBe('https://api.github.com/users'); + }); + + it('should insert missing slash', function () { + expect(combineURLs('https://api.github.com', 'users')).toBe('https://api.github.com/users'); + }); +});