From a13e8c45444cb298a20ee579c70df4fa994b1006 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 26 Jan 2017 10:28:25 +0200 Subject: [PATCH] fix(cloudcmd) prefix: duplicate (#102) --- client/client.js | 7 +++---- client/cloudcmd.js | 15 ++++++++++++--- server/prefixer.js | 8 ++++---- test/server/prefixer.js | 26 ++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 test/server/prefixer.js diff --git a/client/client.js b/client/client.js index 3072d40b..bf71b293 100644 --- a/client/client.js +++ b/client/client.js @@ -158,11 +158,12 @@ var Util, DOM, CloudFunc, join; return new F(); } - /** Конструктор CloudClient, который + /** + * Конструктор CloudClient, который * выполняет весь функционал по * инициализации */ - this.init = function(config) { + this.init = function(prefix, config) { var func = function() { Util.exec.series([ initModules, @@ -185,8 +186,6 @@ var Util, DOM, CloudFunc, join; }); }; - var prefix = config.prefix; - CloudCmd.PREFIX = prefix; CloudCmd.PREFIX_URL = prefix + CloudFunc.apiURL; diff --git a/client/cloudcmd.js b/client/cloudcmd.js index e5ab763b..5b158fb8 100644 --- a/client/cloudcmd.js +++ b/client/cloudcmd.js @@ -6,8 +6,7 @@ var CloudCmd; CloudCmd = load; function load(config) { - var prefix = config.prefix || ''; - + var prefix = getPrefix(config.prefix); var modules = '/modules/'; var client = 'client/'; var files = [ @@ -53,7 +52,7 @@ var CloudCmd; var urlFiles = getJoinURL(allFiles); createScript(prefix + urlFiles, function() { - CloudCmd.init(config); + CloudCmd.init(prefix, config); }); } @@ -70,6 +69,16 @@ var CloudCmd; return name + '/dist/' + name + '.min'; } + function getPrefix(prefix) { + if (!prefix) + return ''; + + if (!prefix.indexOf('/')) + return prefix; + + return '/' + prefix; + } + function createScript(url, callback) { var script = document.createElement('script'); diff --git a/server/prefixer.js b/server/prefixer.js index 85446a32..3b5f3a95 100644 --- a/server/prefixer.js +++ b/server/prefixer.js @@ -2,14 +2,14 @@ module.exports = (value) => { if (typeof value !== 'string') - return value; - - if (value && !~value.indexOf('/')) - return '/' + value; + return ''; if (value.length === 1) return ''; + if (value && !~value.indexOf('/')) + return '/' + value; + return value; }; diff --git a/test/server/prefixer.js b/test/server/prefixer.js new file mode 100644 index 00000000..a7c7e824 --- /dev/null +++ b/test/server/prefixer.js @@ -0,0 +1,26 @@ +'use strict'; + +const test = require('tape'); + +const dir = '../../server/'; +const prefixer = require(dir + 'prefixer'); + +test('prefixer: prefix without a slash', (t) => { + t.equal(prefixer('hello'), '/hello', 'should add slash'); + t.end(); +}); + +test('prefixer: root', (t) => { + t.equal(prefixer('/'), '', 'should add slash'); + t.end(); +}); + +test('prefixer: with slash', (t) => { + t.equal(prefixer('/hello'), '/hello', 'should add slash'); + t.end(); +}); + +test('prefixer: not a string', (t) => { + t.equal(prefixer(false), '', 'should add slash'); + t.end(); +});