removeDuplicates function

Parser removeDuplicates (Parser parser)

Returns a copy of parser with all duplicates parsers collapsed.

Implementation

Parser removeDuplicates(Parser parser) {
  final uniques = <Parser>{};
  return transformParser(parser, (source) {
    final target = uniques.firstWhere((each) {
      return source != each && source.isEqualTo(each);
    }, orElse: () => null);
    if (target == null) {
      uniques.add(source);
      return source;
    } else {
      return target;
    }
  });
}