@igorskyflyer/upath
v3.1.0
Published
🎍 Provides a universal way of formatting file-paths in Unix-like and Windows operating systems as an alternative to the built-in path.normalize(). 🧬
Downloads
28
Maintainers
Readme
📃 Table of Contents
🤖 Features
- 🖥️ Cross‑platform paths - Works seamlessly on Windows and UNIX‑like systems
- 🔄 Auto‑slash handling - Normalizes
/and\based on OS or target platform - 📂 UNC path support - Detects and correctly formats network paths
- ➕ Optional trailing slash - Add it only when you need it
- ⚡ Cached regex - Faster repeated path splitting
- 🪞 Aliases for brevity -
u()forupath()convenience - 🎯 Force‑platform modes -
uw()for Windows,ux()for UNIX regardless of host - 🧹 Empty‑safe - Returns an empty String for invalid or empty input
- 🛠️ Single transform core - All functions share one clean, reusable logic
💡 TIP
WHY
uPath?Simple. Working with file paths inside
Strings in Windows + JavaScript is a hassle. By callingu(), the file path you provided is converted to your OS-preferred file path format, i.e. you can get away with writing "C:/Users/JohnDoe/" without having to worry about those backslashes that Windows uses. On the other hand, UNIX-like slashes in file paths are left as-is. An extra feature thatuPathprovides is redundant trailing slash removal.*
ℹ️ NOTE
✨ Since version
1.2.0support for UNC paths has been added, thanks to unc-path.
🕵🏼 Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/upathyarn add @igorskyflyer/upathnpm i @igorskyflyer/upath🤹🏼 API
upath() => returns a proper file path depending on the host OS.
ℹ️ NOTE
upath()has an alias, namedu()for your convenience.
Signature
upath(fsPath: string, addTrailingSlash: boolean = false): stringu(fsPath: string, addTrailingSlash: boolean = false): stringParameters
fsPath: string // a string that represents the path to process,
addTrailingSlash: boolean = false
// a boolean that represents whether a trailing slash should be added to the fsPath or notExample - Windows OS
import { u } from '@igorskyflyer/upath'
console.log(u('C:/')) // returns 'C:\\'
console.log(u('C:/WINDOWS//////')) // returns 'C:\\WINDOWS\\'
console.log(u('C:/Users', true)) // returns 'C:\\Users\\'
console.log(u('\\\\ComputerName\\SharedFolder')) // returns '\\\\ComputerName\\SharedFolder'
console.log(u('//ComputerName/SharedFolder')) // returns '\\\\ComputerName\\SharedFolder'Example - UNIX-like OS
import { u } from '@igorskyflyer/upath'
console.log(u('/mnt/')) // returns '/mnt/'
console.log(u('/usr/bin/////////')) // returns '/usr/bin/'
console.log(u('/usr/bin', true)) // returns '/usr/bin/'
console.log(u('//ComputerName/SharedFolder')) // returns '//ComputerName/SharedFolder'
console.log(u('\\\\ComputerName\\SharedFolder')) // returns '//ComputerName/SharedFolder'uw() => returns a proper file path for Windows operating system.
Signature
uw(fsPath: string, addTrailingSlash: boolean = false): stringParameters
fsPath: string // a string that represents the path to process,
addTrailingSlash: boolean = false
// a boolean that represents whether a trailing slash should be added to the fsPath or notExample - Any OS
import { uw } from '@igorskyflyer/upath'
console.log(uw('C:/WINDOWS/System32')) // returns 'C:\\WINDOWS\\System32'
console.log(uw('//ComputerName/SharedFolder')) // returns '\\\\ComputerName\\SharedFolder'ux() => returns a proper file path for UNIX-like operating systems.
Signature
ux(fsPath: string, addTrailingSlash: boolean = false): stringParameters
fsPath: string // a string that represents the path to process,
addTrailingSlash: boolean = false
// a boolean that represents whether a trailing slash should be added to the fsPath or notExample - Any OS
import { ux } from '@igorskyflyer/upath'
console.log(ux('/usr/local')) // returns '/usr/local'
console.log(ux('\\\\ComputerName\\SharedFolder'))
// returns '//ComputerName/SharedFolder'💡 TIP
Since
v.1.0.3a string property namedslashis exposed as well, provides an easy way to access file path OS-specific delimiter.On Windows
slash = '\\'.On UNIX-like
slash = '/'.
📝 Changelog
📑 The changelog is available here, CHANGELOG.md.
🪪 License
Licensed under the MIT license which is available here, MIT license.
💖 Support
🧬 Related
✒ DúöScríbî allows you to convert letters with diacritics to regular letters. 🤓
🧬 A lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects. 🪁
🧵 Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. 🔍
🔱 Odin is an Object wrapper that allows you to create objects and set their attributes - all at once! 🔺
🧮 Calculates an estimated file size of Mp3 files. 🎶
👨🏻💻 Author
Created by Igor Dimitrijević (@igorskyflyer).
