1.0.10 • Published 6 months ago

@chriscdn/build-url v1.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@chriscdn/build-url

A small utility for building URLs.

Motivation

This package is a fork of @googlicius/build-url. Full credit to googlicius for the concept, source code, and documentation. 👍

I forked this package for three reasons:

  • to provide an ES6 build,
  • to fix this issue, and
  • to provide a named export.

Installation

Using npm:

npm install @chriscdn/build-url

Using yarn:

yarn add @chriscdn/build-url

Usage

Create a url:

import { buildUrl } from "@chriscdn/build-url";

buildUrl("http://my-website.com/post", {
  queryParams: {
    page: 2,
  },
});

// Output: http://my-website.com/post?page=2

Add another query parameter:

buildUrl("http://my-website.com/post?page=2", {
  queryParams: {
    sort: "title:asc",
  },
});

// Output: http://my-website.com/post?page=2&sort=title%3Aasc

Input url/path is omitted:

buildUrl({
  queryParams: {
    sort: "title:asc",
  },
});

// Output: /?sort=title%3Aasc

Remove a query parameter:

buildUrl("images?page=2&sort=title:asc", {
  queryParams: {
    page: null,
  },
});

// Output: /images?sort=title%3Aasc

Return an absolute url:

// Assume that current url is: http://awesome-website.com

buildUrl("/posts", {
  returnAbsoluteUrl: true,
  queryParams: {
    page: 2,
  },
});

// Output: http://awesome-website.com/posts?page=2

License

MIT

1.0.9

8 months ago

1.0.10

6 months ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago