From bd60854219d700011b9661727a6ff004b3c21ef0 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 6 Jan 2016 17:25:55 +0100 Subject: [PATCH] improve code quality --- .../handlebars/collapsedDirFromPath.js | 4 +- .../js/helpers/handlebars/fileFromPath.js | 2 + server/sonar-web/src/main/js/helpers/path.js | 50 ++++++++++++++ .../sonar-web/src/main/js/libs/application.js | 56 --------------- .../main/js/main/nav/global/search-view.js | 3 +- .../src/main/js/widgets/old/bubble-chart.js | 5 +- server/sonar-web/tests/application-test.js | 69 ------------------- server/sonar-web/tests/helpers/path-test.js | 50 ++++++++++++++ 8 files changed, 110 insertions(+), 129 deletions(-) delete mode 100644 server/sonar-web/tests/application-test.js create mode 100644 server/sonar-web/tests/helpers/path-test.js diff --git a/server/sonar-web/src/main/js/helpers/handlebars/collapsedDirFromPath.js b/server/sonar-web/src/main/js/helpers/handlebars/collapsedDirFromPath.js index bfc6ce69f6b..e9ef398d739 100644 --- a/server/sonar-web/src/main/js/helpers/handlebars/collapsedDirFromPath.js +++ b/server/sonar-web/src/main/js/helpers/handlebars/collapsedDirFromPath.js @@ -17,6 +17,8 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { collapsedDirFromPath } from '../path'; + module.exports = function (path) { - return window.collapsedDirFromPath(path); + return collapsedDirFromPath(path); }; diff --git a/server/sonar-web/src/main/js/helpers/handlebars/fileFromPath.js b/server/sonar-web/src/main/js/helpers/handlebars/fileFromPath.js index f495dbedb39..7da656f0cd8 100644 --- a/server/sonar-web/src/main/js/helpers/handlebars/fileFromPath.js +++ b/server/sonar-web/src/main/js/helpers/handlebars/fileFromPath.js @@ -17,6 +17,8 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { fileFromPath } from '../path'; + module.exports = function (path) { return window.fileFromPath(path); }; diff --git a/server/sonar-web/src/main/js/helpers/path.js b/server/sonar-web/src/main/js/helpers/path.js index e2e07cd567f..98a685f8358 100644 --- a/server/sonar-web/src/main/js/helpers/path.js +++ b/server/sonar-web/src/main/js/helpers/path.js @@ -44,3 +44,53 @@ export function collapsePath (path, limit = 30) { var body = [].concat(head, cut ? ['...'] : [], middle, tail); return body.join('/'); } + + +/** + * Return a collapsed path without a file name + * @example + * // returns 'src/.../js/components/navigator/app/models/' + * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js') + * @param {string} path + * @returns {string|null} + */ +export function collapsedDirFromPath (path) { + var limit = 30; + if (typeof path === 'string') { + var tokens = _.initial(path.split('/')); + if (tokens.length > 2) { + var head = _.first(tokens), + tail = _.last(tokens), + middle = _.initial(_.rest(tokens)), + cut = false; + while (middle.join().length > limit && middle.length > 0) { + middle.shift(); + cut = true; + } + var body = [].concat(head, cut ? ['...'] : [], middle, tail); + return body.join('/') + '/'; + } else { + return tokens.join('/') + '/'; + } + } else { + return null; + } +} + + +/** + * Return a file name for a given file path + * * @example + * // returns 'state.js' + * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js') + * @param {string} path + * @returns {string|null} + */ +export function fileFromPath (path) { + if (typeof path === 'string') { + var tokens = path.split('/'); + return _.last(tokens); + } else { + return null; + } +} diff --git a/server/sonar-web/src/main/js/libs/application.js b/server/sonar-web/src/main/js/libs/application.js index 58905deff78..6abe69488e7 100644 --- a/server/sonar-web/src/main/js/libs/application.js +++ b/server/sonar-web/src/main/js/libs/application.js @@ -225,62 +225,6 @@ function closeModalWindow () { -/* - * File Path - */ - -(function () { - /** - * Return a collapsed path without a file name - * @example - * // returns 'src/.../js/components/navigator/app/models/' - * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js') - * @param {string} path - * @returns {string|null} - */ - window.collapsedDirFromPath = function (path) { - var limit = 30; - if (typeof path === 'string') { - var tokens = _.initial(path.split('/')); - if (tokens.length > 2) { - var head = _.first(tokens), - tail = _.last(tokens), - middle = _.initial(_.rest(tokens)), - cut = false; - while (middle.join().length > limit && middle.length > 0) { - middle.shift(); - cut = true; - } - var body = [].concat(head, cut ? ['...'] : [], middle, tail); - return body.join('/') + '/'; - } else { - return tokens.join('/') + '/'; - } - } else { - return null; - } - }; - - /** - * Return a file name for a given file path - * * @example - * // returns 'state.js' - * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js') - * @param {string} path - * @returns {string|null} - */ - window.fileFromPath = function (path) { - if (typeof path === 'string') { - var tokens = path.split('/'); - return _.last(tokens); - } else { - return null; - } - }; -})(); - - - /* * Users */ diff --git a/server/sonar-web/src/main/js/main/nav/global/search-view.js b/server/sonar-web/src/main/js/main/nav/global/search-view.js index b70700527cc..070143785c4 100644 --- a/server/sonar-web/src/main/js/main/nav/global/search-view.js +++ b/server/sonar-web/src/main/js/main/nav/global/search-view.js @@ -27,6 +27,7 @@ import EmptySearchTemplate from '../templates/nav-search-empty.hbs'; import SearchTemplate from '../templates/nav-search.hbs'; import RecentHistory from '../component/recent-history'; import { translate } from '../../../helpers/l10n'; +import { collapsedDirFromPath, fileFromPath } from '../../../helpers/path'; var SearchItemView = Marionette.ItemView.extend({ tagName: 'li', @@ -158,7 +159,7 @@ export default Marionette.LayoutView.extend({ var isFile = ['FIL', 'UTS'].indexOf(f.qualifier) !== -1; return { url: baseUrl + '/dashboard/index?id=' + encodeURIComponent(f.key) + window.dashboardParameters(true), - name: isFile ? window.collapsedDirFromPath(f.lname) + window.fileFromPath(f.lname) : f.name, + name: isFile ? collapsedDirFromPath(f.lname) + fileFromPath(f.lname) : f.name, icon: 'favorite' }; }); diff --git a/server/sonar-web/src/main/js/widgets/old/bubble-chart.js b/server/sonar-web/src/main/js/widgets/old/bubble-chart.js index 62a0b6c3498..259bb5e654a 100644 --- a/server/sonar-web/src/main/js/widgets/old/bubble-chart.js +++ b/server/sonar-web/src/main/js/widgets/old/bubble-chart.js @@ -18,6 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import $ from 'jquery'; +import { collapsedDirFromPath, fileFromPath } from '../../helpers/path'; window.SonarWidgets = window.SonarWidgets == null ? {} : window.SonarWidgets; @@ -220,8 +221,8 @@ window.SonarWidgets = window.SonarWidgets == null ? {} : window.SonarWidgets; sizeMetricValue = d.measures[widget.sizeMetric].fval; return '
' + - window.collapsedDirFromPath(d.longName) + '
' + - window.fileFromPath(d.longName) + '
' + '
' + + collapsedDirFromPath(d.longName) + '
' + + fileFromPath(d.longName) + '
' + '
' + xMetricName + ': ' + xMetricValue + '
' + yMetricName + ': ' + yMetricValue + '
' + sizeMetricName + ': ' + sizeMetricValue + diff --git a/server/sonar-web/tests/application-test.js b/server/sonar-web/tests/application-test.js deleted file mode 100644 index 14146be362f..00000000000 --- a/server/sonar-web/tests/application-test.js +++ /dev/null @@ -1,69 +0,0 @@ -describe.skip('Application', function () { - describe('#collapsedDirFromPath()', function () { - it('should return null when pass null', function () { - assert.isNull(window.collapsedDirFromPath(null)); - }); - - it('should return "/" when pass "/"', function () { - assert.equal(window.collapsedDirFromPath('/'), '/'); - }); - - it('should not cut short path', function () { - assert.equal(window.collapsedDirFromPath('src/main/js/components/state.js'), 'src/main/js/components/'); - }); - - it('should cut long path', function () { - assert.equal(window.collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js'), - 'src/.../js/components/navigator/app/models/'); - }); - - it('should cut very long path', function () { - assert.equal(window.collapsedDirFromPath('src/main/another/js/components/navigator/app/models/state.js'), - 'src/.../js/components/navigator/app/models/'); - }); - }); - - describe('#fileFromPath()', function () { - it('should return null when pass null', function () { - assert.isNull(window.fileFromPath(null)); - }); - - it('should return empty string when pass "/"', function () { - assert.equal(window.fileFromPath('/'), ''); - }); - - it('should return file name when pass only file name', function () { - assert.equal(window.fileFromPath('file.js'), 'file.js'); - }); - - it('should return file name when pass file path', function () { - assert.equal(window.fileFromPath('src/main/js/file.js'), 'file.js'); - }); - - it('should return file name when pass file name without extension', function () { - assert.equal(window.fileFromPath('src/main/file'), 'file'); - }); - }); - - describe('Severity Comparators', function () { - describe('#severityComparator', function () { - it('should have correct order', function () { - assert.equal(window.severityComparator('BLOCKER'), 0); - assert.equal(window.severityComparator('CRITICAL'), 1); - assert.equal(window.severityComparator('MAJOR'), 2); - assert.equal(window.severityComparator('MINOR'), 3); - assert.equal(window.severityComparator('INFO'), 4); - }); - }); - - describe('#severityColumnsComparator', function () { - it('should have correct order', function () { - assert.equal(window.severityColumnsComparator('BLOCKER'), 0); - assert.equal(window.severityColumnsComparator('CRITICAL'), 2); - assert.equal(window.severityColumnsComparator('MAJOR'), 4); - assert.equal(window.severityColumnsComparator('MINOR'), 1); - assert.equal(window.severityColumnsComparator('INFO'), 3); - }); - }); - }); -}); diff --git a/server/sonar-web/tests/helpers/path-test.js b/server/sonar-web/tests/helpers/path-test.js new file mode 100644 index 00000000000..f663fb59b12 --- /dev/null +++ b/server/sonar-web/tests/helpers/path-test.js @@ -0,0 +1,50 @@ +import { expect } from 'chai'; +import { collapsedDirFromPath, fileFromPath } from '../../src/main/js/helpers/path' + +describe('Path', function () { + describe('#collapsedDirFromPath()', function () { + it('should return null when pass null', function () { + expect(collapsedDirFromPath(null)).to.be.null; + }); + + it('should return "/" when pass "/"', function () { + expect(collapsedDirFromPath('/')).to.equal('/'); + }); + + it('should not cut short path', function () { + expect(collapsedDirFromPath('src/main/js/components/state.js')).to.equal('src/main/js/components/'); + }); + + it('should cut long path', function () { + expect(collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js')) + .to.equal('src/.../js/components/navigator/app/models/'); + }); + + it('should cut very long path', function () { + expect(collapsedDirFromPath('src/main/another/js/components/navigator/app/models/state.js')) + .to.equal('src/.../js/components/navigator/app/models/'); + }); + }); + + describe('#fileFromPath()', function () { + it('should return null when pass null', function () { + expect(fileFromPath(null)).to.be.null; + }); + + it('should return empty string when pass "/"', function () { + expect(fileFromPath('/')).to.equal(''); + }); + + it('should return file name when pass only file name', function () { + expect(fileFromPath('file.js')).to.equal('file.js'); + }); + + it('should return file name when pass file path', function () { + expect(fileFromPath('src/main/js/file.js')).to.equal('file.js'); + }); + + it('should return file name when pass file name without extension', function () { + expect(fileFromPath('src/main/file')).to.equal('file'); + }); + }); +}); -- 2.39.5