You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # strip-dirs
  2. [![NPM version](https://img.shields.io/npm/v/strip-dirs.svg)](https://www.npmjs.com/package/strip-dirs)
  3. [![Build Status](https://img.shields.io/travis/shinnn/node-strip-dirs.svg)](https://travis-ci.org/shinnn/node-strip-dirs)
  4. [![Build status](https://ci.appveyor.com/api/projects/status/pr5edbtg59f6xfgn?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/node-strip-dirs)
  5. [![Coverage Status](https://img.shields.io/coveralls/shinnn/node-strip-dirs.svg)](https://coveralls.io/r/shinnn/node-strip-dirs)
  6. [![Dependency Status](https://david-dm.org/shinnn/node-strip-dirs.svg)](https://david-dm.org/shinnn/node-strip-dirs)
  7. [![devDependency Status](https://david-dm.org/shinnn/node-strip-dirs/dev-status.svg)](https://david-dm.org/shinnn/node-strip-dirs#info=devDependencies)
  8. Remove leading directory components from a path, like [tar(1)](http://linuxcommand.org/man_pages/tar1.html)'s `--strip-components` option
  9. ```javascript
  10. const stripDirs = require('strip-dirs');
  11. stripDirs('foo/bar/baz', 1); //=> 'bar/baz'
  12. stripDirs('foo/bar/baz', 2); //=> 'baz'
  13. stripDirs('foo/bar/baz', 999); //=> 'baz'
  14. ```
  15. ## Installation
  16. [Use npm](https://docs.npmjs.com/cli/install).
  17. ```
  18. npm install --save strip-dirs
  19. ```
  20. ## API
  21. ```javascript
  22. const stripDirs = require('strip-dirs');
  23. ```
  24. ### stripDirs(*path*, *count* [, *option*])
  25. *path*: `String` (A relative path)
  26. *count*: `Number` (0, 1, 2, ...)
  27. *option*: `Object`
  28. Return: `String`
  29. It removes directory components from the beginning of the *path* by *count*.
  30. ```javascript
  31. const stripDirs = require('strip-dirs');
  32. stripDirs('foo/bar', 1); //=> 'bar'
  33. stripDirs('foo/bar/baz', 2); //=> 'bar'
  34. stripDirs('foo/././/bar/./', 1); //=> 'bar'
  35. stripDirs('foo/bar', 0); //=> 'foo/bar'
  36. stripDirs('/foo/bar', 1) // throw an error because the path is an absolute path
  37. ```
  38. If you want to remove all directory components certainly, use [`path.basename`](https://nodejs.org/api/path.html#path_path_basename_path_ext) instead of this module.
  39. #### option.disallowOverflow
  40. Type: `Boolean`
  41. Default: `false`
  42. By default, it keeps the last path component when path components are fewer than the *count*.
  43. If this option is enabled, it throws an error in this situation.
  44. ```javascript
  45. stripDirs('foo/bar/baz', 9999); //=> 'baz'
  46. stripDirs('foo/bar/baz', 9999, {disallowOverflow: true}); // throws an range error
  47. ```
  48. ## License
  49. Copyright (c) 2014 - 2016 [Shinnosuke Watanabe](https://github.com/shinnn)
  50. Licensed under [the MIT License](./LICENSE).