Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for running a callback for non-matches #23

Open
pintocarlos opened this issue Aug 2, 2017 · 7 comments
Open

Support for running a callback for non-matches #23

pintocarlos opened this issue Aug 2, 2017 · 7 comments

Comments

@pintocarlos
Copy link

A 4th parameter as callback to process non-match items would be useful.

For instance, in ReactNative, plain text cannot be outside of <Text> components. The non matches need to be wrapped in a component as well. A callback for these would come quite handy.

Example:

const matchCallback = (match, i) => (<Text key={i} style={styles.highlight}>{match}</Text>);
const nonMatchCallback = (nonmatch, i) => (<Text key={i}>{nonmatch}</Text>);
const components = reactStringReplace(input, regex, matchCallback, nonMatchCallback);
@iansinnott
Copy link
Owner

Ah good point, I like this idea. I think it deserves a bit of thought though. One issue would be that this would mean we can't do #6. Another potential issue is that this sort of API seems pretty uncommon (have any examples of libraries that do something similar?).

I'm fully onboard with being able to control non-match output. However, a fourth arg would work but it feels like a quick fix rather than a considered solution. Interested to know what you think @pintocarlos

@pintocarlos
Copy link
Author

pintocarlos commented Aug 2, 2017

Thanks for the reply @iansinnott
Regarding the use for the API, it is entirely subjective. My example posted above is a valid real world example. In general, I can think of general styling purposes where you'd like to style the non-matches in some way and the matches in another way instead of running the input through reactStringReplace twice with an inverse regex of the first pass through.

With regards to #6, could it coexist with this issue by using a negative lookahead of the input regex? Meaning, the negative regex of the input would return the non-matches.

Now about the 4th param callback for non-matches, it seemed to me the most natural place to put this callback, but I am open to other suggestions that could also work well.

@iansinnott
Copy link
Owner

What about just adding a second function to this library? The concept of collecting results based on both outcomes of a predicate reminds me of a partition function. Maybe something like stringPartition which would take args:

stringPartition(input, regex, match, nonMatch)

This way it's not a breaking change and doesn't overload the functionality of the replace function any more.

@pintocarlos
Copy link
Author

I see your point. Makes sense. reactStringPartition(input, regex, match, nonMatch) ?

@iansinnott
Copy link
Owner

iansinnott commented Aug 2, 2017 via email

@ghost
Copy link

ghost commented Dec 9, 2020

any updates on this guys?

@iansinnott
Copy link
Owner

any updates on this guys?

Nope, but I'd be happy to merge a PR if anyone wants to open one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants