Skip to content

Commit

Permalink
fix: update getValueMatches to handle back-to-back separators
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Sep 11, 2020
1 parent f0e5737 commit 3c94c7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 12 additions & 9 deletions src/web/isDynamic.ts
Expand Up @@ -182,12 +182,14 @@ export const getValueMatches = (
addMatchToList();
} else if (tokenType === 'static' && lastTokenType === 'dynamic') {
// When we start a new static block after a dynamic block, reset the current
// block string, and potentially add the previous split character to it.
const lastCharOfPreviousBlock = value[currentPosition - 1];
if (SPLIT_CHARACTERS.includes(lastCharOfPreviousBlock)) {
currentStaticBlock = lastCharOfPreviousBlock;
} else {
currentStaticBlock = '';
// block string, and potentially add all preceding split characters to it.
currentStaticBlock = '';
let backwardPosition = currentPosition - 1;
let previousCharacter = value[backwardPosition];
while (SPLIT_CHARACTERS.includes(previousCharacter)) {
if (tokenType === 'static') currentStaticBlock = previousCharacter + currentStaticBlock;
backwardPosition -= 1;
previousCharacter = value[backwardPosition];
}
}

Expand All @@ -201,11 +203,12 @@ export const getValueMatches = (
// Keep track of where we are in the original value string
currentPosition += token.length;

// Add back in the split-by character
const nextCharacter = value[currentPosition];
if (SPLIT_CHARACTERS.includes(nextCharacter)) {
// Add back in any split-by characters
let nextCharacter = value[currentPosition];
while (SPLIT_CHARACTERS.includes(nextCharacter)) {
if (tokenType === 'static') currentStaticBlock += nextCharacter;
currentPosition += 1;
nextCharacter = value[currentPosition];
}

lastTokenType = tokenType;
Expand Down
8 changes: 4 additions & 4 deletions test/web/isDynamic.test.ts
Expand Up @@ -144,7 +144,7 @@ describe('getValueMatches', () => {
});

test('multiple matches', () => {
const matches = getValueMatches('input-25-red-bj84jd9-_-lastName');
const matches = getValueMatches('input-25-red--bj84jd9-_-lastName');
expect(matches).toMatchInlineSnapshot(`
Array [
Object {
Expand All @@ -154,15 +154,15 @@ describe('getValueMatches', () => {
"type": "startsWith",
},
Object {
"match": "-red-",
"match": "-red--",
"operator": "*=",
"startPosition": 8,
"type": "contains",
},
Object {
"match": "-lastName",
"match": "-_-lastName",
"operator": "$=",
"startPosition": 20,
"startPosition": 21,
"type": "endsWith",
},
]
Expand Down

0 comments on commit 3c94c7f

Please sign in to comment.