normalizePath method

String normalizePath (String path)

Resolves '.' and '..' in the path

Implementation

String normalizePath(String path) {
  final List<String> pathList = <String>[];
  final List<String> segments = path.split('/').reversed.toList();

  while (segments.isNotEmpty) {
    final String segment = segments.removeLast();
    switch (segment) {
      case '.':
        break;
      case '..':
        if (pathList.isEmpty || pathList.last == '..') {
          pathList.add(segment);
        } else {
          pathList.removeLast();
        }
        break;
      default:
        pathList.add(segment);
        break;
    }
  }

  return pathList.join('/');

// 3.7.1 20180718
//  contexts.Eval.prototype.normalizePath = function (path) {
//      var
//          segments = path.split('/').reverse(),
//          segment;
//
//      path = [];
//      while (segments.length !== 0) {
//          segment = segments.pop();
//          switch ( segment ) {
//              case '.':
//                  break;
//              case '..':
//                  if ((path.length === 0) || (path[path.length - 1] === '..')) {
//                      path.push( segment );
//                  } else {
//                      path.pop();
//                  }
//                  break;
//              default:
//                  path.push(segment);
//                  break;
//          }
//      }
//
//      return path.join('/');
//  };
}