From 5496a9a9281ae66f00488bbc9df6e1f9a5c9e777 Mon Sep 17 00:00:00 2001 From: Caleb Fritz Date: Mon, 8 Apr 2019 16:47:52 -0500 Subject: [PATCH 1/2] escape asterisks in target and match against escaped source --- src/stringSimilarity.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); }; From 8acccafdef7f70b6963d26102b71841766ac8fa4 Mon Sep 17 00:00:00 2001 From: Caleb Fritz Date: Mon, 8 Apr 2019 16:48:25 -0500 Subject: [PATCH 2/2] add necessary tests for fixed situations --- src/stringSimilarity.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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); + }); });