Skip to content

Commit

Permalink
matchRecursive: make global non-sticky match with valueNames return a…
Browse files Browse the repository at this point in the history
…n empty array if no matches found
  • Loading branch information
slevithan committed Aug 3, 2021
1 parent bf480ec commit 6e1711e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/addons/matchrecursive.js
Expand Up @@ -238,7 +238,7 @@ export default (XRegExp) => {
}
}

if (global && !sticky && vN && vN[0] && str.length > lastOuterEnd) {
if (global && output.length > 0 && !sticky && vN && vN[0] && str.length > lastOuterEnd) {

This comment has been minimized.

Copy link
@dvargas92495

dvargas92495 Aug 6, 2021

Contributor

Isn't this a breaking change? I was previously relying on the text being returned when the output length was 0.

The workaround was simple on my end, just wanted to highlight that there may be others relying on this (was originally concerned that I broke something! haha)

This comment has been minimized.

Copy link
@slevithan

slevithan Aug 7, 2021

Author Owner

@dvargas92495 yes, it's a breaking change, but I was hoping it was too edge-casey to matter and for anyone to be relying on it. Clearly, I was wrong. There is definitely some logic to the old behavior, but when I wrote new tests and realized that every other case with no matches returned an empty array (valueNames without g, and no valueNames with and without g) I thought it would be better to handle this case the same way as other cases with no matches for consistency.

I'm not sure my breaking change is even an improvement, though, so I'm open to undoing this with a 5.1.1 hotfix. What do you think?

This comment has been minimized.

Copy link
@dvargas92495

dvargas92495 Aug 7, 2021

Contributor

I think consistency makes sense! I initially found it odd that they differed but then said "well this is what I want anyway" and continued. As a user of the library, my workaround is already live so it makes no difference to me.

I do see users (like myself) who would want just the between text array returned when there are no matches, so an opt in for that would be nice

This comment has been minimized.

Copy link
@moopmonster

moopmonster Oct 29, 2021

We faced this issue as well. A note on this breaking change in the documentation would be helpful!

output.push(row(vN[0], str.slice(lastOuterEnd), lastOuterEnd, str.length));
}

Expand Down
11 changes: 5 additions & 6 deletions tests/spec/s-addons-matchrecursive.js
Expand Up @@ -155,12 +155,11 @@ describe('XRegExp.matchRecursive addon:', function() {
valueNames: ['between', 'left', 'match', 'right']
})
).toEqual([]);
// TODO: Change behavior to match test (don't return `between` value with full string)
// expect(
// XRegExp.matchRecursive('.', '<', '>', 'g', {
// valueNames: ['between', 'left', 'match', 'right']
// })
// ).toEqual([]);
expect(
XRegExp.matchRecursive('.', '<', '>', 'g', {
valueNames: ['between', 'left', 'match', 'right']
})
).toEqual([]);
});

});
Expand Down

0 comments on commit 6e1711e

Please sign in to comment.