normalize static method

Map normalize(
  1. dynamic path, {
  2. List<String?>? keys,
})

Implementation

static Map normalize(
  dynamic path, {
  List<String?>? keys,
}) {
  String stringPath = path;
  keys ??= [];
  if (path is RegExp) {
    return {'regexp': path, 'keys': keys};
  } else if (path is List) {
    stringPath = '(${path.join('|')})';
  }

  stringPath += '/?';

  stringPath =
      stringPath.replaceAllMapped(RegExp(r'(\.)?:(\w+)(\?)?'), (placeholder) {
    var replace = StringBuffer('(?:');

    if (placeholder[1] != null) {
      replace.write('.');
    }

    replace.write('([\\w%+-._~!\$&\'()*,;=:@]+))');

    if (placeholder[3] != null) {
      replace.write('?');
    }

    keys!.add(placeholder[2]);

    return replace.toString();
  }).replaceAll('//', '/');

  return {'regexp': RegExp('^$stringPath\$'), 'keys': keys};
}