Move addToPath to its own util

This commit is contained in:
Michael Lange 2019-05-15 15:33:58 -07:00
parent 8414a279aa
commit 96a1a5e812
3 changed files with 44 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import { inject as service } from '@ember/service';
import Watchable from './watchable';
import addToPath from 'nomad-ui/utils/add-to-path';
export default Watchable.extend({
system: service(),
@ -118,14 +119,3 @@ function associateNamespace(url, namespace) {
}
return url;
}
function addToPath(url, extension = '') {
const [path, params] = url.split('?');
let newUrl = `${path}${extension}`;
if (params) {
newUrl += `?${params}`;
}
return newUrl;
}

View File

@ -0,0 +1,11 @@
// Adds a string to the end of a URL path while being mindful of query params
export default function addToPath(url, extension = '') {
const [path, params] = url.split('?');
let newUrl = `${path}${extension}`;
if (params) {
newUrl += `?${params}`;
}
return newUrl;
}

View File

@ -0,0 +1,32 @@
import { module, test } from 'qunit';
import addToPath from 'nomad-ui/utils/add-to-path';
const testCases = [
{
name: 'Only domain',
in: ['https://domain.com', '/path'],
out: 'https://domain.com/path',
},
{
name: 'Deep path',
in: ['https://domain.com/a/path', '/to/nowhere'],
out: 'https://domain.com/a/path/to/nowhere',
},
{
name: 'With Query Params',
in: ['https://domain.com?interesting=development', '/this-is-an'],
out: 'https://domain.com/this-is-an?interesting=development',
},
];
module('Unit | Util | addToPath', function() {
testCases.forEach(testCase => {
test(testCase.name, function(assert) {
assert.equal(
addToPath.apply(null, testCase.in),
testCase.out,
`[${testCase.in.join(', ')}] => ${testCase.out}`
);
});
});
});