Projekt

Obecné

Profil

Stáhnout (2.25 KB) Statistiky
| Větev: | Revize:
1
# resolve-pathname [![Travis][build-badge]][build] [![npm package][npm-badge]][npm]
2

    
3
[build-badge]: https://img.shields.io/travis/mjackson/resolve-pathname/master.svg?style=flat-square
4
[build]: https://travis-ci.org/mjackson/resolve-pathname
5
[npm-badge]: https://img.shields.io/npm/v/resolve-pathname.svg?style=flat-square
6
[npm]: https://www.npmjs.org/package/resolve-pathname
7

    
8
[resolve-pathname](https://www.npmjs.com/package/resolve-pathname) resolves URL pathnames identical to the way browsers resolve the pathname of an `<a href>` value. The goals are:
9

    
10
- 100% compatibility with browser pathname resolution
11
- Pure JavaScript implementation (no DOM dependency)
12

    
13
## Installation
14

    
15
Using [npm](https://www.npmjs.com/):
16

    
17
    $ npm install --save resolve-pathname
18

    
19
Then, use as you would anything else:
20

    
21
```js
22
// using ES6 modules
23
import resolvePathname from 'resolve-pathname';
24

    
25
// using CommonJS modules
26
var resolvePathname = require('resolve-pathname');
27
```
28

    
29
The UMD build is also available on [unpkg](https://unpkg.com):
30

    
31
```html
32
<script src="https://unpkg.com/resolve-pathname"></script>
33
```
34

    
35
You can find the library on `window.resolvePathname`.
36

    
37
## Usage
38

    
39
```js
40
import resolvePathname from 'resolve-pathname';
41

    
42
// Simply pass the pathname you'd like to resolve. Second
43
// argument is the path we're coming from, or the current
44
// pathname. It defaults to "/".
45
resolvePathname('about', '/company/jobs'); // /company/about
46
resolvePathname('../jobs', '/company/team/ceo'); // /company/jobs
47
resolvePathname('about'); // /about
48
resolvePathname('/about'); // /about
49

    
50
// Index paths (with a trailing slash) are also supported and
51
// work the same way as browsers.
52
resolvePathname('about', '/company/info/'); // /company/info/about
53

    
54
// In browsers, it's easy to resolve a URL pathname relative to
55
// the current page. Just use window.location! e.g. if
56
// window.location.pathname == '/company/team/ceo' then
57
resolvePathname('cto', window.location.pathname); // /company/team/cto
58
resolvePathname('../jobs', window.location.pathname); // /company/jobs
59
```
60

    
61
## Prior Work
62

    
63
- [url.resolve](https://nodejs.org/api/url.html#url_url_resolve_from_to) - node's `url.resolve` implementation for full URLs
64
- [resolve-url](https://www.npmjs.com/package/resolve-url) - A DOM-dependent implementation of the same algorithm
(2-2/4)