joinPath function

String joinPath(
  1. String dir,
  2. String file
)

Copied from patching (Repo: https://github.com/huanguan1978/patching) Methods: joinPath, extname, fnv1a64Hex Update: Replace the implementation below with latest code from repo. Joins a directory path dir and a file name or relative path file, returning a normalized file path suitable for the current operating system.

Design Motivation

Designed specifically for lightweight file logging and path concatenation. Eliminates the external package:path dependency in simple path joining scenarios, keeping this package lean and free of unnecessary dependencies.

Built on top of dart:io's Platform.pathSeparator, providing:

  • Smart Delimiter Handling: Automatically detects whether dir already ends with a slash (/ or \) to avoid duplicate separators (e.g. dir//file).
  • Safe Boundary Behavior: Returns file directly when dir is empty ("") or current directory ("."), avoiding redundant prefixes.
  • Platform Compatibility: Uses / on POSIX systems (Linux, macOS, Android, iOS) and \ on Windows.

Examples

// 1. Relative path joining
joinPath('logs', '2026-11-05.log');
// Linux / macOS: 'logs/2026-11-05.log'
// Windows:       'logs\2026-11-05.log'

// 2. Trailing separator already present (preserved without duplicates)
joinPath('logs/', '2026-11-05.log'); // Returns: 'logs/2026-11-05.log'
joinPath(r'logs\', '2026-11-05.log'); // Returns: r'logs\2026-11-05.log'

// 3. Current or empty directory
joinPath('.', 'app.log'); // Returns: 'app.log'
joinPath('', 'app.log');  // Returns: 'app.log'

// 4. Absolute paths
joinPath('/var/log', 'app.log');  // POSIX: '/var/log/app.log'
joinPath(r'C:\logs', 'app.log');  // Windows: r'C:\logs\app.log'

Implementation

// --- Implementation ---

/// Joins a directory path [dir] and a file name or relative path [file],
/// returning a normalized file path suitable for the current operating system.
///
/// ## Design Motivation
/// Designed specifically for lightweight file logging and path concatenation.
/// Eliminates the external `package:path` dependency in simple path joining
/// scenarios, keeping this package lean and free of unnecessary dependencies.
///
/// Built on top of `dart:io`'s [Platform.pathSeparator], providing:
/// - **Smart Delimiter Handling**: Automatically detects whether [dir] already ends
///   with a slash (`/` or `\`) to avoid duplicate separators (e.g. `dir//file`).
/// - **Safe Boundary Behavior**: Returns [file] directly when [dir] is empty (`""`)
///   or current directory (`"."`), avoiding redundant prefixes.
/// - **Platform Compatibility**: Uses `/` on POSIX systems (Linux, macOS, Android, iOS)
///   and `\` on Windows.
///
/// ## Examples
/// ```dart
/// // 1. Relative path joining
/// joinPath('logs', '2026-11-05.log');
/// // Linux / macOS: 'logs/2026-11-05.log'
/// // Windows:       'logs\2026-11-05.log'
///
/// // 2. Trailing separator already present (preserved without duplicates)
/// joinPath('logs/', '2026-11-05.log'); // Returns: 'logs/2026-11-05.log'
/// joinPath(r'logs\', '2026-11-05.log'); // Returns: r'logs\2026-11-05.log'
///
/// // 3. Current or empty directory
/// joinPath('.', 'app.log'); // Returns: 'app.log'
/// joinPath('', 'app.log');  // Returns: 'app.log'
///
/// // 4. Absolute paths
/// joinPath('/var/log', 'app.log');  // POSIX: '/var/log/app.log'
/// joinPath(r'C:\logs', 'app.log');  // Windows: r'C:\logs\app.log'
/// ```
String joinPath(String dir, String file) {
  if (dir.isEmpty || dir == '.') return file;
  final last = dir[dir.length - 1];
  if (last == '/' || last == r'\') {
    return '$dir$file';
  }
  return '$dir${Platform.pathSeparator}$file';
}