Skip to content

Commit

Permalink
feat: Extract phrases and emoticons [PT-188319600] [PT-188319636]
Browse files Browse the repository at this point in the history
This updates the plugin to pass the full trimmed text when pattern matching text and then changes the word boundary metacharacters in the pattern to be optional if the text begins/ends with non-word characters.

This also adds a missing escape of any regex characters in the free form text and changes spaces to match multiple spaces and expands quotes to "smart" quotes.
  • Loading branch information
dougmartin committed Oct 1, 2024
1 parent ec4ae4d commit eb47a62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/stores/feature_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export class FeatureStore {
if (iFeature.info.kind === 'search') {
const tDetails = iFeature.info.details as SearchDetails,
tFirstPart = namingAbbreviations[tDetails.where],
tMatch = tDetails.freeFormText.match( /\w+/),
tSecondPart = tDetails.freeFormText !== '' ? `"${tMatch ? tMatch[0] : ''}"` :
tSecondPart = tDetails.freeFormText !== '' ? `"${tDetails.freeFormText.trim()}"` :
tDetails.punctuation !== '' ? tDetails.punctuation :
tDetails.wordList && tDetails.wordList.datasetName !== '' ? tDetails.wordList.datasetName :
tDetails.what === 'any number' ? 'anyNumber' : ''
Expand Down
25 changes: 24 additions & 1 deletion src/stores/target_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,30 @@ export class TargetStore {
const option = (iNewFeature.info.details as SearchDetails).where;
const tBegins = option === featureDescriptors.containsOptions[2] ? '^' : '';
const tEnds = option === featureDescriptors.containsOptions[3] ? '$' : '';
const tParamString = `${tTargetAttr},"${tBegins}\\\\\\\\b${(iNewFeature.info.details as SearchDetails).freeFormText}\\\\\\\\b${tEnds}"`;
const text = (iNewFeature.info.details as SearchDetails).freeFormText.trim();
// note: the multiple slash escaping is due to all the layers between this code and the CODAP formula evaluator
const escapedText = text
.replace(/[.*+?^${}()|[\]\\]/g, '\\\\\\\\$&') // escape regex modifiers
.replace(/\s+/g, '\\\\\\\\s+') // allow multiple spaces between words
.replace(/['"“”‘’]/g, (match) => { // allow both regular and smart quotes to match each other
switch (match) {
case '"':
case '“':
case '”':
return `["“”]`;
case "'":
case '‘':
case '’':
return `['‘’]`;
default:
return match;
}
});
// don't add word boundaries when the user input starts/ends with non-word characters, like ! or , as that would fail matching
const wordBoundary = `\\\\\\\\b`;
const maybeStartingWordBoundary = /^\w/.test(text) ? wordBoundary : '';
const maybeEndingWordBoundary = /\w$/.test(text) ? wordBoundary : '';
const tParamString = `${tTargetAttr},"${tBegins}${maybeStartingWordBoundary}${escapedText}${maybeEndingWordBoundary}${tEnds}"`;
let tResult = '';
switch (option) {//['contain', 'not contain', 'start with', 'end with']
case featureDescriptors.containsOptions[0]: // contain
Expand Down

0 comments on commit eb47a62

Please sign in to comment.