diff --git a/src/stringSimilarity.js b/src/stringSimilarity.js index b5660a7..e8a2ccb 100644 --- a/src/stringSimilarity.js +++ b/src/stringSimilarity.js @@ -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); }; diff --git a/src/stringSimilarity.test.js b/src/stringSimilarity.test.js index 631a8de..5883b12 100644 --- a/src/stringSimilarity.test.js +++ b/src/stringSimilarity.test.js @@ -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); + }); });