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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Natural index numbers on callback function #64

Open
hallaji opened this issue Jul 19, 2020 · 2 comments
Open

Natural index numbers on callback function #64

hallaji opened this issue Jul 19, 2020 · 2 comments

Comments

@hallaji
Copy link

hallaji commented Jul 19, 2020

Hi there 馃憢

I have a situation where I'd like to replace the matched regex with my own array of nodes respectively. It would be great receiving natural index numbers 0, 1, 2 on callback instead of odd ones.

const nodes: React.ReactNodeArray

// [
//   <span key='something'>label</span>,
//   <h1 key='otherthing'>heading</h1>
// ]

reactStringReplace('These are my {} and {} and {} values.', /({})/g, (match, i) => {
  // odd index numbers
  return nodes[(i - 1) / 2]
})
@iansinnott
Copy link
Owner

Hm, that would prob be more intuitive since no one needs to know how the iteration is taking place from the outside. Candidate for a v2 since that could potentially break functionality people are using, even if it wasn't intended functionality.

In the meantime for anyone else needing this functionality I'd suggest the more verbose, less elegant counter approach.

let count = -1;
reactStringReplace('These are my {} and {} and {} values.', /({})/g, (match, _) => {
  count++;
  return nodes[count]
})

@Nitue
Copy link

Nitue commented Jul 13, 2022

I suggest adding a note to README.md about the returned index values for now. In my opinion, the currently returned values are quite unexpected.

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

3 participants