diff options
author | Stas Vilchik <stas.vilchik@sonarsource.com> | 2017-08-23 09:38:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-23 09:38:42 +0200 |
commit | c49ca3ba841ea7763148a76d9a84e1bf7f5698ad (patch) | |
tree | 7f49588f65540949da16846b06202dc7752c92e1 /server/sonar-web/src/main/js/helpers/path.ts | |
parent | 008f13434b7a6ba482a5c9535708746627f32665 (diff) | |
download | sonarqube-c49ca3ba841ea7763148a76d9a84e1bf7f5698ad.tar.gz sonarqube-c49ca3ba841ea7763148a76d9a84e1bf7f5698ad.zip |
move some helpers to ts (#2399)
Diffstat (limited to 'server/sonar-web/src/main/js/helpers/path.ts')
-rw-r--r-- | server/sonar-web/src/main/js/helpers/path.ts | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/server/sonar-web/src/main/js/helpers/path.ts b/server/sonar-web/src/main/js/helpers/path.ts new file mode 100644 index 00000000000..2ea8d4f5963 --- /dev/null +++ b/server/sonar-web/src/main/js/helpers/path.ts @@ -0,0 +1,108 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +export function collapsePath(path: string, limit = 30): string { + if (typeof path !== 'string') { + return ''; + } + + const tokens = path.split('/'); + + if (tokens.length <= 2) { + return path; + } + + const head = tokens[0]; + const tail = tokens[tokens.length - 1]; + const middle = tokens.slice(1, -1); + let cut = false; + + while (middle.join().length > limit && middle.length > 0) { + middle.shift(); + cut = true; + } + + const body = [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') + */ +export function collapsedDirFromPath(path: string | null): string | null { + const limit = 30; + if (typeof path === 'string') { + const tokens = path.split('/').slice(0, -1); + if (tokens.length > 2) { + const head = tokens[0]; + const tail = tokens[tokens.length - 1]; + const middle = tokens.slice(1, -1); + + let cut = false; + while (middle.join().length > limit && middle.length > 0) { + middle.shift(); + cut = true; + } + const body = [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') + */ +export function fileFromPath(path: string | null): string | null { + if (typeof path === 'string') { + const tokens = path.split('/'); + return tokens[tokens.length - 1]; + } else { + return null; + } +} + +export function splitPath(path: string): { head: string; tail: string } | null { + if (typeof path === 'string') { + const tokens = path.split('/'); + return { + head: tokens.slice(0, -1).join('/'), + tail: tokens[tokens.length - 1] + }; + } else { + return null; + } +} + +export function limitComponentName(str: string, limit = 30): string { + if (typeof str === 'string') { + return str.length > limit ? str.substr(0, limit) + '...' : str; + } else { + return ''; + } +} |