Fix appendPath to support empty strings
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 13 Apr 2017 11:02:11 +0200
changeset 111 a3d7461ef63a
parent 110 432a59a73c7d
child 112 2316727a0495
Fix appendPath to support empty strings
src/utils.js
test/index.js
--- a/src/utils.js	Thu Apr 13 10:45:07 2017 +0200
+++ b/src/utils.js	Thu Apr 13 11:02:11 2017 +0200
@@ -7,8 +7,12 @@
 }
 
 export function appendPath(url, path) {
-    path = path[0] === '/' ? path.slice(1) : path;
-    url = url.slice(-1) === '/' ? url : `${url}/`;
+    if (path.length > 0) {
+        path = path[0] === '/' ? path : `/${path}`;
+    }
+    if (url.length > 0) {
+        url = url.slice(-1) === '/' ? url.slice(0,-1) : url;
+    }
     return url + path;
 }
 
--- a/test/index.js	Thu Apr 13 10:45:07 2017 +0200
+++ b/test/index.js	Thu Apr 13 11:02:11 2017 +0200
@@ -8,7 +8,7 @@
 import React from 'react';
 import {merge} from 'lodash/object';
 
-import {buildFormData} from '../src/utils';
+import {buildFormData, appendPath} from '../src/utils';
 import {Api} from '../src/Api';
 import defaultApi from '../src/Api';
 import {ActionLink, ActionsDropDown, CollectionItemLink} from '../src/components/BaseViews';
@@ -350,24 +350,34 @@
 
 describe('Utils', () => {
     describe('appendPath', () => {
-        it('should add slash between BASE_URL and path when it is not already there', () => {
-            const baseUrl = 'http://example.com';
-            const path = 'foo';
-            const api = new Api(baseUrl);
+        it('should add slash between prefix and suffix when it is not already there', () => {
+            const prefix = 'http://example.com';
+            const suffix = 'foo';
 
-            const url = api.buildUrl(path);
+            const result = appendPath(prefix, suffix);
 
-            expect(url).to.be.equal('http://example.com/foo');
+            expect(result).to.be.equal('http://example.com/foo');
         });
 
-        it('should keep only one slash when BASE_URL ends with "/" and path starts with "/"', () => {
-            const baseUrl = "http://example.com/";
-            const path = "/foo";
-            const api = new Api(baseUrl);
+        it('should keep only one slash when prefix ends with "/" and suffix starts with "/"', () => {
+            const prefix = "http://example.com/";
+            const suffix = "/foo";
+
+            const result = appendPath(prefix, suffix);
+
+            expect(result).to.be.equal('http://example.com/foo');
+        });
 
-            const url = api.buildUrl(path);
+        it('should not alter empty strings', () => {
+            const prefix1 = '';
+            const suffix1 = '';
+            const prefix2 = 'a';
+            const suffix2 = 'b';
 
-            expect(url).to.be.equal('http://example.com/foo');
+            expect(appendPath(prefix1, suffix1)).to.be.equal('');
+            expect(appendPath(prefix1, suffix2)).to.be.equal('/b');
+            expect(appendPath(prefix2, suffix1)).to.be.equal('a');
+            expect(appendPath(prefix2, suffix2)).to.be.equal('a/b');
         });
     });
 });