Skip to content

Commit 0003b54

Browse files
committed
feat: add pathnorm & pathnorm/posix + update README.md
1 parent 8c84fb1 commit 0003b54

File tree

11 files changed

+446
-63
lines changed

11 files changed

+446
-63
lines changed

README.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
1-
# NPM package template
1+
# pathnorm
22

3-
## Development
4-
5-
- Install dependencies: `yarn`
6-
- Run development server + build package on changes: `yarn dev`
7-
- Check `./package.json` for `dev` script. It runs dev commands for docusaurus and package concurrently.
8-
- Run only docusaurus dev server: `yarn workspace docs start`
9-
- Run only package build: `yarn workspace your-package-name dev`
10-
- Package is pre-configured to build out 2 dist paths:
11-
- `dist/index.*js` for vanilla JS exports
12-
- `dist/react.*js` for React exports
13-
- Docs has an example of importing an example `addNumber` export from `your-package-name` package in `getting-started.mdx`
3+
Normalize and join path/URL segments. Full Win32, UNC, POSIX, and URL support.
144

15-
## Configure package and its deployment on NPM
5+
Read the [`README.md`](./package/README.md) documentation for more details.
166

17-
- Replace all instances of `your-package-name` with your package name
18-
- Replace all instances of `your-org` with your GitHub org name / your username
19-
- Required Settings in Github Repo Settings:
20-
- Enable Permission: Allow GitHub Actions to create and approve pull requests: <https://github.com/changesets/changesets/discussions/1090>
21-
- Add `NPM_TOKEN` to your Repo Actions Secrets.
22-
23-
## Deploy Docusaurus on GitHub pages
7+
## Development
248

25-
- Set GitHub Pages source to "GitHub Actions"
26-
- Go to Settings -> Pages section -> Build and deployment -> Source: GitHub Actions
9+
```sh
10+
yarn install
11+
yarn build
12+
yarn test
13+
```
2714

28-
## Issues
15+
## Contributing
2916

30-
- Is `changeset` unable to publish?
31-
- <https://github.com/changesets/action/issues/98#issuecomment-2546826646>
17+
Contributions are welcome! Please open an issue or submit a pull request.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"publish": "yarn changeset publish",
77
"version": "yarn changeset",
8-
"dev": "yarn workspace your-package-name dev"
8+
"dev": "yarn workspace pathnorm dev",
9+
"build": "yarn workspace pathnorm build",
10+
"test": "yarn workspace pathnorm-test test"
911
},
1012
"devDependencies": {
1113
"@biomejs/biome": "2.4.4",
@@ -15,7 +17,7 @@
1517
"tsup": "^8.5.1",
1618
"typescript": "^5.9.3"
1719
},
18-
"author": "Author of your-package-name",
20+
"author": "Ashutosh Khanduala <https://github.com/ashuvssut>",
1921
"license": "MIT",
2022
"workspaces": [
2123
"package"

package/README.md

Lines changed: 149 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,150 @@
1-
# your-package-name
1+
# pathnorm
22

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+
```

package/package.json

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
{
2-
"name": "your-package-name",
2+
"name": "pathnorm",
33
"version": "0.0.0",
4-
"description": "description of your-package-name",
4+
"description": "Normalize and join path and URL segments. Supports URLs, POSIX paths, Win32 drive letters, UNC, and namespace paths.",
55
"source": "src/index.ts",
66
"main": "dist/index.js",
77
"module": "dist/index.mjs",
88
"types": "dist/index.d.ts",
99
"exports": {
1010
".": {
11-
"types": "./dist/index.d.ts",
12-
"import": "./dist/index.mjs",
13-
"require": "./dist/index.js"
11+
"import": {
12+
"types": "./dist/index.d.mts",
13+
"default": "./dist/index.mjs"
14+
},
15+
"require": {
16+
"types": "./dist/index.d.ts",
17+
"default": "./dist/index.js"
18+
}
1419
},
15-
"./react": {
16-
"types": "./dist/react.d.ts",
17-
"import": "./dist/react.mjs",
18-
"require": "./dist/react.js"
20+
"./posix": {
21+
"import": {
22+
"types": "./dist/posix.d.mts",
23+
"default": "./dist/posix.mjs"
24+
},
25+
"require": {
26+
"types": "./dist/posix.d.ts",
27+
"default": "./dist/posix.js"
28+
}
1929
}
2030
},
2131
"publishConfig": {
2232
"access": "public"
2333
},
2434
"files": [
25-
"**"
35+
"dist"
2636
],
2737
"scripts": {
2838
"dev:tsc": "tsc -w -p tsc.tsconfig.json",
@@ -33,13 +43,22 @@
3343
"lint": "yarn lint:tsc && yarn lint:biome",
3444
"format": "biome check --write ./src"
3545
},
36-
"author": "Author of your-package-name",
46+
"author": "Ashutosh Khanduala <https://github.com/ashuvssut>",
3747
"license": "MIT",
3848
"bugs": {
39-
"url": "https://github.com/your-org/your-package-name/issues"
49+
"url": "https://github.com/stacknide/pathnorm/issues"
4050
},
41-
"homepage": "https://github.com/your-org/your-package-name#readme",
42-
"keywords": [],
51+
"homepage": "https://github.com/stacknide/pathnorm#readme",
52+
"keywords": [
53+
"path",
54+
"url",
55+
"normalize",
56+
"join",
57+
"posix",
58+
"win32",
59+
"unc",
60+
"cross-platform"
61+
],
4362
"devDependencies": {
4463
"@biomejs/biome": "2.4.4",
4564
"lefthook": "^2.1.1",

package/src/addNumber.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

package/src/export/react.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

package/src/export/vanilla.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)