strip-dirs
Remove leading directory components from a path, like tar(1)'s --strip-components
option
const stripDirs = require('strip-dirs');
stripDirs('foo/bar/baz', 1);
stripDirs('foo/bar/baz', 2);
stripDirs('foo/bar/baz', 999);
Installation
Use npm.
npm install strip-dirs
API
const stripDirs = require('strip-dirs');
stripDirs(path, count [, option])
path: string
(A relative path)
count: integer
(>= 0
)
option: Object
Return: string
It removes directory components from the beginning of the path by count.
const stripDirs = require('strip-dirs');
stripDirs('foo/bar', 1);
stripDirs('foo/bar/baz', 2);
stripDirs('foo/././/bar/./', 1);
stripDirs('foo/bar', 0);
stripDirs('/foo/bar', 1)
If you want to remove all directory components certainly, use path.basename
instead of this module.
option.disallowOverflow
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 this situation.
stripDirs('foo/bar/baz', 9999);
stripDirs('foo/bar/baz', 9999, {disallowOverflow: true});
License
ISC License © 2017 - 2018 Shinnosuke Watanabe