Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wildcarding not working properly against content with asterisks #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/stringSimilarity.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export default (source, target) => {
if (!source || (source || '') === (target || '')) {
return source === target;
}

const asteriskEscapedTarget = target && target.replace(new RegExp(escapeRegExp('*'), 'g'), '\\*');
const wildcardedSource = source
.replace(new RegExp(escapeRegExp('*'), 'g'), '\\*')
.replace(new RegExp(escapeRegExp(WILDCARD_MARKER_ESCAPED), 'g'), '*');

return matcher.isMatch(target, wildcardedSource);
return matcher.isMatch(asteriskEscapedTarget, wildcardedSource);
};
12 changes: 12 additions & 0 deletions src/stringSimilarity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ describe('stringSimilarity', () => {
it('should not match if the source and target are undefined and null', () => {
expect(stringSimilarity(null, 'something')).toBe(false);
});

it('should match a target with asterisks against a wildcarded source', () => {
expect(stringSimilarity(`Some ${WILDCARD} with asterisks ***`, 'Some text with asterisks ***')).toBe(true);
});

it('should not match target with asterisk within a char group against a wildcarded source without asterisk', () => {
expect(stringSimilarity(`This ${WILDCARD} a t*`, 'This is a test')).toBe(false);
});

it('should match a target against a source that wildcards the asterisks within the target', () => {
expect(stringSimilarity(`These are asterisks: ${WILDCARD}`, 'These are asterisks: ***')).toBe(true);
});
});