Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/igorskyflyer/npm-valid-path

🧰 Provides ways of testing whether a given value can be a valid file/directory name. 🏜
https://github.com/igorskyflyer/npm-valid-path

back-end directory file file-system filesystem igorskyflyer javascript mocha node path storage validation

Last synced: 14 days ago
JSON representation

🧰 Provides ways of testing whether a given value can be a valid file/directory name. 🏜

Awesome Lists containing this project

README

        

## Valid Path

🧰 Provides ways of testing whether a given value can be a valid file/directory name. 🏜


> 💃 Since `v.1.1.0` this package provides both a CommonJS module and an ES module, thanks to [`Modern Module`](https://github.com/igorskyflyer/npm-modern-module).
>
> What do you need to do to use one or the other?
>
> Nothing. If you need a CommonJS module `require` it, if you need an ES module `import` it instead. Simple, right?


### Usage

Install it by running,

```shell
npm i "@igor.dvlpr/valid-path"
```


### API

```ts
function isValidPath(path: string, isFile = true): boolean
```

Returns whether the given path can be a valid file/directory name on the host machine.


```ts
function isValidPathUnix(path: string, isFile = true): boolean
```

Returns whether the given path can be a valid file/directory name on Unix and Unix-like OS'.


```ts
function isValidPathWin(path: string, isFile = true): boolean
```

Returns whether the given path can be a valid file/directory name on Windows OS.


### Examples

```js
const { isValidPathUnix, isValidPathWin, isValidPath } = require('@igor.dvlpr/valid-path')

console.log(isValidPathUnix('hello&world.js', true)) // prints true
console.log(isValidPathUnix('hello/world.js', true)) // prints false
console.log(isValidPathUnix('hello/world')) // prints false

console.log(isValidPathWin('hello/world.js', true)) // prints false
console.log(isValidPathWin('hello/world', false)) // prints false
console.log(isValidPathWin('CON7')) // prints true
console.log(isValidPathWin('COM7.txt')) // prints false
console.log(isValidPathWin('CONnection')) // prints true
console.log(isValidPathWin('lpt1')) // prints false
console.log(isValidPathWin('hello&world.js', true)) // prints true

// isValidPath() internally uses the appropriate method for supported OS' (Unix-like and Windows)
```