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

String.splitMap #40072

Open
Hixie opened this issue Jan 13, 2020 · 2 comments · May be fixed by #59686
Open

String.splitMap #40072

Hixie opened this issue Jan 13, 2020 · 2 comments · May be fixed by #59686
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented Jan 13, 2020

There's a String.splitMapJoin method, but when turning a string into a bunch of widgets it would be useful to have a String.splitMap method.

Iterable<T> splitMap<T>(Pattern pattern, {
  T onMatch(Match match),
  T onNonMatch(String nonMatch),
});

The existing splitMapJoin method is then implementable as:

String splitMapJoin(Pattern pattern, {
  String onMatch(Match match),
  String onNonMatch(String nonMatch),
}) => splitMap<String>(pattern, onMatch: onMatch, onNonMatch: onNonMatch).join('');

The splitMap concept would be very useful when, e.g., replacing URLs with clickable links in widget build methods (where you have to turn the incoming string into a list of TextSpans).

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug labels Jan 13, 2020
@lrhn
Copy link
Member

lrhn commented Jan 13, 2020

Interesting idea.
It does make sense as a kind of general string parser (split around patterns, handle matches and non-matches individually) where you can choose what to do with each.

@simonhoward-2
Copy link

simonhoward-2 commented Oct 3, 2023

Second this request. Given there is already splitMapJoin, it seems logical to have the sub functionality available too.
Currently this has to be achieved with something as follows:

var split = text
      .splitMapJoin(
        RegExp(r'\[url=.*\]'),
        onMatch: (m) => '#${m[0]}#',
        onNonMatch: (m) => m,
      )
      .split('#');

drxddy added a commit to drxddy/sdk that referenced this issue Dec 8, 2024
Introduced a new public method splitMap to convert image into a generic iterable,
inspired by splitMapJoin, this new method uses the same parameters and returns Iterable<T>

Closes dart-lang#40072
drxddy added a commit to drxddy/sdk that referenced this issue Dec 8, 2024
Introduced a new public method splitMap to convert a string into a generic iterable,
inspired by splitMapJoin, this new method uses the same parameters and returns Iterable<T>

Closes dart-lang#40072
@drxddy drxddy linked a pull request Dec 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants