Skip to content

Commit 069bb78

Browse files
committed
add stringMatchesSomePattern utility function
1 parent efb31a1 commit 069bb78

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

packages/utils/src/string.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ export function isMatchingPattern(
110110
return false;
111111
}
112112

113+
/**
114+
* Test the given string against an array of strings and regexes. By default, string matching is done on a
115+
* substring-inclusion basis rather than a strict equality basis
116+
*
117+
* @param testString The string to test
118+
* @param patterns The patterns against which to test the string
119+
* @param requireExactStringMatch If true, `testString` must match one of the given string patterns exactly in order to
120+
* count. If false, `testString` will match a string pattern if it contains that pattern.
121+
* @returns
122+
*/
123+
export function stringMatchesSomePattern(
124+
testString: string,
125+
patterns: Array<string | RegExp> = [],
126+
requireExactStringMatch: boolean = false,
127+
): boolean {
128+
return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch));
129+
}
130+
113131
/**
114132
* Given a string, escape characters which have meaning in the regex grammar, such that the result is safe to feed to
115133
* `new RegExp()`.

packages/utils/test/string.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isMatchingPattern, truncate } from '../src/string';
1+
import { isMatchingPattern, stringMatchesSomePattern, truncate } from '../src/string';
22

33
describe('truncate()', () => {
44
test('it works as expected', () => {
@@ -63,3 +63,48 @@ describe('isMatchingPattern()', () => {
6363
expect(isMatchingPattern([] as any, 'foo')).toEqual(false);
6464
});
6565
});
66+
67+
describe('stringMatchesSomePattern()', () => {
68+
test('match using string substring if `requireExactStringMatch` not given', () => {
69+
expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'])).toEqual(true);
70+
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true);
71+
expect(stringMatchesSomePattern('foobar', ['baz', 'nope'])).toEqual(false);
72+
});
73+
74+
test('match using string substring if `requireExactStringMatch` is `false`', () => {
75+
expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], false)).toEqual(true);
76+
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], false)).toEqual(true);
77+
expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], false)).toEqual(false);
78+
});
79+
80+
test('match using exact string match if `requireExactStringMatch` is `true`', () => {
81+
expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], true)).toEqual(true);
82+
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], true)).toEqual(false);
83+
expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], true)).toEqual(false);
84+
});
85+
86+
test('matches when `testString` constains a pattern but not vice-versa', () => {
87+
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true);
88+
expect(stringMatchesSomePattern('foobar', ['foobarbaz', 'nope'])).toEqual(false);
89+
});
90+
91+
test('match using regexp test', () => {
92+
expect(stringMatchesSomePattern('foobar', [/^foo/, 'nope'])).toEqual(true);
93+
expect(stringMatchesSomePattern('foobar', [/foo/, 'nope'])).toEqual(true);
94+
expect(stringMatchesSomePattern('foobar', [/b.{1}r/, 'nope'])).toEqual(true);
95+
expect(stringMatchesSomePattern('foobar', [/^foo$/, 'nope'])).toEqual(false);
96+
});
97+
98+
test('should match empty pattern as true', () => {
99+
expect(stringMatchesSomePattern('foo', ['', 'nope'])).toEqual(true);
100+
expect(stringMatchesSomePattern('bar', ['', 'nope'])).toEqual(true);
101+
expect(stringMatchesSomePattern('', ['', 'nope'])).toEqual(true);
102+
});
103+
104+
test('should bail out with false when given non-string value', () => {
105+
expect(stringMatchesSomePattern(null as any, ['foo', 'nope'])).toEqual(false);
106+
expect(stringMatchesSomePattern(undefined as any, ['foo', 'nope'])).toEqual(false);
107+
expect(stringMatchesSomePattern({} as any, ['foo', 'nope'])).toEqual(false);
108+
expect(stringMatchesSomePattern([] as any, ['foo', 'nope'])).toEqual(false);
109+
});
110+
});

0 commit comments

Comments
 (0)