6.0.1 • Published 5 years ago

build-url v6.0.1

Weekly downloads
50,944
License
MIT
Repository
github
Last release
5 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-1273withings-toolboxtelegram-cmstemp-shmisraeli-bank-scrapersspeakerbob-clientstatus-aggregatoruubookkit-exporter@rudymalhi/israeli-bank-scrapersisraeli-bank-scrapers-forkedisraeli-bank-scrapersisraeli-bank-scrapers-by-e.aisraeli-bank-scrapers-coreisraeli-bank-scrapers-forkitamarco-israeli-bank-scrapersjoin-apispaceflightnewsapi-nodeuconn-sso-clienttest-bodi-sdktldwtradingeconomics@apcovernight/node-utils@andreo141/ember-metis@adetoola/sms@brafdlog/israeli-bank-scrapers-core@datagica/datanote-api-client@danhab99/facebook-login-for-robotyoshi-fork-teamcityvue-trap-packurlparamify@dynamico/core@ekingdom/drakemall-payment-gateway@ekingdom/payment-gateway@indigov/plivo@infinitebrahmanuniverse/nolb-build-@edde/dasa-lib@jetti/jetti.cli@ookla/speedtest-js-sdk@open-age/send-it-client@open-age/cli@softwaredevelopment/bodhi-client-nestopenmeetingsopen-repo-cliopen-age-clioddcast-tts-demoreact-cas-clientreact-flickr-heroreact-flickr-lightboxreact-localist-viewernews-wrappernipcaplivoplivo-kunalplivo-tureverse-websocket-tunnelreverse-websocket-tunnel-zyshamrock_utilsgoblin-base-benchmarkhippodamuslocalist-viewerlinkfirelablookmelifymelifyp@trapcode/xjs@trapcode/xpresser-frontend-helpers@zalastax/nolb-build-@swell/storefront-nuxtanchor-form-parseraem-to-juno-migrationaxios-elasticache-memcached-adapterconnect-social-middlewarechaoxing-assistantcred-auth-managerbbb-promisebodhi-clientbodhi-client-nestdevtorian-jsdokuinjseasypoll-cas2eghlember-metisexpress-cors-anywhereexpress-social-middlewaref_yoshiflusight-csv-tools
6.0.1

5 years ago

6.0.0

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.0.0

5 years ago

3.1.0

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.11

7 years ago

1.0.10

8 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.12

10 years ago

0.0.9

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago