mirror of
https://github.com/tenrok/axios.git
synced 2026-06-02 16:04:10 +03:00
Add combineURLs helper
This commit is contained in:
@@ -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(/^\/+/, '');
|
||||
};
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user