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

feat request: string pattern support #82

Open
ridvanaltun opened this issue Aug 8, 2024 · 1 comment
Open

feat request: string pattern support #82

ridvanaltun opened this issue Aug 8, 2024 · 1 comment

Comments

@ridvanaltun
Copy link

Hey, I did something like this to support string patterns:

import React from 'react';

import Autolink, {AutolinkProps, CustomMatcher} from 'react-native-autolink';

const escapeRegExp = (value: string) => {
  return value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

interface CustomMatcherInstance extends Omit<CustomMatcher, 'pattern'> {
  pattern: RegExp | string;
}

interface Props extends Omit<AutolinkProps, 'matchers'> {
  matchers: CustomMatcherInstance[];
}

const AutolinkInstance = ({matchers, ...props}: Props) => {
  return (
    <Autolink
      matchers={matchers.map(matcher => ({
        ...matcher,
        pattern:
          typeof matcher.pattern === 'string'
            ? new RegExp(escapeRegExp(matcher.pattern))
            : matcher.pattern,
      }))}
      {...props}
    />
  );
};

export default AutolinkInstance;

My aim increase the usability.
I don't know is there any issue with it but it worked so far.

What do you think? @joshswan

@joshswan
Copy link
Owner

joshswan commented Aug 9, 2024

My initial reaction is that I like the idea of a createMatcher (or similar) utility that helps create custom matchers and can support using string patterns. But it should probably also make it easy to create capturing groups, similar in concept to path-to-regexp.

Needing to rebuild the regex on every render is something I'd like to avoid, which is why I think it should be separate from the component itself.

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

No branches or pull requests

2 participants