Skip to content

Commit

Permalink
allow isMatchingPattern to require exact string match
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Nov 15, 2022
1 parent 966098b commit 44df6ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 14 additions & 6 deletions packages/utils/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,29 @@ export function safeJoin(input: any[], delimiter?: string): string {
}

/**
* Checks if the value matches a regex or includes the string
* @param value The string value to be checked against
* @param pattern Either a regex or a string that must be contained in value
* Checks if the given value matches a regex or string
*
* @param value The string to test
* @param pattern Either a regex or a string against which `value` will be matched
* @param requireExactStringMatch If true, `value` must match `pattern` exactly. If false, `value` will match
* `pattern` if it contains `pattern`. Only applies to string-type patterns.
*/
export function isMatchingPattern(value: string, pattern: RegExp | string): boolean {
export function isMatchingPattern(
value: string,
pattern: RegExp | string,
requireExactStringMatch: boolean = false,
): boolean {
if (!isString(value)) {
return false;
}

if (isRegExp(pattern)) {
return pattern.test(value);
}
if (typeof pattern === 'string') {
return value.indexOf(pattern) !== -1;
if (isString(pattern)) {
return requireExactStringMatch ? value === pattern : value.includes(pattern);
}

return false;
}

Expand Down
20 changes: 19 additions & 1 deletion packages/utils/test/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ describe('truncate()', () => {
});

describe('isMatchingPattern()', () => {
test('match using string substring', () => {
test('match using string substring if `requireExactStringMatch` not given', () => {
expect(isMatchingPattern('foobar', 'foobar')).toEqual(true);
expect(isMatchingPattern('foobar', 'foo')).toEqual(true);
expect(isMatchingPattern('foobar', 'bar')).toEqual(true);
expect(isMatchingPattern('foobar', 'nope')).toEqual(false);
});

test('match using string substring if `requireExactStringMatch` is `false`', () => {
expect(isMatchingPattern('foobar', 'foobar', false)).toEqual(true);
expect(isMatchingPattern('foobar', 'foo', false)).toEqual(true);
expect(isMatchingPattern('foobar', 'bar', false)).toEqual(true);
expect(isMatchingPattern('foobar', 'nope', false)).toEqual(false);
});

test('match using exact string match if `requireExactStringMatch` is `true`', () => {
expect(isMatchingPattern('foobar', 'foobar', true)).toEqual(true);
expect(isMatchingPattern('foobar', 'foo', true)).toEqual(false);
expect(isMatchingPattern('foobar', 'nope', true)).toEqual(false);
});

test('matches when `value` constains `pattern` but not vice-versa', () => {
expect(isMatchingPattern('foobar', 'foo')).toEqual(true);
expect(isMatchingPattern('foobar', 'foobarbaz')).toEqual(false);
});

test('match using regexp test', () => {
expect(isMatchingPattern('foobar', /^foo/)).toEqual(true);
expect(isMatchingPattern('foobar', /foo/)).toEqual(true);
Expand Down

0 comments on commit 44df6ba

Please sign in to comment.