Skip to content

Commit

Permalink
refactor: Move isUUID() to string utils (#27044)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 3, 2024
1 parent 60dda0d commit 441ddd5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/modules/platform/bitbucket/index.ts
Expand Up @@ -8,8 +8,9 @@ import * as git from '../../../util/git';
import * as hostRules from '../../../util/host-rules';
import { BitbucketHttp, setBaseUrl } from '../../../util/http/bitbucket';
import type { HttpOptions } from '../../../util/http/types';
import { isUUID, regEx } from '../../../util/regex';
import { regEx } from '../../../util/regex';
import { sanitize } from '../../../util/sanitize';
import { isUUID } from '../../../util/string';
import type {
BranchStatusConfig,
CreatePRConfig,
Expand Down
10 changes: 1 addition & 9 deletions lib/util/regex.spec.ts
@@ -1,6 +1,6 @@
import RE2 from 're2';
import { CONFIG_VALIDATION } from '../constants/error-messages';
import { configRegexPredicate, isUUID, regEx } from './regex';
import { configRegexPredicate, regEx } from './regex';

describe('util/regex', () => {
beforeEach(() => {
Expand Down Expand Up @@ -38,14 +38,6 @@ describe('util/regex', () => {
expect(regex.regEx('foo')).toBeInstanceOf(RegExp);
});

describe('isUUID', () => {
it('proper checks valid and invalid UUID strings', () => {
expect(isUUID('{90b6646d-1724-4a64-9fd9-539515fe94e9}')).toBe(true);
expect(isUUID('{90B6646D-1724-4A64-9FD9-539515FE94E9}')).toBe(true);
expect(isUUID('not-a-uuid')).toBe(false);
});
});

describe('configRegexPredicate', () => {
it('allows valid regex pattern', () => {
expect(configRegexPredicate('/hello/')).not.toBeNull();
Expand Down
8 changes: 0 additions & 8 deletions lib/util/regex.ts
Expand Up @@ -111,11 +111,3 @@ export function configRegexPredicate(
}
return null;
}

const UUIDRegex = regEx(
/^\{[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\}$/i,
);

export function isUUID(input: string): boolean {
return UUIDRegex.test(input);
}
10 changes: 9 additions & 1 deletion lib/util/string.spec.ts
@@ -1,4 +1,4 @@
import { coerceString, looseEquals, replaceAt } from './string';
import { coerceString, isUUID, looseEquals, replaceAt } from './string';

describe('util/string', () => {
describe('replaceAt', () => {
Expand Down Expand Up @@ -40,4 +40,12 @@ describe('util/string', () => {
expect(coerceString(null)).toBe('');
expect(coerceString(null, 'foo')).toBe('foo');
});

describe('isUUID', () => {
it('proper checks valid and invalid UUID strings', () => {
expect(isUUID('{90b6646d-1724-4a64-9fd9-539515fe94e9}')).toBe(true);
expect(isUUID('{90B6646D-1724-4A64-9FD9-539515FE94E9}')).toBe(true);
expect(isUUID('not-a-uuid')).toBe(false);
});
});
});
8 changes: 8 additions & 0 deletions lib/util/string.ts
Expand Up @@ -117,3 +117,11 @@ export function anyMatchRegexOrMinimatch(
): boolean | null {
return patterns.some((pattern) => matchRegexOrMinimatch(input, pattern));
}

const UUIDRegex = regEx(
/^\{[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\}$/i,
);

export function isUUID(input: string): boolean {
return UUIDRegex.test(input);
}

0 comments on commit 441ddd5

Please sign in to comment.