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.

126 lines
5.3 KiB

2 years ago
  1. # normalize-path [![NPM version](https://img.shields.io/npm/v/normalize-path.svg?style=flat)](https://www.npmjs.com/package/normalize-path) [![NPM monthly downloads](https://img.shields.io/npm/dm/normalize-path.svg?style=flat)](https://npmjs.org/package/normalize-path) [![NPM total downloads](https://img.shields.io/npm/dt/normalize-path.svg?style=flat)](https://npmjs.org/package/normalize-path) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/normalize-path.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/normalize-path)
  2. > Normalize slashes in a file path to be posix/unix-like forward slashes. Also condenses repeat slashes to a single slash and removes and trailing slashes, unless disabled.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save normalize-path
  8. ```
  9. ## Usage
  10. ```js
  11. const normalize = require('normalize-path');
  12. console.log(normalize('\\foo\\bar\\baz\\'));
  13. //=> '/foo/bar/baz'
  14. ```
  15. **win32 namespaces**
  16. ```js
  17. console.log(normalize('\\\\?\\UNC\\Server01\\user\\docs\\Letter.txt'));
  18. //=> '//?/UNC/Server01/user/docs/Letter.txt'
  19. console.log(normalize('\\\\.\\CdRomX'));
  20. //=> '//./CdRomX'
  21. ```
  22. **Consecutive slashes**
  23. Condenses multiple consecutive forward slashes (except for leading slashes in win32 namespaces) to a single slash.
  24. ```js
  25. console.log(normalize('.//foo//bar///////baz/'));
  26. //=> './foo/bar/baz'
  27. ```
  28. ### Trailing slashes
  29. By default trailing slashes are removed. Pass `false` as the last argument to disable this behavior and _**keep** trailing slashes_:
  30. ```js
  31. console.log(normalize('foo\\bar\\baz\\', false)); //=> 'foo/bar/baz/'
  32. console.log(normalize('./foo/bar/baz/', false)); //=> './foo/bar/baz/'
  33. ```
  34. ## Release history
  35. ### v3.0
  36. No breaking changes in this release.
  37. * a check was added to ensure that [win32 namespaces](https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces) are handled properly by win32 `path.parse()` after a path has been normalized by this library.
  38. * a minor optimization was made to simplify how the trailing separator was handled
  39. ## About
  40. <details>
  41. <summary><strong>Contributing</strong></summary>
  42. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  43. </details>
  44. <details>
  45. <summary><strong>Running Tests</strong></summary>
  46. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  47. ```sh
  48. $ npm install && npm test
  49. ```
  50. </details>
  51. <details>
  52. <summary><strong>Building docs</strong></summary>
  53. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  54. To generate the readme, run the following command:
  55. ```sh
  56. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  57. ```
  58. </details>
  59. ### Related projects
  60. Other useful path-related libraries:
  61. * [contains-path](https://www.npmjs.com/package/contains-path): Return true if a file path contains the given path. | [homepage](https://github.com/jonschlinkert/contains-path "Return true if a file path contains the given path.")
  62. * [is-absolute](https://www.npmjs.com/package/is-absolute): Returns true if a file path is absolute. Does not rely on the path module… [more](https://github.com/jonschlinkert/is-absolute) | [homepage](https://github.com/jonschlinkert/is-absolute "Returns true if a file path is absolute. Does not rely on the path module and can be used as a polyfill for node.js native `path.isAbolute`.")
  63. * [is-relative](https://www.npmjs.com/package/is-relative): Returns `true` if the path appears to be relative. | [homepage](https://github.com/jonschlinkert/is-relative "Returns `true` if the path appears to be relative.")
  64. * [parse-filepath](https://www.npmjs.com/package/parse-filepath): Pollyfill for node.js `path.parse`, parses a filepath into an object. | [homepage](https://github.com/jonschlinkert/parse-filepath "Pollyfill for node.js `path.parse`, parses a filepath into an object.")
  65. * [path-ends-with](https://www.npmjs.com/package/path-ends-with): Return `true` if a file path ends with the given string/suffix. | [homepage](https://github.com/jonschlinkert/path-ends-with "Return `true` if a file path ends with the given string/suffix.")
  66. * [unixify](https://www.npmjs.com/package/unixify): Convert Windows file paths to unix paths. | [homepage](https://github.com/jonschlinkert/unixify "Convert Windows file paths to unix paths.")
  67. ### Contributors
  68. | **Commits** | **Contributor** |
  69. | --- | --- |
  70. | 35 | [jonschlinkert](https://github.com/jonschlinkert) |
  71. | 1 | [phated](https://github.com/phated) |
  72. ### Author
  73. **Jon Schlinkert**
  74. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  75. * [GitHub Profile](https://github.com/jonschlinkert)
  76. * [Twitter Profile](https://twitter.com/jonschlinkert)
  77. ### License
  78. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  79. Released under the [MIT License](LICENSE).
  80. ***
  81. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 19, 2018._