joinPathWithRoot static method

String joinPathWithRoot(
  1. List<String> parts, {
  2. String separator = '/',
})

Implementation

static String joinPathWithRoot(List<String> parts, {String separator = '/'}) {
  if (parts.isEmpty) return '';

  final isAbsolute = RegExp(r'^[\\/]').hasMatch(parts.first);

  final normalized = parts
      .where((e) => e.trim().isNotEmpty)
      .map(
        (e) => e
            .replaceAll(RegExp(r'[\\/]'), separator)
            .replaceAll(
              RegExp(
                '^${RegExp.escape(separator)}+|${RegExp.escape(separator)}+\$',
              ),
              '',
            ),
      )
      .join(separator);

  return isAbsolute ? '$separator$normalized' : normalized;
}