@igor.dvlpr/upath
v3.0.1
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
91
Maintainers
Readme
uPath
🎍 Provides a universal way of formatting file-paths in Unix-like and Windows operating systems as an alternative to the built-in path.normalize(). 🧬
Why
uPath
? Simple. Working with file paths insideString
s 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 thatuPath
provides is redundant trailing slash removal.
✨Since version 1.2.0
support for UNC paths has been added, thanks to unc-path.
🕵🏼 Usage
Install it by executing:
npm i "@igor.dvlpr/upath"
🤹🏼 API
upath()
=>
returns a proper file path depending on the host OS.
✨Note: upath()
has an alias, named u()
for your convenience.
Signature
upath(fsPath: string, addTrailingSlash: boolean = false): string
u(fsPath: string, addTrailingSlash: boolean = false): string
Parameters
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 not
Example - Windows OS
import { u } from '@igor.dvlpr/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 '@igor.dvlpr/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): string
Parameters
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 not
Example - Any OS
import { uw } from '@igor.dvlpr/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): string
Parameters
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 not
Example - Any OS
import { ux } from '@igor.dvlpr/upath'
console.log(ux('/usr/local')) // returns '/usr/local'
console.log(ux('\\\\ComputerName\\SharedFolder'))
// returns '//ComputerName/SharedFolder'
✨ Since v.1.0.3
a string property named slash
is exposed as well, provides an easy way to access file path OS-specific delimiter.
On Windows
slash = '\\'
.
On UNIX-like
slash = '/'
.
🪪 License
Licensed under the MIT license which is available here, MIT license.
🧬 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. 🎶
Provided by Igor Dimitrijević (@igorskyflyer).