build-url

A small library that builds a URL given its components

This package is no longer maintained
Last updated a year ago by steverydz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install build-url 
SYNC missed versions from official npm registry.

build-url

Build Status

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

Current Tags

  • 6.0.1                                ...           latest (a year ago)

41 Versions

  • 6.0.1 [deprecated]           ...           a year ago
  • 6.0.0 [deprecated]           ...           a year ago
  • 5.0.2 [deprecated]           ...           a year ago
  • 5.0.1 [deprecated]           ...           a year ago
  • 5.0.0 [deprecated]           ...           a year ago
  • 4.0.0 [deprecated]           ...           a year ago
  • 3.1.0 [deprecated]           ...           a year ago
  • 3.0.4 [deprecated]           ...           a year ago
  • 3.0.3 [deprecated]           ...           a year ago
  • 3.0.2 [deprecated]           ...           a year ago
  • 3.0.1 [deprecated]           ...           a year ago
  • 3.0.0 [deprecated]           ...           a year ago
  • 2.0.0 [deprecated]           ...           2 years ago
  • 1.3.3 [deprecated]           ...           2 years ago
  • 1.3.2 [deprecated]           ...           3 years ago
  • 1.3.1 [deprecated]           ...           3 years ago
  • 1.3.0 [deprecated]           ...           3 years ago
  • 1.2.0 [deprecated]           ...           3 years ago
  • 1.1.2 [deprecated]           ...           3 years ago
  • 1.1.1 [deprecated]           ...           4 years ago
  • 1.1.0 [deprecated]           ...           4 years ago
  • 1.0.11 [deprecated]           ...           4 years ago
  • 1.0.10 [deprecated]           ...           4 years ago
  • 1.0.9 [deprecated]           ...           6 years ago
  • 1.0.8 [deprecated]           ...           6 years ago
  • 1.0.7 [deprecated]           ...           6 years ago
  • 1.0.6 [deprecated]           ...           6 years ago
  • 1.0.5 [deprecated]           ...           6 years ago
  • 1.0.4 [deprecated]           ...           6 years ago
  • 1.0.3 [deprecated]           ...           6 years ago
  • 1.0.2 [deprecated]           ...           6 years ago
  • 1.0.1 [deprecated]           ...           6 years ago
  • 1.0.0 [deprecated]           ...           6 years ago
  • 0.0.12 [deprecated]           ...           6 years ago
  • 0.0.9 [deprecated]           ...           6 years ago
  • 0.0.6 [deprecated]           ...           6 years ago
  • 0.0.5 [deprecated]           ...           6 years ago
  • 0.0.4 [deprecated]           ...           6 years ago
  • 0.0.3 [deprecated]           ...           6 years ago
  • 0.0.2 [deprecated]           ...           6 years ago
  • 0.0.1 [deprecated]           ...           6 years ago
Maintainers (1)
Downloads
Today 18
This Week 104
This Month 660
Last Day 42
Last Week 215
Last Month 1,080
Dependencies (0)
None
Dev Dependencies (6)
Dependents (92)

Copyright 2014 - 2021 © npmmirror.com |