6.0.1 • Published 4 years ago

build-url v6.0.1

Weekly downloads
50,944
License
MIT
Repository
github
Last release
4 years ago

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

@gettruck/payfast-jsgamiphy-dashboard-reactgamiphy-i18naix-event-clientnodejs-consumervpo-order-service@dxos/xbox-dashboardj4-apileague-jimmykovalevr-sberidpayfast-js@everything-registry/sub-chunk-1273@indigov/plivo@infinitebrahmanuniverse/nolb-build-devtorian-jsdokuinjstelegram-cmstemp-shmisraeli-bank-scraperstest-bodi-sdktldwtradingeconomicsuubookkit-exporteruconn-sso-clienturlparamifyxpresservue-trap-packwithings-toolboxyoshi-fork-teamcity@softwaredevelopment/bodhi-client-nest@ookla/speedtest-js-sdk@open-age/cli@open-age/send-it-clientlablooklinkfire@rudymalhi/israeli-bank-scrapers@jetti/jetti.clijoin-apinews-wrapper@ekingdom/payment-gateway@ekingdom/drakemall-payment-gateway@dynamico/corelocalist-viewereghlnipcaember-metiseasypoll-cas2@zalastax/nolb-build-@swell/storefront-nuxt@trapcode/xjs@trapcode/xpresser-frontend-helpersmelifymelifyp@edde/dasa-libanchor-form-parseropen-repo-cliopenmeetingsaem-to-juno-migrationf_yoshioddcast-tts-demoexpress-social-middlewareaxios-elasticache-memcached-adapterplivoplivo-kunalplivo-tubbb-promisereverse-websocket-tunnelreverse-websocket-tunnel-zyflusight-csv-tools@adetoola/sms@andreo141/ember-metis@apcovernight/node-utilsshamrock_utilsreact-localist-viewerbodhi-clientbodhi-client-nestgoblin-base-benchmarkreact-cas-clientexpress-cors-anywhereopen-age-clichaoxing-assistant@brafdlog/israeli-bank-scrapers-corereact-flickr-heroreact-flickr-lightboxhippodamuscred-auth-managerspaceflightnewsapi-nodespeakerbob-clientconnect-social-middleware@danhab99/facebook-login-for-robot@datagica/datanote-api-clientstatus-aggregatorisraeli-bank-scrapers-forkisraeli-bank-scrapersisraeli-bank-scrapers-coreitamarco-israeli-bank-scrapers
6.0.1

4 years ago

6.0.0

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.0.0

4 years ago

3.1.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.11

6 years ago

1.0.10

7 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.12

8 years ago

0.0.9

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago