diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 07:08:36 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 07:08:36 +0000 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /app/assets/javascripts/ide/utils.js | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) | |
download | gitlab-ce-48aff82709769b098321c738f3444b9bdaa694c6.tar.gz |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'app/assets/javascripts/ide/utils.js')
-rw-r--r-- | app/assets/javascripts/ide/utils.js | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/app/assets/javascripts/ide/utils.js b/app/assets/javascripts/ide/utils.js index cde53e1ef00..4cf4f5e1d81 100644 --- a/app/assets/javascripts/ide/utils.js +++ b/app/assets/javascripts/ide/utils.js @@ -1,6 +1,7 @@ import { languages } from 'monaco-editor'; import { flatten, isString } from 'lodash'; import { SIDE_LEFT, SIDE_RIGHT } from './constants'; +import { performanceMarkAndMeasure } from '~/performance_utils'; const toLowerCase = x => x.toLowerCase(); @@ -42,16 +43,17 @@ const KNOWN_TYPES = [ }, ]; -export function isTextFile({ name, content, mimeType = '' }) { +export function isTextFile({ name, raw, content, mimeType = '' }) { const knownType = KNOWN_TYPES.find(type => type.isMatch(mimeType, name)); - if (knownType) return knownType.isText; // does the string contain ascii characters only (ranges from space to tilde, tabs and new lines) const asciiRegex = /^[ -~\t\n\r]+$/; + const fileContents = raw || content; + // for unknown types, determine the type by evaluating the file contents - return isString(content) && (content === '' || asciiRegex.test(content)); + return isString(fileContents) && (fileContents === '' || asciiRegex.test(fileContents)); } export const createPathWithExt = p => { @@ -137,3 +139,49 @@ export function readFileAsDataURL(file) { export function getFileEOL(content = '') { return content.includes('\r\n') ? 'CRLF' : 'LF'; } + +/** + * Adds or increments the numeric suffix to a filename/branch name. + * Retains underscore or dash before the numeric suffix if it already exists. + * + * Examples: + * hello -> hello-1 + * hello-2425 -> hello-2425 + * hello.md -> hello-1.md + * hello_2.md -> hello_3.md + * hello_ -> hello_1 + * master-patch-22432 -> master-patch-22433 + * patch_332 -> patch_333 + * + * @param {string} filename File name or branch name + * @param {number} [randomize] Should randomize the numeric suffix instead of auto-incrementing? + */ +export function addNumericSuffix(filename, randomize = false) { + return filename.replace(/([ _-]?)(\d*)(\..+?$|$)/, (_, before, number, after) => { + const n = randomize + ? Math.random() + .toString() + .substring(2, 7) + .slice(-5) + : Number(number) + 1; + return `${before || '-'}${n}${after}`; + }); +} + +export const measurePerformance = ( + mark, + measureName, + measureStart = undefined, + measureEnd = mark, +) => { + performanceMarkAndMeasure({ + mark, + measures: [ + { + name: measureName, + start: measureStart, + end: measureEnd, + }, + ], + }); +}; |