BREAKING_CHANGE(iso): rename pathJoin to join for better API consistency

This commit is contained in:
Juergen Kunz
2025-07-28 22:31:00 +00:00
parent ff1e6242a3
commit 047d7fcab9
5 changed files with 43 additions and 35 deletions

View File

@@ -1,5 +1,13 @@
# Changelog
## [6.0.0] - 2025-01-28
### BREAKING CHANGES
- Renamed `pathJoin()` to `join()` for better API consistency and brevity
### Changed
- Updated all references and tests to use the new `join()` function name
## [5.1.0] - 2025-01-28
### Added

View File

@@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartpath",
"version": "5.1.0",
"version": "6.0.0",
"private": false,
"description": "A library offering smart ways to handle file and directory paths.",
"exports": {

View File

@@ -25,7 +25,7 @@ For cross-platform path operations that work in any JavaScript environment (Node
import * as isoPath from '@push.rocks/smartpath/iso';
// Join paths with automatic platform detection
const joinedPath = isoPath.pathJoin('/home/user', 'documents', 'file.txt');
const joinedPath = isoPath.join('/home/user', 'documents', 'file.txt');
// Unix: /home/user/documents/file.txt
// Windows: C:\Users\documents\file.txt

View File

@@ -3,46 +3,46 @@ import * as smartpath from '../ts_iso/index.js';
tap.test('pathJoin - should join path segments correctly', async () => {
// Basic path joining
expect(smartpath.pathJoin('path', 'to', 'file.txt')).toEqual('path/to/file.txt');
expect(smartpath.pathJoin('/path', 'to', 'file.txt')).toEqual('/path/to/file.txt');
expect(smartpath.join('path', 'to', 'file.txt')).toEqual('path/to/file.txt');
expect(smartpath.join('/path', 'to', 'file.txt')).toEqual('/path/to/file.txt');
// Windows paths
expect(smartpath.pathJoin('C:', 'Users', 'test')).toEqual('C:\\Users\\test');
expect(smartpath.pathJoin('C:\\Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.join('C:', 'Users', 'test')).toEqual('C:\\Users\\test');
expect(smartpath.join('C:\\Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
// Empty segments
expect(smartpath.pathJoin('path', '', 'file.txt')).toEqual('path/file.txt');
expect(smartpath.pathJoin('', 'path', 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('path', '', 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('', 'path', 'file.txt')).toEqual('path/file.txt');
// No segments
expect(smartpath.pathJoin()).toEqual('');
expect(smartpath.pathJoin('')).toEqual('');
expect(smartpath.join()).toEqual('');
expect(smartpath.join('')).toEqual('');
// Single segment
expect(smartpath.pathJoin('path')).toEqual('path');
expect(smartpath.pathJoin('/path')).toEqual('/path');
expect(smartpath.join('path')).toEqual('path');
expect(smartpath.join('/path')).toEqual('/path');
// Multiple separators
expect(smartpath.pathJoin('path/', '/to/', '/file.txt')).toEqual('path/to/file.txt');
expect(smartpath.pathJoin('path\\\\', '\\\\to\\\\', '\\\\file.txt')).toEqual('path\\to\\file.txt');
expect(smartpath.join('path/', '/to/', '/file.txt')).toEqual('path/to/file.txt');
expect(smartpath.join('path\\\\', '\\\\to\\\\', '\\\\file.txt')).toEqual('path\\to\\file.txt');
// Root paths
expect(smartpath.pathJoin('/')).toEqual('/');
expect(smartpath.pathJoin('/', 'path')).toEqual('/path');
expect(smartpath.join('/')).toEqual('/');
expect(smartpath.join('/', 'path')).toEqual('/path');
});
tap.test('pathJoin - should handle file:// URLs', async () => {
// Unix file URLs
expect(smartpath.pathJoin('file:///home/user', 'documents', 'file.txt')).toEqual('/home/user/documents/file.txt');
expect(smartpath.pathJoin('file:///home/user/', 'documents')).toEqual('/home/user/documents');
expect(smartpath.join('file:///home/user', 'documents', 'file.txt')).toEqual('/home/user/documents/file.txt');
expect(smartpath.join('file:///home/user/', 'documents')).toEqual('/home/user/documents');
// Windows file URLs
expect(smartpath.pathJoin('file:///C:/Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.pathJoin('file:///D:/Projects/', 'app')).toEqual('D:\\Projects\\app');
expect(smartpath.join('file:///C:/Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.join('file:///D:/Projects/', 'app')).toEqual('D:\\Projects\\app');
// Mixed file URL and path
expect(smartpath.pathJoin('file:///home/user', '../test')).toEqual('/home/user/../test');
expect(smartpath.pathJoin('documents', 'file:///home/user/file.txt')).toEqual('documents/home/user/file.txt');
expect(smartpath.join('file:///home/user', '../test')).toEqual('/home/user/../test');
expect(smartpath.join('documents', 'file:///home/user/file.txt')).toEqual('documents/home/user/file.txt');
});
tap.test('fileUrlToPath - should convert file URLs to paths', async () => {
@@ -110,36 +110,36 @@ tap.test('dirname - should extract directory from paths and URLs', async () => {
tap.test('edge cases - should handle edge cases correctly', async () => {
// Non-string values filtered out
expect(smartpath.pathJoin('path', null as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.pathJoin('path', undefined as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.pathJoin('path', 123 as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('path', null as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('path', undefined as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('path', 123 as any, 'file.txt')).toEqual('path/file.txt');
// Very long paths
const longSegment = 'a'.repeat(100);
const result = smartpath.pathJoin(longSegment, longSegment, 'file.txt');
const result = smartpath.join(longSegment, longSegment, 'file.txt');
expect(result).toEqual(`${longSegment}/${longSegment}/file.txt`);
// Unicode characters
expect(smartpath.pathJoin('path', '文件夹', 'файл.txt')).toEqual('path/文件夹/файл.txt');
expect(smartpath.join('path', '文件夹', 'файл.txt')).toEqual('path/文件夹/файл.txt');
expect(smartpath.fileUrlToPath('file:///home/用户/文件.txt')).toEqual('/home/用户/文件.txt');
expect(smartpath.pathToFileUrl('/home/用户/文件.txt')).toEqual('file:///home/%E7%94%A8%E6%88%B7/%E6%96%87%E4%BB%B6.txt');
// Backslashes in Unix-style paths (should be preserved)
expect(smartpath.pathJoin('/home/user', 'path\\with\\backslashes')).toEqual('/home/user/path\\with\\backslashes');
expect(smartpath.join('/home/user', 'path\\with\\backslashes')).toEqual('/home/user/path\\with\\backslashes');
});
tap.test('cross-platform behavior - should detect separators correctly', async () => {
// Should detect Windows paths and use backslashes
expect(smartpath.pathJoin('C:\\Users', 'test')).toEqual('C:\\Users\\test');
expect(smartpath.pathJoin('D:', 'Projects', 'app')).toEqual('D:\\Projects\\app');
expect(smartpath.join('C:\\Users', 'test')).toEqual('C:\\Users\\test');
expect(smartpath.join('D:', 'Projects', 'app')).toEqual('D:\\Projects\\app');
// Should detect Unix paths and use forward slashes
expect(smartpath.pathJoin('/home', 'user')).toEqual('/home/user');
expect(smartpath.pathJoin('/var', 'log', 'app.log')).toEqual('/var/log/app.log');
expect(smartpath.join('/home', 'user')).toEqual('/home/user');
expect(smartpath.join('/var', 'log', 'app.log')).toEqual('/var/log/app.log');
// Mixed paths - first segment determines separator
expect(smartpath.pathJoin('C:\\Users', 'test/file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.pathJoin('/home', 'user\\documents')).toEqual('/home/user\\documents');
expect(smartpath.join('C:\\Users', 'test/file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.join('/home', 'user\\documents')).toEqual('/home/user\\documents');
});
await tap.start();

View File

@@ -4,7 +4,7 @@
* @param segments - Path segments to join
* @returns Joined path string
*/
export function pathJoin(...segments: string[]): string {
export function join(...segments: string[]): string {
// Filter out empty strings and non-string values
const validSegments = segments.filter(segment =>
typeof segment === 'string' && segment.length > 0