Skip to content

Commit efb31a1

Browse files
committed
allow isMatchingPattern to require exact string match
1 parent 23c19bf commit efb31a1

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

packages/utils/src/string.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,29 @@ export function safeJoin(input: any[], delimiter?: string): string {
8484
}
8585

8686
/**
87-
* Checks if the value matches a regex or includes the string
88-
* @param value The string value to be checked against
89-
* @param pattern Either a regex or a string that must be contained in value
87+
* Checks if the given value matches a regex or string
88+
*
89+
* @param value The string to test
90+
* @param pattern Either a regex or a string against which `value` will be matched
91+
* @param requireExactStringMatch If true, `value` must match `pattern` exactly. If false, `value` will match
92+
* `pattern` if it contains `pattern`. Only applies to string-type patterns.
9093
*/
91-
export function isMatchingPattern(value: string, pattern: RegExp | string): boolean {
94+
export function isMatchingPattern(
95+
value: string,
96+
pattern: RegExp | string,
97+
requireExactStringMatch: boolean = false,
98+
): boolean {
9299
if (!isString(value)) {
93100
return false;
94101
}
95102

96103
if (isRegExp(pattern)) {
97104
return pattern.test(value);
98105
}
99-
if (typeof pattern === 'string') {
100-
return value.indexOf(pattern) !== -1;
106+
if (isString(pattern)) {
107+
return requireExactStringMatch ? value === pattern : value.includes(pattern);
101108
}
109+
102110
return false;
103111
}
104112

packages/utils/test/string.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ describe('truncate()', () => {
1818
});
1919

2020
describe('isMatchingPattern()', () => {
21-
test('match using string substring', () => {
21+
test('match using string substring if `requireExactStringMatch` not given', () => {
2222
expect(isMatchingPattern('foobar', 'foobar')).toEqual(true);
2323
expect(isMatchingPattern('foobar', 'foo')).toEqual(true);
2424
expect(isMatchingPattern('foobar', 'bar')).toEqual(true);
2525
expect(isMatchingPattern('foobar', 'nope')).toEqual(false);
2626
});
2727

28+
test('match using string substring if `requireExactStringMatch` is `false`', () => {
29+
expect(isMatchingPattern('foobar', 'foobar', false)).toEqual(true);
30+
expect(isMatchingPattern('foobar', 'foo', false)).toEqual(true);
31+
expect(isMatchingPattern('foobar', 'bar', false)).toEqual(true);
32+
expect(isMatchingPattern('foobar', 'nope', false)).toEqual(false);
33+
});
34+
35+
test('match using exact string match if `requireExactStringMatch` is `true`', () => {
36+
expect(isMatchingPattern('foobar', 'foobar', true)).toEqual(true);
37+
expect(isMatchingPattern('foobar', 'foo', true)).toEqual(false);
38+
expect(isMatchingPattern('foobar', 'nope', true)).toEqual(false);
39+
});
40+
41+
test('matches when `value` constains `pattern` but not vice-versa', () => {
42+
expect(isMatchingPattern('foobar', 'foo')).toEqual(true);
43+
expect(isMatchingPattern('foobar', 'foobarbaz')).toEqual(false);
44+
});
45+
2846
test('match using regexp test', () => {
2947
expect(isMatchingPattern('foobar', /^foo/)).toEqual(true);
3048
expect(isMatchingPattern('foobar', /foo/)).toEqual(true);

0 commit comments

Comments
 (0)