parse static method

PathInfo parse(
  1. String source, {
  2. bool validator = true,
})

Retrieves path information, including pairs of segments and an ending.

Implementation

static PathInfo parse(String source, {bool validator = true}) {
  final path = source.startsWith("/") ? source.substring(1) : source;
  final isValid =
      validator ? path.isNotEmpty && RegExp(_pathRegs).hasMatch(path) : true;
  if (isValid) {
    var segments = path.split("/");
    int length = segments.length;
    String? end = length.isOdd ? segments.last : null;
    List<String> x = [];
    List<String> y = [];
    List.generate(length.isOdd ? length - 1 : length, (i) {
      i.isEven ? x.add(segments[i]) : y.add(segments[i]);
    });
    return PathInfo(
      source: source,
      ending: end,
      collections: x,
      documents: y,
      pairs: List.generate(x.length, (index) {
        return PathPair(x[index], y[index]);
      }),
    );
  }
  return PathInfo(source: source, invalid: true);
}