@igorskyflyer/pathexists
v3.0.0
Published
🧲 Provides ways of properly checking if a path exists inside a given array of files/directories both on Windows and UNIX-like operating systems, using String manipulation techniques. 🗺
Maintainers
Readme
📃 Table of Contents
🤖 Features
- 🖥 OS‑aware checks - Adapts comparison rules for Windows or UNIX‑like systems
- 🔍 Path lookup - Quickly finds if a path exists in a given list
- 🛡 Case handling - Case‑insensitive on Windows, case‑sensitive on UNIX‑like
- ⚙️ Custom comparator - Plug in your own matching logic when needed
- 🔄 Reusable helpers - Separate functions for Windows, UNIX, and auto‑detect modes
- 📦 Lightweight - Minimal code, no heavy dependencies
- 🌍 Cross‑platform - Works consistently across different operating systems
⚠️ WARNING
It does NOT actually check if the file/directory exists on the disk, it only checks if the given path exists in the list of paths. Useful when you have already obtained a list of files/directories and you want to check if a given path is eligible/available to, for example, create a new file at it.
🕵🏼 Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/pathexistsyarn add @igorskyflyer/pathexistsnpm i @igorskyflyer/pathexists🤹🏼 API
import { pathExists, pathExistsWindows, pathExistsUnix } from '@igorskyflyer/pathexists'
const allPaths: string[] = ['abc.js', 'etc.bak']
const winPaths: string[] = ['D:\\', 'D:\\Temp\\a.js']
const unixPaths: string[] = ['/home/user', '/home/user/a.js']
// #1 - host OS dependent mode - uses the appropriate function for each host OS
console.log(pathExists('abc.js', allPaths)) // prints true
// #1 - force Windows mode
console.log(pathExistsWindows('abc.js', allPaths)) // prints true
// #1 - force UNIX-like mode
console.log(pathExistsUnix('abc.js', allPaths)) // prints true
// #2 - force Windows mode
console.log(pathExistsWindows('abC.js', allPaths)) // prints true
// #2 - force UNIX-like mode
console.log(pathExistsUnix('abC.js', allPaths)) // prints false
// #3 - force Windows mode
console.log(pathExistsWindows('D:\\Temp\\A.js', winPaths)) // prints true
// #3 - force UNIX-like mode
console.log(pathExistsUnix('D:\\Temp\\A.js', unixPaths)) // prints false
// #4 - force Windows mode, custom Comparator
console.log(pathExistsWindows('Temp', winPaths, (entry, value) => entry.indexOf(value) > -1)) // prints true
// #4 - force UNIX-like mode, custom Comparator
console.log(pathExistsUnix('Temp', unixPaths, (entry, value) => entry.indexOf(value) > -1)) // prints true📝 Changelog
📑 The changelog is available here, CHANGELOG.md.
🪪 License
Licensed under the MIT license which is available here, MIT license.
💖 Support
🧬 Related
@igorskyflyer/adblock-header-extract
✂️ An npm module that provides ways to extract header and metadata from an Adblock filter file. 📃
🎍 Provides a universal way of formatting file-paths in Unix-like and Windows operating systems as an alternative to the built-in path.normalize(). 🧬
🎉 Provides UArray, an Array type that supports negative indices/indexes, just wrap your regular JavaScript array with UArray() and you are all set! 🙌
🌐 Gets the origin URL a local Git repository. 🗺️
🥽 Provides ways of parsing UNC paths and checking whether they are valid. 🎱
👨🏻💻 Author
Created by Igor Dimitrijević (@igorskyflyer).
