Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alii/pathcat
Simply path/URL building in JavaScript
https://github.com/alii/pathcat
http networking nodejs typescript urls utility
Last synced: 6 days ago
JSON representation
Simply path/URL building in JavaScript
- Host: GitHub
- URL: https://github.com/alii/pathcat
- Owner: alii
- License: unlicense
- Created: 2023-12-26T05:29:56.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-10-10T16:48:45.000Z (26 days ago)
- Last Synced: 2024-10-23T06:55:45.212Z (13 days ago)
- Topics: http, networking, nodejs, typescript, urls, utility
- Language: TypeScript
- Homepage: https://npm.im/pathcat
- Size: 1.07 MB
- Stars: 182
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# `pathcat` 🐾
Simply path/URL building in JavaScript. Intelligently handles URL params and query strings.
This library owes thanks to urlcat, but unfortunately it seems to be abandoned. You can mostly use pathcat as a replacement.
## Features
- Very intuitive API
- Supports URL params and query string
- Avoids double slashes
- Zero dependencies
- Absolutely tiny install size## Install
```sh
yarn add pathcat
```## Usage
```typescript
import { pathcat } from "pathcat";pathcat("https://example.com", "/:id", {
id: 123,
});
// => 'https://example.com/123'pathcat("https://example.com", "/:id", {
id: "123",
foo: "bar",
});
// => 'https://example.com/123?foo=bar'// Base URL is optional, works on just paths as well.
pathcat("/users/:user_id/posts/:post_id", {
user_id: "123",
post_id: 456,
cool_flag: true,
});
// => '/users/123/posts/456?cool_flag=true'// You can also use arrays for query string values
pathcat("/users/:user_id/posts/:post_id", {
user_id: "123",
post_id: 456,
cool_flag: true,
fields: ["title", "body"],
});
// => '/users/123/posts/456?cool_flag=true&fields=title&fields=body'
```## Benchmark:
Results when running on an M3 Max
```
$ node --import=tsx benchmark.tsWith a base URL x 2,628,829 ops/sec ±0.70% (95 runs sampled)
With no base URL x 3,160,695 ops/sec ±0.50% (96 runs sampled)
With a base URL, and no params x 70,782,166 ops/sec ±1.93% (88 runs sampled)
```## Notes:
- Any params or query string values that are `undefined` will be omitted.
- Params that were not specified in the object will be left as is.