diff --git a/packages/utils/src/string.ts b/packages/utils/src/string.ts index 6178cbc14bd4..90b76b6f4621 100644 --- a/packages/utils/src/string.ts +++ b/packages/utils/src/string.ts @@ -110,6 +110,24 @@ export function isMatchingPattern( return false; } +/** + * Test the given string against an array of strings and regexes. By default, string matching is done on a + * substring-inclusion basis rather than a strict equality basis + * + * @param testString The string to test + * @param patterns The patterns against which to test the string + * @param requireExactStringMatch If true, `testString` must match one of the given string patterns exactly in order to + * count. If false, `testString` will match a string pattern if it contains that pattern. + * @returns + */ +export function stringMatchesSomePattern( + testString: string, + patterns: Array = [], + requireExactStringMatch: boolean = false, +): boolean { + return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch)); +} + /** * Given a string, escape characters which have meaning in the regex grammar, such that the result is safe to feed to * `new RegExp()`. diff --git a/packages/utils/test/string.test.ts b/packages/utils/test/string.test.ts index a0971109de6d..bb49a7833a11 100644 --- a/packages/utils/test/string.test.ts +++ b/packages/utils/test/string.test.ts @@ -1,4 +1,4 @@ -import { isMatchingPattern, truncate } from '../src/string'; +import { isMatchingPattern, stringMatchesSomePattern, truncate } from '../src/string'; describe('truncate()', () => { test('it works as expected', () => { @@ -63,3 +63,48 @@ describe('isMatchingPattern()', () => { expect(isMatchingPattern([] as any, 'foo')).toEqual(false); }); }); + +describe('stringMatchesSomePattern()', () => { + test('match using string substring if `requireExactStringMatch` not given', () => { + expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('foobar', ['baz', 'nope'])).toEqual(false); + }); + + test('match using string substring if `requireExactStringMatch` is `false`', () => { + expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], false)).toEqual(true); + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], false)).toEqual(true); + expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], false)).toEqual(false); + }); + + test('match using exact string match if `requireExactStringMatch` is `true`', () => { + expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], true)).toEqual(true); + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], true)).toEqual(false); + expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], true)).toEqual(false); + }); + + test('matches when `testString` constains a pattern but not vice-versa', () => { + expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('foobar', ['foobarbaz', 'nope'])).toEqual(false); + }); + + test('match using regexp test', () => { + expect(stringMatchesSomePattern('foobar', [/^foo/, 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('foobar', [/foo/, 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('foobar', [/b.{1}r/, 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('foobar', [/^foo$/, 'nope'])).toEqual(false); + }); + + test('should match empty pattern as true', () => { + expect(stringMatchesSomePattern('foo', ['', 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('bar', ['', 'nope'])).toEqual(true); + expect(stringMatchesSomePattern('', ['', 'nope'])).toEqual(true); + }); + + test('should bail out with false when given non-string value', () => { + expect(stringMatchesSomePattern(null as any, ['foo', 'nope'])).toEqual(false); + expect(stringMatchesSomePattern(undefined as any, ['foo', 'nope'])).toEqual(false); + expect(stringMatchesSomePattern({} as any, ['foo', 'nope'])).toEqual(false); + expect(stringMatchesSomePattern([] as any, ['foo', 'nope'])).toEqual(false); + }); +});