| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/strip-dirs/ |
Upload File : |
# strip-dirs
[](https://www.npmjs.com/package/strip-dirs)
[](https://travis-ci.org/shinnn/node-strip-dirs)
[](https://ci.appveyor.com/project/ShinnosukeWatanabe/node-strip-dirs)
[](https://coveralls.io/r/shinnn/node-strip-dirs)
[](https://david-dm.org/shinnn/node-strip-dirs)
[](https://david-dm.org/shinnn/node-strip-dirs#info=devDependencies)
Remove leading directory components from a path, like [tar(1)](http://linuxcommand.org/man_pages/tar1.html)'s `--strip-components` option
```javascript
var stripDirs = require('strip-dirs');
stripDirs('foo/bar/baz', 1); //=> 'bar/baz'
stripDirs('foo/bar/baz', 2); //=> 'baz'
stripDirs('foo/bar/baz', 999); //=> 'baz'
```
## Installation
[Use npm](https://docs.npmjs.com/cli/install).
```sh
npm install --save strip-dirs
```
## API
```javascript
var stripDirs = require('strip-dirs');
```
### stripDirs(*path*, *count* [, *option*])
*path*: `String` (A relative path)
*count*: `Number` (0, 1, 2, ...)
*option*: `Object`
Return: `String`
It removes directory components from the beginning of the *path* by *count*.
```javascript
var stripDirs = require('strip-dirs');
stripDirs('foo/bar', 1); //=> 'bar'
stripDirs('foo/bar/baz', 2); //=> 'bar'
stripDirs('foo/././/bar/./', 1); //=> 'bar'
stripDirs('foo/bar', 0); //=> 'foo/bar'
stripDirs('/foo/bar', 1) // throw an error because the path is an absolute path
```
If you want to remove all directory components certainly, use [`path.basename`](http://nodejs.org/api/path.html#path_path_basename_p_ext) instead of this module.
#### option.narrow
Type: `Boolean`
Default: `false`
By default, it keeps the last path component when path components are fewer than the *count*.
If this option is enabled, it throws an error in such case.
```javascript
stripDirs('foo/bar/baz', 9999); //=> 'baz'
stripDirs('foo/bar/baz', 9999, {narrow: true}); // throws an error
```
## CLI
You can use this module as `strip-dirs` command by installing it globally.
```sh
npm install -g strip-dirs
```
### Usage
```sh
strip-dirs <string> --count(or -c) <number> [--narrow(or -n)]
```
Or, use with pipe(`|`):
```sh
echo <string> | strip-dirs --count(or -c) <number> [--narrow(or -n)]
```
### Flags
```sh
--count, -c: Number of directories to strip from the path
--narrow, -n: Disallow surplus count of directory level
```
## License
Copyright (c) [Shinnosuke Watanabe](https://github.com/shinnn)
Licensed under [the MIT License](./LICENSE).