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
|
|
3
|
> 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.
|
4
|
|
5
|
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
|
6
|
|
7
|
## Install
|
8
|
|
9
|
Install with [npm](https://www.npmjs.com/):
|
10
|
|
11
|
```sh
|
12
|
$ npm install --save normalize-path
|
13
|
```
|
14
|
|
15
|
## Usage
|
16
|
|
17
|
```js
|
18
|
const normalize = require('normalize-path');
|
19
|
|
20
|
console.log(normalize('\\foo\\bar\\baz\\'));
|
21
|
//=> '/foo/bar/baz'
|
22
|
```
|
23
|
|
24
|
**win32 namespaces**
|
25
|
|
26
|
```js
|
27
|
console.log(normalize('\\\\?\\UNC\\Server01\\user\\docs\\Letter.txt'));
|
28
|
//=> '//?/UNC/Server01/user/docs/Letter.txt'
|
29
|
|
30
|
console.log(normalize('\\\\.\\CdRomX'));
|
31
|
//=> '//./CdRomX'
|
32
|
```
|
33
|
|
34
|
**Consecutive slashes**
|
35
|
|
36
|
Condenses multiple consecutive forward slashes (except for leading slashes in win32 namespaces) to a single slash.
|
37
|
|
38
|
```js
|
39
|
console.log(normalize('.//foo//bar///////baz/'));
|
40
|
//=> './foo/bar/baz'
|
41
|
```
|
42
|
|
43
|
### Trailing slashes
|
44
|
|
45
|
By default trailing slashes are removed. Pass `false` as the last argument to disable this behavior and _**keep** trailing slashes_:
|
46
|
|
47
|
```js
|
48
|
console.log(normalize('foo\\bar\\baz\\', false)); //=> 'foo/bar/baz/'
|
49
|
console.log(normalize('./foo/bar/baz/', false)); //=> './foo/bar/baz/'
|
50
|
```
|
51
|
|
52
|
## Release history
|
53
|
|
54
|
### v3.0
|
55
|
|
56
|
No breaking changes in this release.
|
57
|
|
58
|
* 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.
|
59
|
* a minor optimization was made to simplify how the trailing separator was handled
|
60
|
|
61
|
## About
|
62
|
|
63
|
<details>
|
64
|
<summary><strong>Contributing</strong></summary>
|
65
|
|
66
|
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
67
|
|
68
|
</details>
|
69
|
|
70
|
<details>
|
71
|
<summary><strong>Running Tests</strong></summary>
|
72
|
|
73
|
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:
|
74
|
|
75
|
```sh
|
76
|
$ npm install && npm test
|
77
|
```
|
78
|
|
79
|
</details>
|
80
|
|
81
|
<details>
|
82
|
<summary><strong>Building docs</strong></summary>
|
83
|
|
84
|
_(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.)_
|
85
|
|
86
|
To generate the readme, run the following command:
|
87
|
|
88
|
```sh
|
89
|
$ npm install -g verbose/verb#dev verb-generate-readme && verb
|
90
|
```
|
91
|
|
92
|
</details>
|
93
|
|
94
|
### Related projects
|
95
|
|
96
|
Other useful path-related libraries:
|
97
|
|
98
|
* [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.")
|
99
|
* [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`.")
|
100
|
* [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.")
|
101
|
* [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.")
|
102
|
* [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.")
|
103
|
* [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.")
|
104
|
|
105
|
### Contributors
|
106
|
|
107
|
| **Commits** | **Contributor** |
|
108
|
| --- | --- |
|
109
|
| 35 | [jonschlinkert](https://github.com/jonschlinkert) |
|
110
|
| 1 | [phated](https://github.com/phated) |
|
111
|
|
112
|
### Author
|
113
|
|
114
|
**Jon Schlinkert**
|
115
|
|
116
|
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
|
117
|
* [GitHub Profile](https://github.com/jonschlinkert)
|
118
|
* [Twitter Profile](https://twitter.com/jonschlinkert)
|
119
|
|
120
|
### License
|
121
|
|
122
|
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
|
123
|
Released under the [MIT License](LICENSE).
|
124
|
|
125
|
***
|
126
|
|
127
|
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 19, 2018._
|