|
1 | | -# your-package-name |
| 1 | +# pathnorm |
2 | 2 |
|
3 | | -Package Description |
| 3 | +Normalize and join path or URL segments into a single clean string. |
| 4 | + |
| 5 | +- Zero dependencies |
| 6 | +- Dual CJS + ESM build |
| 7 | +- Full TypeScript support |
| 8 | +- Tree-shakeable |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```sh |
| 13 | +npm install pathnorm |
| 14 | +# or |
| 15 | +yarn add pathnorm |
| 16 | +# or |
| 17 | +pnpm add pathnorm |
| 18 | +``` |
| 19 | + |
| 20 | +## Two Entry Points |
| 21 | + |
| 22 | +| Import | Win32 | UNC | Namespace | URLs | POSIX | |
| 23 | +|---|---|---|---|---|---| |
| 24 | +| `pathnorm` | ✅ | ✅ | ✅ | ✅ | ✅ | |
| 25 | +| `pathnorm/posix` | ❌ | ❌ | ❌ | ✅ | ✅ | |
| 26 | + |
| 27 | +Use `pathnorm/posix` in browser or web server contexts where Win32 paths will never appear — it's lighter and purpose-built for URLs and POSIX paths. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## `pathnorm` |
| 32 | + |
| 33 | +Exports `np` and `unixNp`. |
| 34 | + |
| 35 | +```ts |
| 36 | +import { np, unixNp } from 'pathnorm' |
| 37 | +``` |
| 38 | + |
| 39 | +### `np(...parts)` |
| 40 | + |
| 41 | +Joins and normalizes path or URL segments. Detects and handles URLs, POSIX paths, Win32 drive letters, UNC paths, and Win32 namespace paths automatically. |
| 42 | + |
| 43 | +```ts |
| 44 | +import { np } from 'pathnorm' |
| 45 | +// URLs |
| 46 | +np("https://abc.def//212/", "dw//we", "23123") |
| 47 | +// → "https://abc.def/212/dw/we/23123" |
| 48 | + |
| 49 | +np("exp://abc.def//212/", "dw/we") |
| 50 | +// → "exp://abc.def/212/dw/we" |
| 51 | + |
| 52 | +// POSIX |
| 53 | +np("abc.def//212/", "dwwe", "23123") |
| 54 | +// → "abc.def/212/dwwe/23123" |
| 55 | + |
| 56 | +np("foo//bar", "/baz") |
| 57 | +// → "/foo/bar/baz" |
| 58 | + |
| 59 | +np("/foo//bar", "baz") |
| 60 | +// → "/foo/bar/baz" |
| 61 | + |
| 62 | +// Win32 drive letter |
| 63 | +np("C:\\foo\\\\bar", "baz") |
| 64 | +// → "C:\foo\bar\baz" |
| 65 | + |
| 66 | +// UNC |
| 67 | +np("\\\\server\\share\\\\folder", "file.txt") |
| 68 | +// → "//server/share/folder/file.txt" |
| 69 | + |
| 70 | +// Win32 namespace |
| 71 | +np("\\\\?\\C:\\foo\\\\bar", "baz") |
| 72 | +// → "//?/C:/foo/bar/baz" |
| 73 | + |
| 74 | +// Mixed slashes in Win32 |
| 75 | +np("C:\\foo//bar\\\\baz") |
| 76 | +// → "C:\foo\bar\baz" |
| 77 | +``` |
| 78 | + |
| 79 | +### `unixNp(...parts)` |
| 80 | + |
| 81 | +Like `np`, but always returns a Unix-style path. Useful when working with Win32 paths but the consumer expects forward slashes. |
| 82 | + |
| 83 | +```ts |
| 84 | +import { unixNp } from 'pathnorm' |
| 85 | +unixNp("C:\\foo\\\\bar", "baz") |
| 86 | +// → "C:/foo/bar/baz" |
| 87 | + |
| 88 | +unixNp("\\\\server\\share\\\\folder", "file.txt") |
| 89 | +// → "//server/share/folder/file.txt" |
| 90 | + |
| 91 | +unixNp("\\\\?\\C:\\foo\\\\bar", "baz") |
| 92 | +// → "//?/C:/foo/bar/baz" |
| 93 | + |
| 94 | +// POSIX paths pass through unchanged |
| 95 | +unixNp("/foo//bar", "baz") |
| 96 | +// → "/foo/bar/baz" |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## `pathnorm/posix` |
| 102 | + |
| 103 | +Exports `np`. No Win32 support — ideal for browser and web server environments. |
| 104 | + |
| 105 | +```ts |
| 106 | +import { np } from 'pathnorm/posix' |
| 107 | +``` |
| 108 | + |
| 109 | +### `np(...parts)` (POSIX + URLs) |
| 110 | + |
| 111 | +Joins and normalizes URL or POSIX path segments. |
| 112 | + |
| 113 | +```ts |
| 114 | +import { np } from 'pathnorm/posix' |
| 115 | +// URLs |
| 116 | +np("https://abc.def//212/", "dw//we", "23123") |
| 117 | +// → "https://abc.def/212/dw/we/23123" |
| 118 | + |
| 119 | +np("exp://abc.def//212/", "dw/we") |
| 120 | +// → "exp://abc.def/212/dw/we" |
| 121 | + |
| 122 | +// POSIX |
| 123 | +np("abc.def//212/", "dwwe", "23123") |
| 124 | +// → "abc.def/212/dwwe/23123" |
| 125 | + |
| 126 | +np("foo//bar", "/baz") |
| 127 | +// → "/foo/bar/baz" |
| 128 | + |
| 129 | +np("/foo//bar", "baz") |
| 130 | +// → "/foo/bar/baz" |
| 131 | + |
| 132 | +// Typical web usage |
| 133 | +np(window.location.origin, "/api/proxy//betterauth") |
| 134 | +// → "https://myapp.com/api/proxy/betterauth" |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Behavior Notes |
| 140 | + |
| 141 | +- Only the **first** segment is checked for a URL scheme (`://`). Subsequent segments that look like URLs are treated as plain path parts. |
| 142 | +- Leading slashes are preserved if **any** segment introduces one. |
| 143 | +- Consecutive slashes (or backslashes in Win32 mode) are collapsed into one. |
| 144 | +- Trailing slashes are not preserved. |
| 145 | + |
| 146 | +```ts |
| 147 | +// Only first scheme is treated as prefix |
| 148 | +np("exp://", "sdfasdf", "abc.def//212/", "dw//we", "23123", "https://") |
| 149 | +// → "exp://sdfasdf/abc.def/212/dw/we/23123/https:/" |
| 150 | +``` |
0 commit comments