mcansh / url

a template string function to create urls and only keeping search params with values - based on this tweet https://x.com/Steve8708/status/1792939860820644201

Repository from Github https://github.commcansh/urlRepository from Github https://github.commcansh/url

@mcansh/url

Note

This repo is now part of mcansh/packages

a template string function to create urls and only keeping search params with values - based on this tweet

import { urlString } from "@mcansh/url";

let filter = undefined;
let user = null;
let q = "my search";
urlString`https://site.com/path?q=${q}&user=${user}&filter=${filter}`;
// => "https://site.com/path?q=my+search"

import { url } from "@mcansh/url";

let filter = undefined;
let user = null;
let q = "my search";
url`https://site.com/path?q=${q}&user=${user}&filter=${filter}`;
// => URL

a url builder based on this tweet

import { UrlBuilder } from "@mcansh/url";

new UrlBuilder()
  .domain("site.com")
  .path("/path")
  .param("q", "my search")
  .param<number>("userId", 5)
  .param<string>("filter", "category")
  .build();
// => "https://site.com/path?q=my+search&userId=5&filter=category"

About

a template string function to create urls and only keeping search params with values - based on this tweet https://x.com/Steve8708/status/1792939860820644201

License:MIT License


Languages

Language:TypeScript 99.5%Language:JavaScript 0.5%