Skip to main content

pathcat makes URLs easy

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
a month ago (1.2.3)

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

Install

yarn add pathcat

Usage

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'

Benchmark:

Results when running bun run benchmark.ts on an M3 Max

pathcat x 1,132,795 ops/sec ±0.74% (95 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.

Add Package

deno add @alistair/pathcat

Import symbol

import * as mod from "@alistair/pathcat";

Add Package

npx jsr add @alistair/pathcat

Import symbol

import * as mod from "@alistair/pathcat";

Add Package

yarn dlx jsr add @alistair/pathcat

Import symbol

import * as mod from "@alistair/pathcat";

Add Package

pnpm dlx jsr add @alistair/pathcat

Import symbol

import * as mod from "@alistair/pathcat";

Add Package

bunx jsr add @alistair/pathcat

Import symbol

import * as mod from "@alistair/pathcat";