# build-url

> A small library that builds a URL given its components

Latest version **6.0.1** (published 2020-08-31) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install build-url
pnpm add build-url
yarn add build-url
bun add build-url
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2020-08-31 |
| First published | 2015-12-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Steve Rydz |
| Maintainers | steverydz |
| Keywords | url, uri, builder, build, concat, concatenate, append, path, hash, query, string, querystring, parameters, params, queryparams |

## Links

- npm: https://www.npmjs.com/package/build-url
- Repository: https://github.com/steverydz/build-url
- Homepage: https://github.com/steverydz/build-url#readme
- Issues: https://github.com/steverydz/build-url/issues
- npm.io page: https://npm.io/package/build-url

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 6.0.1 (latest) — 2020-08-31
- 6.0.0 — 2020-08-31
- 5.0.2 — 2020-08-30
- 5.0.1 — 2020-08-30
- 5.0.0 — 2020-08-29
- 4.0.0 — 2020-08-29
- 3.1.0 — 2020-08-29
- 3.0.4 — 2020-08-29
- 3.0.3 — 2020-08-29
- 3.0.2 — 2020-08-29
- 3.0.1 — 2020-08-29
- 3.0.0 — 2020-08-21
- 2.0.0 — 2019-11-20
- 1.3.3 — 2019-05-15
- 1.3.2 — 2018-12-29
- … 26 more at https://npm.io/package/build-url/versions

## README

# build-url

[![Build Status](https://travis-ci.org/steverydz/build-url.svg?branch=master)](https://travis-ci.org/steverydz/build-url)

A library that builds a URL, including its path, query parameters and fragment identifier. Works in node and in the browser.

## Installation

To install with npm:

```
npm install build-url --save
```

## Usage

Usage in the browser:

```
<script src="../path/to/lib/build-url.js"></script>
<script>
buildUrl('http://example.com', {
  path: 'about',
  hash: 'contact',
  queryParams: {
    foo: bar,
    bar: ['foo', 'bar']
  }
});
</script>
```

Usage with ES6 modules:
```
import buildUrl from '../path/to/lib/build-url';

buildUrl('http://example.com', {
  path: 'about',
  hash: 'contact',
  queryParams: {
    foo: bar,
    bar: ['foo', 'bar']
  }
});
```

Usage with node:

```
var buildUrl = require('build-url');

buildUrl('http://example.com', {
  path: 'about',
  hash: 'contact',
  queryParams: {
    foo: bar,
    bar: ['foo', 'bar']
  }
});
```

## Options

The `buildUrl` function accepts two arguments. The first is a URL e.g. `http://example.com`. The second is an object where you can specify the `path`, `hash`, `lowerCase`, and an object of `queryParams`:

```
buildUrl('http://example.com', {
  path: 'about',
  hash: 'contact',
  queryParams: {
    foo: 'bar',
    bar: 'baz'
  }
});

// returns http://example.com/about?foo=bar&bar=baz#contact
```

If you pass an array to the `queryParams` object, it will be transformed to a comma separated list:

```
buildUrl('http://example.com', {
  queryParams: {
    foo: 'bar',
    bar: ['one', 'two', 'three']
  }
});

// returns http://example.com?foo=bar&bar=one,two,three
```

If you want to change the `path`, `hash` and `queryParams` case to all lowercase  then pass `lowerCase` as true in arguments, default value of this will be `false`:

```
buildUrl('http://example.com', {
  path: 'AbouT',
  hash: 'ConTacT',
  lowerCase: true,
  queryParams: {
    foo: 'bAr',
    bar: ['oNe', 'TWO', 'thrEE', 123]
  }
});

// returns http://example.com/about?foo=bar&bar=one,two,three,123#contact
```

If you pass an array to the `queryParams` object, and want that they should not be comma separated use `disableCSV`:

```
buildUrl('http://example.com', {
  disableCSV: true,
  queryParams: {
    foo: 'bar',
    bar: ['one', 'two', 'three']
  }
});

// returns http://example.com?foo=bar&bar=one&bar=two&bar=three
```


If you only want the query string, path, hash, or any combination of the three you can skip the URL parameter or pass in an empty string or null:

```
buildUrl('', {
  queryParams: {
    foo: 'bar',
    bar: 'baz'
  }
});

// returns ?foo=bar&bar=baz

buildUrl(null, {
  queryParams: {
    foo: 'bar',
    bar: 'baz'
  }
});

// returns ?foo=bar&bar=baz

buildUrl({
  queryParams: {
    foo: 'bar',
    bar: 'baz'
  }
});
```

Any null values in the `queryParams` object will be treated as empty strings:

```
buildUrl('http://example.com', {
  queryParams: {
    foo: 'bar',
    bar: null
  }
});

// returns http://example.com?foo=bar&bar=
```

## License

This is licensed under an MIT License. [See details](LICENSE)

---
_Source: https://npm.io/package/build-url · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
