Skip to content

Commit

Permalink
add stringMatchesSomePattern utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Nov 15, 2022
1 parent 44df6ba commit 9a721f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/utils/src/string.ts
Expand Up @@ -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<string | RegExp> = [],
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()`.
Expand Down
47 changes: 46 additions & 1 deletion 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', () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 9a721f9

Please sign in to comment.