Resolve URL pathnames using JavaScript with 100% browser compatibility
npx @tessl/cli install tessl/npm-resolve-pathname@3.0.00
# resolve-pathname
1
2
resolve-pathname provides a pure JavaScript implementation for resolving URL pathnames that is 100% compatible with browser pathname resolution behavior. It resolves pathnames identically to how browsers resolve the pathname of an `<a href>` value, supporting both absolute and relative path resolution, proper handling of '..' and '.' path segments, and maintaining trailing slash behavior.
3
4
## Package Information
5
6
- **Package Name**: resolve-pathname
7
- **Package Type**: npm
8
- **Language**: JavaScript (ES6 modules with CommonJS build)
9
- **Installation**: `npm install resolve-pathname`
10
11
## Core Imports
12
13
```javascript
14
// ES6 modules
15
import resolvePathname from 'resolve-pathname';
16
```
17
18
For CommonJS:
19
20
```javascript
21
// CommonJS modules
22
var resolvePathname = require('resolve-pathname');
23
```
24
25
For UMD (browser):
26
27
```html
28
<script src="https://unpkg.com/resolve-pathname"></script>
29
<!-- Available as window.resolvePathname -->
30
```
31
32
## Basic Usage
33
34
```javascript
35
import resolvePathname from 'resolve-pathname';
36
37
// Simply pass the pathname you'd like to resolve. Second
38
// argument is the path we're coming from, or the current
39
// pathname. When undefined, it defaults to empty string.
40
resolvePathname('about', '/company/jobs'); // /company/about
41
resolvePathname('../jobs', '/company/team/ceo'); // /company/jobs
42
resolvePathname('about'); // about
43
resolvePathname('/about'); // /about
44
45
// Index paths (with a trailing slash) are also supported and
46
// work the same way as browsers.
47
resolvePathname('about', '/company/info/'); // /company/info/about
48
49
// In browsers, it's easy to resolve a URL pathname relative to
50
// the current page. Just use window.location! e.g. if
51
// window.location.pathname == '/company/team/ceo' then
52
resolvePathname('cto', window.location.pathname); // /company/team/cto
53
resolvePathname('../jobs', window.location.pathname); // /company/jobs
54
```
55
56
## Capabilities
57
58
### Pathname Resolution
59
60
Resolves URL pathnames using the same algorithm as browsers, providing 100% compatibility with browser pathname resolution behavior.
61
62
```javascript { .api }
63
/**
64
* Resolve URL pathnames identical to how browsers resolve the pathname of an <a href> value.
65
*
66
* @param {string} to - The pathname to resolve (can be relative or absolute)
67
* @param {string} [from] - The current pathname to resolve from (optional, defaults to empty string when undefined)
68
* @returns {string} The resolved pathname
69
*/
70
function resolvePathname(to, from);
71
```
72
73
**Parameters:**
74
- `to` (string): The pathname you want to resolve. Can be:
75
- Absolute path (starting with '/'): e.g., '/about'
76
- Relative path: e.g., 'about', '../jobs'
77
- Parent directory reference: e.g., '..'
78
- Current directory reference: e.g., '.'
79
- Empty string: returns the from path unchanged
80
- `from` (string, optional): The path we're coming from, or the current pathname. When undefined, defaults to empty string
81
82
**Returns:**
83
- (string): The resolved pathname
84
85
**Resolution Behavior:**
86
- **Absolute paths**: When `to` starts with '/', it's treated as absolute and returned as-is (after normalization)
87
- **Relative paths**: Resolved relative to the `from` path, with proper handling of:
88
- `..` segments: Navigate to parent directory
89
- `.` segments: Stay in current directory (removed)
90
- Trailing slashes: Preserved when appropriate
91
- **Empty paths**: Empty `to` returns `from` unchanged
92
- **Index paths**: Paths ending with '/' are treated as directory paths
93
94
**Usage Examples:**
95
96
```javascript
97
// Absolute path resolution
98
resolvePathname('/about', '/company/jobs'); // '/about'
99
100
// Relative path resolution
101
resolvePathname('about', '/company/jobs'); // '/company/about'
102
103
// Parent directory navigation
104
resolvePathname('../jobs', '/company/team/ceo'); // '/company/jobs'
105
106
// Multiple parent directory navigation
107
resolvePathname('../../other', '/company/team/ceo'); // '/other'
108
109
// Current directory reference
110
resolvePathname('.', '/company/jobs'); // '/company/'
111
112
// Trailing slash handling
113
resolvePathname('about', '/company/'); // '/company/about'
114
115
// Undefined from parameter (defaults to empty string)
116
resolvePathname('about'); // 'about'
117
resolvePathname('/about'); // '/about'
118
119
// Edge cases
120
resolvePathname('', '/company/jobs'); // '/company/jobs'
121
resolvePathname('../../../../foo', '/a/b'); // '/foo'
122
```